Skip to content

Commit

Permalink
rpc: batch on-chain fee estimate calc includes account version
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ffranr committed Jan 19, 2023
1 parent 441d95c commit 1a14e73
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1a14e73

Please sign in to comment.