forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeth_rpc_client.go
206 lines (164 loc) · 6.22 KB
/
geth_rpc_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
204
205
206
package ethadapter
import (
"bytes"
"context"
"fmt"
"math/big"
"time"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/obscuronet/go-obscuro/go/common/retry"
"github.com/obscuronet/go-obscuro/go/common/log"
"github.com/obscuronet/go-obscuro/go/common"
"github.com/ethereum/go-ethereum"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
const (
connRetryInterval = 500 * time.Millisecond
)
// gethRPCClient implements the EthClient interface and allows connection to a real ethereum node
type gethRPCClient struct {
client *ethclient.Client // the underlying eth rpc client
l2ID gethcommon.Address // the address of the Obscuro node this client is dedicated to
timeout time.Duration // the timeout for connecting to, or communicating with, the L1 node
logger gethlog.Logger
}
// NewEthClient instantiates a new ethadapter.EthClient that connects to an ethereum node
func NewEthClient(ipaddress string, port uint, timeout time.Duration, l2ID gethcommon.Address, logger gethlog.Logger) (EthClient, error) {
client, err := connect(ipaddress, port, timeout)
if err != nil {
return nil, fmt.Errorf("unable to connect to the eth node - %w", err)
}
logger.Trace(fmt.Sprintf("Initialized eth node connection - addr: %s port: %d", ipaddress, port))
return &gethRPCClient{
client: client,
l2ID: l2ID,
timeout: timeout,
logger: logger,
}, nil
}
func (e *gethRPCClient) FetchHeadBlock() *types.Block {
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
blk, err := e.client.BlockByNumber(ctx, nil)
if err != nil {
e.logger.Crit("could not fetch head block.", log.ErrKey, err)
}
return blk
}
func (e *gethRPCClient) Info() Info {
return Info{
L2ID: e.l2ID,
}
}
func (e *gethRPCClient) BlocksBetween(startingBlock *types.Block, lastBlock *types.Block) []*types.Block {
// TODO this should be a stream
var blocksBetween []*types.Block
var err error
for currentBlk := lastBlock; currentBlk != nil && !bytes.Equal(currentBlk.Hash().Bytes(), startingBlock.Hash().Bytes()) && !bytes.Equal(currentBlk.ParentHash().Bytes(), gethcommon.HexToHash("").Bytes()); {
currentBlk, err = e.BlockByHash(currentBlk.ParentHash())
if err != nil {
e.logger.Crit(fmt.Sprintf("could not fetch parent block with hash %s.", currentBlk.ParentHash().String()), log.ErrKey, err)
}
blocksBetween = append(blocksBetween, currentBlk)
}
return blocksBetween
}
func (e *gethRPCClient) IsBlockAncestor(block *types.Block, maybeAncestor common.L1RootHash) bool {
if bytes.Equal(maybeAncestor.Bytes(), block.Hash().Bytes()) || bytes.Equal(maybeAncestor.Bytes(), common.GenesisBlock.Hash().Bytes()) {
return true
}
if block.Number().Int64() == int64(common.L1GenesisHeight) {
return false
}
resolvedBlock, err := e.BlockByHash(maybeAncestor)
if err != nil {
e.logger.Crit(fmt.Sprintf("could not fetch parent block with hash %s.", maybeAncestor.String()), log.ErrKey, err)
}
if resolvedBlock == nil {
if resolvedBlock.Number().Int64() >= block.Number().Int64() {
return false
}
}
p, err := e.BlockByHash(block.ParentHash())
if err != nil {
e.logger.Crit(fmt.Sprintf("could not fetch parent block with hash %s", block.ParentHash().String()), log.ErrKey, err)
}
if p == nil {
return false
}
return e.IsBlockAncestor(p, maybeAncestor)
}
func (e *gethRPCClient) SendTransaction(signedTx *types.Transaction) error {
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
return e.client.SendTransaction(ctx, signedTx)
}
func (e *gethRPCClient) TransactionReceipt(hash gethcommon.Hash) (*types.Receipt, error) {
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
return e.client.TransactionReceipt(ctx, hash)
}
func (e *gethRPCClient) Nonce(account gethcommon.Address) (uint64, error) {
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
return e.client.PendingNonceAt(ctx, account)
}
func (e *gethRPCClient) BlockListener() (chan *types.Header, ethereum.Subscription) {
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
// this channel holds blocks that have been received from the geth network but not yet processed by the host,
// with more than 1 capacity the buffer provides resilience in case of intermittent RPC or processing issues
ch := make(chan *types.Header, 100)
var sub ethereum.Subscription
var err error
err = retry.Do(func() error {
sub, err = e.client.SubscribeNewHead(ctx, ch)
if err != nil {
e.logger.Warn("could not subscribe for new head blocks, retrying...")
}
return err
}, retry.NewTimeoutStrategy(e.timeout, connRetryInterval))
if err != nil {
// todo: handle this scenario better after refactor of node.go (health monitor report L1 unavailable, be able to recover without restarting host)
// couldn't connect after timeout period, cannot continue
e.logger.Crit("could not subscribe for new head blocks.", log.ErrKey, err)
}
return ch, sub
}
func (e *gethRPCClient) BlockByNumber(n *big.Int) (*types.Block, error) {
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
return e.client.BlockByNumber(ctx, n)
}
func (e *gethRPCClient) BlockByHash(hash gethcommon.Hash) (*types.Block, error) {
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
return e.client.BlockByHash(ctx, hash)
}
func (e *gethRPCClient) CallContract(msg ethereum.CallMsg) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
return e.client.CallContract(ctx, msg, nil)
}
func (e *gethRPCClient) EthClient() *ethclient.Client {
return e.client
}
func (e *gethRPCClient) BalanceAt(gethcommon.Address, *big.Int) (*big.Int, error) {
panic("not implemented")
}
func (e *gethRPCClient) Stop() {
e.client.Close()
}
func connect(ipaddress string, port uint, connectionTimeout time.Duration) (*ethclient.Client, error) {
var err error
var c *ethclient.Client
for start := time.Now(); time.Since(start) < connectionTimeout; time.Sleep(time.Second) {
c, err = ethclient.Dial(fmt.Sprintf("ws://%s:%d", ipaddress, port))
if err == nil {
break
}
}
return c, err
}