Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(protocol): adopt optimism new trie codebase #15608

Merged
merged 11 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/protocol/contracts/L1/hooks/AssignmentHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ contract AssignmentHook is EssentialContract, IHook {
pure
returns (uint256)
{
for (uint256 i; i < tierFees.length; ++i) {
for (uint256 i; i < tierFees.length;) {
if (tierFees[i].tier == tierId) return tierFees[i].fee;
unchecked {
++i;
}
dantaik marked this conversation as resolved.
Show resolved Hide resolved
}
revert HOOK_TIER_NOT_FOUND();
}
Expand Down
5 changes: 4 additions & 1 deletion packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ library LibProposing {
// Note that address(this).balance has been updated with msg.value,
// prior to any code in this function has been executed.
address prevHook;
for (uint256 i; i < params.hookCalls.length; ++i) {
for (uint256 i; i < params.hookCalls.length;) {
if (uint160(prevHook) >= uint160(params.hookCalls[i].hook)) {
revert L1_INVALID_HOOK();
}
Expand All @@ -260,6 +260,9 @@ library LibProposing {
);

prevHook = params.hookCalls[i].hook;
unchecked {
++i;
}
dantaik marked this conversation as resolved.
Show resolved Hide resolved
}
// Refund Ether
if (address(this).balance != 0) {
Expand Down
5 changes: 4 additions & 1 deletion packages/protocol/contracts/L1/provers/Guardians.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ abstract contract Guardians is EssentialContract {

// Delete current guardians data
uint256 guardiansLength = guardians.length;
for (uint256 i; i < guardiansLength; ++i) {
for (uint256 i; i < guardiansLength;) {
delete guardianIds[guardians[i]];
unchecked {
++i;
}
dantaik marked this conversation as resolved.
Show resolved Hide resolved
}
assembly {
sstore(guardians.slot, 0)
Expand Down
10 changes: 5 additions & 5 deletions packages/protocol/contracts/L1/verifiers/PseZkVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pragma solidity 0.8.24;

import "../../4844/Lib4844.sol";
import "../../common/EssentialContract.sol";
import "../../thirdparty/LibBytesUtils.sol";
import "../../thirdparty/optimism/Bytes.sol";
import "../TaikoData.sol";
import "./IVerifier.sol";

Expand Down Expand Up @@ -91,14 +91,14 @@ contract PseZkVerifier is EssentialContract, IVerifier {
}

// Validate the instance using bytes utilities.
bool verified = LibBytesUtils.equal(
LibBytesUtils.slice(zkProof.zkp, 0, 32), bytes.concat(bytes16(0), bytes16(instance))
bool verified = Bytes.equal(
Bytes.slice(zkProof.zkp, 0, 32), bytes.concat(bytes16(0), bytes16(instance))
);

if (!verified) revert L1_INVALID_PROOF();

verified = LibBytesUtils.equal(
LibBytesUtils.slice(zkProof.zkp, 32, 32),
verified = Bytes.equal(
Bytes.slice(zkProof.zkp, 32, 32),
bytes.concat(bytes16(0), bytes16(uint128(uint256(instance))))
);
if (!verified) revert L1_INVALID_PROOF();
Expand Down
7 changes: 3 additions & 4 deletions packages/protocol/contracts/L1/verifiers/SgxAndZkVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
pragma solidity 0.8.24;

import "../../common/EssentialContract.sol";
import "../../thirdparty/LibBytesUtils.sol";
import "../../thirdparty/optimism/Bytes.sol";
import "../TaikoData.sol";
import "./IVerifier.sol";

Expand Down Expand Up @@ -44,12 +44,11 @@ contract SgxAndZkVerifier is EssentialContract, IVerifier {
_proof.tier = proof.tier;

// Verify the SGX part
_proof.data = LibBytesUtils.slice(proof.data, 0, SGX_PROOF_SIZE);
_proof.data = Bytes.slice(proof.data, 0, SGX_PROOF_SIZE);
IVerifier(resolve("tier_sgx", false)).verifyProof(ctx, tran, _proof);

// Verify the ZK part
_proof.data =
LibBytesUtils.slice(proof.data, SGX_PROOF_SIZE, (proof.data.length - SGX_PROOF_SIZE));
_proof.data = Bytes.slice(proof.data, SGX_PROOF_SIZE, (proof.data.length - SGX_PROOF_SIZE));
IVerifier(resolve("tier_pse_zkevm", false)).verifyProof(ctx, tran, _proof);
}
}
8 changes: 4 additions & 4 deletions packages/protocol/contracts/L1/verifiers/SgxVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pragma solidity 0.8.24;

import "lib/openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol";
import "../../common/EssentialContract.sol";
import "../../thirdparty/LibBytesUtils.sol";
import "../../thirdparty/optimism/Bytes.sol";
import "../ITaikoL1.sol";
import "./IVerifier.sol";

Expand Down Expand Up @@ -126,9 +126,9 @@ contract SgxVerifier is EssentialContract, IVerifier {
// 4 bytes + 20 bytes + 65 bytes (signature) = 89
if (proof.data.length != 89) revert SGX_INVALID_PROOF();

uint32 id = uint32(bytes4(LibBytesUtils.slice(proof.data, 0, 4)));
address newInstance = address(bytes20(LibBytesUtils.slice(proof.data, 4, 20)));
bytes memory signature = LibBytesUtils.slice(proof.data, 24);
uint32 id = uint32(bytes4(Bytes.slice(proof.data, 0, 4)));
address newInstance = address(bytes20(Bytes.slice(proof.data, 4, 20)));
bytes memory signature = Bytes.slice(proof.data, 24);

address oldInstance =
ECDSA.recover(getSignedHash(tran, newInstance, ctx.prover, ctx.metaHash), signature);
Expand Down
27 changes: 22 additions & 5 deletions packages/protocol/contracts/signal/SignalService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pragma solidity 0.8.24;
import "lib/openzeppelin-contracts/contracts/utils/math/SafeCast.sol";
import "../common/AuthorizableContract.sol";
import "../common/ICrossChainSync.sol";
import "../thirdparty/LibSecureMerkleTrie.sol";
import "../thirdparty/optimism/trie/SecureMerkleTrie.sol";
import "../thirdparty/optimism/rlp/RLPReader.sol";
import "./ISignalService.sol";

/// @title SignalService
Expand Down Expand Up @@ -86,6 +87,7 @@ contract SignalService is AuthorizableContract, ISignalService {
)
public
view
virtual
returns (bool)
{
if (skipProofCheck()) return true;
Expand Down Expand Up @@ -129,18 +131,19 @@ contract SignalService is AuthorizableContract, ISignalService {
hop.signalRootRelay,
hop.signalRoot // as a signal
);
bool verified = LibSecureMerkleTrie.verifyInclusionProof(
bytes.concat(slot), hex"01", hop.storageProof, signalRoot

bool verified = SecureMerkleTrie.verifyInclusionProof(
bytes.concat(slot), hex"01", _transcode(hop.storageProof), signalRoot
);
if (!verified) return false;

signalRoot = hop.signalRoot;
}

return LibSecureMerkleTrie.verifyInclusionProof(
return SecureMerkleTrie.verifyInclusionProof(
bytes.concat(getSignalSlot(srcChainId, app, signal)),
hex"01",
p.storageProof,
_transcode(p.storageProof),
signalRoot
);
}
Expand Down Expand Up @@ -168,4 +171,18 @@ contract SignalService is AuthorizableContract, ISignalService {
function skipProofCheck() public pure virtual returns (bool) {
return false;
}

/// @notice Translate a RLP-encoded list of RLP-encoded TrieNodes into a list of LP-encoded
/// TrieNodes.
function _transcode(bytes memory proof) internal pure returns (bytes[] memory proofs) {
RLPReader.RLPItem[] memory nodes = RLPReader.readList(proof);
proofs = new bytes[](nodes.length);

for (uint256 i; i < nodes.length;) {
proofs[i] = RLPReader.readBytes(nodes[i]);
unchecked {
++i;
}
dantaik marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
128 changes: 0 additions & 128 deletions packages/protocol/contracts/thirdparty/LibBytesUtils.sol

This file was deleted.

Loading
Loading