-
Notifications
You must be signed in to change notification settings - Fork 19
/
ethereum.go
213 lines (174 loc) · 6.6 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
// 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 types
import (
"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")
)
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{}) error {
cfg, err := toEthereumConfigProto(v)
if err != nil {
return err
}
return CheckEthereumConfig(cfg)
}
// 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
}