Skip to content

Commit

Permalink
Supersim & Viem Tests (#471)
Browse files Browse the repository at this point in the history
* Adds simple way to start supersim in unit tests

* Adds @types/node as a dev dep to make autocomplete work in unit tests

* Removes the static l2 genesis file we've been using for our unit tests now that we have supersim

* Adds basic shell for supersim action

* Removes setupInterop since its no longer needed with supersim
  • Loading branch information
nitaliano authored Sep 13, 2024
1 parent 39d6515 commit d9edce7
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 171 deletions.
9 changes: 9 additions & 0 deletions .github/actions/supersim/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Supersim
runs:
using: composite
steps:
- name: Install Supersim
uses: jaxxstorm/[email protected]
with:
repo: ethereum-optimism/supersim

2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ jobs:
uses: foundry-rs/foundry-toolchain@v1
- name: Setup
uses: ./.github/actions/setup
- name: Supersim Setup
uses: ./.github/actions/supersim
- name: Run Tests
run: |
pnpm nx affected --base=$NX_BASE --head=$NX_HEAD --target=test
1 change: 0 additions & 1 deletion packages/viem/assets/l2-genesis.json

This file was deleted.

1 change: 1 addition & 0 deletions packages/viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"lint:fix": "eslint \"**/*.{ts,tsx}\" --fix --quiet && pnpm prettier \"**/*.{ts,tsx}\" --write --loglevel=warn"
},
"devDependencies": {
"@types/node": "^22.5.4",
"@viem/anvil": "^0.0.7",
"resolve-tspaths": "^0.8.18",
"typescript": "^5.2.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/viem/src/actions/buildExecuteL2ToL2Message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { encodeFunctionData } from 'viem'
import { base, optimism } from 'viem/chains'
import { describe, expect, it } from 'vitest'

import { supersimL2A } from '@/chains/supersim.js'
import { publicClient, testAccount } from '@/test/clients.js'
import { ticTacToeABI, ticTacToeAddress } from '@/test/setupTicTacToe.js'
import type { MessageIdentifier } from '@/types/interop.js'
Expand All @@ -12,7 +12,7 @@ describe('buildExecuteL2ToL2Message', () => {
blockNumber: BigInt(100),
logIndex: BigInt(0),
timestamp: BigInt(0),
chainId: BigInt(base.id),
chainId: BigInt(supersimL2A.id),
} as MessageIdentifier

const expectedTarget = ticTacToeAddress
Expand All @@ -31,7 +31,7 @@ describe('buildExecuteL2ToL2Message', () => {

expect(res).toEqual({
account: undefined,
targetChain: optimism,
targetChain: supersimL2A,
id: expectedId,
target: expectedTarget,
message: expectedMessage,
Expand Down
6 changes: 3 additions & 3 deletions packages/viem/src/actions/buildSendL2ToL2Message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { encodeFunctionData } from 'viem'
import { base, optimism } from 'viem/chains'
import { describe, expect, it } from 'vitest'

import { supersimL2A } from '@/chains/supersim.js'
import { publicClient, testAccount } from '@/test/clients.js'
import { ticTacToeABI, ticTacToeAddress } from '@/test/setupTicTacToe.js'

describe('buildSendL2ToL2Message', () => {
const expectedChainId = base.id
const expectedChainId = supersimL2A.id
const expectedTarget = ticTacToeAddress
const expectedMessage = encodeFunctionData({
abi: ticTacToeABI,
Expand All @@ -23,7 +23,7 @@ describe('buildSendL2ToL2Message', () => {

expect(res).toEqual({
account: undefined,
targetChain: optimism,
targetChain: supersimL2A,
destinationChainId: expectedChainId,
target: expectedTarget,
message: expectedMessage,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { encodeFunctionData } from 'viem'
import { base } from 'viem/chains'
import { describe, expect, it } from 'vitest'

import { supersimL2A } from '@/chains/supersim.js'
import { publicClient, testAccount, walletClient } from '@/test/clients.js'
import { ticTacToeABI, ticTacToeAddress } from '@/test/setupTicTacToe.js'
import type { MessageIdentifier } from '@/types/interop.js'
Expand All @@ -13,7 +13,7 @@ describe('estimateExecuteL2ToL2Message', () => {
blockNumber: BigInt(100),
logIndex: BigInt(0),
timestamp: BigInt(0),
chainId: BigInt(base.id),
chainId: BigInt(supersimL2A.id),
} as MessageIdentifier

const encodedMessage = encodeFunctionData({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { encodeFunctionData } from 'viem'
import { base } from 'viem/chains'
import { describe, expect, it } from 'vitest'

import { supersimL2B } from '@/chains/supersim.js'
import { publicClient, testAccount } from '@/test/clients.js'
import { ticTacToeABI, ticTacToeAddress } from '@/test/setupTicTacToe.js'

Expand All @@ -16,7 +16,7 @@ describe('estimateSendL2ToL2Message', () => {
const gas = await publicClient.estimateSendL2ToL2MessageGas({
account: testAccount.address,
target: ticTacToeAddress,
destinationChainId: base.id,
destinationChainId: supersimL2B.id,
message: encodedMessage,
})

Expand Down
4 changes: 2 additions & 2 deletions packages/viem/src/actions/executeL2ToL2Message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { encodeFunctionData, keccak256, parseEventLogs } from 'viem'
import { base } from 'viem/chains'
import { describe, expect, it } from 'vitest'

import { crossL2InboxABI } from '@/abis.js'
import { supersimL2A } from '@/chains/supersim.js'
import { publicClient, testAccount, walletClient } from '@/test/clients.js'
import { ticTacToeABI, ticTacToeAddress } from '@/test/setupTicTacToe.js'
import type { MessageIdentifier } from '@/types/interop.js'
Expand All @@ -14,7 +14,7 @@ describe('executeL2ToL2Message', () => {
blockNumber: BigInt(100),
logIndex: BigInt(0),
timestamp: BigInt(0),
chainId: BigInt(base.id),
chainId: BigInt(supersimL2A.id),
} as MessageIdentifier

const encodedData = encodeFunctionData({
Expand Down
10 changes: 5 additions & 5 deletions packages/viem/src/actions/sendL2ToL2Message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { decodeFunctionData, encodeFunctionData } from 'viem'
import { base, optimism } from 'viem/chains'
import { describe, expect, it } from 'vitest'

import { l2ToL2CrossDomainMessengerABI } from '@/abis.js'
import { buildSendL2ToL2Message } from '@/actions/buildSendL2ToL2Message.js'
import { supersimL2A, supersimL2B } from '@/chains/supersim.js'
import { contracts } from '@/contracts.js'
import { publicClient, testAccount, walletClient } from '@/test/clients.js'
import { ticTacToeABI, ticTacToeAddress } from '@/test/setupTicTacToe.js'
Expand All @@ -19,7 +19,7 @@ describe('sendL2ToL2Message', () => {

const args = await buildSendL2ToL2Message(publicClient, {
account: testAccount.address,
destinationChainId: base.id,
destinationChainId: supersimL2B.id,
target: ticTacToeAddress,
message: encodedData,
})
Expand All @@ -40,7 +40,7 @@ describe('sendL2ToL2Message', () => {

// verifiy message id
expect(id).toBeDefined()
expect(id.chainId).toEqual(BigInt(optimism.id))
expect(id.chainId).toEqual(BigInt(supersimL2A.id))
expect(id.origin.toLowerCase()).toEqual(
contracts.l2ToL2CrossDomainMessenger.address.toLowerCase(),
)
Expand All @@ -55,8 +55,8 @@ describe('sendL2ToL2Message', () => {
})

expect(decodedPayload.args).toEqual([
BigInt(base.id),
BigInt(optimism.id),
BigInt(supersimL2B.id),
BigInt(supersimL2A.id),
currentNonce - 1n,
testAccount.address,
ticTacToeAddress,
Expand Down
10 changes: 5 additions & 5 deletions packages/viem/src/test/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ import {
http,
} from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { optimism } from 'viem/chains'

import { supersimL2A } from '@/chains/supersim.js'
import { publicActionsL2 } from '@/decorators/publicL2.js'
import { walletActionsL2 } from '@/decorators/walletL2.js'

const RPC_URL = `http://localhost:8545/${Number(process.env.VITEST_vitestPool_ID ?? 1)}`
const RPC_URL = supersimL2A.rpcUrls.default.http[0]

// anvil account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
export const testAccount = privateKeyToAccount(
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
)

export const publicClient = createPublicClient({
chain: optimism,
chain: supersimL2A,
transport: http(RPC_URL),
}).extend(publicActionsL2())

export const walletClient = createWalletClient({
account: testAccount,
chain: optimism,
chain: supersimL2A,
transport: http(RPC_URL),
}).extend(walletActionsL2())

export const testClient = createTestClient({
chain: optimism,
chain: supersimL2A,
mode: 'anvil',
transport: http(RPC_URL),
})
16 changes: 3 additions & 13 deletions packages/viem/src/test/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { startProxy } from '@viem/anvil'
import { optimism } from 'viem/chains'

import { configureDependencySet } from '@/test/setupInterop.js'
import { setupTicTacToe } from '@/test/setupTicTacToe.js'

async function main() {
const shutdown = await startProxy({
host: '::',
options: {
init: './assets/l2-genesis.json',
chainId: optimism.id,
},
})
import { startSupersim } from './startSupersim.js'

await configureDependencySet()
async function main() {
const shutdown = await startSupersim()
await setupTicTacToe()

return () => {
Expand Down
29 changes: 0 additions & 29 deletions packages/viem/src/test/setupInterop.ts

This file was deleted.

30 changes: 30 additions & 0 deletions packages/viem/src/test/startSupersim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { spawn } from 'child_process'

export type SupersimParameters = {
l1Port?: number
l2StartingPort?: number
}

export type SupersimReturnArg = Promise<() => void>

export async function startSupersim(
params?: SupersimParameters,
): SupersimReturnArg {
const args = [] as string[]

if (params?.l1Port) {
args.push(`--l1.port ${params.l1Port}`)
}

if (params?.l2StartingPort) {
args.push(` --l2.starting.port ${params.l2StartingPort}`)
}

const supersimProcess = spawn('supersim', args, { shell: true })

return () => {
if (!supersimProcess.killed) {
supersimProcess.kill()
}
}
}
Loading

0 comments on commit d9edce7

Please sign in to comment.