-
Notifications
You must be signed in to change notification settings - Fork 19
/
l2_clients.go
175 lines (150 loc) · 4.72 KB
/
l2_clients.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
// 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"
"errors"
"fmt"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/logging"
"github.com/ethereum/go-ethereum/ethclient"
)
type L2Client struct {
ETHClient
}
type L2Clients struct {
ctx context.Context
log *logging.Logger
// map of chainID -> Client
clients map[string]*L2Client
confirmations map[string]*EthereumConfirmations
}
func NewL2Clients(
ctx context.Context,
log *logging.Logger,
cfg Config,
) (*L2Clients, error) {
// setup logger
log = log.Named(namedLogger)
log.SetLevel(cfg.Level.Get())
clients := map[string]*L2Client{}
confirmations := map[string]*EthereumConfirmations{}
for _, v := range cfg.EVMChainConfigs {
log.Info("starting L2 client",
logging.String("chain-id", v.ChainID),
logging.String("endpoint", v.RPCEndpoint),
)
if len(v.ChainID) <= 0 || len(v.RPCEndpoint) <= 0 {
return nil, errors.New("l2 rpc endpoint configured with empty strings")
}
clt, err := DialL2(ctx, v.RPCEndpoint)
if err != nil {
return nil, err
}
chainID, err := clt.ChainID(ctx)
if err != nil {
return nil, fmt.Errorf("couldn't get chain id: %w", err)
}
if chainID.String() != v.ChainID {
return nil, fmt.Errorf("client retrieve different chain id: %v vs %v", chainID.String(), v.ChainID)
}
clients[v.ChainID] = clt
confirmations[v.ChainID] = NewEthereumConfirmations(
cfg, clt, nil)
}
return &L2Clients{
ctx: ctx,
log: log,
clients: clients,
confirmations: confirmations,
}, nil
}
func (e *L2Clients) UpdateConfirmations(ethCfg *types.EthereumL2Configs) {
for _, v := range ethCfg.Configs {
confs, ok := e.confirmations[v.ChainID]
if !ok {
e.log.Panic("ethereum client for L2 is not configured", logging.String("name", v.Name), logging.String("chain-id", v.ChainID))
}
confs.UpdateConfirmations(v.Confirmations)
}
}
// ReloadConf updates the internal configuration of the execution
// engine and its dependencies.
func (e *L2Clients) ReloadConf(cfg Config) {
e.log.Debug("reloading configuration")
if e.log.GetLevel() != cfg.Level.Get() {
e.log.Info("updating log level",
logging.String("old", e.log.GetLevel().String()),
logging.String("new", cfg.Level.String()),
)
e.log.SetLevel(cfg.Level.Get())
}
e.log.Info("updating L2 clients")
for _, v := range cfg.EVMChainConfigs {
if _, ok := e.clients[v.ChainID]; ok {
e.log.Warn("L2 client already setted up, please stop and restart node to update existing configuration",
logging.String("chain-id", v.ChainID),
logging.String("endpoint", v.RPCEndpoint),
)
continue
}
e.log.Info("starting L2 client",
logging.String("chain-id", v.ChainID),
logging.String("endpoint", v.RPCEndpoint),
)
if len(v.ChainID) <= 0 || len(v.RPCEndpoint) <= 0 {
e.log.Warn("invalid L2 client configuration",
logging.String("chain-id", v.ChainID),
logging.String("endpoint", v.RPCEndpoint),
)
continue
}
clt, err := DialL2(e.ctx, v.RPCEndpoint)
if err != nil {
e.log.Warn("couldn't start L2 client",
logging.String("chain-id", v.ChainID),
logging.String("endpoint", v.RPCEndpoint),
)
continue
}
chainID, err := clt.ChainID(e.ctx)
if err != nil {
e.log.Warn("couldn't get chain id", logging.Error(err))
continue
}
if chainID.String() != v.ChainID {
e.log.Warn("client retrieved different chain id",
logging.String("chain-id", chainID.String()),
logging.String("expected", v.ChainID),
)
continue
}
e.clients[v.ChainID] = clt
e.confirmations[v.ChainID] = NewEthereumConfirmations(
cfg, clt, nil)
}
}
func DialL2(ctx context.Context, endpoint string) (*L2Client, error) {
ethClient, err := ethclient.DialContext(ctx, endpoint)
if err != nil {
return nil, fmt.Errorf("couldn't instantiate Ethereum client: %w", err)
}
return &L2Client{ETHClient: newEthClientWrapper(ethClient)}, nil
}
func (c *L2Clients) Get(chainID string) (*L2Client, *EthereumConfirmations, bool) {
clt, ok1 := c.clients[chainID]
confs, ok2 := c.confirmations[chainID]
return clt, confs, ok1 && ok2
}