-
Notifications
You must be signed in to change notification settings - Fork 12
/
rpc_connection.go
81 lines (69 loc) · 2.01 KB
/
rpc_connection.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
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package adapter
import (
"context"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/orbs-network/orbs-network-go/instrumentation/log"
"github.com/pkg/errors"
"math/big"
"sync"
)
type EthereumRpcConnection struct {
connectorCommon
config ethereumAdapterConfig
mu struct {
sync.Mutex
client *ethclient.Client
}
}
func NewEthereumRpcConnection(config ethereumAdapterConfig, logger log.BasicLogger) *EthereumRpcConnection {
rpc := &EthereumRpcConnection{
config: config,
}
rpc.logger = logger.WithTags(log.String("adapter", "ethereum"))
rpc.getContractCaller = func() (caller EthereumCaller, e error) {
return rpc.dialIfNeededAndReturnClient()
}
return rpc
}
func (rpc *EthereumRpcConnection) dial() error {
rpc.mu.Lock()
defer rpc.mu.Unlock()
if rpc.mu.client != nil {
return nil
}
if client, err := ethclient.Dial(rpc.config.EthereumEndpoint()); err != nil {
return err
} else {
rpc.mu.client = client
}
return nil
}
func (rpc *EthereumRpcConnection) dialIfNeededAndReturnClient() (*ethclient.Client, error) {
if rpc.mu.client == nil {
if err := rpc.dial(); err != nil {
return nil, err
}
}
return rpc.mu.client, nil
}
func (rpc *EthereumRpcConnection) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
client, err := rpc.dialIfNeededAndReturnClient()
if err != nil {
return nil, err
}
header, err := client.HeaderByNumber(ctx, number)
if err != nil {
return nil, err
}
// not supposed to happen since client.HeaderByNumber does not return nil, nil
if header == nil {
return nil, errors.New("ethereum returned nil header without error")
}
return header, nil
}