-
Notifications
You must be signed in to change notification settings - Fork 19
/
process_chain_event.go
284 lines (261 loc) · 9.53 KB
/
process_chain_event.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
// 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 processor
import (
"context"
"errors"
"strconv"
"strings"
"code.vegaprotocol.io/vega/core/datasource/external/ethcall"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/logging"
vgproto "code.vegaprotocol.io/vega/protos/vega"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
)
var (
ErrNotAnERC20Event = errors.New("not an erc20 event")
ErrNotABuiltinAssetEvent = errors.New("not an builtin asset event")
ErrUnsupportedEventAction = errors.New("unsupported event action")
ErrChainEventAssetListERC20WithoutEnoughSignature = errors.New("chain event for erc20 asset list received with missing node signatures")
ErrNotBridgeChainID = errors.New("not the chainID of any bridge")
)
func (app *App) processChainEvent(
ctx context.Context, ce *commandspb.ChainEvent, pubkey string, id string,
) error {
if app.log.GetLevel() <= logging.DebugLevel {
app.log.Debug("received chain event",
logging.String("event", ce.String()),
logging.String("pubkey", pubkey),
)
}
// first verify the event was emitted by a validator
if !app.top.IsValidatorVegaPubKey(pubkey) {
app.log.Debug("received chain event from non-validator",
logging.String("event", ce.String()),
logging.String("pubkey", pubkey),
)
return ErrChainEventFromNonValidator
}
// let the topology know who was the validator that forwarded the event
app.top.AddForwarder(pubkey)
if !app.evtForwarder.Ack(ce) {
// there was an error, or this was already acked
// but that's not a big issue we just going to ignore that.
return nil
}
// OK the event was newly acknowledged, so now we need to
// figure out what to do with it.
switch c := ce.Event.(type) {
case *commandspb.ChainEvent_Heartbeat:
app.evtHeartbeat.ProcessHeartbeat(
c.Heartbeat.ContractAddress,
c.Heartbeat.SourceChainId,
c.Heartbeat.BlockHeight,
c.Heartbeat.BlockTime,
)
return nil
case *commandspb.ChainEvent_StakingEvent:
blockNumber := c.StakingEvent.Block
logIndex := c.StakingEvent.Index
switch evt := c.StakingEvent.Action.(type) {
case *vgproto.StakingEvent_TotalSupply:
stakeTotalSupply, err := types.StakeTotalSupplyFromProto(evt.TotalSupply)
if err != nil {
return err
}
return app.stakingAccounts.ProcessStakeTotalSupply(ctx, stakeTotalSupply)
case *vgproto.StakingEvent_StakeDeposited:
stakeDeposited, err := types.StakeDepositedFromProto(
evt.StakeDeposited, blockNumber, logIndex, ce.TxId, id)
if err != nil {
return err
}
return app.stake.ProcessStakeDeposited(ctx, stakeDeposited)
case *vgproto.StakingEvent_StakeRemoved:
stakeRemoved, err := types.StakeRemovedFromProto(
evt.StakeRemoved, blockNumber, logIndex, ce.TxId, id)
if err != nil {
return err
}
return app.stake.ProcessStakeRemoved(ctx, stakeRemoved)
default:
return errors.New("unsupported StakingEvent")
}
case *commandspb.ChainEvent_Builtin:
// Convert from protobuf to local domain type
ceb, err := types.NewChainEventBuiltinFromProto(c)
if err != nil {
return err
}
return app.processChainEventBuiltinAsset(ctx, ceb, id, ce.Nonce)
case *commandspb.ChainEvent_Erc20:
// Convert from protobuf to local domain type
ceErc, err := types.NewChainEventERC20FromProto(c)
if err != nil {
return err
}
return app.processChainEventERC20(ctx, ceErc, id, ce.TxId)
case *commandspb.ChainEvent_Erc20Multisig:
return app.processChainEventMultisig(c, id, ce.TxId)
case *commandspb.ChainEvent_ContractCall:
callResult, err := ethcall.EthereumContractCallResultFromProto(c.ContractCall)
if err != nil {
app.log.Error("received invalid contract call", logging.Error(err), logging.String("call", c.ContractCall.String()))
return err
}
chainID := app.primaryChainID
if callResult.SourceChainID != nil {
chainID = *callResult.SourceChainID
}
if chainID == app.primaryChainID {
return app.oracles.EthereumOraclesVerifier.ProcessEthereumContractCallResult(callResult)
}
// use l2 then
return app.oracles.EthereumL2OraclesVerifier.ProcessEthereumContractCallResult(callResult)
default:
return ErrUnsupportedChainEvent
}
}
func (app *App) processChainEventBuiltinAsset(ctx context.Context, ce *types.ChainEventBuiltin, id string, nonce uint64) error {
evt := ce.Builtin // nolint
if evt == nil {
return ErrNotABuiltinAssetEvent
}
switch act := evt.Action.(type) {
case *types.BuiltinAssetEventDeposit:
if err := app.checkVegaAssetID(act.Deposit, "BuiltinAsset.Deposit"); err != nil {
return err
}
return app.banking.DepositBuiltinAsset(ctx, act.Deposit, id, nonce)
case *types.BuiltinAssetEventWithdrawal:
if err := app.checkVegaAssetID(act.Withdrawal, "BuiltinAsset.Withdrawal"); err != nil {
return err
}
return errors.New("unreachable")
default:
return ErrUnsupportedEventAction
}
}
func (app *App) processChainEventMultisig(
c *commandspb.ChainEvent_Erc20Multisig, id, txID string,
) error {
if c.Erc20Multisig == nil {
return ErrNotAnERC20Event
}
cid, err := strconv.ParseUint(c.Erc20Multisig.ChainId, 10, 64)
if err != nil {
return err
}
var multisig ERC20MultiSigTopology
switch cid {
case app.primaryChainID:
multisig = app.primaryErc20MultiSigTopology
case app.secondaryChainID:
multisig = app.secondaryErc20MultiSigTopology
default:
return ErrNotBridgeChainID
}
blockNumber := c.Erc20Multisig.Block
logIndex := c.Erc20Multisig.Index
switch pevt := c.Erc20Multisig.Action.(type) {
case *vgproto.ERC20MultiSigEvent_SignerAdded:
evt, err := types.SignerEventFromSignerAddedProto(
pevt.SignerAdded, blockNumber, logIndex, txID, id, c.Erc20Multisig.ChainId)
if err != nil {
return err
}
return multisig.ProcessSignerEvent(evt)
case *vgproto.ERC20MultiSigEvent_SignerRemoved:
evt, err := types.SignerEventFromSignerRemovedProto(
pevt.SignerRemoved, blockNumber, logIndex, txID, id, c.Erc20Multisig.ChainId)
if err != nil {
return err
}
return multisig.ProcessSignerEvent(evt)
case *vgproto.ERC20MultiSigEvent_ThresholdSet:
evt, err := types.SignerThresholdSetEventFromProto(
pevt.ThresholdSet, blockNumber, logIndex, txID, id, c.Erc20Multisig.ChainId)
if err != nil {
return err
}
return multisig.ProcessThresholdEvent(evt)
default:
return errors.New("unsupported erc20 multisig event")
}
}
func (app *App) processChainEventERC20(
ctx context.Context, ce *types.ChainEventERC20, id, txID string,
) error {
evt := ce.ERC20 // nolint
if evt == nil {
return ErrNotAnERC20Event
}
switch act := evt.Action.(type) {
case *types.ERC20EventAssetList:
act.AssetList.VegaAssetID = strings.TrimPrefix(act.AssetList.VegaAssetID, "0x")
if err := app.checkVegaAssetID(act.AssetList, "ERC20.AssetList"); err != nil {
return err
}
// now check that the notary is GO for this asset
_, ok := app.notary.IsSigned(
ctx,
act.AssetList.VegaAssetID,
commandspb.NodeSignatureKind_NODE_SIGNATURE_KIND_ASSET_NEW)
if !ok {
return ErrChainEventAssetListERC20WithoutEnoughSignature
}
return app.banking.EnableERC20(ctx, act.AssetList, id, evt.Block, evt.Index, txID, evt.ChainID)
case *types.ERC20EventAssetDelist:
return errors.New("ERC20.AssetDelist not implemented")
case *types.ERC20EventDeposit:
act.Deposit.VegaAssetID = strings.TrimPrefix(act.Deposit.VegaAssetID, "0x")
if err := app.checkVegaAssetID(act.Deposit, "ERC20.AssetDeposit"); err != nil {
return err
}
return app.banking.DepositERC20(ctx, act.Deposit, id, evt.Block, evt.Index, txID, evt.ChainID)
case *types.ERC20EventWithdrawal:
act.Withdrawal.VegaAssetID = strings.TrimPrefix(act.Withdrawal.VegaAssetID, "0x")
if err := app.checkVegaAssetID(act.Withdrawal, "ERC20.AssetWithdrawal"); err != nil {
return err
}
return app.banking.ERC20WithdrawalEvent(ctx, act.Withdrawal, evt.Block, txID, evt.ChainID)
case *types.ERC20EventAssetLimitsUpdated:
act.AssetLimitsUpdated.VegaAssetID = strings.TrimPrefix(act.AssetLimitsUpdated.VegaAssetID, "0x")
if err := app.checkVegaAssetID(act.AssetLimitsUpdated, "ERC20.AssetLimitsUpdated"); err != nil {
return err
}
return app.banking.UpdateERC20(ctx, act.AssetLimitsUpdated, id, evt.Block, evt.Index, txID, evt.ChainID)
case *types.ERC20EventBridgeStopped:
return app.banking.BridgeStopped(ctx, act.BridgeStopped, id, evt.Block, evt.Index, txID, evt.ChainID)
case *types.ERC20EventBridgeResumed:
return app.banking.BridgeResumed(ctx, act.BridgeResumed, id, evt.Block, evt.Index, txID, evt.ChainID)
default:
return ErrUnsupportedEventAction
}
}
type HasVegaAssetID interface {
GetVegaAssetID() string
}
func (app *App) checkVegaAssetID(a HasVegaAssetID, action string) error {
id := a.GetVegaAssetID()
if _, err := app.assets.Get(id); err != nil {
app.log.Error("invalid vega asset ID",
logging.String("action", action),
logging.Error(err),
logging.String("asset-id", id))
return err
}
return nil
}