Skip to content

Commit ef7101e

Browse files
committed
test: update get validators test to anvil
1 parent 5ca4f41 commit ef7101e

File tree

3 files changed

+92
-30
lines changed

3 files changed

+92
-30
lines changed

src/getValidators.integration.test.ts

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,38 @@ import { rollupAdminLogicPublicActions } from './decorators/rollupAdminLogicPubl
77
import {
88
getInformationFromTestnode,
99
getNitroTestnodePrivateKeyAccounts,
10+
PrivateKeyAccountWithPrivateKey,
1011
testHelper_getRollupCreatorVersionFromEnv,
1112
} from './testHelpers';
1213
import { getValidators } from './getValidators';
14+
import { getAnvilTestStack, isAnvilTestMode } from './integrationTestHelpers/injectedMode';
1315

14-
const { l3RollupOwner } = getNitroTestnodePrivateKeyAccounts();
15-
const { l3Rollup, l3UpgradeExecutor } = getInformationFromTestnode();
16+
const env = isAnvilTestMode() ? getAnvilTestStack() : undefined;
1617

1718
const rollupCreatorVersion = testHelper_getRollupCreatorVersionFromEnv();
1819
// https://github.com/OffchainLabs/nitro-testnode/blob/release/test-node.bash#L634
1920
// https://github.com/OffchainLabs/nitro-contracts/blob/v3.2.0/scripts/rollupCreation.ts#L254-L261
2021
// https://github.com/OffchainLabs/nitro-contracts/blob/v2.1.3/scripts/rollupCreation.ts#L237-L243
2122
const expectedInitialValidators = rollupCreatorVersion === 'v3.2' ? 11 : 10;
2223

