From 441d95c40ee1393134c669a4f98d35e24d42afea Mon Sep 17 00:00:00 2001 From: ffranr Date: Thu, 19 Jan 2023 14:04:23 +0000 Subject: [PATCH] clientdb: add snapshot of spending accounts to local batch snapshot The account versions from the spending accounts snapshot will be used in estimating on-chain fees. --- clientdb/batch.go | 7 +++++-- clientdb/batch_snapshot.go | 36 +++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/clientdb/batch.go b/clientdb/batch.go index a76fd94d4..d3aa8c9ba 100644 --- a/clientdb/batch.go +++ b/clientdb/batch.go @@ -105,6 +105,8 @@ func (db *DB) StorePendingBatch(batch *order.Batch, orders []order.Nonce, return err } + spendingAccountsSnapshot := accounts + var updatedAccounts []*account.Account for idx, acct := range accounts { accountKey := getAccountKey(acct) @@ -125,10 +127,11 @@ func (db *DB) StorePendingBatch(batch *order.Batch, orders []order.Nonce, return err } - // Before we are done, we store a snapshot of the this batch, + // Before we are done, we store a snapshot of the batch, // so we retain this history for later. snapshot, err := NewSnapshot( - batch, updatedOrders, updatedAccounts, + batch, updatedOrders, spendingAccountsSnapshot, + updatedAccounts, ) if err != nil { return err diff --git a/clientdb/batch_snapshot.go b/clientdb/batch_snapshot.go index d9bd9b0d7..3daf6ae95 100644 --- a/clientdb/batch_snapshot.go +++ b/clientdb/batch_snapshot.go @@ -82,6 +82,14 @@ type LocalBatchSnapshot struct { // the batch transaction. BatchTxFeeRate chainfee.SatPerKWeight + // spendingAccountsSnapshot is a snapshot of the local accounts that + // participated in this batch before any potential account updates were + // applied. + // + // NOTE: this field is used for estimating on-chains fee using the + // account version. + SpendingAccountsSnapshot map[[33]byte]*account.Account + // Account holds snapshots of the ending state of the local accounts // that participated in this batch. Accounts map[[33]byte]*account.Account @@ -97,6 +105,7 @@ type LocalBatchSnapshot struct { // NewSnapshot creates a new LocalBatchSnapshot from the passed order batched. func NewSnapshot(batch *order.Batch, ourOrders []order.Order, + spendingAccountsSnapshot []*account.Account, accounts []*account.Account) (*LocalBatchSnapshot, error) { // We only support LinearFeeSchedule at this point (because of @@ -107,6 +116,14 @@ func NewSnapshot(batch *order.Batch, ourOrders []order.Order, batch.ExecutionFee) } + // Reformulate account data structure into a map keyed on trader key. + spendingAccsSnapshot := make(map[[33]byte]*account.Account) + for _, a := range accounts { + var key [33]byte + copy(key[:], a.TraderKey.PubKey.SerializeCompressed()) + spendingAccsSnapshot[key] = a + } + as := make(map[[33]byte]*account.Account) for _, a := range accounts { var key [33]byte @@ -120,15 +137,16 @@ func NewSnapshot(batch *order.Batch, ourOrders []order.Order, } snapshot := &LocalBatchSnapshot{ - Version: batch.Version, - BatchID: batch.ID, - ClearingPrices: batch.ClearingPrices, - ExecutionFee: *feeSched, - BatchTX: batch.BatchTX, - BatchTxFeeRate: batch.BatchTxFeeRate, - Accounts: as, - Orders: os, - MatchedOrders: batch.MatchedOrders, + Version: batch.Version, + BatchID: batch.ID, + ClearingPrices: batch.ClearingPrices, + ExecutionFee: *feeSched, + BatchTX: batch.BatchTX, + BatchTxFeeRate: batch.BatchTxFeeRate, + SpendingAccountsSnapshot: spendingAccsSnapshot, + Accounts: as, + Orders: os, + MatchedOrders: batch.MatchedOrders, } return snapshot, nil