Skip to content

Commit

Permalink
fix(protocol): fix bug in getLatestSyncedHeader on TaikoL1 (#13042)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik authored Jan 25, 2023
1 parent 9367ddb commit a56c4ea
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
8 changes: 4 additions & 4 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ 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 % getConfig().blockHashHistory
state.latestVerifiedHeight,
getConfig().blockHashHistory
);
}

Expand Down
11 changes: 8 additions & 3 deletions packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ library LibVerifying {
}

uint64 latestL2Height = state.latestVerifiedHeight;
bytes32 latestL2Hash = state.l2Hashes[latestL2Height];
bytes32 latestL2Hash = state.l2Hashes[
latestL2Height % config.blockHashHistory
];
uint64 processed = 0;

for (
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/L1/TaikoL1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/utils/propose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit a56c4ea

Please sign in to comment.