From ed44aa74553fa3f9dc10d039a6ff7cd8b7307d7a Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 27 Aug 2024 11:46:09 -0500 Subject: [PATCH] rpc: fix ordering of feature bit check in rpcCommitmentType We need to check if it has a tapsript root first, as if it has a tapscript root, then it's also a taproot channel. By checking if it has a tapscript root first, we'll now display the proper commitment type for RPC responses. --- rpcserver.go | 6 +++-- rpcserver_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index 302b566b2..35212fa43 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -4547,12 +4547,13 @@ func rpcCommitmentType(chanType channeldb.ChannelType) lnrpc.CommitmentType { // first check whether it has anchors, since in that case it would also // be tweakless. switch { - case chanType.IsTaproot(): - return lnrpc.CommitmentType_SIMPLE_TAPROOT case chanType.HasTapscriptRoot(): return lnrpc.CommitmentType_SIMPLE_TAPROOT_OVERLAY + case chanType.IsTaproot(): + return lnrpc.CommitmentType_SIMPLE_TAPROOT + case chanType.HasLeaseExpiration(): return lnrpc.CommitmentType_SCRIPT_ENFORCED_LEASE @@ -4561,6 +4562,7 @@ func rpcCommitmentType(chanType channeldb.ChannelType) lnrpc.CommitmentType { case chanType.IsTweakless(): return lnrpc.CommitmentType_STATIC_REMOTE_KEY + default: return lnrpc.CommitmentType_LEGACY diff --git a/rpcserver_test.go b/rpcserver_test.go index 9dd3c3f86..b96a25ac3 100644 --- a/rpcserver_test.go +++ b/rpcserver_test.go @@ -77,3 +77,65 @@ func TestAuxDataParser(t *testing.T) { require.NotNil(t, resp) require.Equal(t, []byte{0x00, 0x00}, resp.CustomChannelData) } + +// TestRpcCommitmentType tests the rpcCommitmentType returns the corect +// commitment type given a channel type. +func TestRpcCommitmentType(t *testing.T) { + tests := []struct { + name string + chanType channeldb.ChannelType + want lnrpc.CommitmentType + }{ + { + name: "tapscript overlay", + chanType: channeldb.ChannelType( + channeldb.SimpleTaprootFeatureBit | + channeldb.TapscriptRootBit, + ), + want: lnrpc.CommitmentType_SIMPLE_TAPROOT_OVERLAY, + }, + { + name: "simple taproot", + chanType: channeldb.ChannelType( + channeldb.SimpleTaprootFeatureBit, + ), + want: lnrpc.CommitmentType_SIMPLE_TAPROOT, + }, + { + name: "lease expiration", + chanType: channeldb.ChannelType( + channeldb.LeaseExpirationBit, + ), + want: lnrpc.CommitmentType_SCRIPT_ENFORCED_LEASE, + }, + { + name: "anchors", + chanType: channeldb.ChannelType( + channeldb.AnchorOutputsBit, + ), + want: lnrpc.CommitmentType_ANCHORS, + }, + { + name: "tweakless", + chanType: channeldb.ChannelType( + channeldb.SingleFunderTweaklessBit, + ), + want: lnrpc.CommitmentType_STATIC_REMOTE_KEY, + }, + { + name: "legacy", + chanType: channeldb.ChannelType( + channeldb.SingleFunderBit, + ), + want: lnrpc.CommitmentType_LEGACY, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal( + t, tt.want, rpcCommitmentType(tt.chanType), + ) + }) + } +}