-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
block_header.go
67 lines (59 loc) · 1.77 KB
/
block_header.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
package blockheaderfeeder
import (
"bytes"
"context"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/pkg/errors"
)
type Client interface {
BatchCallContext(ctx context.Context, b []rpc.BatchElem) error
}
type GethBlockHeaderProvider struct {
client Client
}
func NewGethBlockHeaderProvider(client Client) *GethBlockHeaderProvider {
return &GethBlockHeaderProvider{
client: client,
}
}
// RlpHeadersBatch retrieves RLP-encoded block headers
// this function is not supported for Avax because Avalanche
// block header format is different from go-ethereum types.Header.
// validation for invalid chain ID is done upstream in blockheaderfeeder.validate.go
func (p *GethBlockHeaderProvider) RlpHeadersBatch(ctx context.Context, blockRange []*big.Int) ([][]byte, error) {
var reqs []rpc.BatchElem
for _, num := range blockRange {
parentBlockNum := big.NewInt(num.Int64() + 1)
req := rpc.BatchElem{
Method: "eth_getHeaderByNumber",
// Get child block since it's the one that has the parent hash in its header.
Args: []interface{}{hexutil.EncodeBig(parentBlockNum)},
Result: &types.Header{},
}
reqs = append(reqs, req)
}
err := p.client.BatchCallContext(ctx, reqs)
if err != nil {
return nil, err
}
var headers [][]byte
for _, req := range reqs {
header, ok := req.Result.(*types.Header)
if !ok {
return nil, errors.Errorf("received invalid type: %T", req.Result)
}
if header == nil {
return nil, errors.New("invariant violation: got nil header")
}
headerBuffer := new(bytes.Buffer)
err := header.EncodeRLP(headerBuffer)
if err != nil {
return nil, err
}
headers = append(headers, headerBuffer.Bytes())
}
return headers, nil
}