Skip to content

Commit

Permalink
Set L1 starting block strictly from L2 starting timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ironbeer committed Apr 14, 2024
1 parent 8df1708 commit c49b1c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
32 changes: 32 additions & 0 deletions src/features/common/block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { providers } from "ethers";

// Search for the block that exactly matches the specified timestamp.
export const getBlockByTime = async (
provider: providers.Provider,
targetBlockTime: number,
blockPeriod: number
): Promise<null | providers.Block> => {
const latest = await provider.getBlock("latest");
if (targetBlockTime > latest.timestamp) {
return null;
}

let number = latest.number - ~~((latest.timestamp - targetBlockTime) / blockPeriod);
if (number < 0) {
return null;
}

while (true) {
const block = await provider.getBlock(number);
const diff = block.timestamp - targetBlockTime;
if (diff === 0) {
return block;
}
if (Math.abs(diff) < blockPeriod) {
break;
}
number -= ~(diff / blockPeriod);
}

return null;
};
1 change: 1 addition & 0 deletions src/features/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './download';
export * from './contracts';
export * from './transaction';
export * from './amount';
export * from './block';
17 changes: 13 additions & 4 deletions src/features/optimism/verse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import {
getNamedAddressesV2,
getSigner,
getL1BuildDepositContract,
getL1BuildAgentContract,
getSystemConfigContract,
getOasysL2OutputOracleContract,
getOasysPortalContract,
getBlockByTime,
} from "@/features";
import { Genesis, GenesisParams } from "@/types/optimism/genesis";
import {
Expand Down Expand Up @@ -110,6 +110,15 @@ export const getVerseInfoV2 = async (
// Go ERR: cannot unmarshal deploy config: json: cannot unmarshal hex number with leading zero digits into Go struct field DeployConfig.l2GenesisBlockGasLimit of type hexutil.Uint64
l2GasLimit = l2GasLimit.replace(/^(0x)0+/, '$1');

// Search for the L1 starting block from the L2OO starting timestamp.
const l2StartTime: BigNumber = await l2OOContract.startingTimestamp();
if (l2StartTime.isZero()) {
throw new Error("`startingTimestamp` is not set for the L2OutputOracle");
}
const l1StartBlock = await getBlockByTime(signer.provider, Number(l2StartTime), L1BlockTime);
if (!l1StartBlock) {
throw new Error(`Could not find L1 block matching L2 starting timestamp: ${l2StartTime}`);
}

return {
chainId: verseChainId,
Expand Down Expand Up @@ -140,7 +149,7 @@ export const getVerseInfoV2 = async (
l1FeeVaultRecipient: namedAddresses.FinalSystemOwner,
l1FeeVaultWithdrawalNetwork: L1FeeVaultWithdrawalNetwork,
l1StandardBridgeProxy: namedAddresses.L1StandardBridgeProxy,
l1StartingBlockTag: latestL1Block.hash,
l1StartingBlockTag: l1StartBlock.hash,
l2BlockTime: Number(await l2OOContract.L2_BLOCK_TIME()),
l2ChainID: verseChainId,
l2GenesisBlockBaseFeePerGas: L2GenesisBlockBaseFeePerGas,
Expand All @@ -149,9 +158,9 @@ export const getVerseInfoV2 = async (
l2OutputOracleChallenger: namedAddresses.L2OutputOracleChallenger,
l2OutputOracleProposer: namedAddresses.L2OutputOracleProposer,
l2OutputOracleStartingBlockNumber: Number(await l2OOContract.startingBlockNumber()),
l2OutputOracleStartingTimestamp: Number(await l2OOContract.startingTimestamp()),
l2OutputOracleStartingTimestamp: Number(l2StartTime),
l2OutputOracleSubmissionInterval: Number(await l2OOContract.SUBMISSION_INTERVAL()),
l2ZeroFeeTime: latestL1Block.timestamp,
l2ZeroFeeTime: Number(l2StartTime),
maxSequencerDrift: MaxSequencerDrift,
optimismPortalProxy: namedAddresses.OptimismPortalProxy,
p2pSequencerAddress: namedAddresses.P2PSequencer,
Expand Down

0 comments on commit c49b1c4

Please sign in to comment.