breacharbiter test: extract common init logic
This commit is contained in:
parent
b5a228808b
commit
9aa55b164e
@ -933,11 +933,10 @@ restartCheck:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestBreachHandoffSuccess tests that a channel's close observer properly
|
func initBreachedState(t *testing.T) (*breachArbiter,
|
||||||
// delivers retribution information to the breach arbiter in response to a
|
*lnwallet.LightningChannel, *lnwallet.LightningChannel,
|
||||||
// breach close. This test verifies correctness in the event that the handoff
|
*lnwallet.LocalForceCloseSummary, chan *ContractBreachEvent,
|
||||||
// experiences no interruptions.
|
func(), func()) {
|
||||||
func TestBreachHandoffSuccess(t *testing.T) {
|
|
||||||
// Create a pair of channels using a notifier that allows us to signal
|
// Create a pair of channels using a notifier that allows us to signal
|
||||||
// a spend of the funding transaction. Alice's channel will be the on
|
// a spend of the funding transaction. Alice's channel will be the on
|
||||||
// observing a breach.
|
// observing a breach.
|
||||||
@ -945,7 +944,6 @@ func TestBreachHandoffSuccess(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create test channels: %v", err)
|
t.Fatalf("unable to create test channels: %v", err)
|
||||||
}
|
}
|
||||||
defer cleanUpChans()
|
|
||||||
|
|
||||||
// Instantiate a breach arbiter to handle the breach of alice's channel.
|
// Instantiate a breach arbiter to handle the breach of alice's channel.
|
||||||
contractBreaches := make(chan *ContractBreachEvent)
|
contractBreaches := make(chan *ContractBreachEvent)
|
||||||
@ -956,7 +954,6 @@ func TestBreachHandoffSuccess(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to initialize test breach arbiter: %v", err)
|
t.Fatalf("unable to initialize test breach arbiter: %v", err)
|
||||||
}
|
}
|
||||||
defer cleanUpArb()
|
|
||||||
|
|
||||||
// Send one HTLC to Bob and perform a state transition to lock it in.
|
// Send one HTLC to Bob and perform a state transition to lock it in.
|
||||||
htlcAmount := lnwire.NewMSatFromSatoshis(20000)
|
htlcAmount := lnwire.NewMSatFromSatoshis(20000)
|
||||||
@ -991,6 +988,20 @@ func TestBreachHandoffSuccess(t *testing.T) {
|
|||||||
t.Fatalf("Can't update the channel state: %v", err)
|
t.Fatalf("Can't update the channel state: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return brar, alice, bob, bobClose, contractBreaches, cleanUpChans,
|
||||||
|
cleanUpArb
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBreachHandoffSuccess tests that a channel's close observer properly
|
||||||
|
// delivers retribution information to the breach arbiter in response to a
|
||||||
|
// breach close. This test verifies correctness in the event that the handoff
|
||||||
|
// experiences no interruptions.
|
||||||
|
func TestBreachHandoffSuccess(t *testing.T) {
|
||||||
|
brar, alice, _, bobClose, contractBreaches,
|
||||||
|
cleanUpChans, cleanUpArb := initBreachedState(t)
|
||||||
|
defer cleanUpChans()
|
||||||
|
defer cleanUpArb()
|
||||||
|
|
||||||
chanPoint := alice.ChanPoint
|
chanPoint := alice.ChanPoint
|
||||||
|
|
||||||
// Signal a spend of the funding transaction and wait for the close
|
// Signal a spend of the funding transaction and wait for the close
|
||||||
@ -1052,59 +1063,11 @@ func TestBreachHandoffSuccess(t *testing.T) {
|
|||||||
// arbiter fails to write the information to disk, and that a subsequent attempt
|
// arbiter fails to write the information to disk, and that a subsequent attempt
|
||||||
// at the handoff succeeds.
|
// at the handoff succeeds.
|
||||||
func TestBreachHandoffFail(t *testing.T) {
|
func TestBreachHandoffFail(t *testing.T) {
|
||||||
// Create a pair of channels using a notifier that allows us to signal
|
brar, alice, _, bobClose, contractBreaches,
|
||||||
// a spend of the funding transaction. Alice's channel will be the on
|
cleanUpChans, cleanUpArb := initBreachedState(t)
|
||||||
// observing a breach.
|
|
||||||
alice, bob, cleanUpChans, err := createInitChannels(1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create test channels: %v", err)
|
|
||||||
}
|
|
||||||
defer cleanUpChans()
|
defer cleanUpChans()
|
||||||
|
|
||||||
// Instantiate a breach arbiter to handle the breach of alice's channel.
|
|
||||||
contractBreaches := make(chan *ContractBreachEvent)
|
|
||||||
|
|
||||||
brar, cleanUpArb, err := createTestArbiter(
|
|
||||||
t, contractBreaches, alice.State().Db,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to initialize test breach arbiter: %v", err)
|
|
||||||
}
|
|
||||||
defer cleanUpArb()
|
defer cleanUpArb()
|
||||||
|
|
||||||
// Send one HTLC to Bob and perform a state transition to lock it in.
|
|
||||||
htlcAmount := lnwire.NewMSatFromSatoshis(20000)
|
|
||||||
htlc, _ := createHTLC(0, htlcAmount)
|
|
||||||
if _, err := alice.AddHTLC(htlc, nil); err != nil {
|
|
||||||
t.Fatalf("alice unable to add htlc: %v", err)
|
|
||||||
}
|
|
||||||
if _, err := bob.ReceiveHTLC(htlc); err != nil {
|
|
||||||
t.Fatalf("bob unable to recv add htlc: %v", err)
|
|
||||||
}
|
|
||||||
if err := forceStateTransition(alice, bob); err != nil {
|
|
||||||
t.Fatalf("Can't update the channel state: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate the force close summary at this point in time, this will
|
|
||||||
// serve as the old state bob will broadcast.
|
|
||||||
bobClose, err := bob.ForceClose()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to force close bob's channel: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now send another HTLC and perform a state transition, this ensures
|
|
||||||
// Alice is ahead of the state Bob will broadcast.
|
|
||||||
htlc2, _ := createHTLC(1, htlcAmount)
|
|
||||||
if _, err := alice.AddHTLC(htlc2, nil); err != nil {
|
|
||||||
t.Fatalf("alice unable to add htlc: %v", err)
|
|
||||||
}
|
|
||||||
if _, err := bob.ReceiveHTLC(htlc2); err != nil {
|
|
||||||
t.Fatalf("bob unable to recv add htlc: %v", err)
|
|
||||||
}
|
|
||||||
if err := forceStateTransition(alice, bob); err != nil {
|
|
||||||
t.Fatalf("Can't update the channel state: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Before alerting Alice of the breach, instruct our failing retribution
|
// Before alerting Alice of the breach, instruct our failing retribution
|
||||||
// store to fail the next database operation, which we expect to write
|
// store to fail the next database operation, which we expect to write
|
||||||
// the information handed off by the channel's close observer.
|
// the information handed off by the channel's close observer.
|
||||||
@ -1139,7 +1102,7 @@ func TestBreachHandoffFail(t *testing.T) {
|
|||||||
assertNoArbiterBreach(t, brar, chanPoint)
|
assertNoArbiterBreach(t, brar, chanPoint)
|
||||||
assertNotPendingClosed(t, alice)
|
assertNotPendingClosed(t, alice)
|
||||||
|
|
||||||
brar, cleanUpArb, err = createTestArbiter(
|
brar, cleanUpArb, err := createTestArbiter(
|
||||||
t, contractBreaches, alice.State().Db,
|
t, contractBreaches, alice.State().Db,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user