Skip to content

Commit

Permalink
Merge pull request #16 from unruggable-labs/develop
Browse files Browse the repository at this point in the history
Linea Sepolia V3 Fix
  • Loading branch information
clowestab authored Dec 6, 2024
2 parents 2258a34 + 268e3fb commit d098635
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
49 changes: 38 additions & 11 deletions src/linea/LineaRollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
ProviderPair,
ProofSequence,
} from '../types.js';
import { Contract } from 'ethers/contract';
import { Contract, EventLog } from 'ethers/contract';
import { LineaProver } from './LineaProver.js';
import { ROLLUP_ABI } from './types.js';
import { CHAINS } from '../chains.js';
Expand All @@ -25,11 +25,13 @@ import { ABI_CODER } from '../utils.js';
export type LineaConfig = {
L1MessageService: HexAddress;
SparseMerkleProof: HexAddress;
firstCommitV3?: bigint;
};

export type LineaCommit = RollupCommit<LineaProver> & {
readonly stateRoot: HexString32;
readonly prevStateRoot: HexString32;
readonly startIndex: bigint | undefined;
};

export class LineaRollup extends AbstractRollup<LineaCommit> {
Expand All @@ -47,8 +49,12 @@ export class LineaRollup extends AbstractRollup<LineaCommit> {
L1MessageService: '0xB218f8A4Bc926cF1cA7b3423c154a0D627Bdb7E5',
// https://github.com/Consensys/linea-ens/blob/main/packages/linea-ens-resolver/deployments/sepolia/SparseMerkleProof.json
SparseMerkleProof: '0x718D20736A637CDB15b6B586D8f1BF081080837f',
// deploy: https://sepolia.etherscan.io/tx/0x5a59e374a18369aa624fbc5afa77864aec1a1e19f38769c18d55318937f79356
// commit: https://sepolia.etherscan.io/tx/0xa2a4a0cf7205e7dc6eac8cdef5a7fd1cb750dac25539e6886e76f76278c27893
firstCommitV3: 6391917n,
};

readonly firstCommitV3: bigint | undefined;
readonly L1MessageService: Contract;
constructor(providers: ProviderPair, config: LineaConfig) {
super(providers);
Expand All @@ -57,6 +63,7 @@ export class LineaRollup extends AbstractRollup<LineaCommit> {
ROLLUP_ABI,
this.provider1
);
this.firstCommitV3 = config.firstCommitV3;
}

override async fetchLatestCommitIndex(): Promise<bigint> {
Expand All @@ -69,7 +76,7 @@ export class LineaRollup extends AbstractRollup<LineaCommit> {
const index: bigint = await this.L1MessageService.currentL2BlockNumber({
blockTag: this.latestBlockTag,
});
let commit = await this._fetchCommit(index);
let commit = await this.fetchCommit(index);
for (;;) {
if (await commit.prover.isShomeiReady()) return commit.index;
commit = await this.fetchParentCommit(commit);
Expand All @@ -78,26 +85,46 @@ export class LineaRollup extends AbstractRollup<LineaCommit> {
protected override async _fetchParentCommitIndex(
commit: LineaCommit
): Promise<bigint> {
if (commit.startIndex) return commit.startIndex - 1n;
const [event] = await this.L1MessageService.queryFilter(
this.L1MessageService.filters.DataFinalized(
null,
null,
commit.prevStateRoot
)
);
if (!event) throw new Error('no prior DataFinalized event');
return BigInt(event.topics[1]); // l2BlockNumber
if (!(event instanceof EventLog)) {
throw new Error('no prior DataFinalized event');
}
return BigInt(event.args.lastBlockFinalized);
}
protected override async _fetchCommit(index: bigint): Promise<LineaCommit> {
const [event] = await this.L1MessageService.queryFilter(
this.L1MessageService.filters.DataFinalized(index)
);
if (!event) throw new Error('no DataFinalized event');
const prevStateRoot: HexString32 = event.topics[2]; // start
const stateRoot: HexString32 = event.topics[3]; // end
let prevStateRoot: HexString32;
let stateRoot: HexString32;
let startIndex: bigint | undefined;
if (this.firstCommitV3 && index >= this.firstCommitV3) {
const [event] = await this.L1MessageService.queryFilter(
this.L1MessageService.filters.DataFinalizedV3(null, index)
);
if (!(event instanceof EventLog)) {
throw new Error('no DataFinalizedV3 event');
}
startIndex = event.args.startBlockNumber;
prevStateRoot = event.args.parentStateRootHash;
stateRoot = event.args.finalStateRootHash;
} else {
const [event] = await this.L1MessageService.queryFilter(
this.L1MessageService.filters.DataFinalized(index)
);
if (!(event instanceof EventLog)) {
throw new Error('no DataFinalized event');
}
prevStateRoot = event.args.startingRootHash;
stateRoot = event.args.finalRootHash;
}
const prover = new LineaProver(this.provider2, index);
prover.stateRoot = stateRoot;
return { index, stateRoot, prevStateRoot, prover };
return { index, startIndex, stateRoot, prevStateRoot, prover };
}
override encodeWitness(
commit: LineaCommit,
Expand Down
7 changes: 7 additions & 0 deletions src/linea/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export const ROLLUP_ABI = new Interface([
bytes32 indexed finalRootHash,
bool withProof
)`,
`event DataFinalizedV3(
uint256 indexed startBlockNumber,
uint256 indexed endBlockNumber,
bytes32 indexed shnarf,
bytes32 parentStateRootHash,
bytes32 finalStateRootHash
)`,
`event DataSubmittedV2(
bytes32 indexed shnarf,
uint256 indexed startBlock,
Expand Down

0 comments on commit d098635

Please sign in to comment.