-
Notifications
You must be signed in to change notification settings - Fork 22
/
checkpoint.go
286 lines (244 loc) · 8.25 KB
/
checkpoint.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
// 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"
"sort"
"sync/atomic"
"code.vegaprotocol.io/vega/core/assets"
"code.vegaprotocol.io/vega/core/events"
"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"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
"github.com/emirpasic/gods/sets/treeset"
)
func (e *Engine) Name() types.CheckpointName {
return types.BankingCheckpoint
}
func (e *Engine) Checkpoint() ([]byte, error) {
msg := &checkpoint.Banking{
TransfersAtTime: e.getScheduledTransfers(),
RecurringTransfers: e.getRecurringTransfers(),
BridgeState: e.getBridgeState(),
LastSeenEthBlock: e.lastSeenEthBlock,
}
msg.SeenRefs = make([]string, 0, e.seen.Size())
iter := e.seen.Iterator()
for iter.Next() {
msg.SeenRefs = append(msg.SeenRefs, iter.Value().(string))
}
msg.AssetActions = make([]*checkpoint.AssetAction, 0, len(e.assetActs))
for _, aa := range e.getAssetActions() {
msg.AssetActions = append(msg.AssetActions, aa.IntoProto())
}
ret, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
return ret, nil
}
func (e *Engine) Load(ctx context.Context, data []byte) error {
b := checkpoint.Banking{}
if err := proto.Unmarshal(data, &b); err != nil {
return err
}
evts, err := e.loadScheduledTransfers(ctx, b.TransfersAtTime)
if err != nil {
return err
}
evts = append(evts, e.loadRecurringTransfers(ctx, b.RecurringTransfers)...)
e.loadBridgeState(b.BridgeState)
e.seen = treeset.NewWithStringComparator()
for _, v := range b.SeenRefs {
e.seen.Add(v)
}
e.lastSeenEthBlock = b.LastSeenEthBlock
if e.lastSeenEthBlock != 0 {
e.log.Info("restoring collateral bridge starting block", logging.Uint64("block", e.lastSeenEthBlock))
e.ethEventSource.UpdateCollateralStartingBlock(e.lastSeenEthBlock)
}
aa := make([]*types.AssetAction, 0, len(b.AssetActions))
for _, a := range b.AssetActions {
aa = append(aa, types.AssetActionFromProto(a))
}
e.loadAssetActions(aa)
for _, aa := range e.assetActs {
e.witness.StartCheck(aa, e.onCheckDone, e.timeService.GetTimeNow().Add(defaultValidationDuration))
}
if len(evts) > 0 {
e.broker.SendBatch(evts)
}
return nil
}
func (e *Engine) loadAssetActions(aa []*types.AssetAction) {
for _, v := range aa {
var (
err error
asset *assets.Asset
bridgeStopped *types.ERC20EventBridgeStopped
bridgeResumed *types.ERC20EventBridgeResumed
)
// only others action than bridge stop and resume
// have an actual asset associated
if !v.BridgeResume && !v.BridgeStopped {
asset, err = e.assets.Get(v.Asset)
if err != nil {
e.log.Panic("trying to restore an assetAction with no asset", logging.String("asset", v.Asset))
}
}
if v.BridgeStopped {
bridgeStopped = &types.ERC20EventBridgeStopped{BridgeStopped: true}
}
if v.BridgeResume {
bridgeResumed = &types.ERC20EventBridgeResumed{BridgeResumed: true}
}
state := &atomic.Uint32{}
state.Store(v.State)
aa := &assetAction{
id: v.ID,
state: state,
blockHeight: v.BlockNumber,
asset: asset,
logIndex: v.TxIndex,
txHash: v.Hash,
builtinD: v.BuiltinD,
erc20AL: v.Erc20AL,
erc20D: v.Erc20D,
erc20AssetLimitsUpdated: v.ERC20AssetLimitsUpdated,
erc20BridgeStopped: bridgeStopped,
erc20BridgeResumed: bridgeResumed,
// this is needed every time now
bridgeView: e.bridgeView,
}
if len(aa.getRef().Hash) == 0 {
// if we're here it means that the IntoProto code has not done its job properly for a particular asset action type
e.log.Panic("asset action has not been serialised correct and is empty", logging.String("txHash", aa.txHash))
}
e.assetActs[v.ID] = aa
// store the deposit in the deposits
if v.BuiltinD != nil {
e.deposits[v.ID] = e.newDeposit(v.ID, v.BuiltinD.PartyID, v.BuiltinD.VegaAssetID, v.BuiltinD.Amount, v.Hash)
} else if v.Erc20D != nil {
e.deposits[v.ID] = e.newDeposit(v.ID, v.Erc20D.TargetPartyID, v.Erc20D.VegaAssetID, v.Erc20D.Amount, v.Hash)
}
}
}
func (e *Engine) loadBridgeState(state *checkpoint.BridgeState) {
// this would eventually be nil if we restore from a checkpoint
// which have been produce from an old version of the core.
// we set it to active by default in the case
if state == nil {
e.bridgeState = &bridgeState{
active: true,
}
return
}
e.bridgeState = &bridgeState{
active: state.Active,
block: state.BlockHeight,
logIndex: state.LogIndex,
}
}
func (e *Engine) loadScheduledTransfers(
ctx context.Context, r []*checkpoint.ScheduledTransferAtTime,
) ([]events.Event, error) {
evts := []events.Event{}
for _, v := range r {
transfers := make([]scheduledTransfer, 0, len(v.Transfers))
for _, v := range v.Transfers {
transfer, err := scheduledTransferFromProto(v)
if err != nil {
return nil, err
}
evts = append(evts, events.NewOneOffTransferFundsEvent(ctx, transfer.oneoff))
transfers = append(transfers, transfer)
}
e.scheduledTransfers[v.DeliverOn] = transfers
}
return evts, nil
}
func (e *Engine) loadRecurringTransfers(
ctx context.Context, r *checkpoint.RecurringTransfers,
) []events.Event {
evts := []events.Event{}
for _, v := range r.RecurringTransfers {
transfer := types.RecurringTransferFromEvent(v)
e.recurringTransfers = append(e.recurringTransfers, transfer)
e.recurringTransfersMap[transfer.ID] = transfer
evts = append(evts, events.NewRecurringTransferFundsEvent(ctx, transfer))
}
return evts
}
func (e *Engine) getBridgeState() *checkpoint.BridgeState {
return &checkpoint.BridgeState{
Active: e.bridgeState.active,
BlockHeight: e.bridgeState.block,
LogIndex: e.bridgeState.logIndex,
}
}
func (e *Engine) getRecurringTransfers() *checkpoint.RecurringTransfers {
out := &checkpoint.RecurringTransfers{
RecurringTransfers: make([]*eventspb.Transfer, 0, len(e.recurringTransfers)),
}
for _, v := range e.recurringTransfers {
out.RecurringTransfers = append(out.RecurringTransfers, v.IntoEvent(nil))
}
return out
}
func (e *Engine) getScheduledTransfers() []*checkpoint.ScheduledTransferAtTime {
out := make([]*checkpoint.ScheduledTransferAtTime, 0, len(e.scheduledTransfers))
for k, v := range e.scheduledTransfers {
transfers := make([]*checkpoint.ScheduledTransfer, 0, len(v))
for _, v := range v {
transfers = append(transfers, v.ToProto())
}
out = append(out, &checkpoint.ScheduledTransferAtTime{DeliverOn: k, Transfers: transfers})
}
sort.SliceStable(out, func(i, j int) bool { return out[i].DeliverOn < out[j].DeliverOn })
return out
}
func (e *Engine) getAssetActions() []*types.AssetAction {
aa := make([]*types.AssetAction, 0, len(e.assetActs))
for _, v := range e.assetActs {
// this is optional as bridge action don't have one
var assetID string
if v.asset != nil {
assetID = v.asset.ToAssetType().ID
}
var bridgeStopped bool
if v.erc20BridgeStopped != nil {
bridgeStopped = true
}
var bridgeResumed bool
if v.erc20BridgeResumed != nil {
bridgeResumed = true
}
aa = append(aa, &types.AssetAction{
ID: v.id,
State: v.state.Load(),
BlockNumber: v.blockHeight,
Asset: assetID,
TxIndex: v.logIndex,
Hash: v.txHash,
BuiltinD: v.builtinD,
Erc20AL: v.erc20AL,
Erc20D: v.erc20D,
ERC20AssetLimitsUpdated: v.erc20AssetLimitsUpdated,
BridgeStopped: bridgeStopped,
BridgeResume: bridgeResumed,
})
}
sort.SliceStable(aa, func(i, j int) bool { return aa[i].ID < aa[j].ID })
return aa
}