-
Notifications
You must be signed in to change notification settings - Fork 39
/
cblock.go
189 lines (167 loc) · 4.07 KB
/
cblock.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
package cblock
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"strings"
"sync"
"time"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/ethclient"
"github.com/ava-labs/coreth/interfaces"
"github.com/ava-labs/coreth/rpc"
)
var ErrNotFound = errors.New("block not found")
type Block struct {
Header types.Header `json:"header"`
Uncles []types.Header `json:"uncles"`
TxsBytes *[][]byte `json:"txs,omitempty"`
Version uint32 `json:"version"`
BlockExtraData []byte `json:"blockExtraData"`
Txs []types.Transaction `json:"transactions,omitempty"`
}
func New(bl *types.Block) (*Block, error) {
var cblock Block
cblock.Version = bl.Version()
cblock.BlockExtraData = bl.ExtData()
if cblock.BlockExtraData != nil {
if len(cblock.BlockExtraData) == 0 {
cblock.BlockExtraData = nil
}
}
var h *types.Header = bl.Header()
if h != nil {
cblock.Header = *h
}
for _, u := range bl.Uncles() {
if u == nil {
continue
}
cblock.Uncles = append(cblock.Uncles, *u)
}
for _, t := range bl.Transactions() {
cblock.Txs = append(cblock.Txs, *t)
}
return &cblock, nil
}
func Marshal(bl *types.Block) ([]byte, error) {
b, err := New(bl)
if err != nil {
return nil, err
}
if b == nil {
return nil, fmt.Errorf("invalid block")
}
return json.Marshal(b)
}
func Unmarshal(data []byte) (*Block, error) {
var block Block
err := json.Unmarshal(data, &block)
if err != nil {
return nil, err
}
if block.TxsBytes != nil && len(*block.TxsBytes) != 0 {
// convert the tx bytes into transactions.
for _, t := range *block.TxsBytes {
var tr types.Transaction
err := tr.UnmarshalJSON(t)
if err != nil {
return nil, err
}
block.Txs = append(block.Txs, tr)
}
}
return &block, err
}
type TransactionTrace struct {
Hash string `json:"hash"`
Idx uint32 `json:"idx"`
Trace []byte `json:"trace"`
}
type Client struct {
rpcClient *rpc.Client
ethClient *ethclient.Client
lock sync.Mutex
}
func NewClient(url string) (*Client, error) {
rc, err := rpc.Dial(url)
if err != nil {
return nil, err
}
cl := &Client{}
cl.rpcClient = rc
cl.ethClient = ethclient.NewClient(rc)
return cl, nil
}
func (c *Client) Latest(rpcTimeout time.Duration) (*big.Int, error) {
c.lock.Lock()
defer c.lock.Unlock()
ctx, cancelCTX := context.WithTimeout(context.Background(), rpcTimeout)
defer cancelCTX()
bl, err := c.ethClient.BlockNumber(ctx)
if err != nil {
return nil, err
}
return big.NewInt(0).SetUint64(bl), nil
}
func (c *Client) Close() {
c.rpcClient.Close()
}
type TracerParam struct {
Tracer string `json:"tracer"`
Timeout string `json:"timeout"`
}
type BlockContainer struct {
Block *types.Block
Traces []*TransactionTrace
Logs []*types.Log
}
func (c *Client) ReadBlock(blockNumber *big.Int, rpcTimeout time.Duration) (*BlockContainer, error) {
c.lock.Lock()
defer c.lock.Unlock()
ctx, cancelCTX := context.WithTimeout(context.Background(), rpcTimeout)
defer cancelCTX()
bl, err := c.ethClient.BlockByNumber(ctx, blockNumber)
if err != nil {
return nil, err
}
txTraces := make([]*TransactionTrace, 0, len(bl.Transactions()))
for _, tx := range bl.Transactions() {
txh := tx.Hash().Hex()
if !strings.HasPrefix(txh, "0x") {
txh = "0x" + txh
}
var results []interface{}
err = c.rpcClient.CallContext(ctx, &results, "debug_traceTransaction", txh, TracerParam{Tracer: TracerJS, Timeout: "1m"})
if err != nil {
return nil, err
}
for ipos, result := range results {
traceBits, err := json.Marshal(result)
if err != nil {
return nil, err
}
txTraces = append(txTraces,
&TransactionTrace{
Hash: txh,
Idx: uint32(ipos),
Trace: traceBits,
},
)
}
}
blhash := bl.Hash()
fq := interfaces.FilterQuery{BlockHash: &blhash}
fls, err := c.ethClient.FilterLogs(ctx, fq)
if err != nil {
return nil, err
}
flrs := make([]*types.Log, 0, len(fls))
for _, fl := range fls {
flcopy := fl
flrs = append(flrs, &flcopy)
}
return &BlockContainer{Block: bl, Traces: txTraces, Logs: flrs}, nil
}