mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-07-26 04:42:35 +02:00
contractcourt/chanarb test: expand TestChannelArbitratorCommitFailure to coop and local force close
This commit is contained in:
@@ -742,12 +742,71 @@ func TestChannelArbitratorPersistence(t *testing.T) {
|
|||||||
// TestChannelArbitratorCommitFailure tests that the channel arbitrator is able
|
// TestChannelArbitratorCommitFailure tests that the channel arbitrator is able
|
||||||
// to recover from a failed CommitState call at restart.
|
// to recover from a failed CommitState call at restart.
|
||||||
func TestChannelArbitratorCommitFailure(t *testing.T) {
|
func TestChannelArbitratorCommitFailure(t *testing.T) {
|
||||||
// Start out with a log that will fail committing to StateContractClosed.
|
|
||||||
|
testCases := []struct {
|
||||||
|
|
||||||
|
// closeType is the type of channel close we want ot test.
|
||||||
|
closeType channeldb.ClosureType
|
||||||
|
|
||||||
|
// sendEvent is a function that will send the event
|
||||||
|
// corresponding to this test's closeType to the passed
|
||||||
|
// ChannelArbitrator.
|
||||||
|
sendEvent func(chanArb *ChannelArbitrator)
|
||||||
|
|
||||||
|
// expectedStates is the states we expect the state machine to
|
||||||
|
// go through after a restart and successful log commit.
|
||||||
|
expectedStates []ArbitratorState
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
closeType: channeldb.CooperativeClose,
|
||||||
|
sendEvent: func(chanArb *ChannelArbitrator) {
|
||||||
|
closeInfo := &CooperativeCloseInfo{
|
||||||
|
&channeldb.ChannelCloseSummary{},
|
||||||
|
}
|
||||||
|
chanArb.cfg.ChainEvents.CooperativeClosure <- closeInfo
|
||||||
|
},
|
||||||
|
expectedStates: []ArbitratorState{StateFullyResolved},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
closeType: channeldb.RemoteForceClose,
|
||||||
|
sendEvent: func(chanArb *ChannelArbitrator) {
|
||||||
|
commitSpend := &chainntnfs.SpendDetail{
|
||||||
|
SpenderTxHash: &chainhash.Hash{},
|
||||||
|
}
|
||||||
|
|
||||||
|
uniClose := &lnwallet.UnilateralCloseSummary{
|
||||||
|
SpendDetail: commitSpend,
|
||||||
|
HtlcResolutions: &lnwallet.HtlcResolutions{},
|
||||||
|
}
|
||||||
|
chanArb.cfg.ChainEvents.RemoteUnilateralClosure <- uniClose
|
||||||
|
},
|
||||||
|
expectedStates: []ArbitratorState{StateContractClosed, StateFullyResolved},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
closeType: channeldb.LocalForceClose,
|
||||||
|
sendEvent: func(chanArb *ChannelArbitrator) {
|
||||||
|
chanArb.cfg.ChainEvents.LocalUnilateralClosure <- &LocalUnilateralCloseInfo{
|
||||||
|
&chainntnfs.SpendDetail{},
|
||||||
|
&lnwallet.LocalForceCloseSummary{
|
||||||
|
CloseTx: &wire.MsgTx{},
|
||||||
|
HtlcResolutions: &lnwallet.HtlcResolutions{},
|
||||||
|
},
|
||||||
|
&channeldb.ChannelCloseSummary{},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
expectedStates: []ArbitratorState{StateContractClosed, StateFullyResolved},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range testCases {
|
||||||
log := &mockArbitratorLog{
|
log := &mockArbitratorLog{
|
||||||
state: StateDefault,
|
state: StateDefault,
|
||||||
newStates: make(chan ArbitratorState, 5),
|
newStates: make(chan ArbitratorState, 5),
|
||||||
failCommit: true,
|
failCommit: true,
|
||||||
failCommitState: StateContractClosed,
|
|
||||||
|
// Set the log to fail on the first expected state
|
||||||
|
// after state machine progress for this test case.
|
||||||
|
failCommitState: test.expectedStates[0],
|
||||||
}
|
}
|
||||||
|
|
||||||
chanArb, resolved, err := createTestChannelArbitrator(log)
|
chanArb, resolved, err := createTestChannelArbitrator(log)
|
||||||
@@ -763,21 +822,14 @@ func TestChannelArbitratorCommitFailure(t *testing.T) {
|
|||||||
assertState(t, chanArb, StateDefault)
|
assertState(t, chanArb, StateDefault)
|
||||||
|
|
||||||
closed := make(chan struct{})
|
closed := make(chan struct{})
|
||||||
chanArb.cfg.MarkChannelClosed = func(*channeldb.ChannelCloseSummary) error {
|
chanArb.cfg.MarkChannelClosed = func(
|
||||||
|
*channeldb.ChannelCloseSummary) error {
|
||||||
close(closed)
|
close(closed)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a remote force close event.
|
// Send the test event to trigger the state machine.
|
||||||
commitSpend := &chainntnfs.SpendDetail{
|
test.sendEvent(chanArb)
|
||||||
SpenderTxHash: &chainhash.Hash{},
|
|
||||||
}
|
|
||||||
|
|
||||||
uniClose := &lnwallet.UnilateralCloseSummary{
|
|
||||||
SpendDetail: commitSpend,
|
|
||||||
HtlcResolutions: &lnwallet.HtlcResolutions{},
|
|
||||||
}
|
|
||||||
chanArb.cfg.ChainEvents.RemoteUnilateralClosure <- uniClose
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-closed:
|
case <-closed:
|
||||||
@@ -785,16 +837,18 @@ func TestChannelArbitratorCommitFailure(t *testing.T) {
|
|||||||
t.Fatalf("channel was not marked closed")
|
t.Fatalf("channel was not marked closed")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since the channel was marked closed in the database, but the commit
|
// Since the channel was marked closed in the database, but the
|
||||||
// to the next state failed, the state should still be StateDefault.
|
// commit to the next state failed, the state should still be
|
||||||
|
// StateDefault.
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
if log.state != StateDefault {
|
if log.state != StateDefault {
|
||||||
t.Fatalf("expected to stay in StateDefault")
|
t.Fatalf("expected to stay in StateDefault, instead "+
|
||||||
|
"has %v", log.state)
|
||||||
}
|
}
|
||||||
chanArb.Stop()
|
chanArb.Stop()
|
||||||
|
|
||||||
// Start the arbitrator again, with IsPendingClose reporting the
|
// Start the arbitrator again, with IsPendingClose reporting
|
||||||
// channel closed in the database.
|
// the channel closed in the database.
|
||||||
chanArb, resolved, err = createTestChannelArbitrator(log)
|
chanArb, resolved, err = createTestChannelArbitrator(log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create ChannelArbitrator: %v", err)
|
t.Fatalf("unable to create ChannelArbitrator: %v", err)
|
||||||
@@ -804,16 +858,16 @@ func TestChannelArbitratorCommitFailure(t *testing.T) {
|
|||||||
|
|
||||||
chanArb.cfg.IsPendingClose = true
|
chanArb.cfg.IsPendingClose = true
|
||||||
chanArb.cfg.ClosingHeight = 100
|
chanArb.cfg.ClosingHeight = 100
|
||||||
chanArb.cfg.CloseType = channeldb.RemoteForceClose
|
chanArb.cfg.CloseType = test.closeType
|
||||||
|
|
||||||
if err := chanArb.Start(); err != nil {
|
if err := chanArb.Start(); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since the channel is marked closed in the database, it should
|
// Since the channel is marked closed in the database, it
|
||||||
// advance to StateContractClosed and StateFullyResolved.
|
// should advance to the expected states.
|
||||||
assertStateTransitions(
|
assertStateTransitions(
|
||||||
t, log.newStates, StateContractClosed, StateFullyResolved,
|
t, log.newStates, test.expectedStates...,
|
||||||
)
|
)
|
||||||
|
|
||||||
// It should also mark the channel as resolved.
|
// It should also mark the channel as resolved.
|
||||||
@@ -823,6 +877,7 @@ func TestChannelArbitratorCommitFailure(t *testing.T) {
|
|||||||
case <-time.After(5 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
t.Fatalf("contract was not resolved")
|
t.Fatalf("contract was not resolved")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestChannelArbitratorEmptyResolutions makes sure that a channel that is
|
// TestChannelArbitratorEmptyResolutions makes sure that a channel that is
|
||||||
|
Reference in New Issue
Block a user