From 73e622a31e7aac10e84bad683e395785e70a4fad Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 7 Mar 2024 20:08:13 -0800 Subject: [PATCH] lnwallet/chancloser: add fee rate to ClosePending This'll be useful to communicate what the new fee rate is to an RPC caller. --- lnwallet/chancloser/rbf_coop_states.go | 6 ++++++ lnwallet/chancloser/rbf_coop_transitions.go | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lnwallet/chancloser/rbf_coop_states.go b/lnwallet/chancloser/rbf_coop_states.go index b6728629b..5b8ba8bcc 100644 --- a/lnwallet/chancloser/rbf_coop_states.go +++ b/lnwallet/chancloser/rbf_coop_states.go @@ -663,6 +663,9 @@ type LocalOfferSent struct { // ProposedFee is the fee we proposed to the remote party. ProposedFee btcutil.Amount + // ProposedFeeRate is the fee rate we proposed to the remote party. + ProposedFeeRate chainfee.SatPerVByte + // LocalSig is the signature we sent to the remote party. LocalSig lnwire.Sig } @@ -706,6 +709,9 @@ func (l *LocalOfferSent) IsTerminal() bool { type ClosePending struct { // CloseTx is the pending close transaction. CloseTx *wire.MsgTx + + // FeeRate is the fee rate of the closing transaction. + FeeRate chainfee.SatPerVByte } // String returns the name of the state for ClosePending. diff --git a/lnwallet/chancloser/rbf_coop_transitions.go b/lnwallet/chancloser/rbf_coop_transitions.go index 1c7612541..9dec4ccfb 100644 --- a/lnwallet/chancloser/rbf_coop_transitions.go +++ b/lnwallet/chancloser/rbf_coop_transitions.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/mempool" "github.com/btcsuite/btcd/wire" @@ -14,6 +15,7 @@ import ( "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnutils" "github.com/lightningnetwork/lnd/lnwallet" + "github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/protofsm" "github.com/lightningnetwork/lnd/tlv" @@ -171,7 +173,7 @@ func (c *ChannelActive) ProcessEvent(event ProtocolEvent, env *Environment, } chancloserLog.Infof("ChannelPoint(%v): sending shutdown msg, "+ - "delivery_script=%v", env.ChanPoint, shutdownScript) + "delivery_script=%x", env.ChanPoint, shutdownScript) // From here, we'll transition to the shutdown pending state. In // this state we await their shutdown message (self loop), then @@ -730,6 +732,7 @@ func (l *LocalCloseStart) ProcessEvent(event ProtocolEvent, env *Environment, return &CloseStateTransition{ NextState: &LocalOfferSent{ ProposedFee: absoluteFee, + ProposedFeeRate: msg.TargetFeeRate, LocalSig: wireSig, CloseChannelTerms: l.CloseChannelTerms, }, @@ -839,6 +842,7 @@ func (l *LocalOfferSent) ProcessEvent(event ProtocolEvent, env *Environment, return &CloseStateTransition{ NextState: &ClosePending{ CloseTx: closeTx, + FeeRate: l.ProposedFeeRate, }, NewEvents: fn.Some(protofsm.EmittedEvent[ProtocolEvent]{ ExternalEvents: broadcastEvent, @@ -995,11 +999,20 @@ func (l *RemoteCloseStart) ProcessEvent(event ProtocolEvent, env *Environment, sendEvent, broadcastEvent, } + // We'll also compute the final fee rate that the remote party + // paid based off the absolute fee and the size of the closing + // transaction. + vSize := mempool.GetTxVirtualSize(btcutil.NewTx(closeTx)) + feeRate := chainfee.SatPerVByte( + int64(msg.SigMsg.FeeSatoshis) / vSize, + ) + // Now that we've extracted the signature, we'll transition to // the next state where we'll sign+broadcast the sig. return &CloseStateTransition{ NextState: &ClosePending{ CloseTx: closeTx, + FeeRate: feeRate, }, NewEvents: fn.Some(protofsm.EmittedEvent[ProtocolEvent]{ ExternalEvents: daemonEvents,