From 09b6ef9fca47bc0c82e3c64124a32c4de70c4c98 Mon Sep 17 00:00:00 2001 From: Christophe Date: Wed, 25 Sep 2024 20:46:24 +0200 Subject: [PATCH] test: Add upgradeExecutor integration tests --- .../upgradeExecutor.integration.test.ts | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/actions/upgradeExecutor.integration.test.ts diff --git a/src/actions/upgradeExecutor.integration.test.ts b/src/actions/upgradeExecutor.integration.test.ts new file mode 100644 index 00000000..0d8e8241 --- /dev/null +++ b/src/actions/upgradeExecutor.integration.test.ts @@ -0,0 +1,93 @@ +import { describe, it, expect } from 'vitest'; +import { getInformationFromTestnode, getNitroTestnodePrivateKeyAccounts } from '../testHelpers'; +import { Address, createPublicClient, http } from 'viem'; +import { nitroTestnodeL2 } from '../chains'; +import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; +import { upgradeExecutorFetchPrivilegedAccounts } from '../upgradeExecutorFetchPrivilegedAccounts'; +import { buildAddExecutor } from './buildAddExecutor'; +import { UPGRADE_EXECUTOR_ROLE_EXECUTOR } from '../upgradeExecutorEncodeFunctionData'; +import { buildRemoveExecutor } from './buildRemoveExecutor'; +import { hasRole } from './hasRole'; + +const { l3UpgradeExecutor } = getInformationFromTestnode(); +const { l3RollupOwner } = getNitroTestnodePrivateKeyAccounts(); + +const client = createPublicClient({ + chain: nitroTestnodeL2, + transport: http(), +}); + +describe('Upgrade executor', () => { + it('buildAddExecutor and buildRemoveExecutor update upgradeExecutor', async () => { + async function changeUpgradeExecutor(address: Address, add: boolean) { + const fn = add ? buildAddExecutor : buildRemoveExecutor; + const transactionRequest = await fn(client, { + account: l3RollupOwner.address, + upgradeExecutor: l3UpgradeExecutor, + params: { + address, + }, + }); + const txHash = await client.sendRawTransaction({ + serializedTransaction: await l3RollupOwner.signTransaction(transactionRequest), + }); + await client.waitForTransactionReceipt({ hash: txHash }); + } + const addUpgradeExecutor = (address: Address) => changeUpgradeExecutor(address, true); + const removeUpgradeExecutor = (address: Address) => changeUpgradeExecutor(address, false); + + const randomAddress = privateKeyToAccount(generatePrivateKey()).address; + + const initialState = await upgradeExecutorFetchPrivilegedAccounts({ + upgradeExecutorAddress: l3UpgradeExecutor, + publicClient: client, + }); + + expect( + await hasRole(client, { + params: { + role: UPGRADE_EXECUTOR_ROLE_EXECUTOR, + address: randomAddress, + }, + upgradeExecutor: l3UpgradeExecutor, + }), + ).toBeFalsy(); + + await addUpgradeExecutor(randomAddress); + expect( + await upgradeExecutorFetchPrivilegedAccounts({ + upgradeExecutorAddress: l3UpgradeExecutor, + publicClient: client, + }), + ).toEqual({ + ...initialState, + [randomAddress]: [UPGRADE_EXECUTOR_ROLE_EXECUTOR], + }); + expect( + await hasRole(client, { + params: { + role: UPGRADE_EXECUTOR_ROLE_EXECUTOR, + address: randomAddress, + }, + upgradeExecutor: l3UpgradeExecutor, + }), + ).toBeTruthy(); + + await removeUpgradeExecutor(randomAddress); + expect( + await upgradeExecutorFetchPrivilegedAccounts({ + upgradeExecutorAddress: l3UpgradeExecutor, + publicClient: client, + }), + ).toEqual(initialState); + expect( + await hasRole(client, { + params: { + role: UPGRADE_EXECUTOR_ROLE_EXECUTOR, + address: randomAddress, + }, + upgradeExecutor: l3UpgradeExecutor, + }), + ).toBeFalsy(); + }); +});