-
Notifications
You must be signed in to change notification settings - Fork 8
/
mapclient.go
136 lines (122 loc) · 4.47 KB
/
mapclient.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
package ethclient
import (
"context"
"encoding/json"
"fmt"
"github.com/mapprotocol/atlas/helper/bls"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/mapprotocol/atlas/consensus/istanbul/backend"
"github.com/mapprotocol/atlas/core/types"
)
type rpcMAPBlock struct {
Hash common.Hash `json:"hash"`
Transactions []rpcMAPTransaction `json:"transactions"`
Randomness *types.Randomness `json:"randomness"`
EpochSnarkData *EpochSnarkData `json:"epochSnarkData"`
}
type EpochSnarkData struct {
Bitmap string
Signature string
}
type rpcMAPTransaction struct {
tx *types.Transaction
txExtraInfo
}
func (tx *rpcMAPTransaction) UnmarshalJSON(msg []byte) error {
if err := json.Unmarshal(msg, &tx.tx); err != nil {
return err
}
return json.Unmarshal(msg, &tx.txExtraInfo)
}
func (ec *Client) getMAPBlock(ctx context.Context, method string, args ...interface{}) (*Block, error) {
var raw json.RawMessage
err := ec.c.CallContext(ctx, &raw, method, args...)
if err != nil {
return nil, err
} else if len(raw) == 0 {
return nil, ethereum.NotFound
}
// Decode header and transactions.
var head *types.Header
var body Block
if err := json.Unmarshal(raw, &head); err != nil {
return nil, err
}
if err := json.Unmarshal(raw, &body); err != nil {
return nil, err
}
if head.TxHash == types.EmptyRootHash && len(body.Transactions) > 0 {
return nil, fmt.Errorf("server returned non-empty transaction list but block header indicates no transactions")
}
if head.TxHash != types.EmptyRootHash && len(body.Transactions) == 0 {
return nil, fmt.Errorf("server returned empty transaction list but block header indicates transactions")
}
return &body, nil
}
func (ec *Client) getTronBlock(ctx context.Context, method string, args ...interface{}) (*Block, error) {
var raw json.RawMessage
err := ec.c.CallContext(ctx, &raw, method, args...)
if err != nil {
return nil, err
} else if len(raw) == 0 {
return nil, ethereum.NotFound
}
// Decode header and transactions.
var body Block
if err := json.Unmarshal(raw, &body); err != nil {
return nil, err
}
if common.HexToHash(body.TransactionsRoot) == types.EmptyRootHash && len(body.Transactions) > 0 {
return nil, fmt.Errorf("server returned non-empty transaction list but block header indicates no transactions")
}
if common.HexToHash(body.TransactionsRoot) != types.EmptyRootHash && len(body.Transactions) == 0 {
return nil, fmt.Errorf("server returned empty transaction list but block header indicates transactions")
}
return &body, nil
}
// MAPBlockByHash returns the given full block.
//
// Note that loading full blocks requires two requests. Use HeaderByHash
// if you don't need all transactions or uncle headers.
func (ec *Client) MAPBlockByHash(ctx context.Context, hash common.Hash) (*Block, error) {
return ec.getMAPBlock(ctx, "eth_getBlockByHash", hash, true)
}
// MAPBlockByNumber returns a block from the current canonical chain. If number is nil, the
// latest known block is returned.
//
// Note that loading full blocks requires two requests. Use HeaderByNumber
// if you don't need all transactions or uncle headers.
func (ec *Client) MAPBlockByNumber(ctx context.Context, number *big.Int) (*Block, error) {
return ec.getMAPBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true)
}
func (ec *Client) TronBlockByNumber(ctx context.Context, number *big.Int) (*Block, error) {
return ec.getTronBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true)
}
// MAPHeaderByNumber returns a block header from the current canonical chain. If number is
// nil, the latest known header is returned.
func (ec *Client) MAPHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
var head *types.Header
err := ec.c.CallContext(ctx, &head, "eth_getBlockByNumber", toBlockNumArg(number), false)
if err == nil && head == nil {
err = ethereum.NotFound
}
return head, err
}
func (ec *Client) GetSnapshot(ctx context.Context, number *big.Int) (*backend.Snapshot, error) {
var snap *backend.Snapshot
err := ec.c.CallContext(ctx, &snap, "istanbul_getSnapshot", toBlockNumArg(number))
if err != nil {
return nil, err
}
return snap, err
}
func (ec *Client) GetValidatorsBLSPublicKeys(ctx context.Context, number *big.Int) ([]bls.SerializedPublicKey, error) {
var snap []bls.SerializedPublicKey
err := ec.c.CallContext(ctx, &snap, "istanbul_getValidatorsBLSPublicKeys", toBlockNumArg(number))
if err != nil {
return nil, err
}
return snap, err
}