23-
const client = createPublicClient({
24-
chain: nitroTestnodeL2,
24+
let l3RollupOwner: PrivateKeyAccountWithPrivateKey;
25+
let l3Rollup: Address;
26+
let l3UpgradeExecutor: Address;
27+
28+
if (env) {
29+
l3RollupOwner = env.l3.accounts.rollupOwner;
30+
l3Rollup = env.l3.rollup;
31+
l3UpgradeExecutor = env.l3.upgradeExecutor;
32+
} else {
33+
l3RollupOwner = getNitroTestnodePrivateKeyAccounts().l3RollupOwner;
34+
35+
const testNodeInformation = getInformationFromTestnode();
36+
l3Rollup = testNodeInformation.l3Rollup;
37+
l3UpgradeExecutor = testNodeInformation.l3UpgradeExecutor;
38+
}
39+
40+
const l2Client = createPublicClient({
41+
chain: env ? env.l2.chain : nitroTestnodeL2,
2542
transport: http(),
2643
}).extend(
2744
rollupAdminLogicPublicActions({
@@ -30,19 +47,19 @@ const client = createPublicClient({
3047
);
3148

3249
async function setValidator(validator: Address, state: boolean) {
33-
const tx = await client.rollupAdminLogicPrepareTransactionRequest({
50+
const tx = await l2Client.rollupAdminLogicPrepareTransactionRequest({
3451
functionName: 'setValidator',
3552
args: [[validator], [state]],
3653
account: l3RollupOwner.address,
3754
upgradeExecutor: l3UpgradeExecutor,
3855
rollup: l3Rollup,
3956
});
4057

41-
const txHash = await client.sendRawTransaction({
58+
const txHash = await l2Client.sendRawTransaction({
4259
serializedTransaction: await l3RollupOwner.signTransaction(tx),
4360
});
4461

45-
await client.waitForTransactionReceipt({
62+
await l2Client.waitForTransactionReceipt({
4663
hash: txHash,
4764
});
4865
}
@@ -53,7 +70,7 @@ describe('successfully get validators', () => {
5370
const randomAccount = privateKeyToAccount(generatePrivateKey()).address;
5471

5572
const { isAccurate: isAccurateInitially, validators: initialValidators } = await getValidators(
56-
client,
73+
l2Client,
5774
{
5875
rollup: l3Rollup,
5976
},
@@ -65,22 +82,25 @@ describe('successfully get validators', () => {
6582
await setValidator(randomAccount, false);
6683
await setValidator(randomAccount, false);
6784

68-
const { isAccurate: isStillAccurate, validators: newValidators } = await getValidators(client, {
69-
rollup: l3Rollup,
70-
});
85+
const { isAccurate: isStillAccurate, validators: newValidators } = await getValidators(
86+
l2Client,
87+
{
88+
rollup: l3Rollup,
89+
},
90+
);
7191
// Setting the same validator multiple time to false doesn't add new validators
7292
expect(newValidators).toEqual(initialValidators);
7393
expect(isStillAccurate).toBeTruthy();
7494

7595
await setValidator(randomAccount, true);
76-
const { validators, isAccurate } = await getValidators(client, { rollup: l3Rollup });
96+
const { validators, isAccurate } = await getValidators(l2Client, { rollup: l3Rollup });
7797
expect(validators).toEqual(initialValidators.concat(randomAccount));
7898
expect(isAccurate).toBeTruthy();
7999

80100
// Reset state for future tests
81101
await setValidator(randomAccount, false);
82102
const { isAccurate: isAccurateFinal, validators: validatorsFinal } = await getValidators(
83-
client,
103+
l2Client,
84104
{
85105
rollup: l3Rollup,
86106
},
@@ -93,7 +113,7 @@ describe('successfully get validators', () => {
93113
const randomAccount = privateKeyToAccount(generatePrivateKey()).address;
94114

95115
const { isAccurate: isAccurateInitially, validators: initialValidators } = await getValidators(
96-
client,
116+
l2Client,
97117
{
98118
rollup: l3Rollup,
99119
},
@@ -104,23 +124,26 @@ describe('successfully get validators', () => {
104124

105125
await setValidator(randomAccount, true);
106126
await setValidator(randomAccount, true);
107-
const { isAccurate: isStillAccurate, validators: newValidators } = await getValidators(client, {
108-
rollup: l3Rollup,
109-
});
127+
const { isAccurate: isStillAccurate, validators: newValidators } = await getValidators(
128+
l2Client,
129+
{
130+
rollup: l3Rollup,
131+
},
132+
);
110133

111134
expect(newValidators).toEqual(initialValidators.concat(randomAccount));
112135
expect(isStillAccurate).toBeTruthy();
113136

114137
// Reset state for futures tests
115138
await setValidator(randomAccount, false);
116-
const { validators, isAccurate } = await getValidators(client, { rollup: l3Rollup });
139+
const { validators, isAccurate } = await getValidators(l2Client, { rollup: l3Rollup });
117140
expect(validators).toEqual(initialValidators);
118141
expect(isAccurate).toBeTruthy();
119142
});
120143

121144
it('when adding an existing validator', async () => {
122145
const { isAccurate: isAccurateInitially, validators: initialValidators } = await getValidators(
123-
client,
146+
l2Client,
124147
{ rollup: l3Rollup },
125148
);
126149

@@ -130,14 +153,14 @@ describe('successfully get validators', () => {
130153
const firstValidator = initialValidators[0];
131154
await setValidator(firstValidator, true);
132155

133-
const { isAccurate, validators } = await getValidators(client, { rollup: l3Rollup });
156+
const { isAccurate, validators } = await getValidators(l2Client, { rollup: l3Rollup });
134157
expect(validators).toEqual(initialValidators);
135158
expect(isAccurate).toBeTruthy();
136159
});
137160

138161
it('when removing an existing validator', async () => {
139162
const { isAccurate: isAccurateInitially, validators: initialValidators } = await getValidators(
140-
client,
163+
l2Client,
141164
{ rollup: l3Rollup },
142165
);
143166

@@ -146,13 +169,13 @@ describe('successfully get validators', () => {
146169

147170
const lastValidator = initialValidators[initialValidators.length - 1];
148171
await setValidator(lastValidator, false);
149-
const { isAccurate, validators } = await getValidators(client, { rollup: l3Rollup });
172+
const { isAccurate, validators } = await getValidators(l2Client, { rollup: l3Rollup });
150173
expect(validators).toEqual(initialValidators.slice(0, -1));
151174
expect(isAccurate).toBeTruthy();
152175

153176
await setValidator(lastValidator, true);
154177
const { isAccurate: isAccurateFinal, validators: validatorsFinal } = await getValidators(
155-
client,
178+
l2Client,
156179
{ rollup: l3Rollup },
157180
);
158181
expect(validatorsFinal).toEqual(initialValidators);

src/integrationTestHelpers/anvilHarness.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import { ethers } from 'ethers';
2222
import TestWETH9 from '@arbitrum/token-bridge-contracts/build/contracts/contracts/tokenbridge/test/TestWETH9.sol/TestWETH9.json';
2323

2424
import { registerCustomParentChain } from '../chains';
25+
import {
26+
rollupCreatorABI,
27+
rollupCreatorAddress as rollupCreatorV3Dot2Address,
28+
} from '../contracts/RollupCreator/v3.2';
2529
import { createRollup } from '../createRollup';
2630
import { createRollupPrepareDeploymentParamsConfig } from '../createRollupPrepareDeploymentParamsConfig';
2731
import { prepareChainConfig } from '../prepareChainConfig';
@@ -104,12 +108,41 @@ let cleanupHookRegistered = false;
104108
let teardownStarted = false;
105109
let l1RpcCachingProxy: RpcCachingProxy | undefined;
106110

111+
const NITRO_TESTNODE_AUTHORIZED_VALIDATORS = 10;
112+
const NITRO_TESTNODE_VALIDATOR_SIGNER = '0x6A568afe0f82d34759347bb36F14A6bB171d2CBe' as Address;
113+
107114
function prepareNitroRuntimeDir(runtimeDir: string) {
108115
chmodSync(runtimeDir, 0o777);
109116
mkdirSync(join(runtimeDir, 'nitro-data'), { recursive: true, mode: 0o777 });
110117
chmodSync(join(runtimeDir, 'nitro-data'), 0o777);
111118
}
112119

120+
async function getNitroTestnodeStyleValidators(
121+
publicClient: PublicClient<Transport, Chain>,
122+
rollupCreator: Address,
123+
): Promise<Address[]> {
124+
const validatorWalletCreator = await publicClient.readContract({
125+
address: rollupCreator,
126+
abi: rollupCreatorABI,
127+
functionName: 'validatorWalletCreator',
128+
});
129+
130+
const validators: Address[] = [];
131+
132+
for (let i = 1; i <= NITRO_TESTNODE_AUTHORIZED_VALIDATORS; i++) {
133+
validators.push(
134+
ethers.utils.getContractAddress({
135+
from: validatorWalletCreator,
136+
nonce: ethers.BigNumber.from(i).toHexString(),
137+
}) as Address,
138+
);
139+
}
140+
141+
validators.push(NITRO_TESTNODE_VALIDATOR_SIGNER);
142+
143+
return validators;
144+
}
145+
113146
export function dehydrateAnvilTestStack(env: AnvilTestStack): InjectedAnvilTestStack {
114147
return {
115148
...env,
@@ -224,6 +257,11 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
224257
});
225258

226259
const l1RpcUrl = `http://127.0.0.1:${l1RpcPort}`;
260+
const l1Client = createPublicClient({
261+
chain: sepolia,
262+
transport: http(l1RpcUrl),
263+
});
264+
227265
await waitForRpc({
228266
rpcUrl: l1RpcUrl,
229267
timeoutMs: 60_000,
@@ -242,7 +280,7 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
242280
// Deploying L2 rollup contracts on L1
243281
console.log('Deploying L2 rollup contracts on L1...');
244282
const l2RollupConfig = createRollupPrepareDeploymentParamsConfig(
245-
createPublicClient({ chain: sepolia, transport: http(l1RpcUrl) }),
283+
l1Client,
246284
{
247285
chainId: BigInt(l2ChainId),
248286
owner: harnessDeployer.address,
@@ -266,15 +304,15 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
266304
params: {
267305
config: l2RollupConfig,
268306
batchPosters: [harnessDeployer.address],
269-
validators: [harnessDeployer.address],
307+
validators: await getNitroTestnodeStyleValidators(
308+
l1Client,
309+
rollupCreatorV3Dot2Address[sepolia.id],
310+
),
270311
nativeToken: zeroAddress,
271312
maxFeePerGasForRetryables: parseGwei('0.1'),
272313
} as CreateRollupParams<'v3.2'>,
273314
account: harnessDeployer,
274-
parentChainPublicClient: createPublicClient({
275-
chain: sepolia,
276-
transport: http(l1RpcUrl),
277-
}),
315+
parentChainPublicClient: l1Client,
278316
rollupCreatorVersion: 'v3.2',
279317
});
280318
console.log('L2 rollup contracts deployed on L1\n');
@@ -471,7 +509,7 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
471509
params: {
472510
config: l3RollupConfig,
473511
batchPosters: [harnessDeployer.address],
474-
validators: [harnessDeployer.address],
512+
validators: await getNitroTestnodeStyleValidators(l2Client, l2RollupCreator),
475513
nativeToken: customGasToken.address as Address,
476514
maxDataSize: 104_857n,
477515
} as CreateRollupParams<'v3.2'>,

vitest.integration.anvil.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default mergeConfig(
1818
'./src/createRollup.integration.test.ts',
1919
'./src/decorators/arbAggregatorActions.integration.test.ts',
2020
'./src/getBatchPosters.integration.test.ts',
21+
'./src/getValidators.integration.test.ts',
2122
],
2223
fileParallelism: false,
2324
},

0 commit comments

Comments
 (0)