Skip to content

Commit

Permalink
test: add ckb header mock
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuanhust committed Dec 9, 2023
1 parent 8e49c1d commit b073abe
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 31 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;
}
}
30 changes: 0 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
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);
});
});
8 changes: 7 additions & 1 deletion 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();
await CkbProof.link(ckbLightClient.address);
console.log("ckbLightClient deployed on ", ckbLightClient.address);
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);

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

0 comments on commit b073abe

Please sign in to comment.