-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcalculator.go
519 lines (442 loc) · 18.4 KB
/
calculator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package balance
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/balance"
"github.com/code-payments/code-server/pkg/code/data/timelock"
"github.com/code-payments/code-server/pkg/metrics"
"github.com/code-payments/code-server/pkg/solana"
timelock_token "github.com/code-payments/code-server/pkg/solana/timelock/v1"
)
type Source uint8
const (
UnknownSource Source = iota
CacheSource
BlockchainSource
)
const (
metricsPackageName = "balance"
)
var (
// ErrNegativeBalance indicates that a balance calculation resulted in a
// negative value.
ErrNegativeBalance = errors.New("balance calculation resulted in negative value")
// ErrNotManagedByCode indicates that an account is not owned by Code.
// It's up to callers to determine how to handle this situation within
// the context of a balance.
ErrNotManagedByCode = errors.New("explicitly not handling account not managed by code")
// ErrUnhandledAccount indicates that the balance calculator does not
// have strategies to handle the provided account.
ErrUnhandledAccount = errors.New("unhandled account")
)
// Calculator is a function that calculates a token account's balance
type Calculator func(ctx context.Context, data code_data.Provider, tokenAccount *common.Account) (uint64, error)
type Strategy func(ctx context.Context, tokenAccount *common.Account, state *State) (*State, error)
type State struct {
// We allow for negative balances in intermediary steps. This is to simplify
// coordination between strategies. In the end, the sum of all strategies must
// reflect an accurate picture of the balance, at which point we'll enforce this
// is positive.
current int64
}
// CalculateFromCache is the default and recommended strategy for reliably estimating
// a token account's balance using cached values.
//
// Note: Use this method when calculating balances for accounts that are managed by
// Code (ie. Timelock account) and operate within the L2 system.
func CalculateFromCache(ctx context.Context, data code_data.Provider, tokenAccount *common.Account) (uint64, error) {
tracer := metrics.TraceMethodCall(ctx, metricsPackageName, "CalculateFromCache")
tracer.AddAttribute("account", tokenAccount.PublicKey().ToBase58())
defer tracer.End()
timelockRecord, err := data.GetTimelockByVault(ctx, tokenAccount.PublicKey().ToBase58())
if err == timelock.ErrTimelockNotFound {
tracer.OnError(ErrNotManagedByCode)
return 0, ErrNotManagedByCode
} else if err != nil {
tracer.OnError(err)
return 0, err
}
// The strategy uses cached values from the intents system. The account must
// be managed by Code in order to return accurate values.
isManagedByCode := common.IsManagedByCode(ctx, timelockRecord)
if !isManagedByCode {
tracer.OnError(ErrNotManagedByCode)
return 0, ErrNotManagedByCode
}
// Pick a set of strategies relevant for the type of account, so we can optimize
// the number of DB calls.
//
// Overall, we're using a simple strategy that iterates over an account's history
// to unblock a scheduler implementation optimized for privacy.
//
// todo: Come up with a heurisitc that enables some form of checkpointing, so
// we're not iterating over all records every time.
strategies := []Strategy{
FundingFromExternalDeposits(ctx, data),
NetBalanceFromIntentActions(ctx, data),
}
if timelockRecord.DataVersion == timelock_token.DataVersionLegacy {
strategies = []Strategy{
FundingFromExternalDepositsForPrePrivacy2022Accounts(ctx, data),
NetBalanceFromPrePrivacy2022Intents(ctx, data),
}
}
balance, err := Calculate(
ctx,
tokenAccount,
0,
strategies...,
)
if err != nil {
tracer.OnError(err)
return 0, errors.Wrap(err, "error calculating token account balance")
}
return balance, nil
}
// CalculateFromBlockchain is the default and recommended strategy for reliably
// estimating a token account's balance from the blockchain. This strategy is
// resistant to various RPC failure nodes, and may return a cached value. The
// source of the balance calculation is returned.
//
// Note: Use this method when calculating token account balances that are external
// and not managed by Code and outside the L2 system.
//
// todo: add a batching variant
func CalculateFromBlockchain(ctx context.Context, data code_data.Provider, tokenAccount *common.Account) (uint64, Source, error) {
var cachedQuarks uint64
var cachedSlot uint64
var cachedUpdateTs time.Time
checkpointRecord, err := data.GetBalanceCheckpoint(ctx, tokenAccount.PublicKey().ToBase58())
if err == nil {
cachedQuarks = checkpointRecord.Quarks
cachedSlot = checkpointRecord.SlotCheckpoint
cachedUpdateTs = checkpointRecord.LastUpdatedAt
} else if err != balance.ErrCheckpointNotFound {
return 0, UnknownSource, err
}
// todo: we may need something that's more resistant to RPC nodes with stale account state
quarks, slot, err := data.GetBlockchainBalance(ctx, tokenAccount.PublicKey().ToBase58())
if err == solana.ErrNoBalance {
// We can't tell whether
// 1. RPC node is behind, and observed a state before the account existed
// 2. RPC node is ahead, and the account was closed
// because we don't have a slot to compare against the checkpoint.
//
// If the checkpoint was recently updated, we opt to trust that, optimizing
// to reduce potential race conditions for 1.
if time.Since(cachedUpdateTs) < 5*time.Minute {
return cachedQuarks, CacheSource, nil
}
return 0, BlockchainSource, nil
} else if err != nil {
// RPC node threw an error. Return the cached balance
return cachedQuarks, CacheSource, nil
}
// RPC node is behind, use cached balance
if cachedSlot > slot {
return cachedQuarks, CacheSource, nil
}
// Observed a balance that's more recent. Best-effort update the checkpoint.
if cachedSlot == 0 || (slot > cachedSlot && quarks != cachedQuarks) {
newCheckpointRecord := &balance.Record{
TokenAccount: tokenAccount.PublicKey().ToBase58(),
Quarks: quarks,
SlotCheckpoint: slot,
}
data.SaveBalanceCheckpoint(ctx, newCheckpointRecord)
}
return quarks, BlockchainSource, nil
}
// Calculate calculates a token account's balance using a starting point and a set
// of strategies. Each may be incomplete individually, but in total must form a
// complete balance calculation.
func Calculate(ctx context.Context, tokenAccount *common.Account, initialBalance uint64, strategies ...Strategy) (balance uint64, err error) {
balanceState := &State{
current: int64(initialBalance),
}
for _, strategy := range strategies {
balanceState, err = strategy(ctx, tokenAccount, balanceState)
if err != nil {
return 0, err
}
}
if balanceState.current < 0 {
return 0, ErrNegativeBalance
}
return uint64(balanceState.current), nil
}
// NetBalanceFromIntentActions is a balance calculation strategy that incorporates
// the net balance by applying payment intents to the current balance.
func NetBalanceFromIntentActions(ctx context.Context, data code_data.Provider) Strategy {
return func(ctx context.Context, tokenAccount *common.Account, state *State) (*State, error) {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "NetBalanceFromIntentActions",
"account": tokenAccount.PublicKey().ToBase58(),
})
netBalance, err := data.GetNetBalanceFromActions(ctx, tokenAccount.PublicKey().ToBase58())
if err != nil {
log.WithError(err).Warn("failure getting net balance from intent actions")
return nil, errors.Wrap(err, "error getting net balance from intent actions")
}
state.current += netBalance
return state, nil
}
}
func NetBalanceFromPrePrivacy2022Intents(ctx context.Context, data code_data.Provider) Strategy {
return func(ctx context.Context, tokenAccount *common.Account, state *State) (*State, error) {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "NetBalanceFromPrePrivacy2022Intents",
"account": tokenAccount.PublicKey().ToBase58(),
})
netBalance, err := data.GetNetBalanceFromPrePrivacy2022Intents(ctx, tokenAccount.PublicKey().ToBase58())
if err != nil {
log.WithError(err).Warn("failure getting net balance from pre-privacy intents")
return nil, errors.Wrap(err, "error getting net balance from pre-privacy intents")
}
state.current += netBalance
return state, nil
}
}
// FundingFromExternalDeposits is a balance calculation strategy that adds funding
// from deposits from external accounts.
func FundingFromExternalDeposits(ctx context.Context, data code_data.Provider) Strategy {
return func(ctx context.Context, tokenAccount *common.Account, state *State) (*State, error) {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "FundingFromExternalDeposits",
"account": tokenAccount.PublicKey().ToBase58(),
})
amount, err := data.GetTotalExternalDepositedAmountInQuarks(ctx, tokenAccount.PublicKey().ToBase58())
if err != nil {
log.WithError(err).Warn("failure getting external deposit amount")
return nil, errors.Wrap(err, "error getting external deposit amount")
}
state.current += int64(amount)
return state, nil
}
}
func FundingFromExternalDepositsForPrePrivacy2022Accounts(ctx context.Context, data code_data.Provider) Strategy {
return func(ctx context.Context, tokenAccount *common.Account, state *State) (*State, error) {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "FundingFromLegacyExternalDeposits",
"account": tokenAccount.PublicKey().ToBase58(),
})
amount, err := data.GetLegacyTotalExternalDepositAmountFromPrePrivacy2022Accounts(ctx, tokenAccount.PublicKey().ToBase58())
if err != nil {
log.WithError(err).Warn("failure getting external deposit amount")
return nil, errors.Wrap(err, "error getting external deposit amount")
}
state.current += int64(amount)
return state, nil
}
}
// BatchCalculator is a functiona that calculates a batch of accounts' balances
type BatchCalculator func(ctx context.Context, data code_data.Provider, accountRecordsBatch []*common.AccountRecords) (map[string]uint64, error)
type BatchStrategy func(ctx context.Context, tokenAccounts []string, state *BatchState) (*BatchState, error)
type BatchState struct {
// We allow for negative balances in intermediary steps. This is to simplify
// coordination between strategies. In the end, the sum of all strategies must
// reflect an accurate picture of the balance, at which point we'll enforce this
// is positive.
current map[string]int64
}
// BatchCalculateFromCacheWithAccountRecords is the default and recommended batch strategy
// or reliably estimating a set of token accounts' balance when common.AccountRecords are
// available.
//
// Note: Use this method when calculating balances for accounts that are managed by
// Code (ie. Timelock account) and operate within the L2 system.
//
// Note: This only supports post-privacy accounts. Use CalculateFromCache instead.
func BatchCalculateFromCacheWithAccountRecords(ctx context.Context, data code_data.Provider, accountRecordsBatch ...*common.AccountRecords) (map[string]uint64, error) {
tracer := metrics.TraceMethodCall(ctx, metricsPackageName, "BatchCalculateFromCacheWithAccountRecords")
defer tracer.End()
timelockRecords := make([]*timelock.Record, 0)
for _, accountRecords := range accountRecordsBatch {
if !accountRecords.IsTimelock() {
tracer.OnError(ErrNotManagedByCode)
return nil, ErrNotManagedByCode
}
timelockRecords = append(timelockRecords, accountRecords.Timelock)
}
balanceByTokenAccount, err := defaultBatchCalculationFromCache(ctx, data, timelockRecords)
if err != nil {
tracer.OnError(err)
return nil, err
}
return balanceByTokenAccount, nil
}
// BatchCalculateFromCacheWithTokenAccounts is the default and recommended batch strategy
// or reliably estimating a set of token accounts' balance when common.Account are
// available.
//
// Note: Use this method when calculating balances for accounts that are managed by
// Code (ie. Timelock account) and operate within the L2 system.
//
// Note: This only supports post-privacy accounts. Use CalculateFromCache instead.
func BatchCalculateFromCacheWithTokenAccounts(ctx context.Context, data code_data.Provider, tokenAccounts ...*common.Account) (map[string]uint64, error) {
tracer := metrics.TraceMethodCall(ctx, metricsPackageName, "BatchCalculateFromCacheWithTokenAccounts")
defer tracer.End()
tokenAccountStrings := make([]string, len(tokenAccounts))
for i, tokenAccount := range tokenAccounts {
tokenAccountStrings[i] = tokenAccount.PublicKey().ToBase58()
}
timelockRecordsByVault, err := data.GetTimelockByVaultBatch(ctx, tokenAccountStrings...)
if err == timelock.ErrTimelockNotFound {
tracer.OnError(ErrNotManagedByCode)
return nil, ErrNotManagedByCode
} else if err != nil {
tracer.OnError(err)
return nil, err
}
timelockRecords := make([]*timelock.Record, 0, len(timelockRecordsByVault))
for _, timelockRecord := range timelockRecordsByVault {
timelockRecords = append(timelockRecords, timelockRecord)
}
balanceByTokenAccount, err := defaultBatchCalculationFromCache(ctx, data, timelockRecords)
if err != nil {
tracer.OnError(err)
return nil, err
}
return balanceByTokenAccount, nil
}
func defaultBatchCalculationFromCache(ctx context.Context, data code_data.Provider, timelockRecords []*timelock.Record) (map[string]uint64, error) {
var tokenAccounts []string
for _, timelockRecord := range timelockRecords {
// The strategy uses cached values from the intents system. The account must
// be managed by Code in order to return accurate values.
isManagedByCode := common.IsManagedByCode(ctx, timelockRecord)
if !isManagedByCode {
return nil, ErrNotManagedByCode
}
// We only support post-privacy accounts
switch timelockRecord.DataVersion {
case timelock_token.DataVersion1:
tokenAccounts = append(tokenAccounts, timelockRecord.VaultAddress)
default:
return nil, ErrUnhandledAccount
}
}
return CalculateBatch(
ctx,
tokenAccounts,
FundingFromExternalDepositsBatch(ctx, data),
NetBalanceFromIntentActionsBatch(ctx, data),
)
}
// CalculateBatch calculates a set of token accounts' balance using a starting point
// and a set of strategies. Each may be incomplete individually, but in total must
// form a complete balance calculation.
func CalculateBatch(ctx context.Context, tokenAccounts []string, strategies ...BatchStrategy) (balanceByTokenAccount map[string]uint64, err error) {
balanceState := &BatchState{
current: make(map[string]int64),
}
for _, strategy := range strategies {
balanceState, err = strategy(ctx, tokenAccounts, balanceState)
if err != nil {
return nil, err
}
}
res := make(map[string]uint64)
for tokenAccount, balance := range balanceState.current {
if balance < 0 {
return nil, ErrNegativeBalance
}
res[tokenAccount] = uint64(balance)
}
return res, nil
}
// NetBalanceFromIntentActionsBatch is a balance calculation strategy that incorporates
// the net balance by applying payment intents to the current balance.
func NetBalanceFromIntentActionsBatch(ctx context.Context, data code_data.Provider) BatchStrategy {
return func(ctx context.Context, tokenAccounts []string, state *BatchState) (*BatchState, error) {
log := logrus.StandardLogger().WithField("method", "NetBalanceFromIntentActionsBatch")
netBalanceByAccount, err := data.GetNetBalanceFromActionsBatch(ctx, tokenAccounts...)
if err != nil {
log.WithError(err).Warn("failure getting net balance from intent actions")
return nil, errors.Wrap(err, "error getting net balance from intent actions")
}
for tokenAccount, netBalance := range netBalanceByAccount {
state.current[tokenAccount] += netBalance
}
return state, nil
}
}
// FundingFromExternalDepositsBatch is a balance calculation strategy that adds
// funding from deposits from external accounts.
func FundingFromExternalDepositsBatch(ctx context.Context, data code_data.Provider) BatchStrategy {
return func(ctx context.Context, tokenAccounts []string, state *BatchState) (*BatchState, error) {
log := logrus.StandardLogger().WithField("method", "FundingFromExternalDepositsBatch")
amountByAccount, err := data.GetTotalExternalDepositedAmountInQuarksBatch(ctx, tokenAccounts...)
if err != nil {
log.WithError(err).Warn("failure getting external deposit amount")
return nil, errors.Wrap(err, "error getting external deposit amount")
}
for tokenAccount, amount := range amountByAccount {
state.current[tokenAccount] += int64(amount)
}
return state, nil
}
}
// GetPrivateBalance gets an owner account's total private balance.
//
// Note: Assumes all private accounts have the same mint
func GetPrivateBalance(ctx context.Context, data code_data.Provider, owner *common.Account) (uint64, error) {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "GetPrivateBalance",
"owner": owner.PublicKey().ToBase58(),
})
tracer := metrics.TraceMethodCall(ctx, metricsPackageName, "GetPrivateBalance")
tracer.AddAttribute("owner", owner.PublicKey().ToBase58())
defer tracer.End()
accountRecordsByType, err := common.GetLatestTokenAccountRecordsForOwner(ctx, data, owner)
if err != nil {
log.WithError(err).Warn("failure getting latest token account records")
tracer.OnError(err)
return 0, err
}
if len(accountRecordsByType) == 0 {
tracer.OnError(ErrNotManagedByCode)
return 0, ErrNotManagedByCode
}
var accountRecordsBatch []*common.AccountRecords
for _, accountRecords := range accountRecordsByType {
switch accountRecords[0].General.AccountType {
case commonpb.AccountType_PRIMARY,
commonpb.AccountType_LEGACY_PRIMARY_2022,
commonpb.AccountType_REMOTE_SEND_GIFT_CARD,
commonpb.AccountType_RELATIONSHIP,
commonpb.AccountType_SWAP:
continue
}
accountRecordsBatch = append(accountRecordsBatch, accountRecords...)
}
balanceByAccount, err := BatchCalculateFromCacheWithAccountRecords(ctx, data, accountRecordsBatch...)
if err != nil {
log.WithError(err).Warn("failure getting balances")
tracer.OnError(err)
return 0, err
}
var total uint64
for _, batchRecords := range accountRecordsByType {
for _, records := range batchRecords {
total += balanceByAccount[records.General.TokenAccount]
}
}
return total, nil
}
func (s Source) String() string {
switch s {
case UnknownSource:
return "unknown"
case CacheSource:
return "cache"
case BlockchainSource:
return "blockchain"
}
return "unknown"
}