From ac1ca310846a075a663c119d404dc8f5f591eb9c Mon Sep 17 00:00:00 2001 From: Daniel Wang <99078276+dantaik@users.noreply.github.com> Date: Mon, 12 Feb 2024 01:46:42 +0800 Subject: [PATCH] fix(protocol): need to fix a bug in LibTrieProof (or its test) (#15739) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Keszey Dániel --- packages/protocol/contracts/libs/LibTrieProof.sol | 11 ++++++----- packages/protocol/contracts/signal/SignalService.sol | 5 ++--- packages/protocol/test/libs/LibTrieProof.t.sol | 5 ++--- .../protocol/test/team/airdrop/ERC20Airdrop.t.sol | 12 +++++++----- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/protocol/contracts/libs/LibTrieProof.sol b/packages/protocol/contracts/libs/LibTrieProof.sol index 1dee05fe13e..df4968ddf9d 100644 --- a/packages/protocol/contracts/libs/LibTrieProof.sol +++ b/packages/protocol/contracts/libs/LibTrieProof.sol @@ -18,6 +18,7 @@ library LibTrieProof { uint256 private constant ACCOUNT_FIELD_INDEX_STORAGE_HASH = 2; error LTP_INVALID_ACCOUNT_PROOF(); + error LTP_INVALID_INCLUSION_PROOF(); /** * Verifies that the value of a slot in the storage of an account is value. @@ -27,18 +28,16 @@ library LibTrieProof { * @param slot The slot in the contract. * @param value The value to be verified. * @param mkproof The proof obtained by encoding storage proof. - * @return verified The verification result. */ function verifyFullMerkleProof( bytes32 stateRoot, address addr, bytes32 slot, - bytes32 value, + bytes memory value, bytes memory mkproof ) internal pure - returns (bool verified) { (bytes[] memory accountProof, bytes[] memory storageProof) = abi.decode(mkproof, (bytes[], bytes[])); @@ -53,8 +52,10 @@ library LibTrieProof { bytes memory storageRoot = RLPReader.readBytes(accountState[ACCOUNT_FIELD_INDEX_STORAGE_HASH]); - verified = SecureMerkleTrie.verifyInclusionProof( - bytes.concat(slot), bytes.concat(value), storageProof, bytes32(storageRoot) + bool verified = SecureMerkleTrie.verifyInclusionProof( + bytes.concat(slot), value, storageProof, bytes32(storageRoot) ); + + if (!verified) revert LTP_INVALID_INCLUSION_PROOF(); } } diff --git a/packages/protocol/contracts/signal/SignalService.sol b/packages/protocol/contracts/signal/SignalService.sol index 0c46e1eee6e..40787a60c0a 100644 --- a/packages/protocol/contracts/signal/SignalService.sol +++ b/packages/protocol/contracts/signal/SignalService.sol @@ -169,10 +169,9 @@ contract SignalService is EssentialContract, ISignalService { address signalService = resolve(srcChainId, "signal_service", false); bytes32 slot = getSignalSlot(srcChainId, srcApp, srcSignal); - bool verified = - LibTrieProof.verifyFullMerkleProof(stateRoot, signalService, slot, hex"01", merkleProof); - if (!verified) revert SS_INVALID_PROOF(); + // verifyFullMerkleProof() will revert in case if something is not valid + LibTrieProof.verifyFullMerkleProof(stateRoot, signalService, slot, hex"01", merkleProof); } /// @notice Checks if multi-hop is enabled. diff --git a/packages/protocol/test/libs/LibTrieProof.t.sol b/packages/protocol/test/libs/LibTrieProof.t.sol index ece888fbd4d..a497477a026 100644 --- a/packages/protocol/test/libs/LibTrieProof.t.sol +++ b/packages/protocol/test/libs/LibTrieProof.t.sol @@ -4,8 +4,8 @@ pragma solidity 0.8.24; import "../TaikoTest.sol"; import "../../contracts/libs/LibTrieProof.sol"; -contract TestVerifyFullMerkleProof is TaikoTest { - function test_verifyFullMerkleProof() public { +contract TestLibTrieProof is TaikoTest { + function test_verifyFullMerkleProof() public pure { // Not needed for now, but leave it as is. //uint64 chainId = 11_155_111; // Created the proofs on a deployed Sepolia // contract, this is why this chainId. @@ -51,7 +51,6 @@ contract TestVerifyFullMerkleProof is TaikoTest { hex"e3a1209749684f52b5c0717a7ca78127fb56043d637d81763c04e9d30ba4d4746d56e901"; bytes memory merkleProof = abi.encode(accountProof, storageProof); - vm.startPrank(Alice); LibTrieProof.verifyFullMerkleProof( worldStateRoot, contractWhichStoresValue1AtSlot, diff --git a/packages/protocol/test/team/airdrop/ERC20Airdrop.t.sol b/packages/protocol/test/team/airdrop/ERC20Airdrop.t.sol index fedb847b8f9..c9f458e0f00 100644 --- a/packages/protocol/test/team/airdrop/ERC20Airdrop.t.sol +++ b/packages/protocol/test/team/airdrop/ERC20Airdrop.t.sol @@ -37,11 +37,13 @@ contract TestERC20Airdrop is TaikoTest { claimEnd = uint64(block.timestamp + 10_000); merkleProof = new bytes32[](3); - token = TaikoToken( deployProxy({ - name: "taiko_token", - impl: address(new TaikoToken()), - data: abi.encodeCall(TaikoToken.init, ("Taiko Token", "TKO", owner)) })); - + token = TaikoToken( + deployProxy({ + name: "taiko_token", + impl: address(new TaikoToken()), + data: abi.encodeCall(TaikoToken.init, ("Taiko Token", "TKO", owner)) + }) + ); airdrop = ERC20Airdrop( deployProxy({