-
Notifications
You must be signed in to change notification settings - Fork 19
/
ethereum.go
368 lines (304 loc) · 10.9 KB
/
ethereum.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
// 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 types
import (
"encoding/json"
"errors"
"fmt"
vgreflect "code.vegaprotocol.io/vega/libs/reflect"
proto "code.vegaprotocol.io/vega/protos/vega"
ethcmn "github.com/ethereum/go-ethereum/common"
)
var (
ErrMissingNetworkID = errors.New("missing network ID in Ethereum config")
ErrMissingChainID = errors.New("missing chain ID in Ethereum config")
ErrMissingCollateralBridgeAddress = errors.New("missing collateral bridge contract address in Ethereum config")
ErrMissingMultiSigControlAddress = errors.New("missing multisig control contract address in Ethereum config")
ErrUnsupportedCollateralBridgeDeploymentBlockHeight = errors.New("setting collateral bridge contract deployment block height in Ethereum config is not supported")
ErrAtLeastOneOfStakingOrVestingBridgeAddressMustBeSet = errors.New("at least one of the stacking bridge or token vesting contract addresses must be specified")
ErrConfirmationsMustBeHigherThan0 = errors.New("confirmation must be > 0 in Ethereum config")
ErrBlockIntervalMustBeHigherThan0 = errors.New("block interval must be > 0 in Ethereum config")
ErrMissingNetworkName = errors.New("missing network name")
ErrDuplicateNetworkName = errors.New("duplicate network name")
ErrDuplicateNetworkID = errors.New("duplicate network ID name")
ErrDuplicateChainID = errors.New("duplicate chain ID name")
ErrCannotRemoveL2Config = errors.New("L2 config cannot be removed")
ErrCanOnlyAmendedConfirmationsAndBlockInterval = errors.New("can only amended L2 config confirmations and block interval")
)
type EthereumConfig struct {
chainID string
networkID string
confirmations uint64
collateralBridge EthereumContract
multiSigControl EthereumContract
stakingBridge EthereumContract
vestingBridge EthereumContract
}
func EthereumConfigFromUntypedProto(v interface{}) (*EthereumConfig, error) {
cfg, err := toEthereumConfigProto(v)
if err != nil {
return nil, fmt.Errorf("couldn't convert untyped proto to EthereumConfig proto: %w", err)
}
ethConfig, err := EthereumConfigFromProto(cfg)
if err != nil {
return nil, fmt.Errorf("couldn't build EthereumConfig: %w", err)
}
return ethConfig, nil
}
func EthereumConfigFromProto(cfgProto *proto.EthereumConfig) (*EthereumConfig, error) {
if err := CheckEthereumConfig(cfgProto); err != nil {
return nil, fmt.Errorf("invalid Ethereum configuration: %w", err)
}
cfg := &EthereumConfig{
chainID: cfgProto.ChainId,
networkID: cfgProto.NetworkId,
confirmations: uint64(cfgProto.Confirmations),
collateralBridge: EthereumContract{
address: cfgProto.CollateralBridgeContract.Address,
},
multiSigControl: EthereumContract{
address: cfgProto.MultisigControlContract.Address,
deploymentBlockHeight: cfgProto.MultisigControlContract.DeploymentBlockHeight,
},
}
if cfgProto.StakingBridgeContract != nil {
cfg.stakingBridge = EthereumContract{
address: cfgProto.StakingBridgeContract.Address,
deploymentBlockHeight: cfgProto.StakingBridgeContract.DeploymentBlockHeight,
}
}
if cfgProto.TokenVestingContract != nil {
cfg.vestingBridge = EthereumContract{
address: cfgProto.TokenVestingContract.Address,
deploymentBlockHeight: cfgProto.TokenVestingContract.DeploymentBlockHeight,
}
}
return cfg, nil
}
func (c *EthereumConfig) ChainID() string {
return c.chainID
}
func (c *EthereumConfig) NetworkID() string {
return c.networkID
}
func (c *EthereumConfig) Confirmations() uint64 {
return c.confirmations
}
func (c *EthereumConfig) CollateralBridge() EthereumContract {
return c.collateralBridge
}
func (c *EthereumConfig) MultiSigControl() EthereumContract {
return c.multiSigControl
}
func (c *EthereumConfig) StakingBridge() EthereumContract {
return c.stakingBridge
}
func (c *EthereumConfig) VestingBridge() EthereumContract {
return c.vestingBridge
}
// StakingBridgeAddresses returns the registered staking bridge addresses. It
// might return the staking bridge, or the token vesting, or both contract
// address. The vesting contract can also be used to get information needed by
// the staking engine.
func (c *EthereumConfig) StakingBridgeAddresses() []ethcmn.Address {
var addresses []ethcmn.Address
if c.stakingBridge.HasAddress() {
addresses = append(addresses, c.stakingBridge.Address())
}
if c.vestingBridge.HasAddress() {
addresses = append(addresses, c.vestingBridge.Address())
}
return addresses
}
type EthereumContract struct {
address string
deploymentBlockHeight uint64
}
func (c EthereumContract) DeploymentBlockHeight() uint64 {
return c.deploymentBlockHeight
}
func (c EthereumContract) HasAddress() bool {
return len(c.address) > 0
}
func (c EthereumContract) Address() ethcmn.Address {
return ethcmn.HexToAddress(c.address)
}
func (c EthereumContract) HexAddress() string {
return c.address
}
// CheckUntypedEthereumConfig verifies the `v` parameter is a proto.EthereumConfig
// struct and check if it's valid.
func CheckUntypedEthereumConfig(v interface{}, _ interface{}) error {
cfg, err := toEthereumConfigProto(v)
if err != nil {
return err
}
return CheckEthereumConfig(cfg)
}
func CheckUntypedEthereumL2Configs(v interface{}, o interface{}) error {
cfg, err := toEthereumL2ConfigsProto(v)
if err != nil {
return err
}
ocfg := &proto.EthereumL2Configs{}
json.Unmarshal([]byte(o.(string)), ocfg)
return CheckEthereumL2Configs(cfg, ocfg)
}
// CheckEthereumConfig verifies the proto.EthereumConfig is valid.
func CheckEthereumConfig(cfgProto *proto.EthereumConfig) error {
if len(cfgProto.NetworkId) == 0 {
return ErrMissingNetworkID
}
if len(cfgProto.ChainId) == 0 {
return ErrMissingChainID
}
if cfgProto.Confirmations == 0 {
return ErrConfirmationsMustBeHigherThan0
}
noMultiSigControlSetUp := cfgProto.MultisigControlContract == nil || len(cfgProto.MultisigControlContract.Address) == 0
if noMultiSigControlSetUp {
return ErrMissingMultiSigControlAddress
}
noCollateralBridgeSetUp := cfgProto.CollateralBridgeContract == nil || len(cfgProto.CollateralBridgeContract.Address) == 0
if noCollateralBridgeSetUp {
return ErrMissingCollateralBridgeAddress
}
if cfgProto.CollateralBridgeContract.DeploymentBlockHeight != 0 {
return ErrUnsupportedCollateralBridgeDeploymentBlockHeight
}
noStakingBridgeSetUp := cfgProto.StakingBridgeContract == nil || len(cfgProto.StakingBridgeContract.Address) == 0
noVestingBridgeSetUp := cfgProto.TokenVestingContract == nil || len(cfgProto.TokenVestingContract.Address) == 0
if noStakingBridgeSetUp && noVestingBridgeSetUp {
return ErrAtLeastOneOfStakingOrVestingBridgeAddressMustBeSet
}
return nil
}
func toEthereumConfigProto(v interface{}) (*proto.EthereumConfig, error) {
cfg, ok := v.(*proto.EthereumConfig)
if !ok {
return nil, fmt.Errorf("type \"%s\" is not a EthereumConfig proto", vgreflect.TypeName(v))
}
return cfg, nil
}
type EthereumL2Configs struct {
Configs []EthereumL2Config
}
type EthereumL2Config struct {
ChainID string
NetworkID string
Confirmations uint64
Name string
BlockInterval uint64
}
func toEthereumL2ConfigsProto(v interface{}) (*proto.EthereumL2Configs, error) {
cfg, ok := v.(*proto.EthereumL2Configs)
if !ok {
return nil, fmt.Errorf("type \"%s\" is not a EthereumL2Configs proto", vgreflect.TypeName(v))
}
return cfg, nil
}
func EthereumL2ConfigsFromUntypedProto(v interface{}) (*EthereumL2Configs, error) {
cfg, err := toEthereumL2ConfigsProto(v)
if err != nil {
return nil, fmt.Errorf("couldn't convert untyped proto to EthereumL2Configs proto: %w", err)
}
ethConfig, err := EthereumL2ConfigsFromProto(cfg)
if err != nil {
return nil, fmt.Errorf("couldn't build EthereumL2Configs: %w", err)
}
return ethConfig, nil
}
func EthereumL2ConfigsFromProto(cfgProto *proto.EthereumL2Configs) (*EthereumL2Configs, error) {
if err := CheckEthereumL2Configs(cfgProto, nil); err != nil {
return nil, fmt.Errorf("invalid Ethereum configuration: %w", err)
}
cfg := &EthereumL2Configs{}
for _, v := range cfgProto.Configs {
cfg.Configs = append(cfg.Configs, EthereumL2Config{
NetworkID: v.NetworkId,
ChainID: v.ChainId,
Name: v.Name,
Confirmations: uint64(v.Confirmations),
BlockInterval: v.BlockInterval,
})
}
return cfg, nil
}
// CheckEthereumConfig verifies the proto.EthereumConfig is valid.
func CheckEthereumL2Configs(cfgProto *proto.EthereumL2Configs, prev *proto.EthereumL2Configs) error {
names := map[string]*proto.EthereumL2Config{}
cids := map[string]*proto.EthereumL2Config{}
nids := map[string]*proto.EthereumL2Config{}
for _, v := range cfgProto.Configs {
// check network id and ensure no duplicates
if len(v.NetworkId) == 0 {
return ErrMissingNetworkID
}
if _, ok := nids[v.NetworkId]; ok {
return ErrDuplicateNetworkID
}
nids[v.NetworkId] = v
// check chain id and ensure no duplicates
if len(v.ChainId) == 0 {
return ErrMissingChainID
}
if _, ok := cids[v.ChainId]; ok {
return ErrDuplicateChainID
}
cids[v.ChainId] = v
// check network name and ensure no duplicates
if len(v.Name) == 0 {
return ErrMissingNetworkName
}
if _, ok := names[v.Name]; ok {
return ErrDuplicateNetworkName
}
names[v.Name] = v
if v.Confirmations == 0 {
return ErrConfirmationsMustBeHigherThan0
}
if v.BlockInterval == 0 {
return ErrBlockIntervalMustBeHigherThan0
}
}
// it wasn't previously set to anything (from genesis) so nothing to check
if prev == nil {
return nil
}
// compare against currently set configs - we make sure they only amend confirmations, or are new additions
// but for now nothing can bre removed.
for _, c := range prev.Configs {
v, ok := nids[c.NetworkId]
if !ok {
return ErrCannotRemoveL2Config
}
if !isUpdate(v, c) {
return ErrCanOnlyAmendedConfirmationsAndBlockInterval
}
}
return nil
}
func isUpdate(v, c *proto.EthereumL2Config) bool {
if v.ChainId != c.ChainId {
return false
}
if v.NetworkId != c.NetworkId {
return false
}
if v.Name != c.Name {
return false
}
return true
}