From 1bfa69b4458f7edc4b72efe9c2d8cf9c7050853e Mon Sep 17 00:00:00 2001 From: jeff <113397187+cyberhorsey@users.noreply.github.com> Date: Wed, 25 Jan 2023 05:48:31 -0800 Subject: [PATCH] fix(protocol): fix two protocol bugs (#13034) Co-authored-by: Daniel Wang <99078276+dantaik@users.noreply.github.com> Co-authored-by: David --- packages/protocol/contracts/L1/TaikoL1.sol | 11 +++++++---- packages/protocol/contracts/L1/libs/LibUtils.sol | 11 ++++++++--- packages/protocol/contracts/L1/libs/LibVerifying.sol | 6 ++++-- packages/protocol/test/L1/TaikoL1.test.ts | 2 +- packages/protocol/test/utils/propose.ts | 2 +- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/protocol/contracts/L1/TaikoL1.sol b/packages/protocol/contracts/L1/TaikoL1.sol index 076a09c092c..5116f71aef4 100644 --- a/packages/protocol/contracts/L1/TaikoL1.sol +++ b/packages/protocol/contracts/L1/TaikoL1.sol @@ -241,13 +241,16 @@ contract TaikoL1 is EssentialContract, IHeaderSync, TaikoEvents { function getSyncedHeader( uint256 number - ) public view override returns (bytes32 header) { - header = state.getL2BlockHash(number); - require(header != 0, "L1:number"); + ) public view override returns (bytes32) { + return state.getL2BlockHash(number, getConfig().blockHashHistory); } function getLatestSyncedHeader() public view override returns (bytes32) { - return state.getL2BlockHash(state.latestVerifiedHeight); + return + state.getL2BlockHash( + state.latestVerifiedHeight, + getConfig().blockHashHistory + ); } function getStateVariables() diff --git a/packages/protocol/contracts/L1/libs/LibUtils.sol b/packages/protocol/contracts/L1/libs/LibUtils.sol index 22059debc17..7fd91857d4d 100644 --- a/packages/protocol/contracts/L1/libs/LibUtils.sol +++ b/packages/protocol/contracts/L1/libs/LibUtils.sol @@ -37,10 +37,15 @@ library LibUtils { function getL2BlockHash( TaikoData.State storage state, - uint256 number + uint256 number, + uint256 blockHashHistory ) internal view returns (bytes32) { - require(number <= state.latestVerifiedHeight, "L1:id"); - return state.l2Hashes[number]; + require( + number + blockHashHistory > state.latestVerifiedHeight && + number <= state.latestVerifiedHeight, + "L1:number" + ); + return state.l2Hashes[number % blockHashHistory]; } function getStateVariables( diff --git a/packages/protocol/contracts/L1/libs/LibVerifying.sol b/packages/protocol/contracts/L1/libs/LibVerifying.sol index 40bcd1dd68d..ab75b355d70 100644 --- a/packages/protocol/contracts/L1/libs/LibVerifying.sol +++ b/packages/protocol/contracts/L1/libs/LibVerifying.sol @@ -57,12 +57,14 @@ library LibVerifying { } uint64 latestL2Height = state.latestVerifiedHeight; - bytes32 latestL2Hash = state.l2Hashes[latestL2Height]; + bytes32 latestL2Hash = state.l2Hashes[ + latestL2Height % config.blockHashHistory + ]; uint64 processed = 0; for ( uint256 i = state.latestVerifiedId + 1; - i < state.nextBlockId && processed <= maxBlocks; + i < state.nextBlockId && processed < maxBlocks; i++ ) { TaikoData.ForkChoice storage fc = state.forkChoices[i][ diff --git a/packages/protocol/test/L1/TaikoL1.test.ts b/packages/protocol/test/L1/TaikoL1.test.ts index 96d70461eb4..3c7c5fb34c0 100644 --- a/packages/protocol/test/L1/TaikoL1.test.ts +++ b/packages/protocol/test/L1/TaikoL1.test.ts @@ -26,7 +26,7 @@ describe("TaikoL1", function () { describe("getSyncedHeader()", async function () { it("should revert because header number has not been synced", async function () { await expect(taikoL1.getSyncedHeader(1)).to.be.revertedWith( - "L1:id" + "L1:number" ); }); diff --git a/packages/protocol/test/utils/propose.ts b/packages/protocol/test/utils/propose.ts index 7d961c0ae50..2f718fc7610 100644 --- a/packages/protocol/test/utils/propose.ts +++ b/packages/protocol/test/utils/propose.ts @@ -40,7 +40,7 @@ const proposeBlock = async ( const inputs = buildProposeBlockInputs(block, meta); const tx = await taikoL1.proposeBlock(inputs); - console.log("Proposed block", tx.hash); + // console.log("Proposed block", tx.hash); const receipt = await tx.wait(1); return receipt; };