-
Notifications
You must be signed in to change notification settings - Fork 21
/
snapshot.go
500 lines (437 loc) · 16.4 KB
/
snapshot.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package banking
import (
"context"
"fmt"
"math/big"
"sort"
"time"
"code.vegaprotocol.io/vega/core/types"
vgcontext "code.vegaprotocol.io/vega/libs/context"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/proto"
"code.vegaprotocol.io/vega/logging"
checkpoint "code.vegaprotocol.io/vega/protos/vega/checkpoint/v1"
snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
"github.com/emirpasic/gods/sets/treeset"
)
var (
withdrawalsKey = (&types.PayloadBankingWithdrawals{}).Key()
depositsKey = (&types.PayloadBankingDeposits{}).Key()
seenKey = (&types.PayloadBankingSeen{}).Key()
assetActionsKey = (&types.PayloadBankingAssetActions{}).Key()
recurringTransfersKey = (&types.PayloadBankingRecurringTransfers{}).Key()
scheduledTransfersKey = (&types.PayloadBankingScheduledTransfers{}).Key()
primaryBridgeStateKey = (&types.PayloadBankingPrimaryBridgeState{}).Key()
secondaryBridgeStateKey = (&types.PayloadBankingEVMBridgeStates{}).Key()
recurringGovTransfersKey = (&types.PayloadBankingRecurringGovernanceTransfers{}).Key()
scheduledGovTransfersKey = (&types.PayloadBankingScheduledGovernanceTransfers{}).Key()
transferFeeDiscountsKey = (&types.PayloadBankingTransferFeeDiscounts{}).Key()
hashKeys = []string{
withdrawalsKey,
depositsKey,
seenKey,
assetActionsKey,
recurringTransfersKey,
scheduledTransfersKey,
primaryBridgeStateKey,
secondaryBridgeStateKey,
recurringGovTransfersKey,
scheduledGovTransfersKey,
transferFeeDiscountsKey,
}
)
type bankingSnapshotState struct {
serialisedWithdrawals []byte
serialisedDeposits []byte
serialisedSeen []byte
serialisedAssetActions []byte
serialisedRecurringTransfers []byte
serialisedScheduledTransfers []byte
serialisedPrimaryBridgeState []byte
serialisedSecondaryBridgeState []byte
serialisedGovRecurringTransfers []byte
serialisedGovScheduledTransfers []byte
serialisedTransferFeeDiscounts []byte
}
func (e *Engine) Namespace() types.SnapshotNamespace {
return types.BankingSnapshot
}
func (e *Engine) Keys() []string {
return hashKeys
}
func (e *Engine) Stopped() bool {
return false
}
func (e *Engine) serialisePrimaryBridgeState() ([]byte, error) {
payload := types.Payload{
Data: &types.PayloadBankingPrimaryBridgeState{
BankingBridgeState: &types.BankingBridgeState{
Active: e.primaryBridgeState.active,
BlockHeight: e.primaryBridgeState.block,
LogIndex: e.primaryBridgeState.logIndex,
ChainID: e.primaryEthChainID,
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseSecondaryBridgeState() ([]byte, error) {
payload := types.Payload{
Data: &types.PayloadBankingEVMBridgeStates{
// we only have one bridge state atm, its easy
BankingBridgeStates: []*checkpoint.BridgeState{
{
Active: e.secondaryBridgeState.active,
BlockHeight: e.secondaryBridgeState.block,
LogIndex: e.secondaryBridgeState.logIndex,
ChainId: e.secondaryEthChainID,
},
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseRecurringTransfers() ([]byte, error) {
payload := types.Payload{
Data: &types.PayloadBankingRecurringTransfers{
BankingRecurringTransfers: e.getRecurringTransfers(),
NextMetricUpdate: e.nextMetricUpdate,
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseScheduledTransfers() ([]byte, error) {
payload := types.Payload{
Data: &types.PayloadBankingScheduledTransfers{
BankingScheduledTransfers: e.getScheduledTransfers(),
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseRecurringGovernanceTransfers() ([]byte, error) {
payload := types.Payload{
Data: &types.PayloadBankingRecurringGovernanceTransfers{
BankingRecurringGovernanceTransfers: e.getRecurringGovernanceTransfers(),
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseScheduledGovernanceTransfers() ([]byte, error) {
payload := types.Payload{
Data: &types.PayloadBankingScheduledGovernanceTransfers{
BankingScheduledGovernanceTransfers: e.getScheduledGovernanceTransfers(),
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialisedTransferFeeDiscounts() ([]byte, error) {
partyAssetDiscounts := make([]*snapshot.PartyAssetAmount, 0, len(e.feeDiscountPerPartyAndAsset))
for k, v := range e.feeDiscountPerPartyAndAsset {
partyAssetDiscounts = append(partyAssetDiscounts, &snapshot.PartyAssetAmount{
Party: k.party,
Asset: k.asset,
Amount: v.String(),
})
}
sort.SliceStable(partyAssetDiscounts, func(i, j int) bool {
if partyAssetDiscounts[i].Party == partyAssetDiscounts[j].Party {
return partyAssetDiscounts[i].Asset < partyAssetDiscounts[j].Asset
}
return partyAssetDiscounts[i].Party < partyAssetDiscounts[j].Party
})
payload := types.Payload{
Data: &types.PayloadBankingTransferFeeDiscounts{
BankingTransferFeeDiscounts: &snapshot.BankingTransferFeeDiscounts{
PartyAssetDiscount: partyAssetDiscounts,
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseAssetActions() ([]byte, error) {
payload := types.Payload{
Data: &types.PayloadBankingAssetActions{
BankingAssetActions: &types.BankingAssetActions{
AssetAction: e.getAssetActions(),
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseWithdrawals() ([]byte, error) {
withdrawals := make([]*types.RWithdrawal, 0, len(e.withdrawals))
for _, v := range e.withdrawals {
withdrawals = append(withdrawals, &types.RWithdrawal{Ref: v.ref.String(), Withdrawal: v.w})
}
sort.SliceStable(withdrawals, func(i, j int) bool { return withdrawals[i].Ref < withdrawals[j].Ref })
payload := types.Payload{
Data: &types.PayloadBankingWithdrawals{
BankingWithdrawals: &types.BankingWithdrawals{
Withdrawals: withdrawals,
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseSeen() ([]byte, error) {
seen := &types.PayloadBankingSeen{
BankingSeen: &types.BankingSeen{
LastSeenPrimaryEthBlock: e.lastSeenPrimaryEthBlock,
LastSeenSecondaryEthBlock: e.lastSeenSecondaryEthBlock,
},
}
seen.BankingSeen.Refs = make([]string, 0, e.seenAssetActions.Size())
iter := e.seenAssetActions.Iterator()
for iter.Next() {
seen.BankingSeen.Refs = append(seen.BankingSeen.Refs, iter.Value().(string))
}
payload := types.Payload{Data: seen}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseDeposits() ([]byte, error) {
e.log.Debug("serialiseDeposits: called")
deposits := make([]*types.BDeposit, 0, len(e.deposits))
for _, v := range e.deposits {
deposits = append(deposits, &types.BDeposit{ID: v.ID, Deposit: v})
}
sort.SliceStable(deposits, func(i, j int) bool { return deposits[i].ID < deposits[j].ID })
if e.log.IsDebug() {
e.log.Info("serialiseDeposits: number of deposits:", logging.Int("len(deposits)", len(deposits)))
for i, d := range deposits {
e.log.Info("serialiseDeposits:", logging.Int("index", i), logging.String("ID", d.ID), logging.String("deposit", d.Deposit.String()))
}
}
payload := types.Payload{
Data: &types.PayloadBankingDeposits{
BankingDeposits: &types.BankingDeposits{
Deposit: deposits,
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseK(serialFunc func() ([]byte, error), dataField *[]byte) ([]byte, error) {
data, err := serialFunc()
if err != nil {
return nil, err
}
*dataField = data
return data, nil
}
// get the serialised form and hash of the given key.
func (e *Engine) serialise(k string) ([]byte, error) {
switch k {
case depositsKey:
return e.serialiseK(e.serialiseDeposits, &e.bss.serialisedDeposits)
case withdrawalsKey:
return e.serialiseK(e.serialiseWithdrawals, &e.bss.serialisedWithdrawals)
case seenKey:
return e.serialiseK(e.serialiseSeen, &e.bss.serialisedSeen)
case assetActionsKey:
return e.serialiseK(e.serialiseAssetActions, &e.bss.serialisedAssetActions)
case recurringTransfersKey:
return e.serialiseK(e.serialiseRecurringTransfers, &e.bss.serialisedRecurringTransfers)
case scheduledTransfersKey:
return e.serialiseK(e.serialiseScheduledTransfers, &e.bss.serialisedScheduledTransfers)
case recurringGovTransfersKey:
return e.serialiseK(e.serialiseRecurringGovernanceTransfers, &e.bss.serialisedGovRecurringTransfers)
case scheduledGovTransfersKey:
return e.serialiseK(e.serialiseScheduledGovernanceTransfers, &e.bss.serialisedGovScheduledTransfers)
case transferFeeDiscountsKey:
return e.serialiseK(e.serialisedTransferFeeDiscounts, &e.bss.serialisedTransferFeeDiscounts)
case primaryBridgeStateKey:
return e.serialiseK(e.serialisePrimaryBridgeState, &e.bss.serialisedPrimaryBridgeState)
case secondaryBridgeStateKey:
return e.serialiseK(e.serialiseSecondaryBridgeState, &e.bss.serialisedSecondaryBridgeState)
default:
return nil, types.ErrSnapshotKeyDoesNotExist
}
}
func (e *Engine) GetState(k string) ([]byte, []types.StateProvider, error) {
state, err := e.serialise(k)
return state, nil, err
}
func (e *Engine) LoadState(ctx context.Context, p *types.Payload) ([]types.StateProvider, error) {
if e.Namespace() != p.Data.Namespace() {
return nil, types.ErrInvalidSnapshotNamespace
}
// see what we're reloading
switch pl := p.Data.(type) {
case *types.PayloadBankingDeposits:
return nil, e.restoreDeposits(pl.BankingDeposits, p)
case *types.PayloadBankingWithdrawals:
return nil, e.restoreWithdrawals(pl.BankingWithdrawals, p)
case *types.PayloadBankingSeen:
return nil, e.restoreSeen(ctx, pl.BankingSeen, p)
case *types.PayloadBankingAssetActions:
return nil, e.restoreAssetActions(pl.BankingAssetActions, p)
case *types.PayloadBankingRecurringTransfers:
return nil, e.restoreRecurringTransfers(ctx, pl.BankingRecurringTransfers, pl.NextMetricUpdate, p)
case *types.PayloadBankingScheduledTransfers:
return nil, e.restoreScheduledTransfers(ctx, pl.BankingScheduledTransfers, p)
case *types.PayloadBankingRecurringGovernanceTransfers:
return nil, e.restoreRecurringGovernanceTransfers(ctx, pl.BankingRecurringGovernanceTransfers, p)
case *types.PayloadBankingScheduledGovernanceTransfers:
return nil, e.restoreScheduledGovernanceTransfers(ctx, pl.BankingScheduledGovernanceTransfers, p)
case *types.PayloadBankingPrimaryBridgeState:
return nil, e.restorePrimaryBridgeState(pl.BankingBridgeState, p)
case *types.PayloadBankingEVMBridgeStates:
return nil, e.restoreSecondaryBridgeState(pl.BankingBridgeStates, p)
case *types.PayloadBankingTransferFeeDiscounts:
return nil, e.restoreTransferFeeDiscounts(pl.BankingTransferFeeDiscounts, p)
default:
return nil, types.ErrUnknownSnapshotType
}
}
func (e *Engine) restoreRecurringTransfers(ctx context.Context, transfers *checkpoint.RecurringTransfers, nextMetricUpdate time.Time, p *types.Payload) error {
var err error
// ignore events here as we don't need to send them
_ = e.loadRecurringTransfers(ctx, transfers)
e.bss.serialisedRecurringTransfers, err = proto.Marshal(p.IntoProto())
e.nextMetricUpdate = nextMetricUpdate
return err
}
func (e *Engine) restoreRecurringGovernanceTransfers(ctx context.Context, transfers []*checkpoint.GovernanceTransfer, p *types.Payload) error {
var err error
_ = e.loadRecurringGovernanceTransfers(ctx, transfers)
e.bss.serialisedGovRecurringTransfers, err = proto.Marshal(p.IntoProto())
return err
}
func (e *Engine) restoreScheduledTransfers(ctx context.Context, transfers []*checkpoint.ScheduledTransferAtTime, p *types.Payload) error {
var err error
// ignore events
_, err = e.loadScheduledTransfers(ctx, transfers)
if err != nil {
return err
}
e.bss.serialisedScheduledTransfers, err = proto.Marshal(p.IntoProto())
return err
}
func (e *Engine) restoreScheduledGovernanceTransfers(ctx context.Context, transfers []*checkpoint.ScheduledGovernanceTransferAtTime, p *types.Payload) error {
var err error
e.loadScheduledGovernanceTransfers(ctx, transfers)
e.bss.serialisedGovScheduledTransfers, err = proto.Marshal(p.IntoProto())
return err
}
func (e *Engine) restorePrimaryBridgeState(state *types.BankingBridgeState, p *types.Payload) (err error) {
if state != nil {
e.primaryBridgeState = &bridgeState{
active: state.Active,
block: state.BlockHeight,
logIndex: state.LogIndex,
}
}
e.bss.serialisedPrimaryBridgeState, err = proto.Marshal(p.IntoProto())
return
}
func (e *Engine) restoreSecondaryBridgeState(state []*checkpoint.BridgeState, p *types.Payload) (err error) {
if state != nil {
e.secondaryBridgeState = &bridgeState{
active: state[0].Active,
block: state[0].BlockHeight,
logIndex: state[0].LogIndex,
}
}
e.bss.serialisedSecondaryBridgeState, err = proto.Marshal(p.IntoProto())
return
}
func (e *Engine) restoreDeposits(deposits *types.BankingDeposits, p *types.Payload) error {
var err error
for _, d := range deposits.Deposit {
e.deposits[d.ID] = d.Deposit
}
e.bss.serialisedDeposits, err = proto.Marshal(p.IntoProto())
return err
}
func (e *Engine) restoreWithdrawals(withdrawals *types.BankingWithdrawals, p *types.Payload) error {
var err error
for _, w := range withdrawals.Withdrawals {
ref := new(big.Int)
ref.SetString(w.Ref, 10)
e.withdrawalCnt.Add(e.withdrawalCnt, big.NewInt(1))
e.withdrawals[w.Withdrawal.ID] = withdrawalRef{
w: w.Withdrawal,
ref: ref,
}
}
e.bss.serialisedWithdrawals, err = proto.Marshal(p.IntoProto())
return err
}
func (e *Engine) restoreSeen(ctx context.Context, seen *types.BankingSeen, p *types.Payload) error {
var err error
e.log.Info("restoring seen", logging.Int("n", len(seen.Refs)))
e.seenAssetActions = treeset.NewWithStringComparator()
for _, v := range seen.Refs {
e.seenAssetActions.Add(v)
}
if vgcontext.InProgressUpgradeFrom(ctx, "v0.76.8") {
e.log.Info("migration code updating primary bridge last seen",
logging.String("address", e.bridgeAddresses[e.primaryEthChainID]),
logging.Uint64("last-seen", seen.LastSeenPrimaryEthBlock),
)
e.ethEventSource.UpdateContractBlock(
e.bridgeAddresses[e.primaryEthChainID],
e.primaryEthChainID,
seen.LastSeenPrimaryEthBlock,
)
e.log.Info("migration code updating primary bridge last seen",
logging.String("address", e.bridgeAddresses[e.secondaryEthChainID]),
logging.Uint64("last-seen", seen.LastSeenSecondaryEthBlock),
)
e.ethEventSource.UpdateContractBlock(
e.bridgeAddresses[e.secondaryEthChainID],
e.secondaryEthChainID,
seen.LastSeenSecondaryEthBlock,
)
}
e.lastSeenPrimaryEthBlock = seen.LastSeenPrimaryEthBlock
e.lastSeenSecondaryEthBlock = seen.LastSeenSecondaryEthBlock
e.bss.serialisedSeen, err = proto.Marshal(p.IntoProto())
return err
}
func (e *Engine) restoreAssetActions(aa *types.BankingAssetActions, p *types.Payload) error {
var err error
if err := e.loadAssetActions(aa.AssetAction); err != nil {
return fmt.Errorf("could not load asset actions: %w", err)
}
for _, aa := range e.assetActions {
if err := e.witness.RestoreResource(aa, e.onCheckDone); err != nil {
e.log.Panic("unable to restore witness resource", logging.String("id", aa.id), logging.Error(err))
}
}
e.bss.serialisedAssetActions, err = proto.Marshal(p.IntoProto())
return err
}
func (e *Engine) restoreTransferFeeDiscounts(
state *snapshot.BankingTransferFeeDiscounts,
p *types.Payload,
) (err error) {
if state == nil {
return nil
}
e.feeDiscountPerPartyAndAsset = make(map[partyAssetKey]*num.Uint, len(state.PartyAssetDiscount))
for _, v := range state.PartyAssetDiscount {
discount, _ := num.UintFromString(v.Amount, 10)
e.feeDiscountPerPartyAndAsset[e.feeDiscountKey(v.Asset, v.Party)] = discount
}
e.bss.serialisedTransferFeeDiscounts, err = proto.Marshal(p.IntoProto())
return
}
func (e *Engine) OnEpochRestore(_ context.Context, ep types.Epoch) {
e.log.Debug("epoch restoration notification received", logging.String("epoch", ep.String()))
e.currentEpoch = ep.Seq
}