-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblock.go
187 lines (155 loc) · 4.11 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package complex
import (
meta "go-sdk/metadata"
daPallet "go-sdk/metadata/pallets/data_availability"
prim "go-sdk/primitives"
)
type Block struct {
client *Client
Block RPCBlock
events prim.Option[EventRecords]
}
func NewBlock(client *Client, blockHash prim.H256) Block {
block := client.GetBlock(prim.NewSome(blockHash))
events, err := client.GetEvents(prim.NewSome(blockHash))
if err != nil {
panic(err)
}
return Block{
client: client,
Block: block,
events: prim.NewSome(events),
}
}
func (this *Block) TransactionBySigner(accountId meta.AccountId) []BlockTransaction {
var result = []BlockTransaction{}
for _, tx := range this.Block.Extrinsics {
if !sameSignature(&tx, accountId) {
continue
}
result = append(result, NewBlockTransaction(this.client, &tx))
}
return result
}
func (this *Block) TransactionByIndex(txIndex uint32) prim.Option[BlockTransaction] {
for _, tx := range this.Block.Extrinsics {
if tx.TxIndex == txIndex {
return prim.NewSome(NewBlockTransaction(this.client, &tx))
}
}
return prim.NewNone[BlockTransaction]()
}
func (this *Block) TransactionByHash(txHash prim.H256) prim.Option[BlockTransaction] {
for _, tx := range this.Block.Extrinsics {
if tx.TxHash == txHash {
return prim.NewSome(NewBlockTransaction(this.client, &tx))
}
}
return prim.NewNone[BlockTransaction]()
}
func (this *Block) TransactionByAppId(appId uint32) []BlockTransaction {
var result = []BlockTransaction{}
for _, tx := range this.Block.Extrinsics {
if tx.Signed.IsNone() {
continue
}
var signed = tx.Signed.Unwrap()
if signed.AppId != appId {
continue
}
result = append(result, NewBlockTransaction(this.client, &tx))
}
return result
}
func (this *Block) DataSubmissionBySigner(accountId meta.AccountId) []DataSubmission {
var result = []DataSubmission{}
for _, tx := range this.Block.Extrinsics {
if !sameSignature(&tx, accountId) {
continue
}
if res, ok := NewDataSubmission(&tx); ok {
result = append(result, res)
}
}
return result
}
func (this *Block) DataSubmissionByIndex(txIndex uint32) prim.Option[DataSubmission] {
for _, tx := range this.Block.Extrinsics {
if tx.TxIndex != txIndex {
continue
}
if res, ok := NewDataSubmission(&tx); ok {
return prim.NewSome(res)
}
}
return prim.NewNone[DataSubmission]()
}
func (this *Block) DataSubmissionByHash(txHash prim.H256) prim.Option[DataSubmission] {
for _, tx := range this.Block.Extrinsics {
if tx.TxHash != txHash {
continue
}
if res, ok := NewDataSubmission(&tx); ok {
return prim.NewSome(res)
}
}
return prim.NewNone[DataSubmission]()
}
func (this *Block) DataSubmissionByAppId(appId uint32) []DataSubmission {
var result = []DataSubmission{}
for _, tx := range this.Block.Extrinsics {
if tx.Signed.IsNone() {
continue
}
var signed = tx.Signed.Unwrap()
if signed.AppId != appId {
continue
}
if res, ok := NewDataSubmission(&tx); ok {
result = append(result, res)
}
}
return result
}
func (this *Block) Events(appId uint32) prim.Option[EventRecords] {
return this.events
}
func sameSignature(tx *prim.DecodedExtrinsic, accountId meta.AccountId) bool {
txAccountId := tx.Signed.UnwrapOrDefault().Address.Id.UnwrapOrDefault()
if accountId.Value != txAccountId {
return false
}
return true
}
type DataSubmission struct {
TxHash prim.H256
TxIndex uint32
Data []byte
TxSigner prim.MultiAddress
AppId uint32
}
func NewDataSubmission(tx *prim.DecodedExtrinsic) (DataSubmission, bool) {
ok := false
res := DataSubmission{}
callSubmitData := daPallet.CallSubmitData{}
if tx.Call.PalletIndex != callSubmitData.PalletIndex() {
return res, ok
}
if tx.Call.CallIndex != callSubmitData.CallIndex() {
return res, ok
}
// Data submission cannot be done without signed being set.
signed := tx.Signed.UnwrapOrDefault()
decoder := prim.NewDecoder(prim.FromHex(tx.Call.Fields.Value), 0)
if err := decoder.Decode(&callSubmitData); err != nil {
return res, false
}
res = DataSubmission{
TxHash: tx.TxHash,
TxIndex: tx.TxIndex,
Data: callSubmitData.Data,
TxSigner: signed.Address,
AppId: signed.AppId,
}
return res, true
}