Skip to content

Merkle proof verification fail #399

@SetarehGhorshi

Description

@SetarehGhorshi

Hey.

We are making an abci query to our cosmos chain and try to verify the proof. For this, we are attempting to use the ics23::verify_membership function but it always returns false (the calculated root does not match the root hash from the block). we have ascertained that the abci query response is correct. Could you please help with this issue?
Thanks.

Below is the code we are using for the verification:

fn verify_merkle_proof(
    proof_ops: &ProofOps,
    key: &[u8],
    value: &[u8],
    root_hash: &Vec<u8>,
) -> Result<bool> {
    // Convert ProofOps into an ICS23 CommitmentProof
    let proof_op = proof_ops
        .ops
        .iter()
        .find(|op| op.r#field_type == "ics23:iavl")
        .context("ProofOp for IAVL leaf not found")?;

    let commitment_proof;

    match convert_proof_op(&proof_op) {
        Ok(c_proof) => {
            println!("Successfully converted proof: {:?}", c_proof);
            commitment_proof = c_proof;
        }
        Err(err) if err.to_string().contains("decode") => {
            println!("Decoding error: {:?}", err);
            anyhow::bail!("Proof verification failed");
        }
        Err(err) => {
            println!("Unhandled error: {:?}", err);
            anyhow::bail!("Proof verification failed");
        }
    }

    let spec = ics23::iavl_spec();

    // Verify membership using ICS23 utilities.
    let is_valid =
        verify_membership::<HostFunctionsManager>(&commitment_proof, &spec, root_hash, key, value);

    if let Some(Proof::Exist(existence_proof)) = &commitment_proof.proof {
        println!("existence proof found: {:?}", existence_proof);
        match calculate_existence_root::<ics23::HostFunctionsManager>(existence_proof) {
            Ok(root) => {
                println!("Successfully calculated root: {:?}", root);
                println!("root hash from block: {:?}", root_hash);
            }
            Err(e) => {
                println!("Error calculating existence root: {:?}", e);
            }
        }
    } else {
        println!("existence proof not found");
    }

    if is_valid {
        return Ok(is_valid);
    } else {
        anyhow::bail!("Proof verification failed");
    }
}

Update:
I tried defining the spec dynamically based on the proof but the issue is still there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions