contractcourt: update TestChannelArbitratorAnchors

This commit adds a deadline field to mockSweeper that can be used to
track the customized conf target (deadline) used for sweeping anchors.
The relevant test, TestChannelArbitratorAnchors is updated to reflect
that the deadlines are indeed taking effect.
This commit is contained in:
yyforyongyu 2021-05-11 20:57:31 +08:00
parent 391f240ccb
commit 0dc3190fec
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
2 changed files with 79 additions and 3 deletions

@ -20,8 +20,10 @@ import (
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntest/mock" "github.com/lightningnetwork/lnd/lntest/mock"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
) )
const ( const (
@ -2113,13 +2115,30 @@ func TestChannelArbitratorAnchors(t *testing.T) {
reports, reports,
) )
// Add a dummy payment hash to the preimage lookup.
rHash := [lntypes.PreimageSize]byte{1, 2, 3}
mockPreimageDB := newMockWitnessBeacon()
mockPreimageDB.lookupPreimage[rHash] = rHash
// Attack a mock PreimageDB and Registry to channel arbitrator.
chanArb := chanArbCtx.chanArb chanArb := chanArbCtx.chanArb
chanArb.cfg.PreimageDB = newMockWitnessBeacon() chanArb.cfg.PreimageDB = mockPreimageDB
chanArb.cfg.Registry = &mockRegistry{} chanArb.cfg.Registry = &mockRegistry{}
// Setup two pre-confirmation anchor resolutions on the mock channel. // Setup two pre-confirmation anchor resolutions on the mock channel.
chanArb.cfg.Channel.(*mockChannel).anchorResolutions = chanArb.cfg.Channel.(*mockChannel).anchorResolutions =
&lnwallet.AnchorResolutions{} &lnwallet.AnchorResolutions{
Local: &lnwallet.AnchorResolution{
AnchorSignDescriptor: input.SignDescriptor{
Output: &wire.TxOut{Value: 1},
},
},
Remote: &lnwallet.AnchorResolution{
AnchorSignDescriptor: input.SignDescriptor{
Output: &wire.TxOut{Value: 1},
},
},
}
if err := chanArb.Start(nil); err != nil { if err := chanArb.Start(nil); err != nil {
t.Fatalf("unable to start ChannelArbitrator: %v", err) t.Fatalf("unable to start ChannelArbitrator: %v", err)
@ -2139,6 +2158,41 @@ func TestChannelArbitratorAnchors(t *testing.T) {
} }
chanArb.UpdateContractSignals(signals) chanArb.UpdateContractSignals(signals)
// Set current block height.
heightHint := uint32(1000)
chanArbCtx.chanArb.blocks <- int32(heightHint)
// Create testing HTLCs.
htlcExpiryBase := heightHint + uint32(10)
htlcWithPreimage := channeldb.HTLC{
HtlcIndex: 99,
RefundTimeout: htlcExpiryBase + 2,
RHash: rHash,
Incoming: true,
}
htlc := channeldb.HTLC{
HtlcIndex: 100,
RefundTimeout: htlcExpiryBase + 3,
}
// We now send two HTLC updates, one for local HTLC set and the other
// for remote HTLC set.
htlcUpdates <- &ContractUpdate{
HtlcKey: LocalHtlcSet,
// This will make the deadline of the local anchor resolution
// to be htlcWithPreimage's CLTV minus heightHint since the
// incoming HTLC (toLocalHTLCs) has a lower CLTV value and is
// preimage available.
Htlcs: []channeldb.HTLC{htlc, htlcWithPreimage},
}
htlcUpdates <- &ContractUpdate{
HtlcKey: RemoteHtlcSet,
// This will make the deadline of the remote anchor resolution
// to be htlcWithPreimage's CLTV minus heightHint because the
// incoming HTLC (toRemoteHTLCs) has a lower CLTV.
Htlcs: []channeldb.HTLC{htlc, htlcWithPreimage},
}
errChan := make(chan error, 1) errChan := make(chan error, 1)
respChan := make(chan *wire.MsgTx, 1) respChan := make(chan *wire.MsgTx, 1)
@ -2254,6 +2308,20 @@ func TestChannelArbitratorAnchors(t *testing.T) {
} }
assertResolverReport(t, reports, expectedReport) assertResolverReport(t, reports, expectedReport)
// We expect two anchor inputs, the local and the remote to be swept.
// Thus we should expect there are two deadlines used, both are equal
// to htlcWithPreimage's CLTV minus current block height.
require.Equal(t, 2, len(chanArbCtx.sweeper.deadlines))
require.EqualValues(t,
htlcWithPreimage.RefundTimeout-heightHint,
chanArbCtx.sweeper.deadlines[0],
)
require.EqualValues(t,
htlcWithPreimage.RefundTimeout-heightHint,
chanArbCtx.sweeper.deadlines[1],
)
} }
// putResolverReportInChannel returns a put report function which will pipe // putResolverReportInChannel returns a put report function which will pipe

@ -108,14 +108,17 @@ type mockSweeper struct {
sweepTx *wire.MsgTx sweepTx *wire.MsgTx
sweepErr error sweepErr error
createSweepTxChan chan *wire.MsgTx createSweepTxChan chan *wire.MsgTx
deadlines []uint32
} }
func newMockSweeper() *mockSweeper { func newMockSweeper() *mockSweeper {
return &mockSweeper{ return &mockSweeper{
sweptInputs: make(chan input.Input), sweptInputs: make(chan input.Input, 3),
updatedInputs: make(chan wire.OutPoint), updatedInputs: make(chan wire.OutPoint),
sweepTx: &wire.MsgTx{}, sweepTx: &wire.MsgTx{},
createSweepTxChan: make(chan *wire.MsgTx), createSweepTxChan: make(chan *wire.MsgTx),
deadlines: []uint32{},
} }
} }
@ -124,6 +127,11 @@ func (s *mockSweeper) SweepInput(input input.Input, params sweep.Params) (
s.sweptInputs <- input s.sweptInputs <- input
// Update the deadlines used if it's set.
if params.Fee.ConfTarget != 0 {
s.deadlines = append(s.deadlines, params.Fee.ConfTarget)
}
result := make(chan sweep.Result, 1) result := make(chan sweep.Result, 1)
result <- sweep.Result{ result <- sweep.Result{
Tx: s.sweepTx, Tx: s.sweepTx,