Skip to content

Commit b87f96a

Browse files
committed
lnwallet: add table-driven test for evaluateNoOpHtlc helper
1 parent 9594f86 commit b87f96a

File tree

1 file changed

+250
-0
lines changed

1 file changed

+250
-0
lines changed

lnwallet/channel_test.go

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11507,3 +11507,253 @@ func TestNoopAddBelowReserve(t *testing.T) {
1150711507
bobChan.LocalChanReserve()+htlcAmt.ToSatoshis(),
1150811508
)
1150911509
}
11510+
11511+
// TestEvaluateNoOpHtlc tests that the noop htlc evaluator helper function
11512+
// produces the expected balance deltas from various starting states.
11513+
func TestEvaluateNoOpHtlc(t *testing.T) {
11514+
testCases := []struct {
11515+
name string
11516+
localBalance, remoteBalance btcutil.Amount
11517+
localReserve, remoteReserve btcutil.Amount
11518+
entry *paymentDescriptor
11519+
receiver lntypes.ChannelParty
11520+
balanceDeltas *lntypes.Dual[int64]
11521+
expectedDeltas *lntypes.Dual[int64]
11522+
}{
11523+
{
11524+
name: "local above reserve",
11525+
entry: &paymentDescriptor{
11526+
Amount: lnwire.MilliSatoshi(2500),
11527+
},
11528+
receiver: lntypes.Local,
11529+
balanceDeltas: &lntypes.Dual[int64]{
11530+
Local: 0,
11531+
Remote: 0,
11532+
},
11533+
expectedDeltas: &lntypes.Dual[int64]{
11534+
Local: 0,
11535+
Remote: 2_500,
11536+
},
11537+
},
11538+
{
11539+
name: "remote above reserve",
11540+
entry: &paymentDescriptor{
11541+
Amount: lnwire.MilliSatoshi(2500),
11542+
},
11543+
receiver: lntypes.Remote,
11544+
balanceDeltas: &lntypes.Dual[int64]{
11545+
Local: 0,
11546+
Remote: 0,
11547+
},
11548+
expectedDeltas: &lntypes.Dual[int64]{
11549+
Local: 2_500,
11550+
Remote: 0,
11551+
},
11552+
},
11553+
{
11554+
name: "local below reserve",
11555+
entry: &paymentDescriptor{
11556+
Amount: lnwire.MilliSatoshi(2500),
11557+
},
11558+
receiver: lntypes.Local,
11559+
localBalance: 25_000,
11560+
localReserve: 50_000,
11561+
balanceDeltas: &lntypes.Dual[int64]{
11562+
Local: 0,
11563+
Remote: 0,
11564+
},
11565+
expectedDeltas: &lntypes.Dual[int64]{
11566+
Local: 2_500,
11567+
Remote: 0,
11568+
},
11569+
},
11570+
{
11571+
name: "remote below reserve",
11572+
entry: &paymentDescriptor{
11573+
Amount: lnwire.MilliSatoshi(2500),
11574+
},
11575+
receiver: lntypes.Remote,
11576+
remoteBalance: 25_000,
11577+
remoteReserve: 50_000,
11578+
balanceDeltas: &lntypes.Dual[int64]{
11579+
Local: 0,
11580+
Remote: 0,
11581+
},
11582+
expectedDeltas: &lntypes.Dual[int64]{
11583+
Local: 0,
11584+
Remote: 2_500,
11585+
},
11586+
},
11587+
11588+
{
11589+
name: "local above reserve with delta",
11590+
entry: &paymentDescriptor{
11591+
Amount: lnwire.MilliSatoshi(2500),
11592+
},
11593+
receiver: lntypes.Local,
11594+
localBalance: 25_000,
11595+
localReserve: 50_000,
11596+
balanceDeltas: &lntypes.Dual[int64]{
11597+
Local: 25_001_000,
11598+
Remote: 0,
11599+
},
11600+
expectedDeltas: &lntypes.Dual[int64]{
11601+
Local: 25_001_000,
11602+
Remote: 2_500,
11603+
},
11604+
},
11605+
{
11606+
name: "remote above reserve with delta",
11607+
entry: &paymentDescriptor{
11608+
Amount: lnwire.MilliSatoshi(2500),
11609+
},
11610+
receiver: lntypes.Remote,
11611+
remoteBalance: 25_000,
11612+
remoteReserve: 50_000,
11613+
balanceDeltas: &lntypes.Dual[int64]{
11614+
Local: 0,
11615+
Remote: 25_001_000,
11616+
},
11617+
expectedDeltas: &lntypes.Dual[int64]{
11618+
Local: 2_500,
11619+
Remote: 25_001_000,
11620+
},
11621+
},
11622+
{
11623+
name: "local below reserve with delta",
11624+
entry: &paymentDescriptor{
11625+
Amount: lnwire.MilliSatoshi(2500),
11626+
},
11627+
receiver: lntypes.Local,
11628+
localBalance: 25_000,
11629+
localReserve: 50_000,
11630+
balanceDeltas: &lntypes.Dual[int64]{
11631+
Local: 24_999_000,
11632+
Remote: 0,
11633+
},
11634+
expectedDeltas: &lntypes.Dual[int64]{
11635+
Local: 25_001_500,
11636+
Remote: 0,
11637+
},
11638+
},
11639+
{
11640+
name: "remote below reserve with delta",
11641+
entry: &paymentDescriptor{
11642+
Amount: lnwire.MilliSatoshi(2500),
11643+
},
11644+
receiver: lntypes.Remote,
11645+
remoteBalance: 25_000,
11646+
remoteReserve: 50_000,
11647+
balanceDeltas: &lntypes.Dual[int64]{
11648+
Local: 0,
11649+
Remote: 24_998_000,
11650+
},
11651+
expectedDeltas: &lntypes.Dual[int64]{
11652+
Local: 0,
11653+
Remote: 25_000_500,
11654+
},
11655+
},
11656+
{
11657+
name: "local above reserve with negative delta",
11658+
entry: &paymentDescriptor{
11659+
Amount: lnwire.MilliSatoshi(2500),
11660+
},
11661+
receiver: lntypes.Remote,
11662+
localBalance: 55_000,
11663+
localReserve: 50_000,
11664+
balanceDeltas: &lntypes.Dual[int64]{
11665+
Local: -4_999_000,
11666+
Remote: 0,
11667+
},
11668+
expectedDeltas: &lntypes.Dual[int64]{
11669+
Local: -4_999_000,
11670+
Remote: 2_500,
11671+
},
11672+
},
11673+
{
11674+
name: "remote above reserve with negative delta",
11675+
entry: &paymentDescriptor{
11676+
Amount: lnwire.MilliSatoshi(2500),
11677+
},
11678+
receiver: lntypes.Remote,
11679+
remoteBalance: 55_000,
11680+
remoteReserve: 50_000,
11681+
balanceDeltas: &lntypes.Dual[int64]{
11682+
Local: 0,
11683+
Remote: -4_999_000,
11684+
},
11685+
expectedDeltas: &lntypes.Dual[int64]{
11686+
Local: 2_500,
11687+
Remote: -4_999_000,
11688+
},
11689+
},
11690+
{
11691+
name: "local below reserve with negative delta",
11692+
entry: &paymentDescriptor{
11693+
Amount: lnwire.MilliSatoshi(2500),
11694+
},
11695+
receiver: lntypes.Local,
11696+
localBalance: 55_000,
11697+
localReserve: 50_000,
11698+
balanceDeltas: &lntypes.Dual[int64]{
11699+
Local: -5_001_000,
11700+
Remote: 0,
11701+
},
11702+
expectedDeltas: &lntypes.Dual[int64]{
11703+
Local: -4_998_500,
11704+
Remote: 0,
11705+
},
11706+
},
11707+
{
11708+
name: "remote below reserve with negative delta",
11709+
entry: &paymentDescriptor{
11710+
Amount: lnwire.MilliSatoshi(2500),
11711+
},
11712+
receiver: lntypes.Remote,
11713+
remoteBalance: 55_000,
11714+
remoteReserve: 50_000,
11715+
balanceDeltas: &lntypes.Dual[int64]{
11716+
Local: 0,
11717+
Remote: -5_001_000,
11718+
},
11719+
expectedDeltas: &lntypes.Dual[int64]{
11720+
Local: 0,
11721+
Remote: -4_998_500,
11722+
},
11723+
},
11724+
}
11725+
11726+
chanType := channeldb.SimpleTaprootFeatureBit |
11727+
channeldb.AnchorOutputsBit | channeldb.ZeroHtlcTxFeeBit |
11728+
channeldb.SingleFunderTweaklessBit | channeldb.TapscriptRootBit
11729+
aliceChan, _, err := CreateTestChannels(t, chanType)
11730+
require.NoError(t, err, "unable to create test channels")
11731+
11732+
for _, testCase := range testCases {
11733+
tc := testCase
11734+
11735+
t.Logf("Running test case: %s", testCase.name)
11736+
11737+
if tc.localBalance != 0 && tc.localReserve != 0 {
11738+
aliceChan.channelState.LocalChanCfg.ChanReserve =
11739+
tc.localReserve
11740+
11741+
aliceChan.channelState.LocalCommitment.LocalBalance =
11742+
lnwire.NewMSatFromSatoshis(tc.localBalance)
11743+
}
11744+
11745+
if tc.remoteBalance != 0 && tc.remoteReserve != 0 {
11746+
aliceChan.channelState.RemoteChanCfg.ChanReserve =
11747+
tc.remoteReserve
11748+
11749+
aliceChan.channelState.RemoteCommitment.RemoteBalance =
11750+
lnwire.NewMSatFromSatoshis(tc.remoteBalance)
11751+
}
11752+
11753+
aliceChan.evaluateNoOpHtlc(
11754+
tc.entry, tc.receiver, tc.balanceDeltas,
11755+
)
11756+
11757+
require.Equal(t, tc.expectedDeltas, tc.balanceDeltas)
11758+
}
11759+
}

0 commit comments

Comments
 (0)