|
| 1 | +import chai, { expect } from 'chai' |
| 2 | +import { Wallet, Contract, ContractFactory, providers } from 'ethers' |
| 3 | +import { ethers } from 'hardhat' |
| 4 | +import { injectL2Context } from '@eth-optimism/core-utils' |
| 5 | + |
| 6 | +import { |
| 7 | + sleep, |
| 8 | + l2Provider, |
| 9 | + replicaProvider, |
| 10 | + waitForL2Geth, |
| 11 | +} from '../test/shared/utils' |
| 12 | +import { OptimismEnv } from '../test/shared/env' |
| 13 | +import { DockerComposeNetwork } from '../test/shared/docker-compose' |
| 14 | + |
| 15 | +describe('Syncing a replica', () => { |
| 16 | + let env: OptimismEnv |
| 17 | + let wallet: Wallet |
| 18 | + let replica: DockerComposeNetwork |
| 19 | + let provider: providers.JsonRpcProvider |
| 20 | + |
| 21 | + const sequencerProvider = injectL2Context(l2Provider) |
| 22 | + |
| 23 | + /* Helper functions */ |
| 24 | + |
| 25 | + const startReplica = async () => { |
| 26 | + // Bring up new replica |
| 27 | + replica = new DockerComposeNetwork(['replica']) |
| 28 | + await replica.up({ |
| 29 | + commandOptions: ['--scale', 'replica=1'], |
| 30 | + }) |
| 31 | + |
| 32 | + provider = await waitForL2Geth(replicaProvider) |
| 33 | + } |
| 34 | + |
| 35 | + const syncReplica = async (sequencerBlockNumber: number) => { |
| 36 | + // Wait until replica has caught up to the sequencer |
| 37 | + let latestReplicaBlock = (await provider.getBlock('latest')) as any |
| 38 | + while (latestReplicaBlock.number < sequencerBlockNumber) { |
| 39 | + await sleep(500) |
| 40 | + latestReplicaBlock = (await provider.getBlock('latest')) as any |
| 41 | + } |
| 42 | + |
| 43 | + return provider.getBlock(sequencerBlockNumber) |
| 44 | + } |
| 45 | + |
| 46 | + before(async () => { |
| 47 | + env = await OptimismEnv.new() |
| 48 | + wallet = env.l2Wallet |
| 49 | + }) |
| 50 | + |
| 51 | + after(async () => { |
| 52 | + await replica.stop('replica') |
| 53 | + await replica.rm() |
| 54 | + }) |
| 55 | + |
| 56 | + describe('Basic transactions and ERC20s', () => { |
| 57 | + const initialAmount = 1000 |
| 58 | + const tokenName = 'OVM Test' |
| 59 | + const tokenDecimals = 8 |
| 60 | + const TokenSymbol = 'OVM' |
| 61 | + |
| 62 | + let other: Wallet |
| 63 | + let Factory__ERC20: ContractFactory |
| 64 | + let ERC20: Contract |
| 65 | + |
| 66 | + before(async () => { |
| 67 | + other = Wallet.createRandom().connect(ethers.provider) |
| 68 | + Factory__ERC20 = await ethers.getContractFactory('ERC20', wallet) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should sync dummy transaction', async () => { |
| 72 | + const tx = { |
| 73 | + to: '0x' + '1234'.repeat(10), |
| 74 | + gasLimit: 4000000, |
| 75 | + gasPrice: 0, |
| 76 | + data: '0x', |
| 77 | + value: 0, |
| 78 | + } |
| 79 | + const result = await wallet.sendTransaction(tx) |
| 80 | + await result.wait() |
| 81 | + |
| 82 | + const latestSequencerBlock = (await sequencerProvider.getBlock( |
| 83 | + 'latest' |
| 84 | + )) as any |
| 85 | + |
| 86 | + await startReplica() |
| 87 | + |
| 88 | + const matchingReplicaBlock = (await syncReplica( |
| 89 | + latestSequencerBlock.number |
| 90 | + )) as any |
| 91 | + |
| 92 | + expect(matchingReplicaBlock.stateRoot).to.eq( |
| 93 | + latestSequencerBlock.stateRoot |
| 94 | + ) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should sync ERC20 deployment and transfer', async () => { |
| 98 | + ERC20 = await Factory__ERC20.deploy( |
| 99 | + initialAmount, |
| 100 | + tokenName, |
| 101 | + tokenDecimals, |
| 102 | + TokenSymbol |
| 103 | + ) |
| 104 | + |
| 105 | + const transfer = await ERC20.transfer(other.address, 100) |
| 106 | + await transfer.wait() |
| 107 | + |
| 108 | + const latestSequencerBlock = (await provider.getBlock('latest')) as any |
| 109 | + |
| 110 | + const matchingReplicaBlock = (await syncReplica( |
| 111 | + latestSequencerBlock.number |
| 112 | + )) as any |
| 113 | + |
| 114 | + expect(matchingReplicaBlock.stateRoot).to.eq( |
| 115 | + latestSequencerBlock.stateRoot |
| 116 | + ) |
| 117 | + }) |
| 118 | + }) |
| 119 | +}) |
0 commit comments