Skip to content
Open
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
34 changes: 33 additions & 1 deletion src/upgradeExecutor.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { it, expect } from 'vitest';
import { it, expect, vi } from 'vitest';
import { upgradeExecutorFetchPrivilegedAccounts } from './upgradeExecutorFetchPrivilegedAccounts';
import {
UPGRADE_EXECUTOR_ROLE_ADMIN,
UPGRADE_EXECUTOR_ROLE_EXECUTOR,
upgradeExecutorEncodeFunctionData,
} from './upgradeExecutorEncodeFunctionData';
import { createPublicClient, http } from 'viem';
import { getEarliestRollupCreatorDeploymentBlockNumber } from './utils/getEarliestRollupCreatorDeploymentBlockNumber';
import { arbitrum } from 'viem/chains';

const publicClient = createPublicClient({
Expand Down Expand Up @@ -42,3 +43,34 @@ it('it fetches the right privileged accounts from an UpgradeExecutor', async ()
expect(privilegedAccounts[upgradeExecutorAddress]).toEqual([UPGRADE_EXECUTOR_ROLE_ADMIN]);
expect(privilegedAccounts[chainOwner]).toEqual([UPGRADE_EXECUTOR_ROLE_EXECUTOR]);
});

it('batches the getLogs queries when the RPC rejects the full range (#642)', async () => {
const batchingClient = createPublicClient({ chain: arbitrum, transport: http() });

// keep the batched range tiny and network-free
const lowerLimit = getEarliestRollupCreatorDeploymentBlockNumber(batchingClient);
batchingClient.getBlockNumber = vi.fn().mockResolvedValue(lowerLimit + 3n);

const upgradeExecutorAddress = '0x0611b78A42903a537BE7a2f9a8783BE39AC63cD9';
const account = '0x46A78349aBA0369D18292a285DE6d5FC5CC2de5c';
const roleGrantedLog = { args: { role: UPGRADE_EXECUTOR_ROLE_EXECUTOR, account } };

const getLogsMock = vi.fn();
batchingClient.getLogs = getLogsMock;
getLogsMock
// RoleGranted: full-range rejected, then the batched call returns the event
.mockRejectedValueOnce(new Error('query range is too big'))
.mockResolvedValueOnce([roleGrantedLog])
// RoleRevoked: full-range rejected, then the batched call returns nothing
.mockRejectedValueOnce(new Error('query range is too big'))
.mockResolvedValue([]);

const privilegedAccounts = await upgradeExecutorFetchPrivilegedAccounts({
upgradeExecutorAddress,
publicClient: batchingClient,
});

// the full-range attempt for each event is retried in batches rather than thrown
expect(getLogsMock.mock.calls.length).toBeGreaterThanOrEqual(4);
expect(privilegedAccounts[account]).toEqual([UPGRADE_EXECUTOR_ROLE_EXECUTOR]);
});
7 changes: 3 additions & 4 deletions src/upgradeExecutorFetchPrivilegedAccounts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Address, PublicClient, Transport, Chain } from 'viem';
import { AbiEvent } from 'abitype';
import { UpgradeExecutorRole } from './upgradeExecutorEncodeFunctionData';
import { getLogsWithBatching } from './utils/getLogsWithBatching';

/**
* This type is for the params of the {@link upgradeExecutorFetchPrivilegedAccounts} function
Expand Down Expand Up @@ -97,11 +98,10 @@ export async function upgradeExecutorFetchPrivilegedAccounts<TChain extends Chai
const upgradeExecutorPrivilegedAccounts: UpgradeExecutorPrivilegedAccounts = {};

// 1. Find the RoleGranted events
const roleGrantedEvents = await publicClient.getLogs({
const roleGrantedEvents = await getLogsWithBatching(publicClient, {
address: upgradeExecutorAddress,
event: RoleGrantedEventAbi,
fromBlock: 0n,
toBlock: 'latest',
});
if (!roleGrantedEvents || roleGrantedEvents.length <= 0) {
// No roles have been granted
Expand All @@ -120,11 +120,10 @@ export async function upgradeExecutorFetchPrivilegedAccounts<TChain extends Chai
});

// 3. Find the RoleRevoked events
const roleRevokedEvents = await publicClient.getLogs({
const roleRevokedEvents = await getLogsWithBatching(publicClient, {
address: upgradeExecutorAddress,
event: RoleRevokedEventAbi,
fromBlock: 0n,
toBlock: 'latest',
});
if (!roleRevokedEvents || roleRevokedEvents.length <= 0) {
return upgradeExecutorPrivilegedAccounts;
Expand Down
Loading