Skip to content

Commit

Permalink
fix(evm): merkle tree tests bytes32
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Maldonado <[email protected]>
  • Loading branch information
md0x committed Nov 7, 2024
1 parent 18df9a0 commit 231b509
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
8 changes: 4 additions & 4 deletions test/evm/hardhat/SpokePool.ExecuteRootBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ describe("SpokePool Root Bundle Execution", function () {
totalSolanaDistributions: evmDistributions,
mixLeaves: true,
chainId: destinationChainId,
evmTokenAddress: destErc20.address,
evmRelayers: [relayer.address, rando.address],
evmTokenAddress: addressToBytes(destErc20.address),
evmRelayers: [addressToBytes(relayer.address), addressToBytes(rando.address)],
evmRefundAmounts: [consts.amountToRelay.div(evmDistributions), consts.amountToRelay.div(evmDistributions)],
});

Expand Down Expand Up @@ -134,8 +134,8 @@ describe("SpokePool Root Bundle Execution", function () {
totalSolanaDistributions: evmDistributions,
mixLeaves: false,
chainId: destinationChainId,
evmTokenAddress: destErc20.address,
evmRelayers: [relayer.address, rando.address],
evmTokenAddress: addressToBytes(destErc20.address),
evmRelayers: [addressToBytes(relayer.address), addressToBytes(rando.address)],
evmRefundAmounts: [consts.amountToRelay.div(evmDistributions), consts.amountToRelay.div(evmDistributions)],
});

Expand Down
27 changes: 17 additions & 10 deletions test/svm/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { BN, Program } from "@coral-xyz/anchor";
import { Keypair, PublicKey } from "@solana/web3.js";
import { BigNumber, ethers } from "ethers";
import * as crypto from "crypto";
import { BigNumber, ethers } from "ethers";
import { SvmSpoke } from "../../target/types/svm_spoke";

import { MerkleTree } from "@uma/common";
import {
readEvents,
readProgramEvents,
calculateRelayHashUint8Array,
findProgramAddress,
LargeAccountsCoder,
readEvents,
readProgramEvents,
} from "../../src/SvmUtils";
import { MerkleTree } from "@uma/common";
import { getParamType, keccak256 } from "../../test-utils";
import { ParamType } from "ethers/lib/utils";
import { addressToBytes, isBytes32 } from "../../test-utils";

export { readEvents, readProgramEvents, calculateRelayHashUint8Array, findProgramAddress };
export { calculateRelayHashUint8Array, findProgramAddress, readEvents, readProgramEvents };

export async function printLogs(connection: any, program: any, tx: any) {
const latestBlockHash = await connection.getLatestBlockhash();
Expand Down Expand Up @@ -102,6 +101,14 @@ export function buildRelayerRefundMerkleTree({
}): { relayerRefundLeaves: RelayerRefundLeafType[]; merkleTree: MerkleTree<RelayerRefundLeafType> } {
const relayerRefundLeaves: RelayerRefundLeafType[] = [];

if (evmTokenAddress && !isBytes32(evmTokenAddress)) {
throw new Error("EVM token address must be a bytes32 address");
}

if (evmRelayers && evmRelayers.some((address) => !isBytes32(address))) {
throw new Error("EVM relayers must be bytes32 addresses");
}

const createSolanaLeaf = (index: number) => ({
isSolana: true,
leafId: new BN(index),
Expand All @@ -118,8 +125,8 @@ export function buildRelayerRefundMerkleTree({
leafId: BigNumber.from(index),
chainId: BigNumber.from(chainId),
amountToReturn: BigNumber.from(0),
l2TokenAddress: evmTokenAddress ?? randomAddress(),
refundAddresses: evmRelayers || [randomAddress(), randomAddress()],
l2TokenAddress: evmTokenAddress ?? addressToBytes(randomAddress()),
refundAddresses: evmRelayers || [addressToBytes(randomAddress()), addressToBytes(randomAddress())],
refundAmounts: evmRefundAmounts || [BigNumber.from(randomBigInt()), BigNumber.from(randomBigInt())],
} as RelayerRefundLeaf);

Expand Down Expand Up @@ -179,7 +186,7 @@ export const relayerRefundHashFn = (input: RelayerRefundLeaf | RelayerRefundLeaf
const abiCoder = new ethers.utils.AbiCoder();
const encodedData = abiCoder.encode(
[
"tuple( uint256 amountToReturn, uint256 chainId, uint256[] refundAmounts, uint256 leafId, address l2TokenAddress, address[] refundAddresses)",
"tuple( uint256 amountToReturn, uint256 chainId, uint256[] refundAmounts, uint256 leafId, bytes32 l2TokenAddress, bytes32[] refundAddresses)",
],
[
{
Expand Down
2 changes: 2 additions & 0 deletions utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export const bytes32ToAddress = (input: string) => {
return ethers.utils.getAddress("0x" + input.slice(26));
};

export const isBytes32 = (input: string) => /^0x[0-9a-fA-F]{64}$/.test(input);

export async function seedWallet(
walletToFund: Signer,
tokens: Contract[],
Expand Down

0 comments on commit 231b509

Please sign in to comment.