2022-02-04 14:32:15 +01:00
|
|
|
package lnd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestWitnessBeaconIntercept tests that the beacon passes on subscriptions to
|
|
|
|
// the interceptor correctly.
|
|
|
|
func TestWitnessBeaconIntercept(t *testing.T) {
|
|
|
|
var interceptedFwd htlcswitch.InterceptedForward
|
|
|
|
interceptor := func(fwd htlcswitch.InterceptedForward) error {
|
|
|
|
interceptedFwd = fwd
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := newPreimageBeacon(
|
|
|
|
&mockWitnessCache{}, interceptor,
|
|
|
|
)
|
|
|
|
|
|
|
|
preimage := lntypes.Preimage{1, 2, 3}
|
|
|
|
hash := preimage.Hash()
|
|
|
|
|
|
|
|
subscription, err := p.SubscribeUpdates(
|
|
|
|
lnwire.NewShortChanIDFromInt(1),
|
|
|
|
&channeldb.HTLC{
|
|
|
|
RHash: hash,
|
|
|
|
},
|
|
|
|
&hop.Payload{},
|
|
|
|
[]byte{2},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2022-08-27 09:09:02 +02:00
|
|
|
t.Cleanup(subscription.CancelSubscription)
|
2022-02-04 14:32:15 +01:00
|
|
|
|
|
|
|
require.NoError(t, interceptedFwd.Settle(preimage))
|
|
|
|
|
|
|
|
update := <-subscription.WitnessUpdates
|
|
|
|
require.Equal(t, preimage, update)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockWitnessCache struct {
|
|
|
|
witnessCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockWitnessCache) AddSha256Witnesses(
|
|
|
|
preimages ...lntypes.Preimage) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|