-
Notifications
You must be signed in to change notification settings - Fork 19
/
secondary_client.go
157 lines (127 loc) · 4.68 KB
/
secondary_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
// 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"
)
type SecondaryClient struct {
ETHClient
ethConfig *types.EVMChainConfig
// 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
retryDelay time.Duration
}
func SecondaryDial(ctx context.Context, cfg Config) (*SecondaryClient, error) {
if len(cfg.EVMBridgeConfigs) != 1 {
return nil, errors.New("require exactly one EVM bridge configuration")
}
evmCfg := cfg.EVMBridgeConfigs[0]
if len(evmCfg.RPCEndpoint) <= 0 {
return nil, fmt.Errorf("no rpc endpoint configured for chain-id: %s", evmCfg.ChainID)
}
ethClient, err := ethclient.DialContext(ctx, evmCfg.RPCEndpoint)
if err != nil {
return nil, fmt.Errorf("couldn't instantiate secondary Ethereum client: %w", err)
}
return &SecondaryClient{
ETHClient: newEthClientWrapper(ethClient),
retryDelay: cfg.RetryDelay.Get(),
}, nil
}
func (c *SecondaryClient) UpdateEthereumConfig(ctx context.Context, ethConfig *types.EVMChainConfig) error {
if c == nil {
return nil
}
netID, err := c.NetworkID(ctx)
if err != nil {
return fmt.Errorf("couldn't retrieve the network ID from the ethereum client: %w", err)
}
chainID, err := c.ChainID(ctx)
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 match the one set during start up, expected %v got %v", ethConfig.ChainID(), chainID)
}
c.ethConfig = ethConfig
return nil
}
func (c *SecondaryClient) CollateralBridgeAddress() ethcommon.Address {
return c.ethConfig.CollateralBridge().Address()
}
func (c *SecondaryClient) CollateralBridgeAddressHex() string {
return c.ethConfig.CollateralBridge().HexAddress()
}
// IsEthereum returns whether or not this client is the "primary" one and pointing to Ethereum.
func (c *SecondaryClient) IsEthereum() bool {
return false
}
func (c *SecondaryClient) CurrentHeight(ctx context.Context) (uint64, error) {
c.mu.Lock()
defer c.mu.Unlock()
if now := time.Now(); c.currentHeightLastUpdate.Add(c.retryDelay).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 *SecondaryClient) 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 *SecondaryClient) 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
}