Skip to content
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
3 changes: 0 additions & 3 deletions programs/svm-spoke/src/instructions/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ pub fn fill_v3_relay(
fill_status_account.status = FillStatus::Filled;
fill_status_account.relayer = *ctx.accounts.signer.key;

// TODO: remove msg! everywhere
msg!("Tokens transferred successfully.");

// TODO: there might be a better way to do this
// Emit the FilledV3Relay event
let message_clone = relay_data.message.clone(); // Clone the message before it is moved
Expand Down
6 changes: 1 addition & 5 deletions programs/svm-spoke/src/utils/merkle_proof_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ use crate::{error::CustomError, instructions::V3RelayData};
pub fn get_v3_relay_hash(relay_data: &V3RelayData, chain_id: u64) -> [u8; 32] {
let mut input = relay_data.try_to_vec().unwrap();
input.extend_from_slice(&chain_id.to_le_bytes());
// Log the input that will be hashed
msg!("Input to be hashed: {:?}", input);
keccak::hash(&input).0
}

pub fn verify_merkle_proof(root: [u8; 32], leaf: [u8; 32], proof: Vec<[u8; 32]>) -> Result<()> {
msg!("Verifying merkle proof");
let computed_root = process_proof(&proof, &leaf);
if computed_root != root {
msg!("Invalid proof: computed root does not match provided root");
return err!(CustomError::InvalidMerkleProof);
}
msg!("Merkle proof verified successfully");

Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions programs/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ pub mod test {
) -> Result<()> {
let computed_root = process_proof(&proof, &leaf);
if computed_root != root {
msg!("Invalid proof: computed root does not match provided root");
return err!(CustomError::InvalidMerkleProof);
}
msg!("Merkle proof verified successfully");

Ok(())
}

Expand Down
35 changes: 10 additions & 25 deletions test/svm/Utils.Merkle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Test } from "../../target/types/test";
import { expect } from "chai";
import { assert } from "chai";
import { MerkleTree } from "@uma/common/dist/MerkleTree";
import { ethers } from "ethers";
import { BigNumberish } from "ethers";
import { common } from "./SvmSpoke.common";
const { assertSE } = common;

function randomAddress(): string {
const wallet = ethers.Wallet.createRandom();
return wallet.address;
Expand Down Expand Up @@ -79,26 +82,17 @@ describe("utils.merkle", () => {
const leaf = ethers.utils.arrayify(hashFn(relayerRefundLeaves[14]));

// Verify valid leaf
const tx = await program.methods
await program.methods
.verify(
Array.from(root),
Array.from(leaf),
proof.map((p) => Array.from(p))
)
.rpc();

await new Promise((resolve) => setTimeout(resolve, 5000));
const txLogs = await provider.connection.getTransaction(tx, {
commitment: "confirmed",
maxSupportedTransactionVersion: 0,
});

expect(txLogs?.meta?.logMessages?.some((log) => log.includes("Merkle proof verified successfully"))).to.be.true;

// Verify that the excluded element fails to generate a proof and fails verification using the proof generated above.
const invalidLeaf = ethers.utils.arrayify(hashFn(invalidRelayerRefundLeaf));

let error: any;
try {
await program.methods
.verify(
Expand All @@ -107,12 +101,11 @@ describe("utils.merkle", () => {
proof.map((p) => Array.from(p))
)
.rpc();
} catch (e) {
error = e;
assert.fail("Should not be able to verify invalid leaf");
} catch (err: any) {
assert.instanceOf(err, anchor.AnchorError);
assertSE(err.error.errorCode.code, "InvalidMerkleProof", "Expected error code InvalidMerkleProof");
}

expect(error).to.exist;
expect(error.message.includes("Invalid Merkle proof")).to.be.true;
});

it("Test merkle proof verification Across tx", async () => {
Expand All @@ -134,20 +127,12 @@ describe("utils.merkle", () => {
const proofBuffers = proof.map((p) => Buffer.from(p.slice(2), "hex"));

// Verify valid leaf
const tx = await program.methods
await program.methods
.verify(
Array.from(rootBuffer),
Array.from(leafBuffer),
proofBuffers.map((b) => Array.from(b))
)
.rpc();

await new Promise((resolve) => setTimeout(resolve, 5000));
const txLogs = await provider.connection.getTransaction(tx, {
commitment: "confirmed",
maxSupportedTransactionVersion: 0,
});

expect(txLogs?.meta?.logMessages?.some((log) => log.includes("Merkle proof verified successfully"))).to.be.true;
});
});
Loading