Skip to content

Commit

Permalink
Revert when precompile fails
Browse files Browse the repository at this point in the history
and simplify deployment a bit by making some library functions internal
  • Loading branch information
blckngm committed Dec 21, 2023
1 parent c43a5ad commit d33d0ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 61 deletions.
70 changes: 16 additions & 54 deletions contracts/clients/CkbProof.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,59 +57,23 @@ struct Envelope {
bytes content;
}

library CkbLightClient {
event GetHeaderEvent(CKBHeader);
event NotGetHeaderEvent();

function getHeader(bytes32 blockHash) public returns (CKBHeader memory) {
// axon_precompile_address(0x02)
address get_header_addr = address(0x0102);
(bool isSuccess, bytes memory res) = get_header_addr.staticcall(
abi.encode(blockHash)
);

CKBHeader memory header;
if (isSuccess) {
header = abi.decode(res, (CKBHeader));
/*
replace above decode into the following data can pass test
header = CKBHeader({
version: 0,
compactTarget: 0,
timestamp: 0,
number: 0,
epoch: 0,
parentHash: bytes32(0),
transactionsRoot: 0x7c57536c95df426f5477c344f8f949e4dfd25443d6f586b4f350ae3e4b870433,
proposalsHash: bytes32(0),
extraHash: bytes32(0),
dao: bytes32(0),
nonce: uint128(0),
extension: "",
blockHash: bytes32(0)
});
*/
emit GetHeaderEvent(header);
} else {
emit NotGetHeaderEvent();
}
return header;
}
function getHeader(bytes32 blockHash) view returns (CKBHeader memory) {
// axon_precompile_address(0x02)
address get_header_addr = address(0x0102);
(bool isSuccess, bytes memory res) = get_header_addr.staticcall(
abi.encode(blockHash)
);
require(isSuccess, "getHeader");
return abi.decode(res, (CKBHeader));
}

using CkbLightClient for bytes32;

// Define ckb blake2b
function blake2b(bytes memory data) view returns (bytes32) {
// axon_precompile_address(0x06)
address blake2b_addr = address(0x0106);
(bool isSuccess, bytes memory res) = blake2b_addr.staticcall(data);

bytes32 hash;
if (isSuccess) {
hash = abi.decode(res, (bytes32));
}
return hash;
require(isSuccess, "blake2b");
return abi.decode(res, (bytes32));
}

function ckbMbtVerify(VerifyProofPayload memory payload) view returns (bool) {
Expand All @@ -118,11 +82,7 @@ function ckbMbtVerify(VerifyProofPayload memory payload) view returns (bool) {
(bool isSuccess, bytes memory res) = ckb_mbt_addr.staticcall(
abi.encode(payload)
);

if (!isSuccess) {
return false;
}

require(isSuccess, "ckbMbtVerify");
return uint8(res[0]) == 1;
}

Expand Down Expand Up @@ -284,7 +244,7 @@ library CkbProof {
bytes calldata rlpiEncodedProof,
bytes memory path,
bytes calldata value
) public returns (bool) {
) internal view returns (bool) {
// Parse the proof from the abi encoded data
AxonObjectProof memory axonObjProof = decodeAxonObjectProof(
rlpiEncodedProof
Expand All @@ -304,7 +264,7 @@ library CkbProof {
}

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

// Create the VerifyProofPayload
VerifyProofPayload memory payload = VerifyProofPayload({
Expand All @@ -326,6 +286,8 @@ library CkbProof {
);

// Check if the commitment path/value matches the provided path/value
return isCommitInCommitments(commitments, path, value);
require(isCommitInCommitments(commitments, path, value), "commitment mismatch");

return true;
}
}
8 changes: 1 addition & 7 deletions migrations/1_deploy_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const MockClient = artifacts.require("MockClient");
const MockModule = artifacts.require("MockModule");
const CkbClient = artifacts.require("CkbClient");
const Molecule = artifacts.require("Molecule");
const CkbProof = artifacts.require("CkbProof");
const CkbLightClient = artifacts.require("CkbLightClient");

module.exports = async function (deployer, network) {
console.log("Deploy contracts for network", network);
Expand Down Expand Up @@ -47,11 +45,7 @@ module.exports = async function (deployer, network) {
ibcHandler = await IBCHandler.deployed();

const molecule = await Molecule.new();
const ckbLightClient = await CkbLightClient.new();
CkbProof.link(molecule);
CkbProof.link(ckbLightClient);
const ckbProof = await CkbProof.new();
CkbClient.link(ckbProof);
CkbClient.link(molecule);
await deployer.deploy(CkbClient);
const ckbClient = await CkbClient.deployed();

Expand Down

0 comments on commit d33d0ee

Please sign in to comment.