Skip to content

Commit

Permalink
Fix mmr generation (#1307)
Browse files Browse the repository at this point in the history
* fix mmr generation

* updated test command

* fixed test command

* remove redundant logging
  • Loading branch information
alistair-singh authored Oct 14, 2024
1 parent 39ae915 commit eebc7ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
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
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

0 comments on commit eebc7ca

Please sign in to comment.