Skip to content

Commit

Permalink
test: add ckb header mock & real (path, value) pair
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuanhust committed Dec 12, 2023
1 parent 8e49c1d commit 0e4c752
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 37 deletions.
60 changes: 60 additions & 0 deletions contracts/clients/CkbLightClientMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;

import "./CkbClient.sol";

// contract CkbLightClientMock is CkbLightClient {
contract CkbLightClientMock is CkbLightClient {
// Mock function for `getHeader`
function getHeader(
bytes32
) public pure override returns (CKBHeader memory) {
string memory hexString = "6985ea05ba57214c2c3ef93185b0dda2a5d6b56dfcf79e51a1c4e8e2b287d72a";

CKBHeader memory ckbHeader = CKBHeader({
version: 0,
compactTarget: 0,
timestamp: 0,
number: 0,
epoch: 0,
parentHash: bytes32(0),
transactionsRoot: bytes32(fromHex(hexString)),
proposalsHash: bytes32(0),
extraHash: bytes32(0),
dao: bytes32(0),
nonce: uint128(0),
extension: "",
blockHash: bytes32(0)
});
return ckbHeader;
}

// Convert an hexadecimal character to their value
function fromHexChar(uint8 c) public pure returns (uint8) {
if (bytes1(c) >= bytes1("0") && bytes1(c) <= bytes1("9")) {
return c - uint8(bytes1("0"));
}
if (bytes1(c) >= bytes1("a") && bytes1(c) <= bytes1("f")) {
return 10 + c - uint8(bytes1("a"));
}
if (bytes1(c) >= bytes1("A") && bytes1(c) <= bytes1("F")) {
return 10 + c - uint8(bytes1("A"));
}
revert("fail");
}

// Convert an hexadecimal string to raw bytes
function fromHex(string memory s) public pure returns (bytes memory) {
bytes memory ss = bytes(s);
require(ss.length % 2 == 0); // length must be even
bytes memory r = new bytes(ss.length / 2);
for (uint i = 0; i < ss.length / 2; ++i) {
r[i] = bytes1(
fromHexChar(uint8(ss[2 * i])) *
16 +
fromHexChar(uint8(ss[2 * i + 1]))
);
}
return r;
}
}
36 changes: 6 additions & 30 deletions contracts/clients/CkbProof.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,6 @@ contract CkbLightClient {
}
}

contract CkbLightClientMock is CkbLightClient {
// Mock function for `getHeader`
function getHeader(
bytes32
) public pure override returns (CKBHeader memory) {
string memory hexString = "6985ea05ba57214c2c3ef93185b0dda2a5d6b56dfcf79e51a1c4e8e2b287d72a";
bytes32 transactionsRoot;
assembly {
transactionsRoot := mload(add(hexString, 0x20))
}

CKBHeader memory ckbHeader = CKBHeader({
version: 0,
compactTarget: 0,
timestamp: 0,
number: 0,
epoch: 0,
parentHash: bytes32(0),
transactionsRoot: transactionsRoot,
proposalsHash: bytes32(0),
extraHash: bytes32(0),
dao: bytes32(0),
nonce: uint128(0),
extension: "",
blockHash: bytes32(0)
});
return ckbHeader;
}
}

// Define ckb blake2b
contract Blake2b {
function blake2b(bytes memory data) public view returns (bytes32) {
Expand Down Expand Up @@ -249,7 +219,13 @@ function isCommitInCommitments(
return false;
}

// import "hardhat/console.sol";

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

function verifyProof(
bytes memory abiEncodedProof,
bytes memory path,
Expand Down
16 changes: 16 additions & 0 deletions test/clientMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const CkbLightClientMock = artifacts.require("CkbLightClientMock");
const ethers = require('ethers');

contract("CkbLightClientMock", () => {
it("should return correct transactionsRoot", async () => {
const ckbLightClientMock = await CkbLightClientMock.new();
console.log("ckbLightClient deployed on ", ckbLightClientMock.address);
let txRootHexString = "0x6985ea05ba57214c2c3ef93185b0dda2a5d6b56dfcf79e51a1c4e8e2b287d72a";
let txRootBytes32Value = ethers.utils.hexZeroPad(txRootHexString, 32);
let blockNumberHexString = "0x2a";
let blockNumber = ethers.utils.hexZeroPad(blockNumberHexString, 32);
const header = await ckbLightClientMock.getHeader(blockNumber);
console.log("txRoot ", header.transactionsRoot);
expect(header.transactionsRoot).to.equal(txRootBytes32Value);
});
});
1 change: 0 additions & 1 deletion test/hex_object_proof.txt

This file was deleted.

1 change: 1 addition & 0 deletions test/hex_proof.txt

Large diffs are not rendered by default.

21 changes: 15 additions & 6 deletions test/verifyMembership.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const CkbProof = artifacts.require("CkbProof");

const fs = require('fs');
const proof_path = require('path');
const ethers = require('ethers');

contract("CkbProof", (accounts) => {
it("test verifyMembership", async () => {
Expand All @@ -15,8 +16,13 @@ contract("CkbProof", (accounts) => {
await CkbProof.link(molecule.address);

const ckbLightClient = await CkbLightClientMock.new();
console.log("ckbLightClient deployed on ", ckbLightClient.address);
// the blockNumberHexString can be arbitrary hex
let blockNumberHexString = "0x2a";
let blockNumber = ethers.utils.hexZeroPad(blockNumberHexString, 32);
const header = await ckbLightClient.getHeader(blockNumber);
console.log("header transactionsRoot", header.transactionsRoot);
await CkbProof.link(ckbLightClient.address);
console.log("ckbLightClient deployed on ", ckbLightClient.address);

const ckbMbt = await CkbMbt.new();
console.log("ckbMbt deployed on ", ckbMbt.address);
Expand All @@ -31,18 +37,21 @@ contract("CkbProof", (accounts) => {
console.log("CkbProof deployed on ", ckbProofInstance.address);

console.log("abiEncodedProof");
const filePath = proof_path.join(__dirname, './hex_object_proof.txt');
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));

const path = "0x000";
const value = "0x77";

const result = await ckbProofInstance.verifyMembership(abiEncodedProof, path, value);
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;
Expand Down

0 comments on commit 0e4c752

Please sign in to comment.