/ witness_beacon_test.go
witness_beacon_test.go
 1  package lnd
 2  
 3  import (
 4  	"testing"
 5  
 6  	"github.com/lightningnetwork/lnd/channeldb"
 7  	"github.com/lightningnetwork/lnd/htlcswitch"
 8  	"github.com/lightningnetwork/lnd/htlcswitch/hop"
 9  	"github.com/lightningnetwork/lnd/lntypes"
10  	"github.com/lightningnetwork/lnd/lnwire"
11  	"github.com/stretchr/testify/require"
12  )
13  
14  // TestWitnessBeaconIntercept tests that the beacon passes on subscriptions to
15  // the interceptor correctly.
16  func TestWitnessBeaconIntercept(t *testing.T) {
17  	var interceptedFwd htlcswitch.InterceptedForward
18  	interceptor := func(fwd htlcswitch.InterceptedForward) error {
19  		interceptedFwd = fwd
20  
21  		return nil
22  	}
23  
24  	p := newPreimageBeacon(
25  		&mockWitnessCache{}, interceptor,
26  	)
27  
28  	preimage := lntypes.Preimage{1, 2, 3}
29  	hash := preimage.Hash()
30  
31  	subscription, err := p.SubscribeUpdates(
32  		lnwire.NewShortChanIDFromInt(1),
33  		&channeldb.HTLC{
34  			RHash: hash,
35  		},
36  		&hop.Payload{},
37  		[]byte{2},
38  	)
39  	require.NoError(t, err)
40  	t.Cleanup(subscription.CancelSubscription)
41  
42  	require.NoError(t, interceptedFwd.Settle(preimage))
43  
44  	update := <-subscription.WitnessUpdates
45  	require.Equal(t, preimage, update)
46  }
47  
48  type mockWitnessCache struct {
49  	witnessCache
50  }
51  
52  func (w *mockWitnessCache) AddSha256Witnesses(
53  	preimages ...lntypes.Preimage) error {
54  
55  	return nil
56  }