Skip to content

Commit

Permalink
try to revert some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik committed Feb 13, 2024
1 parent 07967b2 commit 67b1e9c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 26 deletions.
9 changes: 7 additions & 2 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,13 @@ contract TaikoL1 is
/// @notice Important: as this contract doesn't send each block's state root as a signal when
/// the block is verified, bridging developers should subscribe to CrossChainSynced events
/// to ensure all synced state roots are verifiable using merkle proofs.
function getSyncedSnippet() public view override returns (ICrossChainSync.Snippet memory) {
return LibUtils.getSyncedSnippet(state, getConfig());
function getSyncedSnippet(uint64 blockId)
public
view
override
returns (ICrossChainSync.Snippet memory)
{
return LibUtils.getSyncedSnippet(state, getConfig(), blockId);
}

/// @notice Gets the state variables of the TaikoL1 contract.
Expand Down
9 changes: 4 additions & 5 deletions packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,24 @@ library LibUtils {

function getSyncedSnippet(
TaikoData.State storage state,
TaikoData.Config memory config
TaikoData.Config memory config,
uint64 blockId
)
external
view
returns (ICrossChainSync.Snippet memory)
{
uint64 _blockId = state.slotB.lastVerifiedBlockId;
uint64 _blockId = blockId == 0 ? state.slotB.lastVerifiedBlockId : blockId;
uint64 slot = _blockId % config.blockRingBufferSize;

TaikoData.Block storage blk = state.blocks[slot];

if (blk.blockId != _blockId) revert L1_BLOCK_MISMATCH();
if (blk.verifiedTransitionId == 0) revert L1_TRANSITION_NOT_FOUND();

TaikoData.TransitionState storage transition =
state.transitions[slot][blk.verifiedTransitionId];

return ICrossChainSync.Snippet({
blockId: _blockId,
blockId: blockId,
blockHash: transition.blockHash,
stateRoot: transition.stateRoot
});
Expand Down
18 changes: 14 additions & 4 deletions packages/protocol/contracts/L2/TaikoL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ contract TaikoL2 is CrossChainOwned, ICrossChainSync {

// Update state variables
l2Hashes[parentId] = blockhash(parentId);
snippets[l1Height] = ICrossChainSync.Snippet({
blockId: l1Height,
blockHash: l1BlockHash,
stateRoot: l1StateRoot
});
publicInputHash = publicInputHashNew;


latestSyncedL1Height = l1Height;
emit Anchored(blockhash(parentId), gasExcess);
}

Expand All @@ -168,8 +172,14 @@ contract TaikoL2 is CrossChainOwned, ICrossChainSync {
}

/// @inheritdoc ICrossChainSync
function getSyncedSnippet() public view returns (ICrossChainSync.Snippet memory) {
return _l1Snippet;
function getSyncedSnippet(uint64 blockId)
public
view
override
returns (ICrossChainSync.Snippet memory)
{
uint256 id = blockId == 0 ? latestSyncedL1Height : blockId;
return snippets[id];
}

/// @notice Gets the basefee and gas excess using EIP-1559 configuration for
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/common/ICrossChainSync.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface ICrossChainSync {
/// @param stateRoot The block's state root.
event CrossChainSynced(uint64 indexed blockId, bytes32 blockHash, bytes32 stateRoot);

/// @notice Fetches the hash of a block from the opposite chain.
/// @notice Fetches the hash of a block from the opposite chain.
/// @param blockId The target block id. Specifying 0 retrieves the hash
/// of the latest block.
/// @return snippet The block hash and signal root synced.
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/HelperContracts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ contract DummyCrossChainSync is EssentialContract, ICrossChainSync {
_snippet.stateRoot = stateRoot;
}

function getSyncedSnippet() public view returns (Snippet memory) {
function getSyncedSnippet(uint64 /*blockId*/ ) public view returns (Snippet memory) {
return _snippet;
}
}
36 changes: 23 additions & 13 deletions packages/protocol/test/L1/TaikoL1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@ contract TaikoL1Test is TaikoL1TestBase {
}

/// @dev getCrossChainBlockHash tests
function test_L1_getCrossChainSnippet_Genesis() external {
ICrossChainSync.Snippet memory snippet = L1.getSyncedSnippet();
assertEq(snippet.blockId, 0);
assertEq(snippet.blockHash, GENESIS_BLOCK_HASH);
assertEq(snippet.stateRoot, 0);
function test_L1_getCrossChainBlockHash0() external {
bytes32 genHash = L1.getSyncedSnippet(0).blockHash;
assertEq(GENESIS_BLOCK_HASH, genHash);

// Reverts if block is not yet verified!
vm.expectRevert(TaikoErrors.L1_BLOCK_MISMATCH.selector);
L1.getSyncedSnippet(1);
}

/// @dev getSyncedSnippet tests
Expand All @@ -208,38 +210,46 @@ contract TaikoL1Test is TaikoL1TestBase {
bytes32 stateRoot;
bytes32[] memory parentHashes = new bytes32[](count);
parentHashes[0] = GENESIS_BLOCK_HASH;

giveEthAndTko(Alice, 1e6 ether, 100_000 ether);
console2.log("Alice balance:", tko.balanceOf(Alice));
giveEthAndTko(Bob, 1e7 ether, 100_000 ether);
console2.log("Bob balance:", tko.balanceOf(Bob));

// Propose blocks
for (uint64 blockId = 1; blockId < count; ++blockId) {
printVariables("before propose");
(meta,) = proposeBlock(Alice, Bob, 1_000_000, 1024);
mine(5);

blockHash = bytes32(1e10 + uint256(blockId));
stateRoot = bytes32(1e9 + uint256(blockId));

proveBlock(
Bob, Bob, meta, parentHashes[blockId - 1], blockHash, stateRoot, meta.minTier, ""
);

vm.roll(block.number + 15 * 12);
uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow + 1);

verifyBlock(Carol, 1);

assertEq(L1.getSyncedSnippet().blockId, blockId);
assertEq(L1.getSyncedSnippet().blockHash, blockHash);
assertEq(L1.getSyncedSnippet().stateRoot, stateRoot);
// Querying written blockhash
assertEq(L1.getSyncedSnippet(blockId).blockHash, blockHash);

mine(5);
parentHashes[blockId] = blockHash;
}

uint64 queriedBlockId = 1;
bytes32 expectedSR = bytes32(1e9 + uint256(queriedBlockId));

assertEq(expectedSR, L1.getSyncedSnippet(queriedBlockId).stateRoot);

// 2nd
queriedBlockId = 2;
expectedSR = bytes32(1e9 + uint256(queriedBlockId));
assertEq(expectedSR, L1.getSyncedSnippet(queriedBlockId).stateRoot);

// Not found -> reverts
vm.expectRevert(TaikoErrors.L1_BLOCK_MISMATCH.selector);
L1.getSyncedSnippet((count + 1));
}

function test_L1_deposit_hash_creation() external {
Expand Down

0 comments on commit 67b1e9c

Please sign in to comment.