-
Notifications
You must be signed in to change notification settings - Fork 2
/
block.go
91 lines (72 loc) · 2.36 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package rpc
import (
"github.com/availproject/avail-go-sdk/src/extrinsic"
"github.com/availproject/avail-go-sdk/src/header"
"github.com/centrifuge/go-substrate-rpc-client/v4/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
)
type SignedBlock struct {
Block Block `json:"block"`
Justification types.Justification `json:"justification"`
}
// Block encoded with header and extrinsics
type Block struct {
Header header.Header `json:"header"`
Extrinsics []extrinsic.Extrinsic
}
// GetBlock returns the header and body of the relay chain block with the given hash
func GetAvailBlock(blockHash types.Hash, client1 client.Client) (*SignedBlock, error) {
return getBlock(&blockHash, client1)
}
func GetAvailBlockLatest(client1 client.Client) (*SignedBlock, error) {
return getBlock(nil, client1)
}
func getBlock(blockHash *types.Hash, client1 client.Client) (*SignedBlock, error) {
var SignedBlock SignedBlock
err := client.CallWithBlockHash(client1, &SignedBlock, "chain_getBlock", blockHash)
if err != nil {
return nil, err
}
return &SignedBlock, err
}
func GetFinalizedHead(client client.Client) (types.Hash, error) {
var res string
err := client.Call(&res, "chain_getFinalizedHead")
if err != nil {
return types.Hash{}, err
}
return types.NewHashFromHexString(res)
}
func getBlockHash(client client.Client, blockNumber *uint64) (types.Hash, error) {
var res string
var err error
if blockNumber == nil {
err = client.Call(&res, "chain_getBlockHash")
} else {
err = client.Call(&res, "chain_getBlockHash", *blockNumber)
}
if err != nil {
return types.Hash{}, err
}
return types.NewHashFromHexString(res)
}
func GetBlockHash(client client.Client, blockNumber uint64) (types.Hash, error) {
return getBlockHash(client, &blockNumber)
}
func GetBlockHashLatest(client client.Client) (types.Hash, error) {
return getBlockHash(client, nil)
}
func getHeader(client1 client.Client, blockHash *types.Hash) (*header.Header, error) {
var Header header.Header
err := client.CallWithBlockHash(client1, &Header, "chain_getHeader", blockHash)
if err != nil {
return nil, err
}
return &Header, err
}
func GetHeaderLatest(client1 client.Client) (*header.Header, error) {
return getHeader(client1, nil)
}
func GetHeader(client1 client.Client, blockHash types.Hash) (*header.Header, error) {
return getHeader(client1, &blockHash)
}