From e34850c7afdea35860224340285acfb9832d2775 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 16 Jan 2018 20:18:53 -0800 Subject: [PATCH] htlcswitch: update tests to account for new API changes --- htlcswitch/link_test.go | 16 ++++++++-- htlcswitch/mock.go | 30 +++++++++++++++++ htlcswitch/test_utils.go | 69 ++++++++++++++++++++++++++++++++++------ 3 files changed, 103 insertions(+), 12 deletions(-) diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 168de48d..2f8af7d9 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -17,6 +17,7 @@ import ( "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" "github.com/roasbeef/btcd/chaincfg/chainhash" @@ -1445,6 +1446,11 @@ func newSingleLinkTestHarness(chanAmt btcutil.Amount) (ChannelLink, func(), erro } ) + pCache := &mockPreimageCache{ + // hash -> preimage + preimageMap: make(map[[32]byte][]byte), + } + aliceCfg := ChannelLinkConfig{ FwrdingPolicy: globalPolicy, Peer: &alicePeer, @@ -1454,8 +1460,12 @@ func newSingleLinkTestHarness(chanAmt btcutil.Amount) (ChannelLink, func(), erro return obfuscator, lnwire.CodeNone }, GetLastChannelUpdate: mockGetChanUpdateMessage, - Registry: invoiveRegistry, - BlockEpochs: globalEpoch, + PreimageCache: pCache, + UpdateContractSignals: func(*contractcourt.ContractSignals) error { + return nil + }, + Registry: invoiveRegistry, + BlockEpochs: globalEpoch, } const startingHeight = 100 @@ -2327,3 +2337,5 @@ func TestChannelLinkRejectDuplicatePayment(t *testing.T) { t.Fatal("error haven't been received") } } + +// TODO(roasbeef): add test for re-sending after hodl mode, to settle any lingering diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 2524c47c..3c336ef2 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -16,6 +16,7 @@ import ( "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" "github.com/roasbeef/btcd/btcec" @@ -25,6 +26,35 @@ import ( "github.com/roasbeef/btcutil" ) +type mockPreimageCache struct { + sync.Mutex + preimageMap map[[32]byte][]byte +} + +func (m *mockPreimageCache) LookupPreimage(hash []byte) ([]byte, bool) { + m.Lock() + defer m.Unlock() + + var h [32]byte + copy(h[:], hash) + + p, ok := m.preimageMap[h] + return p, ok +} + +func (m *mockPreimageCache) AddPreimage(preimage []byte) error { + m.Lock() + defer m.Unlock() + + m.preimageMap[sha256.Sum256(preimage[:])] = preimage + + return nil +} + +func (m *mockPreimageCache) SubcribeUpdates() *contractcourt.WitnessSubcription { + return nil +} + type mockFeeEstimator struct { byteFeeIn chan btcutil.Amount weightFeeIn chan btcutil.Amount diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index adda0a7f..2c7b587d 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -21,6 +21,7 @@ import ( "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/shachain" @@ -261,13 +262,20 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, aliceSigner := &mockSigner{aliceKeyPriv} bobSigner := &mockSigner{bobKeyPriv} - channelAlice, err := lnwallet.NewLightningChannel(aliceSigner, - nil, estimator, aliceChannelState) + pCache := &mockPreimageCache{ + // hash -> preimage + preimageMap: make(map[[32]byte][]byte), + } + + channelAlice, err := lnwallet.NewLightningChannel( + aliceSigner, nil, pCache, aliceChannelState, + ) if err != nil { return nil, nil, nil, nil, err } - channelBob, err := lnwallet.NewLightningChannel(bobSigner, nil, - estimator, bobChannelState) + channelBob, err := lnwallet.NewLightningChannel( + bobSigner, nil, pCache, bobChannelState, + ) if err != nil { return nil, nil, nil, nil, err } @@ -311,7 +319,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, } newAliceChannel, err := lnwallet.NewLightningChannel(aliceSigner, - nil, estimator, aliceStoredChannel) + nil, nil, aliceStoredChannel) if err != nil { return nil, nil, errors.Errorf("unable to create new channel: %v", err) @@ -336,7 +344,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, } newBobChannel, err := lnwallet.NewLightningChannel(bobSigner, nil, - estimator, bobStoredChannel) + nil, bobStoredChannel) if err != nil { return nil, nil, errors.Errorf("unable to create new channel: %v", err) @@ -717,6 +725,11 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, quit: make(chan struct{}), } + pCache := &mockPreimageCache{ + // hash -> preimage + preimageMap: make(map[[32]byte][]byte), + } + globalPolicy := ForwardingPolicy{ MinHTLC: lnwire.NewMSatFromSatoshis(5), BaseFee: lnwire.NewMSatFromSatoshis(1), @@ -744,7 +757,11 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, Registry: aliceServer.registry, BlockEpochs: aliceEpoch, FeeEstimator: feeEstimator, - SyncStates: true, + PreimageCache: pCache, + UpdateContractSignals: func(*contractcourt.ContractSignals) error { + return nil + }, + SyncStates: true, }, aliceChannel, startingHeight, @@ -752,6 +769,11 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, if err := aliceServer.htlcSwitch.addLink(aliceChannelLink); err != nil { t.Fatalf("unable to add alice channel link: %v", err) } + go func() { + for { + <-aliceChannelLink.(*channelLink).htlcUpdates + } + }() bobFirstEpochChan := make(chan *chainntnfs.BlockEpoch) bobFirstEpoch := &chainntnfs.BlockEpochEvent{ @@ -773,7 +795,11 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, Registry: bobServer.registry, BlockEpochs: bobFirstEpoch, FeeEstimator: feeEstimator, - SyncStates: true, + PreimageCache: pCache, + UpdateContractSignals: func(*contractcourt.ContractSignals) error { + return nil + }, + SyncStates: true, }, firstBobChannel, startingHeight, @@ -781,6 +807,11 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, if err := bobServer.htlcSwitch.addLink(firstBobChannelLink); err != nil { t.Fatalf("unable to add first bob channel link: %v", err) } + go func() { + for { + <-firstBobChannelLink.(*channelLink).htlcUpdates + } + }() bobSecondEpochChan := make(chan *chainntnfs.BlockEpoch) bobSecondEpoch := &chainntnfs.BlockEpochEvent{ @@ -802,7 +833,11 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, Registry: bobServer.registry, BlockEpochs: bobSecondEpoch, FeeEstimator: feeEstimator, - SyncStates: true, + PreimageCache: pCache, + UpdateContractSignals: func(*contractcourt.ContractSignals) error { + return nil + }, + SyncStates: true, }, secondBobChannel, startingHeight, @@ -810,6 +845,11 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, if err := bobServer.htlcSwitch.addLink(secondBobChannelLink); err != nil { t.Fatalf("unable to add second bob channel link: %v", err) } + go func() { + for { + <-secondBobChannelLink.(*channelLink).htlcUpdates + } + }() carolBlockEpoch := make(chan *chainntnfs.BlockEpoch) carolEpoch := &chainntnfs.BlockEpochEvent{ @@ -831,7 +871,11 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, Registry: carolServer.registry, BlockEpochs: carolEpoch, FeeEstimator: feeEstimator, - SyncStates: true, + PreimageCache: pCache, + UpdateContractSignals: func(*contractcourt.ContractSignals) error { + return nil + }, + SyncStates: true, }, carolChannel, startingHeight, @@ -839,6 +883,11 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, if err := carolServer.htlcSwitch.addLink(carolChannelLink); err != nil { t.Fatalf("unable to add carol channel link: %v", err) } + go func() { + for { + <-carolChannelLink.(*channelLink).htlcUpdates + } + }() return &threeHopNetwork{ aliceServer: aliceServer,