-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathblocks.go
122 lines (98 loc) · 2.49 KB
/
blocks.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
package substrate
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/pkg/errors"
)
func (s *Substrate) GetCurrentHeight() (uint32, error) {
cl, meta, err := s.GetClient()
if err != nil {
return 0, err
}
var blockNumber uint32
key, err := types.CreateStorageKey(meta, "System", "Number", nil)
if err != nil {
err = errors.Wrap(err, "failed to create storage key")
return 0, err
}
ok, err := cl.RPC.State.GetStorageLatest(key, &blockNumber)
if err != nil {
return 0, err
}
if !ok {
return 0, errors.New("block number not found")
}
return blockNumber, nil
}
func (s *Substrate) FetchEventsForBlockRange(start uint32, end uint32) (types.StorageKey, []types.StorageChangeSet, error) {
cl, meta, err := s.GetClient()
if err != nil {
return nil, nil, err
}
key, err := types.CreateStorageKey(meta, "System", "Events", nil)
if err != nil {
return key, nil, err
}
lbh, err := cl.RPC.Chain.GetBlockHash(uint64(start))
if err != nil {
return key, nil, err
}
uph, err := cl.RPC.Chain.GetBlockHash(uint64(end))
if err != nil {
return key, nil, err
}
rawSet, err := cl.RPC.State.QueryStorage([]types.StorageKey{key}, lbh, uph)
if err != nil {
return key, nil, err
}
return key, rawSet, nil
}
func (s *Substrate) GetEventsForBlock(start uint32) (*EventRecords, error) {
cl, _, err := s.GetClient()
if err != nil {
return nil, err
}
block, err := cl.RPC.Chain.GetBlockHash(uint64(start))
if err != nil {
return nil, err
}
meta, err := cl.RPC.State.GetMetadata(block)
if err != nil {
return nil, err
}
key, err := types.CreateStorageKey(meta, "System", "Events", nil)
if err != nil {
return nil, err
}
var storageData types.StorageDataRaw
ok, err := cl.RPC.State.GetStorage(key, &storageData, block)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("failed to get storage")
}
events := EventRecords{}
err = types.EventRecordsRaw(storageData).DecodeEventRecords(meta, &events)
if err != nil {
return nil, err
}
return &events, nil
}
func (s *Substrate) GetBlock(block types.Hash) (*types.SignedBlock, error) {
cl, _, err := s.GetClient()
if err != nil {
return nil, err
}
return cl.RPC.Chain.GetBlock(block)
}
func (s *Substrate) GetBlockByNumber(blockNumber types.U32) (*types.SignedBlock, error) {
cl, _, err := s.GetClient()
if err != nil {
return nil, err
}
hash, err := cl.RPC.Chain.GetBlockHash(uint64(blockNumber))
if err != nil {
return nil, err
}
return cl.RPC.Chain.GetBlock(hash)
}