Skip to content

Commit

Permalink
feat: batch long-range log queries (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx authored Oct 15, 2024
1 parent 0c87bf6 commit 7cc5266
Show file tree
Hide file tree
Showing 10 changed files with 677 additions and 60 deletions.
49 changes: 5 additions & 44 deletions src/createRollupFetchTransactionHash.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { Address, PublicClient, Transport, Chain } from 'viem';
import { Address, PublicClient, Transport, Chain, GetLogsReturnType } from 'viem';
import { AbiEvent } from 'abitype';

import { ParentChainId, validateParentChain } from './types/ParentChain';
import {
mainnet,
arbitrumOne,
arbitrumNova,
base,
sepolia,
holesky,
arbitrumSepolia,
baseSepolia,
nitroTestnodeL1,
nitroTestnodeL2,
} from './chains';
import { getLogsWithBatching } from './utils/getLogsWithBatching';
import { getEarliestRollupCreatorDeploymentBlockNumber } from './utils/getEarliestRollupCreatorDeploymentBlockNumber';

export type CreateRollupFetchTransactionHashParams<TChain extends Chain | undefined> = {
rollup: Address;
Expand All @@ -40,43 +29,15 @@ const RollupInitializedEventAbi: AbiEvent = {
type: 'event',
};

const earliestRollupCreatorDeploymentBlockNumber: {
[Key in ParentChainId]: bigint;
} = {
// mainnet L1
[mainnet.id]: 18736164n,
// mainnet L2
[arbitrumOne.id]: 150599584n,
[arbitrumNova.id]: 47798739n,
[base.id]: 12978604n,
// testnet L1
[sepolia.id]: 4741823n,
[holesky.id]: 1118493n,
// testnet L2
[arbitrumSepolia.id]: 654628n,
[baseSepolia.id]: 10606961n,
// local nitro-testnode
[nitroTestnodeL1.id]: 0n,
[nitroTestnodeL2.id]: 0n,
};

export async function createRollupFetchTransactionHash<TChain extends Chain | undefined>({
rollup,
publicClient,
}: CreateRollupFetchTransactionHashParams<TChain>) {
const { chainId: parentChainId, isCustom: parentChainIsCustom } =
validateParentChain(publicClient);

const fromBlock = parentChainIsCustom
? 'earliest'
: earliestRollupCreatorDeploymentBlockNumber[parentChainId];

// Find the RollupInitialized event from that Rollup contract
const rollupInitializedEvents = await publicClient.getLogs({
const rollupInitializedEvents = await getLogsWithBatching(publicClient, {
address: rollup,
event: RollupInitializedEventAbi,
fromBlock,
toBlock: 'latest',
fromBlock: getEarliestRollupCreatorDeploymentBlockNumber(publicClient),
});

if (rollupInitializedEvents.length !== 1) {
Expand Down
10 changes: 5 additions & 5 deletions src/getBatchPosters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { upgradeExecutorABI } from './contracts/UpgradeExecutor';
import { gnosisSafeL2ABI } from './contracts/GnosisSafeL2';

import { createRollupFetchTransactionHash } from './createRollupFetchTransactionHash';
import { getLogsWithBatching } from './utils/getLogsWithBatching';

const createRollupABI = getAbiItem({ abi: rollupCreatorABI, name: 'createRollup' });
const createRollupFunctionSelector = getFunctionSelector(createRollupABI);
Expand Down Expand Up @@ -105,11 +106,11 @@ export type GetBatchPostersReturnType = {
* // batch posters list is not guaranteed to be accurate
* }
*/
export async function getBatchPosters<TChain extends Chain | undefined>(
export async function getBatchPosters<TChain extends Chain>(
publicClient: PublicClient<Transport, TChain>,
{ rollup, sequencerInbox }: GetBatchPostersParams,
): Promise<GetBatchPostersReturnType> {
let blockNumber: bigint | 'earliest';
let blockNumber: bigint;
let createRollupTransactionHash: Address | null = null;
try {
createRollupTransactionHash = await createRollupFetchTransactionHash({
Expand All @@ -121,15 +122,14 @@ export async function getBatchPosters<TChain extends Chain | undefined>(
});
blockNumber = receipt.blockNumber;
} catch (e) {
blockNumber = 'earliest';
blockNumber = 0n;
}

const sequencerInboxEvents = await publicClient.getLogs({
const sequencerInboxEvents = await getLogsWithBatching(publicClient, {
address: sequencerInbox,
event: ownerFunctionCalledEventAbi,
args: { id: 1n },
fromBlock: blockNumber,
toBlock: 'latest',
});

const events = createRollupTransactionHash
Expand Down
11 changes: 11 additions & 0 deletions src/getBatchPosters.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ describe('createRollupFunctionSelector', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -272,6 +273,7 @@ describe('setBatchPosterFunctionSelector', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -309,6 +311,7 @@ describe('setBatchPosterFunctionSelector', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -353,6 +356,7 @@ describe('upgradeExecutorExecuteCallFunctionSelector', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -396,6 +400,7 @@ describe('upgradeExecutorExecuteCallFunctionSelector', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -431,6 +436,7 @@ describe('safeL2FunctionSelector', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -465,6 +471,7 @@ describe('safeL2FunctionSelector', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -508,6 +515,7 @@ describe('Detect batch posters added or removed multiple times', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -543,6 +551,7 @@ describe('Detect batch posters added or removed multiple times', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -576,6 +585,7 @@ describe('Detect batch posters added or removed multiple times', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down Expand Up @@ -611,6 +621,7 @@ describe('Detect batch posters added or removed multiple times', () => {
});

const mockClient = createPublicClient({
chain: arbitrum,
transport: mockTransport,
});

Expand Down
10 changes: 5 additions & 5 deletions src/getKeysets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address, Chain, Hex, PublicClient, Transport, getAbiItem } from 'viem';

import { sequencerInboxABI } from './contracts/SequencerInbox';
import { createRollupFetchTransactionHash } from './createRollupFetchTransactionHash';
import { getLogsWithBatching } from './utils/getLogsWithBatching';

const SetValidKeysetEventAbi = getAbiItem({ abi: sequencerInboxABI, name: 'SetValidKeyset' });
const InvalidateKeysetEventAbi = getAbiItem({ abi: sequencerInboxABI, name: 'InvalidateKeyset' });
Expand Down Expand Up @@ -32,11 +33,11 @@ export type GetKeysetsReturnType = {
* });
*
*/
export async function getKeysets<TChain extends Chain | undefined>(
export async function getKeysets<TChain extends Chain>(
publicClient: PublicClient<Transport, TChain>,
{ sequencerInbox }: GetKeysetsParams,
): Promise<GetKeysetsReturnType> {
let blockNumber: bigint | 'earliest';
let blockNumber: bigint;
let createRollupTransactionHash: Address | null = null;
const rollup = await publicClient.readContract({
functionName: 'rollup',
Expand All @@ -54,14 +55,13 @@ export async function getKeysets<TChain extends Chain | undefined>(
blockNumber = receipt.blockNumber;
} catch (e) {
console.warn(`[getKeysets] ${(e as any).message}`);
blockNumber = 'earliest';
blockNumber = 0n;
}

const events = await publicClient.getLogs({
const events = await getLogsWithBatching(publicClient, {
address: sequencerInbox,
events: [SetValidKeysetEventAbi, InvalidateKeysetEventAbi],
fromBlock: blockNumber,
toBlock: 'latest',
});

const keysets = events.reduce((acc, event) => {
Expand Down
10 changes: 5 additions & 5 deletions src/getValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { gnosisSafeL2ABI } from './contracts/GnosisSafeL2';
import { rollupABI } from './contracts/Rollup';

import { createRollupFetchTransactionHash } from './createRollupFetchTransactionHash';
import { getLogsWithBatching } from './utils/getLogsWithBatching';

const createRollupABI = getAbiItem({ abi: rollupCreatorABI, name: 'createRollup' });
const createRollupFunctionSelector = getFunctionSelector(createRollupABI);
Expand Down Expand Up @@ -100,11 +101,11 @@ export type GetValidatorsReturnType = {
* // Validators list is not guaranteed to be accurate
* }
*/
export async function getValidators<TChain extends Chain | undefined>(
export async function getValidators<TChain extends Chain>(
publicClient: PublicClient<Transport, TChain>,
{ rollup }: GetValidatorsParams,
): Promise<GetValidatorsReturnType> {
let blockNumber: bigint | 'earliest';
let blockNumber: bigint;
try {
const createRollupTransactionHash = await createRollupFetchTransactionHash({
rollup,
Expand All @@ -115,15 +116,14 @@ export async function getValidators<TChain extends Chain | undefined>(
});
blockNumber = receipt.blockNumber;
} catch (e) {
blockNumber = 'earliest';
blockNumber = 0n;
}

const events = await publicClient.getLogs({
const events = await getLogsWithBatching(publicClient, {
address: rollup,
event: ownerFunctionCalledEventAbi,
args: { id: 6n },
fromBlock: blockNumber,
toBlock: 'latest',
});

const txs = await Promise.all(
Expand Down
2 changes: 1 addition & 1 deletion src/isAnyTrust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function parseConfig(config: { chainConfig: string }): boolean {
return JSON.parse(config.chainConfig).arbitrum.DataAvailabilityCommittee;
}

export async function isAnyTrust<TChain extends Chain | undefined>({
export async function isAnyTrust<TChain extends Chain>({
rollup,
publicClient,
}: {
Expand Down
44 changes: 44 additions & 0 deletions src/utils/getEarliestRollupCreatorDeploymentBlockNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Chain, PublicClient, Transport } from 'viem';
import {
mainnet,
arbitrumOne,
arbitrumNova,
base,
sepolia,
holesky,
arbitrumSepolia,
baseSepolia,
nitroTestnodeL1,
nitroTestnodeL2,
} from '../chains';
import { validateParentChain } from '../types/ParentChain';

const earliestRollupCreatorDeploymentBlockNumber: Record<number, bigint> = {
// mainnet L1
[mainnet.id]: 18_736_164n,
// mainnet L2
[arbitrumOne.id]: 150_599_584n,
[arbitrumNova.id]: 47_798_739n,
[base.id]: 12_978_604n,
// testnet L1
[sepolia.id]: 4_741_823n,
[holesky.id]: 1_118_493n,
// testnet L2
[arbitrumSepolia.id]: 654_628n,
[baseSepolia.id]: 10_606_961n,
// local nitro-testnode
[nitroTestnodeL1.id]: 0n,
[nitroTestnodeL2.id]: 0n,
};

export function getEarliestRollupCreatorDeploymentBlockNumber<TChain extends Chain | undefined>(
publicClient: PublicClient<Transport, TChain>,
) {
const { chainId, isCustom } = validateParentChain(publicClient);

if (isCustom) {
return 0n;
}

return earliestRollupCreatorDeploymentBlockNumber[chainId];
}
Loading

0 comments on commit 7cc5266

Please sign in to comment.