Skip to content

Commit

Permalink
Merge pull request #199 from Consensys/feat/update-ccip-gateway
Browse files Browse the repository at this point in the history
feat: use latest finalized L1 block to fetch the currentL2BlockNumber avoiding coordinator delay issue
  • Loading branch information
Julink-eth authored Jul 16, 2024
2 parents 782ee3a + 75a2345 commit ec4f9e3
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 71 deletions.
42 changes: 5 additions & 37 deletions packages/linea-ccip-gateway/src/L2ProofService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const currentL2BlockNumberSig =
export class L2ProofService implements IProofService<L2ProvableBlock> {
private readonly rollup: Contract;
private readonly helper: EVMProofHelper;
private readonly l1Provider: JsonRpcProvider;

constructor(
providerL1: JsonRpcProvider,
Expand All @@ -37,49 +36,18 @@ export class L2ProofService implements IProofService<L2ProvableBlock> {
currentL2BlockNumberIface,
providerL1
);
this.l1Provider = providerL1;
}

/**
* @dev Returns an object representing a block whose state can be proven on L1.
*/
async getProvableBlock(): Promise<number> {
try {
const rollupAddress = await this.rollup.getAddress();
// Get the logs on the last 48 hours on L1
const nbBlock48h = 14400;
const l1BlockNumber = await this.l1Provider.getBlockNumber();
const fromBlock =
l1BlockNumber > nbBlock48h ? l1BlockNumber - nbBlock48h : 0;
const filterLog: ethers.Filter = {
fromBlock,
toBlock: "latest",
topics: [
"0x1335f1a2b3ff25f07f5fef07dd35d8fb4312c3c73b138e2fad9347b3319ab53c", // DataFinalized topic
],
address: rollupAddress,
};
const logs = await this.l1Provider.getLogs(filterLog);
const mostRecentlogs = logs.reverse();

if (mostRecentlogs.length === 0) {
throw new Error("No finalized block found");
}

// We take the latest finalized block if we only get one finalization otherwise we take the second most recent
// to avoid delays between the finalization tx and the coordinator notification update
let logIndex = 0;
if (mostRecentlogs.length > 1) {
logIndex = 1;
}

const provableBlock = AbiCoder.defaultAbiCoder().decode(
["uint256"],
mostRecentlogs[logIndex].topics[1]
);
provableBlock.toString();

return parseInt(provableBlock.toString());
const lastBlockFinalized = await this.rollup.currentL2BlockNumber({
blockTag: "finalized",
});
if (!lastBlockFinalized) throw new Error("No block found");
return lastBlockFinalized;
} catch (e) {
logError(e);
throw e;
Expand Down
34 changes: 17 additions & 17 deletions packages/linea-ens-resolver/deployments/sepolia/L1Resolver.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit ec4f9e3

Please sign in to comment.