breacharbiter_test: use ContractBreaches channel

This commit is contained in:
Johan T. Halseth 2018-04-19 11:09:25 +02:00
parent b9970aec47
commit 55ac8735b6
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -21,7 +21,6 @@ import (
"github.com/btcsuite/btclog" "github.com/btcsuite/btclog"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
@ -949,19 +948,10 @@ func TestBreachHandoffSuccess(t *testing.T) {
defer cleanUpChans() 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.
alicePoint := alice.ChannelPoint() contractBreaches := make(chan *ContractBreachEvent)
spendEvents := contractcourt.ChainEventSubscription{
RemoteUnilateralClosure: make(chan *lnwallet.UnilateralCloseSummary, 1),
LocalUnilateralClosure: make(chan *contractcourt.LocalUnilateralCloseInfo, 1),
CooperativeClosure: make(chan struct{}, 1),
ContractBreach: make(chan *lnwallet.BreachRetribution, 1),
ProcessACK: make(chan error, 1),
ChanPoint: *alicePoint,
Cancel: func() {
},
}
brar, cleanUpArb, err := createTestArbiter( brar, cleanUpArb, err := createTestArbiter(
t, &spendEvents, alice.State().Db, t, contractBreaches, alice.State().Db,
) )
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)
@ -1005,13 +995,21 @@ func TestBreachHandoffSuccess(t *testing.T) {
// Signal a spend of the funding transaction and wait for the close // Signal a spend of the funding transaction and wait for the close
// observer to exit. // observer to exit.
spendEvents.ContractBreach <- &lnwallet.BreachRetribution{ breach := &ContractBreachEvent{
BreachTransaction: bobClose.CloseTx, ChanPoint: *chanPoint,
ProcessACK: make(chan error, 1),
BreachRetribution: &lnwallet.BreachRetribution{
BreachTransaction: bobClose.CloseTx,
},
} }
contractBreaches <- breach
// We'll also wait to consume the ACK back from the breach arbiter. // We'll also wait to consume the ACK back from the breach arbiter.
select { select {
case <-spendEvents.ProcessACK: case err := <-breach.ProcessACK:
if err != nil {
t.Fatalf("handoff failed: %v", err)
}
case <-time.After(time.Second * 15): case <-time.After(time.Second * 15):
t.Fatalf("breach arbiter didn't send ack back") t.Fatalf("breach arbiter didn't send ack back")
} }
@ -1038,19 +1036,10 @@ func TestBreachHandoffFail(t *testing.T) {
defer cleanUpChans() 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.
alicePoint := alice.ChannelPoint() contractBreaches := make(chan *ContractBreachEvent)
spendEvents := contractcourt.ChainEventSubscription{
RemoteUnilateralClosure: make(chan *lnwallet.UnilateralCloseSummary, 1),
LocalUnilateralClosure: make(chan *contractcourt.LocalUnilateralCloseInfo, 1),
CooperativeClosure: make(chan struct{}, 1),
ContractBreach: make(chan *lnwallet.BreachRetribution, 1),
ProcessACK: make(chan error, 1),
ChanPoint: *alicePoint,
Cancel: func() {
},
}
brar, cleanUpArb, err := createTestArbiter( brar, cleanUpArb, err := createTestArbiter(
t, &spendEvents, alice.State().Db, t, contractBreaches, alice.State().Db,
) )
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)
@ -1099,11 +1088,18 @@ func TestBreachHandoffFail(t *testing.T) {
// Signal the notifier to dispatch spend notifications of the funding // Signal the notifier to dispatch spend notifications of the funding
// transaction using the transaction from bob's closing summary. // transaction using the transaction from bob's closing summary.
chanPoint := alice.ChanPoint chanPoint := alice.ChanPoint
spendEvents.ContractBreach <- &lnwallet.BreachRetribution{ breach := &ContractBreachEvent{
BreachTransaction: bobClose.CloseTx, ChanPoint: *chanPoint,
ProcessACK: make(chan error, 1),
BreachRetribution: &lnwallet.BreachRetribution{
BreachTransaction: bobClose.CloseTx,
},
} }
contractBreaches <- breach
// We'll also wait to consume the ACK back from the breach arbiter.
select { select {
case err := <-spendEvents.ProcessACK: case err := <-breach.ProcessACK:
if err == nil { if err == nil {
t.Fatalf("breach write should have failed") t.Fatalf("breach write should have failed")
} }
@ -1118,7 +1114,7 @@ func TestBreachHandoffFail(t *testing.T) {
assertNotPendingClosed(t, alice) assertNotPendingClosed(t, alice)
brar, cleanUpArb, err = createTestArbiter( brar, cleanUpArb, err = createTestArbiter(
t, &spendEvents, alice.State().Db, t, contractBreaches, alice.State().Db,
) )
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)
@ -1139,11 +1135,21 @@ func TestBreachHandoffFail(t *testing.T) {
// Signal a spend of the funding transaction and wait for the close // Signal a spend of the funding transaction and wait for the close
// observer to exit. This time we are allowing the handoff to succeed. // observer to exit. This time we are allowing the handoff to succeed.
spendEvents.ContractBreach <- &lnwallet.BreachRetribution{ breach = &ContractBreachEvent{
BreachTransaction: bobClose.CloseTx, ChanPoint: *chanPoint,
ProcessACK: make(chan error, 1),
BreachRetribution: &lnwallet.BreachRetribution{
BreachTransaction: bobClose.CloseTx,
},
} }
contractBreaches <- breach
select { select {
case <-spendEvents.ProcessACK: case err := <-breach.ProcessACK:
if err != nil {
t.Fatalf("handoff failed: %v", err)
}
case <-time.After(time.Second * 15): case <-time.After(time.Second * 15):
t.Fatalf("breach arbiter didn't send ack back") t.Fatalf("breach arbiter didn't send ack back")
} }
@ -1207,7 +1213,7 @@ func assertNotPendingClosed(t *testing.T, c *lnwallet.LightningChannel) {
// createTestArbiter instantiates a breach arbiter with a failing retribution // createTestArbiter instantiates a breach arbiter with a failing retribution
// store, so that controlled failures can be tested. // store, so that controlled failures can be tested.
func createTestArbiter(t *testing.T, chainEvents *contractcourt.ChainEventSubscription, func createTestArbiter(t *testing.T, contractBreaches chan *ContractBreachEvent,
db *channeldb.DB) (*breachArbiter, func(), error) { db *channeldb.DB) (*breachArbiter, func(), error) {
// Create a failing retribution store, that wraps a normal one. // Create a failing retribution store, that wraps a normal one.
@ -1222,13 +1228,11 @@ func createTestArbiter(t *testing.T, chainEvents *contractcourt.ChainEventSubscr
// Assemble our test arbiter. // Assemble our test arbiter.
notifier := makeMockSpendNotifier() notifier := makeMockSpendNotifier()
ba := newBreachArbiter(&BreachConfig{ ba := newBreachArbiter(&BreachConfig{
CloseLink: func(_ *wire.OutPoint, _ htlcswitch.ChannelCloseType) {}, CloseLink: func(_ *wire.OutPoint, _ htlcswitch.ChannelCloseType) {},
DB: db, DB: db,
Estimator: &lnwallet.StaticFeeEstimator{FeeRate: 50}, Estimator: &lnwallet.StaticFeeEstimator{FeeRate: 50},
GenSweepScript: func() ([]byte, error) { return nil, nil }, GenSweepScript: func() ([]byte, error) { return nil, nil },
SubscribeChannelEvents: func(_ wire.OutPoint) (*contractcourt.ChainEventSubscription, error) { ContractBreaches: contractBreaches,
return chainEvents, nil
},
Signer: signer, Signer: signer,
Notifier: notifier, Notifier: notifier,
PublishTransaction: func(_ *wire.MsgTx) error { return nil }, PublishTransaction: func(_ *wire.MsgTx) error { return nil },