Skip to content

Commit

Permalink
test: call verifyProof through verifyMembership, execution err: Panic…
Browse files Browse the repository at this point in the history
…: Oversized array or out of memory
  • Loading branch information
wenyuanhust committed Dec 13, 2023
1 parent 0e4c752 commit 822472d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
17 changes: 5 additions & 12 deletions contracts/clients/CkbProof.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ contract CkbMbt {
}

function parseProof(
bytes memory abiEncodedProof
bytes calldata abiEncodedProof
) pure returns (AxonObjectProof memory) {
AxonObjectProof memory proof = abi.decode(
abiEncodedProof,
Expand Down Expand Up @@ -219,25 +219,15 @@ function isCommitInCommitments(
return false;
}

// import "hardhat/console.sol";

library CkbProof {
// constructor() {
// logger.log("CkbProof deployed");
// }

function verifyProof(
bytes memory abiEncodedProof,
bytes calldata abiEncodedProof,
bytes memory path,
bytes calldata value
) public returns (bool) {
// Parse the proof from the abi encoded data
AxonObjectProof memory axonObjProof = parseProof(abiEncodedProof);

// Get the CKB header
CkbLightClient lightClient;
CKBHeader memory header = lightClient.getHeader(axonObjProof.blockHash);

// Calculate the transaction hash and witness hash
(, bytes32 witnessHash) = calculateHashes(axonObjProof.ckbTransaction);

Expand All @@ -250,6 +240,9 @@ library CkbProof {
) {
return false;
}
// Get the CKB header
CkbLightClient lightClient;
CKBHeader memory header = lightClient.getHeader(axonObjProof.blockHash);

// Create the VerifyProofPayload
VerifyProofPayload memory payload = VerifyProofPayload({
Expand Down
41 changes: 27 additions & 14 deletions test/verifyMembership.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const CkbMbt = artifacts.require('CkbMbt');
const Blake2b = artifacts.require('Blake2b');
const CkbProof = artifacts.require("CkbProof");

const CkbClient = artifacts.require("CkbClient");

const fs = require('fs');
const proof_path = require('path');
const ethers = require('ethers');
Expand All @@ -13,7 +15,7 @@ contract("CkbProof", (accounts) => {
it("test verifyMembership", async () => {
const molecule = await Molecule.new();
console.log("molecule deployed on ", molecule.address);
await CkbProof.link(molecule.address);
await CkbProof.link(molecule);

const ckbLightClient = await CkbLightClientMock.new();
console.log("ckbLightClient deployed on ", ckbLightClient.address);
Expand All @@ -22,39 +24,50 @@ contract("CkbProof", (accounts) => {
let blockNumber = ethers.utils.hexZeroPad(blockNumberHexString, 32);
const header = await ckbLightClient.getHeader(blockNumber);
console.log("header transactionsRoot", header.transactionsRoot);
await CkbProof.link(ckbLightClient.address);
await CkbProof.link(ckbLightClient);

const ckbMbt = await CkbMbt.new();
console.log("ckbMbt deployed on ", ckbMbt.address);
await CkbProof.link(ckbMbt.address);
await CkbProof.link(ckbMbt);

const blake2b = await Blake2b.new();
await CkbProof.link(blake2b.address);
console.log("blake2b deployed on ", blake2b.address);
await CkbProof.link(blake2b);

const ckbProofInstance = await CkbProof.new();
// const ckbProofInstance = await CkbProof.deployed();
console.log("CkbProof deployed on ", ckbProofInstance.address);
// console.log(ckbProofInstance);
await CkbClient.link(ckbProofInstance);

console.log("abiEncodedProof");
const filePath = proof_path.join(__dirname, './hex_proof.txt');
const hexString = fs.readFileSync(filePath, 'utf8');
console.log("hexString len ", hexString.length);

const abiEncodedProof = web3.utils.hexToBytes(hexString);
console.log("abiEncodedProof", abiEncodedProof);
console.log("Proof in hex:", web3.utils.bytesToHex(abiEncodedProof));
console.log("abiEncodedProof len ", abiEncodedProof.length);
// console.log("Proof in hex:", web3.utils.bytesToHex(abiEncodedProof));

const path = "commitments/ports/ccdefc1fc781b8c1a9a946dfdeeb32829ef2f86e47e8e4d69f6e5bbbb960f42c/channels/channel-0/sequences/1";
const value = "0xec577607291e6c583bdf479ab7f8b59f851419121e3d116befeeeb0f1b0a4f87";
const pathBytes = Buffer.from(path);
const valueBytes = Buffer.from(value.slice(2), 'hex'); // remove the "0x" prefix and convert from hexadecimal
console.log(pathBytes);
console.log(valueBytes);
const result = await ckbProofInstance.verifyProof(abiEncodedProof, pathBytes, valueBytes);

// Replace `expected` with the expected result
const expected = true;
assert.equal(result, expected, "The proof verification did not return the expected result");

const ckbClient = await CkbClient.new();
console.log("ckbClient deployed on ", ckbClient.address);
// Client Create
const data = {
revisionNumber: 0,
revisionHeight: 0
};
// const result = await ckbClient.verifyMembership.call("", data, 0, 0, abiEncodedProof, "0x", pathBytes, valueBytes);
const result = await ckbClient.verifyMembership("", data, 0, 0, abiEncodedProof, "0x", pathBytes, valueBytes);

assert.equal(result, true, "The proof verification did not return the expected result");

// library can not call non pure non view function
// const result = await ckbProofInstance.verifyProof(abiEncodedProof, pathBytes, valueBytes);
// const expected = true;
// assert.equal(result, expected, "The proof verification did not return the expected result");
});
});

0 comments on commit 822472d

Please sign in to comment.