-
Notifications
You must be signed in to change notification settings - Fork 22
/
engine.go
491 lines (436 loc) · 14.9 KB
/
engine.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
// 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"
"encoding/hex"
"errors"
"math/big"
"sort"
"strings"
"sync/atomic"
"time"
"code.vegaprotocol.io/vega/core/assets"
"code.vegaprotocol.io/vega/core/broker"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/core/validators"
"code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/libs/num"
vgproto "code.vegaprotocol.io/vega/libs/proto"
"code.vegaprotocol.io/vega/logging"
proto "code.vegaprotocol.io/vega/protos/vega"
snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
"github.com/emirpasic/gods/sets/treeset"
)
//go:generate go run github.com/golang/mock/mockgen -destination mocks/mocks.go -package mocks code.vegaprotocol.io/vega/core/banking Assets,Notary,Collateral,Witness,TimeService,EpochService,Topology,MarketActivityTracker,ERC20BridgeView
const (
// this is temporarily used until we remove expiry completely
// make the expiry 2 years, which will outlive anyway any
// vega network at first.
// 24 hours * 365 * days * 2 years.
withdrawalsDefaultExpiry = 24 * 365 * 2 * time.Hour
)
var (
ErrWrongAssetTypeUsedInBuiltinAssetChainEvent = errors.New("non builtin asset used for builtin asset chain event")
ErrWrongAssetTypeUsedInERC20ChainEvent = errors.New("non ERC20 for ERC20 chain event")
ErrWrongAssetUsedForERC20Withdraw = errors.New("non erc20 asset used for lock withdraw")
ErrInvalidWithdrawalState = errors.New("invalid withdrawal state")
ErrNotMatchingWithdrawalForReference = errors.New("invalid reference for withdrawal chain event")
ErrWithdrawalNotReady = errors.New("withdrawal not ready")
ErrNotEnoughFundsToTransfer = errors.New("not enough funds to transfer")
)
type Assets interface {
Get(assetID string) (*assets.Asset, error)
Enable(ctx context.Context, assetID string) error
ApplyAssetUpdate(ctx context.Context, assetID string) error
}
// Notary ...
type Notary interface {
StartAggregate(resID string, kind types.NodeSignatureKind, signature []byte)
IsSigned(ctx context.Context, id string, kind types.NodeSignatureKind) ([]types.NodeSignature, bool)
OfferSignatures(kind types.NodeSignatureKind, f func(resources string) []byte)
}
// Collateral engine.
type Collateral interface {
Deposit(ctx context.Context, party, asset string, amount *num.Uint) (*types.LedgerMovement, error)
Withdraw(ctx context.Context, party, asset string, amount *num.Uint) (*types.LedgerMovement, error)
EnableAsset(ctx context.Context, asset types.Asset) error
GetPartyGeneralAccount(party, asset string) (*types.Account, error)
TransferFunds(ctx context.Context,
transfers []*types.Transfer,
accountTypes []types.AccountType,
references []string,
feeTransfers []*types.Transfer,
feeTransfersAccountTypes []types.AccountType,
) ([]*types.LedgerMovement, error)
PropagateAssetUpdate(ctx context.Context, asset types.Asset) error
}
// Witness provide foreign chain resources validations.
type Witness interface {
StartCheck(validators.Resource, func(interface{}, bool), time.Time) error
RestoreResource(validators.Resource, func(interface{}, bool)) error
}
// TimeService provide the time of the vega node using the tm time.
type TimeService interface {
GetTimeNow() time.Time
}
// Epochervice ...
type EpochService interface {
NotifyOnEpoch(f func(context.Context, types.Epoch), r func(context.Context, types.Epoch))
}
// Topology ...
type Topology interface {
IsValidator() bool
}
type MarketActivityTracker interface {
GetMarketScores(asset string, markets []string, dispatchMetric proto.DispatchMetric) []*types.MarketContributionScore
GetMarketsWithEligibleProposer(asset string, markets []string, payoutAsset string, funder string) []*types.MarketContributionScore
MarkPaidProposer(market, payoutAsset string, marketsInScope []string, funder string)
}
const (
pendingState uint32 = iota
okState
rejectedState
)
var defaultValidationDuration = 2 * time.Hour
type Engine struct {
cfg Config
log *logging.Logger
timeService TimeService
broker broker.Interface
col Collateral
witness Witness
notary Notary
assets Assets
top Topology
assetActs map[string]*assetAction
seen *treeset.Set
withdrawals map[string]withdrawalRef
withdrawalCnt *big.Int
deposits map[string]*types.Deposit
currentEpoch uint64
bss *bankingSnapshotState
marketActivityTracker MarketActivityTracker
// transfer fee related stuff
scheduledTransfers map[int64][]scheduledTransfer
transferFeeFactor num.Decimal
minTransferQuantumMultiple num.Decimal
// recurring transfers in the order they were created
recurringTransfers []*types.RecurringTransfer
// transfer id to recurringTransfers
recurringTransfersMap map[string]*types.RecurringTransfer
bridgeState *bridgeState
bridgeView ERC20BridgeView
}
type withdrawalRef struct {
w *types.Withdrawal
ref *big.Int
}
func New(
log *logging.Logger,
cfg Config,
col Collateral,
witness Witness,
tsvc TimeService,
assets Assets,
notary Notary,
broker broker.Interface,
top Topology,
epoch EpochService,
marketActivityTracker MarketActivityTracker,
bridgeView ERC20BridgeView,
) (e *Engine) {
defer func() {
epoch.NotifyOnEpoch(e.OnEpoch, e.OnEpochRestore)
}()
log = log.Named(namedLogger)
log.SetLevel(cfg.Level.Get())
return &Engine{
cfg: cfg,
log: log,
timeService: tsvc,
broker: broker,
col: col,
witness: witness,
assets: assets,
notary: notary,
top: top,
assetActs: map[string]*assetAction{},
seen: treeset.NewWithStringComparator(),
withdrawals: map[string]withdrawalRef{},
deposits: map[string]*types.Deposit{},
withdrawalCnt: big.NewInt(0),
bss: &bankingSnapshotState{},
scheduledTransfers: map[int64][]scheduledTransfer{},
recurringTransfers: []*types.RecurringTransfer{},
recurringTransfersMap: map[string]*types.RecurringTransfer{},
transferFeeFactor: num.DecimalZero(),
minTransferQuantumMultiple: num.DecimalZero(),
marketActivityTracker: marketActivityTracker,
bridgeState: &bridgeState{
active: true,
},
bridgeView: bridgeView,
}
}
// ReloadConf updates the internal configuration.
func (e *Engine) ReloadConf(cfg Config) {
e.log.Info("reloading configuration")
if e.log.GetLevel() != cfg.Level.Get() {
e.log.Info("updating log level",
logging.String("old", e.log.GetLevel().String()),
logging.String("new", cfg.Level.String()),
)
e.log.SetLevel(cfg.Level.Get())
}
e.cfg = cfg
}
func (e *Engine) OnEpoch(ctx context.Context, ep types.Epoch) {
switch ep.Action {
case proto.EpochAction_EPOCH_ACTION_START:
e.currentEpoch = ep.Seq
case proto.EpochAction_EPOCH_ACTION_END:
e.distributeRecurringTransfers(ctx, e.currentEpoch)
default:
e.log.Panic("epoch action should never be UNSPECIFIED", logging.String("epoch", ep.String()))
}
}
func (e *Engine) OnTick(ctx context.Context, _ time.Time) {
assetActionKeys := make([]string, 0, len(e.assetActs))
for k := range e.assetActs {
assetActionKeys = append(assetActionKeys, k)
}
sort.Strings(assetActionKeys)
// iterate over asset actions deterministically
for _, k := range assetActionKeys {
v := e.assetActs[k]
state := atomic.LoadUint32(&v.state)
if state == pendingState {
continue
}
// get the action reference to ensure it's not a duplicate
ref := v.getRef()
refKey, err := getRefKey(ref)
if err != nil {
e.log.Error("failed to serialise ref",
logging.String("asset-class", ref.Asset),
logging.String("tx-hash", ref.Hash),
logging.String("action", v.String()))
continue
}
switch state {
case okState:
// check if this transaction have been seen before then
if e.seen.Contains(refKey) {
// do nothing of this transaction, just display an error
e.log.Error("chain event reference a transaction already processed",
logging.String("asset-class", ref.Asset),
logging.String("tx-hash", ref.Hash),
logging.String("action", v.String()))
} else {
// first time we seen this transaction, let's add iter
e.seen.Add(refKey)
if err := e.finalizeAction(ctx, v); err != nil {
e.log.Error("unable to finalize action",
logging.String("action", v.String()),
logging.Error(err))
}
}
case rejectedState:
e.log.Error("network rejected banking action",
logging.String("action", v.String()))
}
// delete anyway the action
// at this point the action was either rejected, so we do no need
// need to keep waiting for its validation, or accepted. in the case
// it's accepted it's then sent to the given collateral function
// (deposit, withdraw, allowlist), then an error can occur down the
// line in the collateral but if that happened there's no way for
// us to recover for this event, so we have no real reason to keep
// it in memory
delete(e.assetActs, k)
}
// we may want a dedicated method on the snapshot engine at some
// point but this will do for now
// this will be restarting the signatures aggregates
e.notary.OfferSignatures(
types.NodeSignatureKindAssetWithdrawal, e.offerERC20NotarySignatures)
// then process all scheduledTransfers
if err := e.distributeScheduledTransfers(ctx); err != nil {
e.log.Error("could not process scheduled transfers",
logging.Error(err),
)
}
}
func (e *Engine) onCheckDone(i interface{}, valid bool) {
aa, ok := i.(*assetAction)
if !ok {
return
}
newState := rejectedState
if valid {
newState = okState
}
atomic.StoreUint32(&aa.state, newState)
}
func (e *Engine) getWithdrawalFromRef(ref *big.Int) (*types.Withdrawal, error) {
// sort withdraws to check deterministically
withdrawalsK := make([]string, 0, len(e.withdrawals))
for k := range e.withdrawals {
withdrawalsK = append(withdrawalsK, k)
}
sort.Strings(withdrawalsK)
for _, k := range withdrawalsK {
v := e.withdrawals[k]
if v.ref.Cmp(ref) == 0 {
return v.w, nil
}
}
return nil, ErrNotMatchingWithdrawalForReference
}
func (e *Engine) finalizeAction(ctx context.Context, aa *assetAction) error {
switch {
case aa.IsBuiltinAssetDeposit():
dep := e.deposits[aa.id]
return e.finalizeDeposit(ctx, dep)
case aa.IsERC20Deposit():
dep := e.deposits[aa.id]
return e.finalizeDeposit(ctx, dep)
case aa.IsERC20AssetList():
return e.finalizeAssetList(ctx, aa.erc20AL.VegaAssetID)
case aa.IsERC20AssetLimitsUpdated():
return e.finalizeAssetLimitsUpdated(ctx, aa.erc20AssetLimitsUpdated.VegaAssetID)
case aa.IsERC20BridgeStopped():
e.bridgeState.NewBridgeStopped(aa.blockHeight, aa.logIndex)
return nil
case aa.IsERC20BridgeResumed():
e.bridgeState.NewBridgeResumed(aa.blockHeight, aa.logIndex)
return nil
default:
return ErrUnknownAssetAction
}
}
func (e *Engine) finalizeAssetList(ctx context.Context, assetID string) error {
asset, err := e.assets.Get(assetID)
if err != nil {
e.log.Error("invalid asset id used to finalise asset list",
logging.Error(err),
logging.AssetID(assetID))
return nil
}
if err := e.assets.Enable(ctx, assetID); err != nil {
e.log.Error("unable to enable asset",
logging.Error(err),
logging.AssetID(assetID))
return err
}
return e.col.EnableAsset(ctx, *asset.ToAssetType())
}
func (e *Engine) finalizeAssetLimitsUpdated(ctx context.Context, assetID string) error {
asset, err := e.assets.Get(assetID)
if err != nil {
e.log.Error("invalid asset id used to finalise asset list",
logging.Error(err),
logging.AssetID(assetID))
return nil
}
if err := e.assets.ApplyAssetUpdate(ctx, assetID); err != nil {
e.log.Error("couldn't apply asset update",
logging.Error(err),
logging.AssetID(assetID))
return err
}
return e.col.PropagateAssetUpdate(ctx, *asset.ToAssetType())
}
func (e *Engine) finalizeDeposit(ctx context.Context, d *types.Deposit) error {
defer func() {
e.broker.Send(events.NewDepositEvent(ctx, *d))
// whatever happens, the deposit is in its final state (cancelled or finalized)
delete(e.deposits, d.ID)
}()
res, err := e.col.Deposit(ctx, d.PartyID, d.Asset, d.Amount)
if err != nil {
d.Status = types.DepositStatusCancelled
return err
}
d.Status = types.DepositStatusFinalized
d.CreditDate = e.timeService.GetTimeNow().UnixNano()
e.broker.Send(events.NewLedgerMovements(ctx, []*types.LedgerMovement{res}))
return nil
}
func (e *Engine) finalizeWithdraw(
ctx context.Context, w *types.Withdrawal,
) error {
// always send the withdrawal event, don't delete it from the map because we
// may still receive events
defer func() {
e.broker.Send(events.NewWithdrawalEvent(ctx, *w))
}()
res, err := e.col.Withdraw(ctx, w.PartyID, w.Asset, w.Amount.Clone())
if err != nil {
w.Status = types.WithdrawalStatusRejected
return err
}
w.Status = types.WithdrawalStatusFinalized
e.broker.Send(events.NewLedgerMovements(ctx, []*types.LedgerMovement{res}))
return nil
}
func (e *Engine) newWithdrawal(
id, partyID, asset string,
amount *num.Uint,
expirationDate time.Time,
wext *types.WithdrawExt,
) (w *types.Withdrawal, ref *big.Int) {
partyID = strings.TrimPrefix(partyID, "0x")
asset = strings.TrimPrefix(asset, "0x")
now := e.timeService.GetTimeNow()
// reference needs to be an int, deterministic for the contracts
ref = big.NewInt(0).Add(e.withdrawalCnt, big.NewInt(now.Unix()))
e.withdrawalCnt.Add(e.withdrawalCnt, big.NewInt(1))
w = &types.Withdrawal{
ID: id,
Status: types.WithdrawalStatusOpen,
PartyID: partyID,
Asset: asset,
Amount: amount,
ExpirationDate: expirationDate.Unix(),
Ext: wext,
CreationDate: now.UnixNano(),
Ref: ref.String(),
}
return
}
func (e *Engine) newDeposit(
id, partyID, asset string,
amount *num.Uint,
txHash string,
) *types.Deposit {
partyID = strings.TrimPrefix(partyID, "0x")
asset = strings.TrimPrefix(asset, "0x")
return &types.Deposit{
ID: id,
Status: types.DepositStatusOpen,
PartyID: partyID,
Asset: asset,
Amount: amount,
CreationDate: e.timeService.GetTimeNow().UnixNano(),
TxHash: txHash,
}
}
func getRefKey(ref snapshot.TxRef) (string, error) {
buf, err := vgproto.Marshal(&ref)
if err != nil {
return "", err
}
return hex.EncodeToString(crypto.Hash(buf)), nil
}