From 1a14e73f220d989bc3056e3ea9ddc60ee54d4bb1 Mon Sep 17 00:00:00 2001 From: ffranr Date: Thu, 19 Jan 2023 11:27:13 +0000 Subject: [PATCH] rpc: batch on-chain fee estimate calc includes account version Account version is taken from a snapshot of spending account states. This is because the account might be updated to a new version within the batch. --- rpcserver.go | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index f78222197..88e73f497 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -2266,6 +2266,9 @@ func (s *rpcServer) prepareLeasesResponse(ctx context.Context, // paid. totalNumChannels := 0 + // Keep a tally of the total number of channels per account key. + accountChannelsCount := make(map[[33]byte]uint32) + // Keep track of the index for the first lease found for this // batch. We'll use this to know where to start from when // populating the ChainFeeSat field for each lease. @@ -2282,7 +2285,8 @@ func (s *rpcServer) prepareLeasesResponse(ctx context.Context, } // Filter out the account if required. - _, ok = accounts[ourOrder.Details().AcctKey] + accountKey := ourOrder.Details().AcctKey + _, ok = accounts[accountKey] if len(accounts) != 0 && !ok { continue } @@ -2418,20 +2422,33 @@ func (s *rpcServer) prepareLeasesResponse(ctx context.Context, // Count the total number of channels. totalNumChannels++ + + // Count the total number of channels per + // account. + accountChannelsCount[accountKey]++ } // Estimate the chain fees paid for the number of // channels created in this batch and tally them. - // - // TODO(guggero): This is just an approximation! We - // should properly calculate the fees _per account_ as - // that's what we do on the server side. Then we can - // also take a look at the actual account version at the - // time of the batch. - chainFee := order.EstimateTraderFee( - uint32(totalNumChannels), batch.BatchTxFeeRate, - account.VersionInitialNoVersion, - ) + chainFee := btcutil.Amount(0) + for accountKey, numChannels := range accountChannelsCount { + // Calculate and sum the account specific chain + // fee estimate. + // + // The account version influences the chain fee. + // It is important to use the account version + // that was used when the batch was created. + // Accounts might be updated within the batch. + // Therefore, we query the account version from + // an account spending snapshot. + spendingAcctSnapshot := + batch.SpendingAccountsSnapshot[accountKey] + + chainFee += order.EstimateTraderFee( + numChannels, batch.BatchTxFeeRate, + spendingAcctSnapshot.Version, + ) + } // We'll need to compute the chain fee paid for each // lease. Since multiple leases can be created within a