-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_block.go
72 lines (62 loc) · 1.55 KB
/
rpc_block.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
package main
import (
"context"
)
type RpcBlockHeader struct {
Hash string `json:"hash"`
Height int `json:"Height"`
Timestamp int `json:"timestamp"`
PrevHash string `json:"prev_hash"`
}
type RpcBlockHeaders struct {
Headers []RpcBlockHeader `json:"headers"`
}
type RpcBlock struct {
BlockHeader RpcBlockHeader `json:"block_header"`
TxHashes []string `json:"tx_hashes"`
}
type GetBlockParams struct {
Hash string `json:"hash"`
}
func NewGetBlockPayload(hash string) RPCRequestPayload {
return RPCRequestPayload{
ID: "0",
JSONRPC: "2.0",
Method: "get_block",
Params: GetBlockParams{
Hash: hash,
},
}
}
func (c *RPCClient) GetBlockByHash(ctx context.Context, hash string) (*RpcBlock, error) {
rpcReq := NewGetBlockPayload(hash)
rpcBlock := RpcBlock{}
err := c.MakeRequest(ctx, rpcReq, &rpcBlock)
if err != nil {
return nil, err
}
return &rpcBlock, nil
}
type GetBlocksRangeParams struct {
StartHeight int `json:"start_height"`
EndHeight int `json:"end_height"`
}
func NewGetBlocksRangePayload(start, end int) RPCRequestPayload {
return RPCRequestPayload{
ID: "0",
JSONRPC: "2.0",
Method: "get_block_headers_range",
Params: GetBlocksRangeParams{
StartHeight: start,
EndHeight: end,
},
}
}
func (c *RPCClient) GetBlockHeadersRange(ctx context.Context, start, end int) ([]RpcBlockHeader, error) {
rpcReq := NewGetBlocksRangePayload(start, end)
rpcBlocks := RpcBlockHeaders{}
if err := c.MakeRequest(ctx, rpcReq, &rpcBlocks); err != nil {
return nil, err
}
return rpcBlocks.Headers, nil
}