From 2c2dbfbef14dccb7d819acb3decc877a2c4e4717 Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Sun, 11 Feb 2024 22:21:44 +0800 Subject: [PATCH 1/7] Update LibTrieProof.t.sol --- packages/protocol/test/libs/LibTrieProof.t.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/protocol/test/libs/LibTrieProof.t.sol b/packages/protocol/test/libs/LibTrieProof.t.sol index ece888fbd4d..10aa06ac853 100644 --- a/packages/protocol/test/libs/LibTrieProof.t.sol +++ b/packages/protocol/test/libs/LibTrieProof.t.sol @@ -4,7 +4,7 @@ pragma solidity 0.8.24; import "../TaikoTest.sol"; import "../../contracts/libs/LibTrieProof.sol"; -contract TestVerifyFullMerkleProof is TaikoTest { +contract TestLibTrieProof is TaikoTest { function test_verifyFullMerkleProof() public { // Not needed for now, but leave it as is. //uint64 chainId = 11_155_111; // Created the proofs on a deployed Sepolia @@ -52,12 +52,14 @@ contract TestVerifyFullMerkleProof is TaikoTest { bytes memory merkleProof = abi.encode(accountProof, storageProof); vm.startPrank(Alice); - LibTrieProof.verifyFullMerkleProof( + bool verified = LibTrieProof.verifyFullMerkleProof( worldStateRoot, contractWhichStoresValue1AtSlot, slotStoredAtTheApp, hex"01", merkleProof ); + + assertEq(verified, true); } } From 0da04387b39913bb3dbab0a0984714e0e52a6d3e Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Sun, 11 Feb 2024 22:23:19 +0800 Subject: [PATCH 2/7] Update LibTrieProof.t.sol --- packages/protocol/test/libs/LibTrieProof.t.sol | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/protocol/test/libs/LibTrieProof.t.sol b/packages/protocol/test/libs/LibTrieProof.t.sol index 10aa06ac853..16fc95c6fb1 100644 --- a/packages/protocol/test/libs/LibTrieProof.t.sol +++ b/packages/protocol/test/libs/LibTrieProof.t.sol @@ -51,7 +51,6 @@ contract TestLibTrieProof is TaikoTest { hex"e3a1209749684f52b5c0717a7ca78127fb56043d637d81763c04e9d30ba4d4746d56e901"; bytes memory merkleProof = abi.encode(accountProof, storageProof); - vm.startPrank(Alice); bool verified = LibTrieProof.verifyFullMerkleProof( worldStateRoot, contractWhichStoresValue1AtSlot, From 99c002520cdd3fd26fc217b45a564b39ce40c81d Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Sun, 11 Feb 2024 22:24:53 +0800 Subject: [PATCH 3/7] more --- packages/protocol/contracts/libs/LibTrieProof.sol | 3 +++ packages/protocol/test/libs/LibTrieProof.t.sol | 4 ++-- .../protocol/test/team/airdrop/ERC20Airdrop.t.sol | 12 +++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/protocol/contracts/libs/LibTrieProof.sol b/packages/protocol/contracts/libs/LibTrieProof.sol index 1dee05fe13e..24740e3b9fe 100644 --- a/packages/protocol/contracts/libs/LibTrieProof.sol +++ b/packages/protocol/contracts/libs/LibTrieProof.sol @@ -56,5 +56,8 @@ library LibTrieProof { verified = SecureMerkleTrie.verifyInclusionProof( bytes.concat(slot), bytes.concat(value), storageProof, bytes32(storageRoot) ); + + // TODO(dani): may I suggest to remove the return value and revert in this fuction if + // verified is false? } } diff --git a/packages/protocol/test/libs/LibTrieProof.t.sol b/packages/protocol/test/libs/LibTrieProof.t.sol index 16fc95c6fb1..2a47ec3a3bc 100644 --- a/packages/protocol/test/libs/LibTrieProof.t.sol +++ b/packages/protocol/test/libs/LibTrieProof.t.sol @@ -51,7 +51,7 @@ contract TestLibTrieProof is TaikoTest { hex"e3a1209749684f52b5c0717a7ca78127fb56043d637d81763c04e9d30ba4d4746d56e901"; bytes memory merkleProof = abi.encode(accountProof, storageProof); - bool verified = LibTrieProof.verifyFullMerkleProof( + bool verified = LibTrieProof.verifyFullMerkleProof( worldStateRoot, contractWhichStoresValue1AtSlot, slotStoredAtTheApp, @@ -59,6 +59,6 @@ contract TestLibTrieProof is TaikoTest { merkleProof ); - assertEq(verified, true); + assertEq(verified, true); } } 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({ From 8da1a303ba406f0460a79bc0a1e448088a4267da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keszey=20D=C3=A1niel?= Date: Sun, 11 Feb 2024 21:08:12 +0530 Subject: [PATCH 4/7] fix test and implementation --- packages/protocol/contracts/libs/LibTrieProof.sol | 8 +++----- packages/protocol/contracts/signal/SignalService.sol | 5 ++--- packages/protocol/test/libs/LibTrieProof.t.sol | 6 +----- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/packages/protocol/contracts/libs/LibTrieProof.sol b/packages/protocol/contracts/libs/LibTrieProof.sol index 24740e3b9fe..1d678a564df 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. @@ -25,7 +26,6 @@ library LibTrieProof { * @param stateRoot The merkle root of state tree. * @param addr The address of contract. * @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. */ @@ -33,7 +33,6 @@ library LibTrieProof { bytes32 stateRoot, address addr, bytes32 slot, - bytes32 value, bytes memory mkproof ) internal @@ -54,10 +53,9 @@ library LibTrieProof { RLPReader.readBytes(accountState[ACCOUNT_FIELD_INDEX_STORAGE_HASH]); verified = SecureMerkleTrie.verifyInclusionProof( - bytes.concat(slot), bytes.concat(value), storageProof, bytes32(storageRoot) + bytes.concat(slot), hex"01", storageProof, bytes32(storageRoot) ); - // TODO(dani): may I suggest to remove the return value and revert in this fuction if - // verified is false? + 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..c3be03bcbfc 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, 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 2a47ec3a3bc..6ee3838f25e 100644 --- a/packages/protocol/test/libs/LibTrieProof.t.sol +++ b/packages/protocol/test/libs/LibTrieProof.t.sol @@ -52,11 +52,7 @@ contract TestLibTrieProof is TaikoTest { bytes memory merkleProof = abi.encode(accountProof, storageProof); bool verified = LibTrieProof.verifyFullMerkleProof( - worldStateRoot, - contractWhichStoresValue1AtSlot, - slotStoredAtTheApp, - hex"01", - merkleProof + worldStateRoot, contractWhichStoresValue1AtSlot, slotStoredAtTheApp, merkleProof ); assertEq(verified, true); From 1ff8bc4c9e5733e7ea32c2ce47d6a42f7c9976cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keszey=20D=C3=A1niel?= Date: Sun, 11 Feb 2024 21:30:19 +0530 Subject: [PATCH 5/7] modify value param type --- packages/protocol/contracts/libs/LibTrieProof.sol | 3 ++- packages/protocol/contracts/signal/SignalService.sol | 2 +- packages/protocol/test/libs/LibTrieProof.t.sol | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/protocol/contracts/libs/LibTrieProof.sol b/packages/protocol/contracts/libs/LibTrieProof.sol index 1d678a564df..7530fdde45f 100644 --- a/packages/protocol/contracts/libs/LibTrieProof.sol +++ b/packages/protocol/contracts/libs/LibTrieProof.sol @@ -33,6 +33,7 @@ library LibTrieProof { bytes32 stateRoot, address addr, bytes32 slot, + bytes memory value, bytes memory mkproof ) internal @@ -53,7 +54,7 @@ library LibTrieProof { RLPReader.readBytes(accountState[ACCOUNT_FIELD_INDEX_STORAGE_HASH]); verified = SecureMerkleTrie.verifyInclusionProof( - bytes.concat(slot), hex"01", storageProof, bytes32(storageRoot) + 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 c3be03bcbfc..40787a60c0a 100644 --- a/packages/protocol/contracts/signal/SignalService.sol +++ b/packages/protocol/contracts/signal/SignalService.sol @@ -171,7 +171,7 @@ contract SignalService is EssentialContract, ISignalService { bytes32 slot = getSignalSlot(srcChainId, srcApp, srcSignal); // verifyFullMerkleProof() will revert in case if something is not valid - LibTrieProof.verifyFullMerkleProof(stateRoot, signalService, slot, merkleProof); + 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 6ee3838f25e..2a47ec3a3bc 100644 --- a/packages/protocol/test/libs/LibTrieProof.t.sol +++ b/packages/protocol/test/libs/LibTrieProof.t.sol @@ -52,7 +52,11 @@ contract TestLibTrieProof is TaikoTest { bytes memory merkleProof = abi.encode(accountProof, storageProof); bool verified = LibTrieProof.verifyFullMerkleProof( - worldStateRoot, contractWhichStoresValue1AtSlot, slotStoredAtTheApp, merkleProof + worldStateRoot, + contractWhichStoresValue1AtSlot, + slotStoredAtTheApp, + hex"01", + merkleProof ); assertEq(verified, true); From 8fee8c882b2f492928b924a77c4d6dcdbcdd3056 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keszey=20D=C3=A1niel?= Date: Sun, 11 Feb 2024 21:34:55 +0530 Subject: [PATCH 6/7] natspec --- packages/protocol/contracts/libs/LibTrieProof.sol | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/protocol/contracts/libs/LibTrieProof.sol b/packages/protocol/contracts/libs/LibTrieProof.sol index 7530fdde45f..562bfea16cf 100644 --- a/packages/protocol/contracts/libs/LibTrieProof.sol +++ b/packages/protocol/contracts/libs/LibTrieProof.sol @@ -26,6 +26,7 @@ library LibTrieProof { * @param stateRoot The merkle root of state tree. * @param addr The address of contract. * @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. */ From fbb2026bfc260d3b80ff3dd4e178c4f9d21a51a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keszey=20D=C3=A1niel?= Date: Sun, 11 Feb 2024 23:14:52 +0530 Subject: [PATCH 7/7] remove retval --- packages/protocol/contracts/libs/LibTrieProof.sol | 4 +--- packages/protocol/test/libs/LibTrieProof.t.sol | 6 ++---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/protocol/contracts/libs/LibTrieProof.sol b/packages/protocol/contracts/libs/LibTrieProof.sol index 562bfea16cf..df4968ddf9d 100644 --- a/packages/protocol/contracts/libs/LibTrieProof.sol +++ b/packages/protocol/contracts/libs/LibTrieProof.sol @@ -28,7 +28,6 @@ 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, @@ -39,7 +38,6 @@ library LibTrieProof { ) internal pure - returns (bool verified) { (bytes[] memory accountProof, bytes[] memory storageProof) = abi.decode(mkproof, (bytes[], bytes[])); @@ -54,7 +52,7 @@ library LibTrieProof { bytes memory storageRoot = RLPReader.readBytes(accountState[ACCOUNT_FIELD_INDEX_STORAGE_HASH]); - verified = SecureMerkleTrie.verifyInclusionProof( + bool verified = SecureMerkleTrie.verifyInclusionProof( bytes.concat(slot), value, storageProof, bytes32(storageRoot) ); diff --git a/packages/protocol/test/libs/LibTrieProof.t.sol b/packages/protocol/test/libs/LibTrieProof.t.sol index 2a47ec3a3bc..a497477a026 100644 --- a/packages/protocol/test/libs/LibTrieProof.t.sol +++ b/packages/protocol/test/libs/LibTrieProof.t.sol @@ -5,7 +5,7 @@ import "../TaikoTest.sol"; import "../../contracts/libs/LibTrieProof.sol"; contract TestLibTrieProof is TaikoTest { - function test_verifyFullMerkleProof() public { + 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,14 +51,12 @@ contract TestLibTrieProof is TaikoTest { hex"e3a1209749684f52b5c0717a7ca78127fb56043d637d81763c04e9d30ba4d4746d56e901"; bytes memory merkleProof = abi.encode(accountProof, storageProof); - bool verified = LibTrieProof.verifyFullMerkleProof( + LibTrieProof.verifyFullMerkleProof( worldStateRoot, contractWhichStoresValue1AtSlot, slotStoredAtTheApp, hex"01", merkleProof ); - - assertEq(verified, true); } }