-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.go
203 lines (163 loc) · 6.7 KB
/
client.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
// 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 eth
import (
"context"
"encoding/hex"
"errors"
"fmt"
"strings"
"sync"
"time"
"code.vegaprotocol.io/vega/core/types"
vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
var (
ErrUnexpectedContractHash = errors.New("hash of contract bytecode not as expected")
ErrUnexpectedSolidityFormat = errors.New("unexpected format of solidity bytecode")
)
// ContractHashes the sha3-256(contract-bytecode stripped of metadata).
var ContractHashes = map[string]string{
"staking": "d66948e12817f8ae6ca94d56b43ca12e66416e7e9bc23bb09056957b25afc6bd",
"vesting": "5278802577f4aca315b9524bfa78790f8f0fae08939ec58bc9e8f0ea40123b09",
"collateral": "1cd7f315188baf26f70c77a764df361c5d01bd365b109b96033b8755ee2b2750",
"multisig": "5b7070e6159628455b38f5796e8d0dc08185aaaa1fb6073767c88552d396c6c2",
}
type Client struct {
ETHClient
ethConfig *types.EthereumConfig
// this is all just to prevent spamming the infura just
// to get the last height of the blockchain
mu sync.Mutex
currentHeightLastUpdate time.Time
currentHeight uint64
cfg Config
}
func Dial(ctx context.Context, cfg Config) (*Client, error) {
if len(cfg.RPCEndpoint) <= 0 {
return nil, errors.New("no ethereum rpc endpoint configured. the configuration have move from the NodeWallet section to the Ethereum section, please make sure your vega configuration is up to date")
}
ethClient, err := ethclient.DialContext(ctx, cfg.RPCEndpoint)
if err != nil {
return nil, fmt.Errorf("couldn't instantiate Ethereum client: %w", err)
}
return &Client{ETHClient: newEthClientWrapper(ethClient), cfg: cfg}, nil
}
func (c *Client) UpdateEthereumConfig(ethConfig *types.EthereumConfig) error {
if c == nil {
return nil
}
netID, err := c.NetworkID(context.Background())
if err != nil {
return fmt.Errorf("couldn't retrieve the network ID form the ethereum client: %w", err)
}
chainID, err := c.ChainID(context.Background())
if err != nil {
return fmt.Errorf("couldn't retrieve the chain ID form the ethereum client: %w", err)
}
if netID.String() != ethConfig.NetworkID() {
return fmt.Errorf("updated network ID does not match the one set during start up, expected %s got %v", ethConfig.NetworkID(), netID)
}
if chainID.String() != ethConfig.ChainID() {
return fmt.Errorf("updated chain ID does not matchthe one set during start up, expected %v got %v", ethConfig.ChainID(), chainID)
}
// if err := c.verifyStakingContract(context.Background(), ethConfig); err != nil {
// return fmt.Errorf("failed to verify staking bridge contract: %w", err)
// }
// if err := c.verifyVestingContract(context.Background(), ethConfig); err != nil {
// return fmt.Errorf("failed to verify vesting bridge contract: %w", err)
// }
if err := c.verifyCollateralContract(context.Background(), ethConfig); err != nil {
return fmt.Errorf("failed to verify collateral bridge contract: %w", err)
}
if err := c.verifyMultisigContract(context.Background(), ethConfig); err != nil {
return fmt.Errorf("failed to verify multisig control contract: %w", err)
}
c.ethConfig = ethConfig
return nil
}
func (c *Client) CollateralBridgeAddress() ethcommon.Address {
return c.ethConfig.CollateralBridge().Address()
}
func (c *Client) CollateralBridgeAddressHex() string {
return c.ethConfig.CollateralBridge().HexAddress()
}
func (c *Client) CurrentHeight(ctx context.Context) (uint64, error) {
c.mu.Lock()
defer c.mu.Unlock()
if now := time.Now(); c.currentHeightLastUpdate.Add(c.cfg.RetryDelay.Get()).Before(now) {
lastBlockHeader, err := c.HeaderByNumber(ctx, nil)
if err != nil {
return c.currentHeight, err
}
c.currentHeightLastUpdate = now
c.currentHeight = lastBlockHeader.Number.Uint64()
}
return c.currentHeight, nil
}
func (c *Client) ConfirmationsRequired() uint64 {
return c.ethConfig.Confirmations()
}
// VerifyContract takes the address of a contract in hex and checks the hash of the byte-code is as expected.
func (c *Client) VerifyContract(ctx context.Context, address ethcommon.Address, expectedHash string) error {
// nil block number means latest block
b, err := c.CodeAt(ctx, address, nil)
if err != nil {
return err
}
// the bytecode of the contract is appended which is deployment specific. We only care about
// the contract code itself and so we need to strip this meta-data before hashing it. For the version
// of Solidity we use, the format is [contract-bytecode]a264[CBOR-encoded meta-data]
asHex := strings.Split(hex.EncodeToString(b), "a264")
if len(asHex) != 2 {
return fmt.Errorf("%w: address: %s", ErrUnexpectedSolidityFormat, address)
}
// Back to bytes for hashing
b, err = hex.DecodeString(asHex[0])
if err != nil {
return err
}
h := hex.EncodeToString(vgcrypto.Hash(b))
if h != expectedHash {
return fmt.Errorf("%w: address: %s, hash: %s, expected: %s", ErrUnexpectedContractHash, address, h, expectedHash)
}
return nil
}
// func (c *Client) verifyStakingContract(ctx context.Context, ethConfig *types.EthereumConfig) error {
// if address := ethConfig.StakingBridge(); address.HasAddress() {
// return c.VerifyContract(ctx, address.Address(), ContractHashes["staking"])
// }
// return nil
// }
// func (c *Client) verifyVestingContract(ctx context.Context, ethConfig *types.EthereumConfig) error {
// if address := ethConfig.VestingBridge(); address.HasAddress() {
// return c.VerifyContract(ctx, address.Address(), ContractHashes["vesting"])
// }
// return nil
// }
func (c *Client) verifyCollateralContract(ctx context.Context, ethConfig *types.EthereumConfig) error {
if address := ethConfig.CollateralBridge(); address.HasAddress() {
return c.VerifyContract(ctx, address.Address(), ContractHashes["collateral"])
}
return nil
}
func (c *Client) verifyMultisigContract(ctx context.Context, ethConfig *types.EthereumConfig) error {
if address := ethConfig.MultiSigControl(); address.HasAddress() {
return c.VerifyContract(ctx, address.Address(), ContractHashes["multisig"])
}
return nil
}