-
Notifications
You must be signed in to change notification settings - Fork 22
/
snapshot.go
333 lines (287 loc) · 9.95 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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package banking
import (
"context"
"math/big"
"sort"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/proto"
"code.vegaprotocol.io/vega/logging"
checkpoint "code.vegaprotocol.io/vega/protos/vega/checkpoint/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()
bridgeStateKey = (&types.PayloadBankingBridgeState{}).Key()
hashKeys = []string{
withdrawalsKey,
depositsKey,
seenKey,
assetActionsKey,
recurringTransfersKey,
scheduledTransfersKey,
bridgeStateKey,
}
)
type bankingSnapshotState struct {
serialisedWithdrawals []byte
serialisedDeposits []byte
serialisedSeen []byte
serialisedAssetActions []byte
serialisedRecurringTransfers []byte
serialisedScheduledTransfers []byte
serialisedBridgeState []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) serialiseBridgeState() ([]byte, error) {
payload := types.Payload{
Data: &types.PayloadBankingBridgeState{
BankingBridgeState: &types.BankingBridgeState{
Active: e.bridgeState.active,
BlockHeight: e.bridgeState.block,
LogIndex: e.bridgeState.logIndex,
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (e *Engine) serialiseRecurringTransfers() ([]byte, error) {
payload := types.Payload{
Data: &types.PayloadBankingRecurringTransfers{
BankingRecurringTransfers: e.getRecurringTransfers(),
},
}
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) 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{
LastSeenEthBlock: e.lastSeenEthBlock,
},
}
seen.BankingSeen.Refs = make([]string, 0, e.seen.Size())
iter := e.seen.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 bridgeStateKey:
return e.serialiseK(e.serialiseBridgeState, &e.bss.serialisedBridgeState)
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(pl.BankingSeen, p)
case *types.PayloadBankingAssetActions:
return nil, e.restoreAssetActions(pl.BankingAssetActions, p)
case *types.PayloadBankingRecurringTransfers:
return nil, e.restoreRecurringTransfers(ctx, pl.BankingRecurringTransfers, p)
case *types.PayloadBankingScheduledTransfers:
return nil, e.restoreScheduledTransfers(ctx, pl.BankingScheduledTransfers, p)
case *types.PayloadBankingBridgeState:
return nil, e.restoreBridgeState(pl.BankingBridgeState, p)
default:
return nil, types.ErrUnknownSnapshotType
}
}
func (e *Engine) restoreRecurringTransfers(ctx context.Context, transfers *checkpoint.RecurringTransfers, 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())
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) restoreBridgeState(state *types.BankingBridgeState, p *types.Payload) (err error) {
if state != nil {
e.bridgeState = &bridgeState{
active: state.Active,
block: state.BlockHeight,
logIndex: state.LogIndex,
}
}
e.bss.serialisedBridgeState, 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(seen *types.BankingSeen, p *types.Payload) error {
var err error
e.log.Info("restoring seen", logging.Int("n", len(seen.Refs)))
e.seen = treeset.NewWithStringComparator()
for _, v := range seen.Refs {
e.seen.Add(v)
}
e.lastSeenEthBlock = seen.LastSeenEthBlock
e.bss.serialisedSeen, err = proto.Marshal(p.IntoProto())
return err
}
func (e *Engine) restoreAssetActions(aa *types.BankingAssetActions, p *types.Payload) error {
var err error
e.loadAssetActions(aa.AssetAction)
for _, aa := range e.assetActs {
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) OnEpochRestore(ctx context.Context, ep types.Epoch) {
e.log.Debug("epoch restoration notification received", logging.String("epoch", ep.String()))
e.currentEpoch = ep.Seq
}
func (e *Engine) OnStateLoaded(ctx context.Context) error {
if e.lastSeenEthBlock != 0 {
e.log.Info("restoring collateral bridge starting block", logging.Uint64("block", e.lastSeenEthBlock))
e.ethEventSource.UpdateCollateralStartingBlock(e.lastSeenEthBlock)
}
return nil
}