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

fix(protocol): fix bug in getLatestSyncedHeader on TaikoL1 #13042

Merged
merged 7 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not check 0 return value (same behavior on TaikoL2) and rely on caller to verify that.

) 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