From d33d0ee32b0b89aafac885fd3f68946ea1f2dc68 Mon Sep 17 00:00:00 2001 From: Yin Guanhao Date: Thu, 21 Dec 2023 19:23:02 +0800 Subject: [PATCH] Revert when precompile fails and simplify deployment a bit by making some library functions internal --- contracts/clients/CkbProof.sol | 70 ++++++++------------------------ migrations/1_deploy_contracts.js | 8 +--- 2 files changed, 17 insertions(+), 61 deletions(-) diff --git a/contracts/clients/CkbProof.sol b/contracts/clients/CkbProof.sol index 1e96210..d285926 100644 --- a/contracts/clients/CkbProof.sol +++ b/contracts/clients/CkbProof.sol @@ -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) { @@ -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; } @@ -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 @@ -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({ @@ -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; } } diff --git a/migrations/1_deploy_contracts.js b/migrations/1_deploy_contracts.js index e62e8cb..a0479a3 100644 --- a/migrations/1_deploy_contracts.js +++ b/migrations/1_deploy_contracts.js @@ -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); @@ -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();