/
subscribe_beefy.go
237 lines (188 loc) · 6.36 KB
/
subscribe_beefy.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package cmd
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/snowfork/go-substrate-rpc-client/v4/types"
"github.com/snowfork/snowbridge/relayer/chain/relaychain"
"github.com/snowfork/snowbridge/relayer/crypto/keccak"
"github.com/snowfork/snowbridge/relayer/crypto/merkle"
"github.com/spf13/cobra"
)
func subBeefyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sub-beefy",
Short: "Subscribe beefy messages",
Args: cobra.ExactArgs(0),
RunE: SubBeefyFn,
}
cmd.Flags().StringP("url", "u", "ws://127.0.0.1:9944", "Polkadot URL")
cmd.Flags().UintP(
"para-id",
"i",
1013,
"Parachain ID",
)
return cmd
}
func SubBeefyFn(cmd *cobra.Command, _ []string) error {
subBeefyJustifications(cmd.Context(), cmd)
return nil
}
func subBeefyJustifications(ctx context.Context, cmd *cobra.Command) error {
url, _ := cmd.Flags().GetString("url")
conn := relaychain.NewConnection(url)
err := conn.Connect(ctx)
if err != nil {
log.Error(err)
return err
}
sub, err := conn.API().RPC.Beefy.SubscribeJustifications()
if err != nil {
return err
}
defer sub.Unsubscribe()
for {
select {
case commitment, ok := <-sub.Chan():
if !ok {
return nil
}
blockNumber := commitment.Commitment.BlockNumber
if len(commitment.Signatures) == 0 {
log.Info("BEEFY commitment has no signatures, skipping...")
continue
}
blockHash, err := conn.API().RPC.Chain.GetBlockHash(uint64(blockNumber))
if err != nil {
return err
}
proof, err := conn.API().RPC.MMR.GenerateProof(blockNumber, blockHash)
if err != nil {
return err
}
simpleProof, err := merkle.ConvertToSimplifiedMMRProof(
proof.BlockHash,
uint64(proof.Proof.LeafIndex),
proof.Leaf,
uint64(proof.Proof.LeafCount),
proof.Proof.Items,
)
if err != nil {
return err
}
leafEncoded, err := types.EncodeToBytes(simpleProof.Leaf)
if err != nil {
return err
}
leafHashBytes := (&keccak.Keccak256{}).Hash(leafEncoded)
var leafHash types.H256
copy(leafHash[:], leafHashBytes[0:32])
root := merkle.CalculateMerkleRoot(&simpleProof, leafHash)
if err != nil {
return err
}
var actualMmrRoot types.H256
mmrRootKey, err := types.CreateStorageKey(conn.Metadata(), "Mmr", "RootHash", nil, nil)
if err != nil {
return err
}
_, err = conn.API().RPC.State.GetStorage(mmrRootKey, &actualMmrRoot, blockHash)
if err != nil {
return err
}
actualParentHash, err := conn.API().RPC.Chain.GetBlockHash(uint64(proof.Leaf.ParentNumberAndHash.ParentNumber))
if err != nil {
return err
}
fmt.Printf("Commitment { BlockNumber: %v, ValidatorSetID: %v}\n",
blockNumber,
commitment.Commitment.ValidatorSetID,
)
fmt.Printf("Leaf { ParentNumber: %v, ParentHash: %v, NextValidatorSetID: %v}\n",
proof.Leaf.ParentNumberAndHash.ParentNumber,
proof.Leaf.ParentNumberAndHash.Hash.Hex(),
proof.Leaf.BeefyNextAuthoritySet.ID,
)
fmt.Printf("Actual ParentHash: %v %v\n", actualParentHash.Hex(), actualParentHash == proof.Leaf.ParentNumberAndHash.Hash)
fmt.Printf("MMR Root: computed=%v actual=%v %v\n",
root.Hex(), actualMmrRoot.Hex(), root == actualMmrRoot,
)
fmt.Printf("\n")
if root != actualMmrRoot {
return nil
}
}
}
}
func printCommitment(commitment *types.SignedCommitment, conn *relaychain.Connection) error {
blockNumber := commitment.Commitment.BlockNumber
blockHash, err := conn.API().RPC.Chain.GetBlockHash(uint64(blockNumber))
if err != nil {
return err
}
proof, err := conn.API().RPC.MMR.GenerateProof(blockNumber, blockHash)
if err != nil {
return err
}
fmt.Printf("Commitment { BlockNumber: %v, ValidatorSetID: %v}; Leaf { ParentNumber: %v, NextValidatorSetID: %v }\n",
blockNumber, commitment.Commitment.ValidatorSetID, proof.Leaf.ParentNumberAndHash.ParentNumber, proof.Leaf.BeefyNextAuthoritySet.ID,
)
return nil
}
// // Copied over from relaychain.Connection
// func fetchParaHeads(co *relaychain.Connection, blockHash types.Hash) (map[uint32][]byte, error) {
// keyPrefix := types.CreateStorageKeyPrefix("Paras", "Heads")
// keys, err := co.API().RPC.State.GetKeys(keyPrefix, blockHash)
// if err != nil {
// log.WithError(err).Error("Failed to get all parachain keys")
// return nil, err
// }
// changeSets, err := co.API().RPC.State.QueryStorageAt(keys, blockHash)
// if err != nil {
// log.WithError(err).Error("Failed to get all parachain headers")
// return nil, err
// }
// heads := make(map[uint32][]byte)
// for _, changeSet := range changeSets {
// for _, change := range changeSet.Changes {
// if change.StorageData.IsNone() {
// continue
// }
// var paraID uint32
// if err := types.DecodeFromBytes(change.StorageKey[40:], ¶ID); err != nil {
// log.WithError(err).Error("Failed to decode parachain ID")
// return nil, err
// }
// _, headDataWrapped := change.StorageData.Unwrap()
// var headData types.Bytes
// if err := types.DecodeFromBytes(headDataWrapped, &headData); err != nil {
// log.WithError(err).Error("Failed to decode HeadData wrapper")
// return nil, err
// }
// heads[paraID] = headData
// }
// }
// return heads, nil
// }
// func GetMMRLeafForBlock(blockNumber uint64, blockHash types.Hash, conn *relaychain.Connection) {
// log.WithFields(logrus.Fields{
// "blockNumber": blockNumber,
// "blockHash": blockHash.Hex(),
// }).Info("Getting MMR Leaf for block...")
// proofResponse, err := conn.API().RPC.MMR.GenerateProof(blockNumber, blockHash)
// if err != nil {
// log.WithError(err).Error("Failed to generate mmr proof")
// }
// log.WithFields(logrus.Fields{
// "BlockHash": proofResponse.BlockHash.Hex(),
// "Leaf.ParentNumber": proofResponse.Leaf.ParentNumberAndHash.ParentNumber,
// "Leaf.Hash": proofResponse.Leaf.ParentNumberAndHash.Hash.Hex(),
// "Leaf.ParachainHeads": proofResponse.Leaf.ParachainHeads.Hex(),
// "Leaf.BeefyNextAuthoritySet.ID": proofResponse.Leaf.BeefyNextAuthoritySet.ID,
// "Leaf.BeefyNextAuthoritySet.Len": proofResponse.Leaf.BeefyNextAuthoritySet.Len,
// "Leaf.BeefyNextAuthoritySet.Root": proofResponse.Leaf.BeefyNextAuthoritySet.Root.Hex(),
// "Proof.LeafIndex": proofResponse.Proof.LeafIndex,
// "Proof.LeafCount": proofResponse.Proof.LeafCount,
// }).Info("Generated MMR Proof")
// }