Skip to content

Commit

Permalink
test: add tests for edge cases in create rollup tx request
Browse files Browse the repository at this point in the history
  • Loading branch information
spsjvc committed Dec 20, 2023
1 parent 737175c commit 58390bb
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/createRollupPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, PublicClient, encodeFunctionData } from 'viem';
import { Address, PublicClient, encodeFunctionData, zeroAddress } from 'viem';

import { CreateRollupParams } from './createRollup';
import { defaults } from './createRollupDefaults';
Expand Down Expand Up @@ -35,14 +35,27 @@ export async function createRollupPrepareTransactionRequest({
throw new Error('chainId is undefined');
}

if (params.batchPoster === zeroAddress) {
throw new Error(`Param "batchPoster" can't be set to the zero address.`);
}

if (
params.validators.length === 0 ||
params.validators.includes(zeroAddress)
) {
throw new Error(
`Param "validators" can't be empty or contain the zero address.`
);
}

const chainConfig: ChainConfig = JSON.parse(params.config.chainConfig);

if (
isCustomFeeTokenAddress(params.nativeToken) &&
!isAnyTrustChainConfig(chainConfig)
) {
throw new Error(
`Custom fee token can only be used on AnyTrust chains. Set "arbitrum.DataAvailabilityCommittee" to "true" in the chain config.`
`Param "nativeToken" can only be used on AnyTrust chains. Set "arbitrum.DataAvailabilityCommittee" to "true" in the chain config.`
);
}

Expand Down
146 changes: 146 additions & 0 deletions src/createRollupPrepareTransactionRequest.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { it, expect } from 'vitest';
import { createPublicClient, http, parseGwei, zeroAddress } from 'viem';

import { nitroTestnodeL2 } from './chains';
import { generateChainId } from './utils';
import { prepareChainConfig } from './prepareChainConfig';
import { createRollupPrepareConfig } from './createRollupPrepareConfig';
import { createRollupPrepareTransactionRequest } from './createRollupPrepareTransactionRequest';

import { getTestPrivateKeyAccount } from './testHelpers';

const deployer = getTestPrivateKeyAccount();

const publicClient = createPublicClient({
chain: nitroTestnodeL2,
transport: http(),
});

it(`fails to prepare transaction request if "batchPoster" is set to the zero address`, async () => {
// generate a random chain id
const chainId = generateChainId();

// create the chain config
const chainConfig = prepareChainConfig({
chainId,
arbitrum: { InitialChainOwner: deployer.address },
});

// prepare the transaction for deploying the core contracts
await expect(
async () =>
await createRollupPrepareTransactionRequest({
params: {
config: createRollupPrepareConfig({
chainId: BigInt(chainId),
owner: deployer.address,
chainConfig,
}),
// set batch poster to the zero address
batchPoster: zeroAddress,
validators: [deployer.address],
},
account: deployer.address,
publicClient,
})
).rejects.toThrowError(
`Param "batchPoster" can't be set to the zero address.`
);
});

it(`fails to prepare transaction request if "validators" is set to an empty array`, async () => {
// generate a random chain id
const chainId = generateChainId();

// create the chain config
const chainConfig = prepareChainConfig({
chainId,
arbitrum: { InitialChainOwner: deployer.address },
});

// prepare the transaction for deploying the core contracts
await expect(
async () =>
await createRollupPrepareTransactionRequest({
params: {
config: createRollupPrepareConfig({
chainId: BigInt(chainId),
owner: deployer.address,
chainConfig,
}),
batchPoster: deployer.address,
// set validators to an empty array
validators: [],
},
account: deployer.address,
publicClient,
})
).rejects.toThrowError(
`Param "validators" can't be empty or contain the zero address.`
);
});

it(`fails to prepare transaction request if "validators" includes the zero address`, async () => {
// generate a random chain id
const chainId = generateChainId();

// create the chain config
const chainConfig = prepareChainConfig({
chainId,
arbitrum: { InitialChainOwner: deployer.address },
});

// prepare the transaction for deploying the core contracts
await expect(
async () =>
await createRollupPrepareTransactionRequest({
params: {
config: createRollupPrepareConfig({
chainId: BigInt(chainId),
owner: deployer.address,
chainConfig,
}),
batchPoster: deployer.address,
// set validators to zero address
validators: [zeroAddress],
},
account: deployer.address,
publicClient,
})
).rejects.toThrowError(
`Param "validators" can't be empty or contain the zero address.`
);
});

it(`fails to prepare transaction request if "nativeToken" is custom and chain is not anytrust`, async () => {
// generate a random chain id
const chainId = generateChainId();

// create the chain config
const chainConfig = prepareChainConfig({
chainId,
arbitrum: { InitialChainOwner: deployer.address },
});

// prepare the transaction for deploying the core contracts
await expect(
async () =>
await createRollupPrepareTransactionRequest({
params: {
config: createRollupPrepareConfig({
chainId: BigInt(chainId),
owner: deployer.address,
chainConfig,
}),
batchPoster: deployer.address,
validators: [deployer.address],
// set native token to anything custom
nativeToken: deployer.address,
},
account: deployer.address,
publicClient,
})
).rejects.toThrowError(
`Param "nativeToken" can only be used on AnyTrust chains. Set "arbitrum.DataAvailabilityCommittee" to "true" in the chain config.`
);
});

0 comments on commit 58390bb

Please sign in to comment.