-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
277 lines (252 loc) · 9.21 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package web3
import (
"context"
"encoding/json"
"fmt"
"log"
"math/big"
"github.com/gochain/gochain/v3/common"
"github.com/gochain/gochain/v3/common/hexutil"
"github.com/gochain/gochain/v3/core/types"
"github.com/gochain/gochain/v3/rpc"
)
// Client is an interface for the web3 RPC API.
type Client interface {
// GetBalance returns the balance for an address at the given block number (nil for latest).
GetBalance(ctx context.Context, address string, blockNumber *big.Int) (*big.Int, error)
// GetCode returns the code for an address at the given block number (nil for latest).
GetCode(ctx context.Context, address string, blockNumber *big.Int) ([]byte, error)
// GetBlockByNumber returns block details by number (nil for latest), optionally including full txs.
GetBlockByNumber(ctx context.Context, number *big.Int, includeTxs bool) (*Block, error)
// GetBlockByHash returns block details for the given hash, optionally include full transaction details.
GetBlockByHash(ctx context.Context, hash string, includeTxs bool) (*Block, error)
// GetTransactionByHash returns transaction details for a hash.
GetTransactionByHash(ctx context.Context, hash common.Hash) (*Transaction, error)
// GetSnapshot returns the latest clique snapshot.
GetSnapshot(ctx context.Context) (*Snapshot, error)
// GetID returns unique identifying information for the network.
GetID(ctx context.Context) (*ID, error)
// GetTransactionReceipt returns the receipt for a transaction hash.
GetTransactionReceipt(ctx context.Context, hash common.Hash) (*Receipt, error)
// GetChainID returns the chain id for the network.
GetChainID(ctx context.Context) (*big.Int, error)
// GetNetworkID returns the network id.
GetNetworkID(ctx context.Context) (*big.Int, error)
// GetGasPrice returns a suggested gas price.
GetGasPrice(ctx context.Context) (*big.Int, error)
// GetPendingTransactionCount returns the transaction count including pending txs.
// This value is also the next legal nonce.
GetPendingTransactionCount(ctx context.Context, account common.Address) (uint64, error)
// SendRawTransaction sends the signed raw transaction bytes.
SendRawTransaction(ctx context.Context, tx []byte) error
// Call executes a call without submitting a transaction.
Call(ctx context.Context, msg CallMsg) ([]byte, error)
Close()
}
// Dial returns a new client backed by dialing url (supported schemes "http", "https", "ws" and "wss").
func Dial(url string) (Client, error) {
r, err := rpc.Dial(url)
if err != nil {
return nil, err
}
return NewClient(r), nil
}
// NewClient returns a new client backed by an existing rpc.Client.
func NewClient(r *rpc.Client) Client {
return &client{r: r}
}
type client struct {
r *rpc.Client
}
func (c *client) Close() {
c.r.Close()
}
func (c *client) Call(ctx context.Context, msg CallMsg) ([]byte, error) {
var result hexutil.Bytes
err := c.r.CallContext(ctx, &result, "eth_call", toCallArg(msg), "latest")
if err != nil {
return nil, err
}
return result, err
}
func (c *client) GetBalance(ctx context.Context, address string, blockNumber *big.Int) (*big.Int, error) {
var result hexutil.Big
err := c.r.CallContext(ctx, &result, "eth_getBalance", common.HexToAddress(address), toBlockNumArg(blockNumber))
return (*big.Int)(&result), err
}
func (c *client) GetCode(ctx context.Context, address string, blockNumber *big.Int) ([]byte, error) {
var result hexutil.Bytes
err := c.r.CallContext(ctx, &result, "eth_getCode", common.HexToAddress(address), toBlockNumArg(blockNumber))
return result, err
}
func (c *client) GetBlockByNumber(ctx context.Context, number *big.Int, includeTxs bool) (*Block, error) {
return c.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), includeTxs)
}
func (c *client) GetBlockByHash(ctx context.Context, hash string, includeTxs bool) (*Block, error) {
return c.getBlock(ctx, "eth_getBlockByHash", hash, includeTxs)
}
func (c *client) GetTransactionByHash(ctx context.Context, hash common.Hash) (*Transaction, error) {
var tx *Transaction
err := c.r.CallContext(ctx, &tx, "eth_getTransactionByHash", hash.String())
if err != nil {
return nil, err
} else if tx == nil {
return nil, NotFoundErr
} else if tx.R == nil {
return nil, fmt.Errorf("server returned transaction without signature")
}
return tx, nil
}
func (c *client) GetSnapshot(ctx context.Context) (*Snapshot, error) {
var s Snapshot
err := c.r.CallContext(ctx, &s, "clique_getSnapshot", "latest")
if err != nil {
return nil, err
}
return &s, nil
}
func (c *client) GetID(ctx context.Context) (*ID, error) {
var block Block
var netIDStr string
chainID := new(hexutil.Big)
batch := []rpc.BatchElem{
{Method: "eth_getBlockByNumber", Args: []interface{}{"0x0", false}, Result: &block},
{Method: "net_version", Result: &netIDStr},
{Method: "eth_chainId", Result: chainID},
}
if err := c.r.BatchCallContext(ctx, batch); err != nil {
return nil, err
}
for _, e := range batch {
if e.Error != nil {
log.Printf("Method %q failed: %v\n", e.Method, e.Error)
}
}
netID := new(big.Int)
if _, ok := netID.SetString(netIDStr, 10); !ok {
return nil, fmt.Errorf("invalid net_version result %q", netIDStr)
}
return &ID{NetworkID: netID, ChainID: (*big.Int)(chainID), GenesisHash: block.Hash}, nil
}
func (c *client) GetNetworkID(ctx context.Context) (*big.Int, error) {
version := new(big.Int)
var ver string
if err := c.r.CallContext(ctx, &ver, "net_version"); err != nil {
return nil, err
}
if _, ok := version.SetString(ver, 10); !ok {
return nil, fmt.Errorf("invalid net_version result %q", ver)
}
return version, nil
}
func (c *client) GetChainID(ctx context.Context) (*big.Int, error) {
var result hexutil.Big
err := c.r.CallContext(ctx, &result, "eth_chainId")
return (*big.Int)(&result), err
}
func (c *client) GetTransactionReceipt(ctx context.Context, hash common.Hash) (*Receipt, error) {
var r *Receipt
err := c.r.CallContext(ctx, &r, "eth_getTransactionReceipt", hash)
if err == nil {
if r == nil {
return nil, NotFoundErr
}
}
return r, err
}
func (c *client) GetGasPrice(ctx context.Context) (*big.Int, error) {
var hex hexutil.Big
if err := c.r.CallContext(ctx, &hex, "eth_gasPrice"); err != nil {
return nil, err
}
return (*big.Int)(&hex), nil
}
func (c *client) GetPendingTransactionCount(ctx context.Context, account common.Address) (uint64, error) {
return c.getTransactionCount(ctx, account, "pending")
}
func (c *client) getTransactionCount(ctx context.Context, account common.Address, blockNumArg string) (uint64, error) {
var result hexutil.Uint64
err := c.r.CallContext(ctx, &result, "eth_getTransactionCount", account, blockNumArg)
return uint64(result), err
}
func (c *client) SendRawTransaction(ctx context.Context, tx []byte) error {
return c.r.CallContext(ctx, nil, "eth_sendRawTransaction", common.ToHex(tx))
}
func (c *client) getBlock(ctx context.Context, method string, hashOrNum string, includeTxs bool) (*Block, error) {
var raw json.RawMessage
err := c.r.CallContext(ctx, &raw, method, hashOrNum, includeTxs)
if err != nil {
return nil, err
} else if len(raw) == 0 {
return nil, NotFoundErr
}
var block Block
if err := json.Unmarshal(raw, &block); err != nil {
return nil, fmt.Errorf("failed to unmarshal json response: %v", err)
}
// Quick-verify transaction and uncle lists. This mostly helps with debugging the server.
if block.Sha3Uncles == types.EmptyUncleHash && len(block.Uncles) > 0 {
return nil, fmt.Errorf("server returned non-empty uncle list but block header indicates no uncles")
}
if block.Sha3Uncles != types.EmptyUncleHash && len(block.Uncles) == 0 {
return nil, fmt.Errorf("server returned empty uncle list but block header indicates uncles")
}
if block.TxsRoot == types.EmptyRootHash && block.TxCount() > 0 {
return nil, fmt.Errorf("server returned non-empty transaction list but block header indicates no transactions")
}
if block.TxsRoot != types.EmptyRootHash && len(block.TxsRoot) == 0 {
return nil, fmt.Errorf("server returned empty transaction list but block header indicates transactions")
}
// Load uncles because they are not included in the block response.
var uncles []*types.Header
if len(block.Uncles) > 0 {
uncles = make([]*types.Header, len(block.Uncles))
reqs := make([]rpc.BatchElem, len(block.Uncles))
for i := range reqs {
reqs[i] = rpc.BatchElem{
Method: "eth_getUncleByBlockHashAndIndex",
Args: []interface{}{block.Hash, hexutil.EncodeUint64(uint64(i))},
Result: &uncles[i],
}
}
if err := c.r.BatchCallContext(ctx, reqs); err != nil {
return nil, err
}
for i := range reqs {
if reqs[i].Error != nil {
return nil, reqs[i].Error
}
if uncles[i] == nil {
return nil, fmt.Errorf("got null header for uncle %d of block %x", i, block.Hash[:])
}
}
}
return &block, nil
}
func toBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
}
return hexutil.EncodeBig(number)
}
func toCallArg(msg CallMsg) interface{} {
arg := map[string]interface{}{
"to": msg.To,
}
if msg.From != nil {
arg["from"] = msg.From
}
if len(msg.Data) > 0 {
arg["data"] = hexutil.Bytes(msg.Data)
}
if msg.Value != nil {
arg["value"] = (*hexutil.Big)(msg.Value)
}
if msg.Gas != 0 {
arg["gas"] = hexutil.Uint64(msg.Gas)
}
if msg.GasPrice != nil {
arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice)
}
return arg
}