Skip to content

Commit

Permalink
clientdb: add snapshot of spending accounts to local batch snapshot
Browse files Browse the repository at this point in the history
The account versions from the spending accounts snapshot will be used in estimating on-chain fees.
  • Loading branch information
ffranr committed Jan 19, 2023
1 parent e0623e7 commit 441d95c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
7 changes: 5 additions & 2 deletions clientdb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
36 changes: 27 additions & 9 deletions clientdb/batch_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 441d95c

Please sign in to comment.