Skip to content

Commit 4f0d187

Browse files
committed
lnwallet: add table-driven test for evaluateNoOpHtlc helper
1 parent 439ba6b commit 4f0d187

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

0 commit comments

Comments
 (0)