Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mmr generation #1307

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions relayer/cmd/parachain_head_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ func parachainHeadProofCmd() *cobra.Command {
)
cmd.MarkFlagRequired("parachain-id")

cmd.Flags().Uint64(
"parachain-block",
0,
"The parachain block you are trying to prove. i.e. The block containing the message.",
)
cmd.MarkFlagRequired("parachain-block")
return cmd
}

Expand All @@ -72,7 +66,8 @@ func ParachainHeadProofFn(cmd *cobra.Command, _ []string) error {
copy(beefyBlockHash[:], beefyBlockHashHex[0:32])

relayChainBlock, _ := cmd.Flags().GetUint64("relaychain-block")
mmrProof, err := conn.GenerateProofForBlock(relayChainBlock, beefyBlockHash)
// magic plus 1 to make the block a leaf index
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this command used in prod or is it a convenience method for testing?

Copy link
Contributor Author

@alistair-singh alistair-singh Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convenience for testing. If you are concerned about the magic +1 we also do that in the code that runs in prod.

https://github.com/Snowfork/snowbridge/blob/alistair/fix-merkle-proof/relayer/relays/parachain/beefy-listener.go#L239

mmrProof, err := conn.GenerateProofForBlock(relayChainBlock+1, beefyBlockHash)
if err != nil {
log.WithError(err).Error("Cannot connect.")
return err
Expand All @@ -84,7 +79,6 @@ func ParachainHeadProofFn(cmd *cobra.Command, _ []string) error {
}).Info("conn.GenerateProofForBlock")

paraID, _ := cmd.Flags().GetUint32("parachain-id")
parachainBlock, _ := cmd.Flags().GetUint64("parachain-block")

relayChainBlockHash, err := conn.API().RPC.Chain.GetBlockHash(relayChainBlock)
if err != nil {
Expand Down Expand Up @@ -116,12 +110,37 @@ func ParachainHeadProofFn(cmd *cobra.Command, _ []string) error {
"paraId": paraID,
"relayChainBlockHash": relayChainBlockHash.Hex(),
}).Info("parachain.CreateParachainMerkleProof")

merkleProofData, err := parachain.CreateParachainMerkleProof(paraHeadsAsSlice, paraID)
numParas := min(parachain.MaxParaHeads, len(paraHeadsAsSlice))
merkleProofData, err := parachain.CreateParachainMerkleProof(paraHeadsAsSlice[:numParas], paraID)
if err != nil {
log.WithError(err).Error("Cannot create merkle proof.")
return err
}

if merkleProofData.Root.Hex() != mmrProof.Leaf.ParachainHeads.Hex() {
log.WithFields(log.Fields{
"computedMmr": merkleProofData.Root.Hex(),
"mmr": mmrProof.Leaf.ParachainHeads.Hex(),
}).Warn("MMR parachain merkle root does not match calculated merkle root. Filtering out parachain heads.")

paraHeadsAsSlice, err = conn.FilterParachainHeads(paraHeadsAsSlice, relayChainBlockHash)
if err != nil {
log.WithError(err).Fatal("Filtering out parachain heads failed.")
}

numParas = min(parachain.MaxParaHeads, len(paraHeadsAsSlice))
merkleProofData, err = parachain.CreateParachainMerkleProof(paraHeadsAsSlice[:numParas], paraID)
if err != nil {
log.WithError(err).Fatal("Filtering out parachain heads failed.")
}
if merkleProofData.Root.Hex() != mmrProof.Leaf.ParachainHeads.Hex() {
log.WithFields(log.Fields{
"computedMmr": merkleProofData.Root.Hex(),
"mmr": mmrProof.Leaf.ParachainHeads.Hex(),
}).Fatal("MMR parachain merkle root does not match calculated merkle root.")
}
}

log.WithFields(log.Fields{
"paraHeadsAsSlice": paraHeadsAsSlice,
"paraId": paraID,
Expand All @@ -132,7 +151,6 @@ func ParachainHeadProofFn(cmd *cobra.Command, _ []string) error {
"parachainId": paraID,
"relaychainBlockHash": relayChainBlockHash.Hex(),
"relaychainBlockNumber": relayChainBlock,
"parachainBlockNumber": parachainBlock,
"paraHeads": paraHeadsAsSlice,
"parachainHeader": parachainHeader,
}).Info("Generated proof input for parachain block.")
Expand Down
5 changes: 3 additions & 2 deletions relayer/relays/parachain/beefy-listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,16 @@ func (li *BeefyListener) generateAndValidateParasHeadsMerkleProof(input *ProofIn

// Try a filtering out parathreads
log.WithFields(log.Fields{
"beefyBlock": merkleProofData.Root.Hex(),
"leafIndex": mmrProof.Leaf.ParachainHeads.Hex(),
"computedMmr": merkleProofData.Root.Hex(),
"mmr": mmrProof.Leaf.ParachainHeads.Hex(),
}).Warn("MMR parachain merkle root does not match calculated merkle root. Trying to filtering out parathreads.")

paraHeads, err = li.relaychainConn.FilterParachainHeads(paraHeads, input.RelayBlockHash)
if err != nil {
return nil, paraHeads, fmt.Errorf("could not filter out parathreads: %w", err)
}

numParas = min(MaxParaHeads, len(paraHeads))
merkleProofData, err = CreateParachainMerkleProof(paraHeads[:numParas], input.ParaID)
if err != nil {
return nil, paraHeads, fmt.Errorf("create parachain header proof: %w", err)
Expand Down
Loading