Commit b62f5a8e authored by smartcontracts's avatar smartcontracts Committed by GitHub

maint(ct): clean up CTC gas tests (#2462)

Another test cleanup PR, this time targeting the CTC gas tests. I'm
going to keep doing this as separate PRs because:
1. These are simple changes
2. They *look* complicated because there are a lot of them

Putting these into different PRs means they'll be easier to review and
I'll have to do less shepherding over relatively minor PRs.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent fa692011
/* External Imports */ /* External Imports */
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
import { Signer, ContractFactory, Contract } from 'ethers' import { Signer, Contract } from 'ethers'
import { smock, FakeContract } from '@defi-wonderland/smock' import { smock, FakeContract } from '@defi-wonderland/smock'
import { import {
AppendSequencerBatchParams, AppendSequencerBatchParams,
...@@ -9,11 +9,10 @@ import { ...@@ -9,11 +9,10 @@ import {
expectApprox, expectApprox,
} from '@eth-optimism/core-utils' } from '@eth-optimism/core-utils'
import { TransactionResponse } from '@ethersproject/abstract-provider' import { TransactionResponse } from '@ethersproject/abstract-provider'
import { keccak256 } from 'ethers/lib/utils'
/* Internal Imports */ /* Internal Imports */
import { import {
makeAddressManager, deploy,
setProxyTarget, setProxyTarget,
L2_GAS_DISCOUNT_DIVISOR, L2_GAS_DISCOUNT_DIVISOR,
ENQUEUE_GAS_COST, ENQUEUE_GAS_COST,
...@@ -31,11 +30,11 @@ const appendSequencerBatch = async ( ...@@ -31,11 +30,11 @@ const appendSequencerBatch = async (
CanonicalTransactionChain: Contract, CanonicalTransactionChain: Contract,
batch: AppendSequencerBatchParams batch: AppendSequencerBatchParams
): Promise<TransactionResponse> => { ): Promise<TransactionResponse> => {
const methodId = keccak256(Buffer.from('appendSequencerBatch()')).slice(2, 10)
const calldata = encodeAppendSequencerBatch(batch)
return CanonicalTransactionChain.signer.sendTransaction({ return CanonicalTransactionChain.signer.sendTransaction({
to: CanonicalTransactionChain.address, to: CanonicalTransactionChain.address,
data: '0x' + methodId + calldata, data:
ethers.utils.id('appendSequencerBatch()').slice(0, 10) +
encodeAppendSequencerBatch(batch),
}) })
} }
...@@ -47,8 +46,10 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -47,8 +46,10 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
let AddressManager: Contract let AddressManager: Contract
let Fake__StateCommitmentChain: FakeContract let Fake__StateCommitmentChain: FakeContract
before(async () => { let CanonicalTransactionChain: Contract
AddressManager = await makeAddressManager() beforeEach(async () => {
AddressManager = await deploy('Lib_AddressManager')
await AddressManager.setAddress( await AddressManager.setAddress(
'OVM_Sequencer', 'OVM_Sequencer',
await sequencer.getAddress() await sequencer.getAddress()
...@@ -63,37 +64,20 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -63,37 +64,20 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
'StateCommitmentChain', 'StateCommitmentChain',
Fake__StateCommitmentChain Fake__StateCommitmentChain
) )
})
let Factory__CanonicalTransactionChain: ContractFactory
let Factory__ChainStorageContainer: ContractFactory
before(async () => {
Factory__CanonicalTransactionChain = await ethers.getContractFactory(
'CanonicalTransactionChain'
)
Factory__ChainStorageContainer = await ethers.getContractFactory(
'ChainStorageContainer'
)
})
let CanonicalTransactionChain: Contract CanonicalTransactionChain = await deploy('CanonicalTransactionChain', {
beforeEach(async () => { signer: sequencer,
CanonicalTransactionChain = await Factory__CanonicalTransactionChain.deploy( args: [
AddressManager.address, AddressManager.address,
MAX_GAS_LIMIT, MAX_GAS_LIMIT,
L2_GAS_DISCOUNT_DIVISOR, L2_GAS_DISCOUNT_DIVISOR,
ENQUEUE_GAS_COST ENQUEUE_GAS_COST,
) ],
})
const batches = await Factory__ChainStorageContainer.deploy( const batches = await deploy('ChainStorageContainer', {
AddressManager.address, args: [AddressManager.address, 'CanonicalTransactionChain'],
'CanonicalTransactionChain' })
)
await Factory__ChainStorageContainer.deploy(
AddressManager.address,
'CanonicalTransactionChain'
)
await AddressManager.setAddress( await AddressManager.setAddress(
'ChainStorageContainer-CTC-batches', 'ChainStorageContainer-CTC-batches',
...@@ -107,12 +91,7 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -107,12 +91,7 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
}) })
describe('appendSequencerBatch [ @skip-on-coverage ]', () => { describe('appendSequencerBatch [ @skip-on-coverage ]', () => {
beforeEach(() => {
CanonicalTransactionChain = CanonicalTransactionChain.connect(sequencer)
})
it('200 transactions in a single context', async () => { it('200 transactions in a single context', async () => {
console.log(`Benchmark: 200 transactions in a single context.`)
const timestamp = (await getEthTime(ethers.provider)) - 100 const timestamp = (await getEthTime(ethers.provider)) - 100
const blockNumber = await getNextBlockNumber(ethers.provider) const blockNumber = await getNextBlockNumber(ethers.provider)
...@@ -143,13 +122,12 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -143,13 +122,12 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
const receipt = await res.wait() const receipt = await res.wait()
const gasUsed = receipt.gasUsed.toNumber() const gasUsed = receipt.gasUsed.toNumber()
console.log('Benchmark complete.')
console.log('Fixed calldata cost:', fixedCalldataCost) console.log('Fixed calldata cost:', fixedCalldataCost)
console.log( console.log(
'Non-calldata overhead gas cost per transaction:', 'Non-calldata overhead gas cost per transaction:',
(gasUsed - fixedCalldataCost) / numTxs (gasUsed - fixedCalldataCost) / numTxs
) )
expectApprox(gasUsed, 1_402_638, { expectApprox(gasUsed, 1_402_638, {
absoluteUpperDeviation: 1000, absoluteUpperDeviation: 1000,
// Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your // Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your
...@@ -159,7 +137,6 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -159,7 +137,6 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
}).timeout(10_000_000) }).timeout(10_000_000)
it('200 transactions in 200 contexts', async () => { it('200 transactions in 200 contexts', async () => {
console.log(`Benchmark: 200 transactions in 200 contexts.`)
const timestamp = (await getEthTime(ethers.provider)) - 100 const timestamp = (await getEthTime(ethers.provider)) - 100
const blockNumber = await getNextBlockNumber(ethers.provider) const blockNumber = await getNextBlockNumber(ethers.provider)
...@@ -190,13 +167,12 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -190,13 +167,12 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
const receipt = await res.wait() const receipt = await res.wait()
const gasUsed = receipt.gasUsed.toNumber() const gasUsed = receipt.gasUsed.toNumber()
console.log('Benchmark complete.')
console.log('Fixed calldata cost:', fixedCalldataCost) console.log('Fixed calldata cost:', fixedCalldataCost)
console.log( console.log(
'Non-calldata overhead gas cost per transaction:', 'Non-calldata overhead gas cost per transaction:',
(gasUsed - fixedCalldataCost) / numTxs (gasUsed - fixedCalldataCost) / numTxs
) )
expectApprox(gasUsed, 1_619_781, { expectApprox(gasUsed, 1_619_781, {
absoluteUpperDeviation: 1000, absoluteUpperDeviation: 1000,
// Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your // Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your
...@@ -248,12 +224,12 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -248,12 +224,12 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
const gasUsed = receipt.gasUsed.toNumber() const gasUsed = receipt.gasUsed.toNumber()
console.log('Benchmark complete.') console.log('Benchmark complete.')
console.log('Fixed calldata cost:', fixedCalldataCost) console.log('Fixed calldata cost:', fixedCalldataCost)
console.log( console.log(
'Non-calldata overhead gas cost per transaction:', 'Non-calldata overhead gas cost per transaction:',
(gasUsed - fixedCalldataCost) / numTxs (gasUsed - fixedCalldataCost) / numTxs
) )
expectApprox(gasUsed, 891_158, { expectApprox(gasUsed, 891_158, {
absoluteUpperDeviation: 1000, absoluteUpperDeviation: 1000,
// Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your // Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your
...@@ -264,13 +240,12 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -264,13 +240,12 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
}) })
describe('enqueue [ @skip-on-coverage ]', () => { describe('enqueue [ @skip-on-coverage ]', () => {
let enqueueL2GasPrepaid const data = '0x' + '12'.repeat(1234)
let data
let enqueueL2GasPrepaid: number
beforeEach(async () => { beforeEach(async () => {
CanonicalTransactionChain = CanonicalTransactionChain.connect(sequencer)
enqueueL2GasPrepaid = enqueueL2GasPrepaid =
await CanonicalTransactionChain.enqueueL2GasPrepaid() await CanonicalTransactionChain.enqueueL2GasPrepaid()
data = '0x' + '12'.repeat(1234)
}) })
it('cost to enqueue a transaction above the prepaid threshold', async () => { it('cost to enqueue a transaction above the prepaid threshold', async () => {
...@@ -281,11 +256,10 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -281,11 +256,10 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
l2GasLimit, l2GasLimit,
data data
) )
const receipt = await res.wait() const receipt = await res.wait()
const gasUsed = receipt.gasUsed.toNumber() const gasUsed = receipt.gasUsed.toNumber()
console.log('Benchmark complete.')
expectApprox(gasUsed, 196_687, { expectApprox(gasUsed, 196_687, {
absoluteUpperDeviation: 500, absoluteUpperDeviation: 500,
// Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your // Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your
...@@ -302,11 +276,10 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () = ...@@ -302,11 +276,10 @@ describe('[GAS BENCHMARK] CanonicalTransactionChain [ @skip-on-coverage ]', () =
l2GasLimit, l2GasLimit,
data data
) )
const receipt = await res.wait() const receipt = await res.wait()
const gasUsed = receipt.gasUsed.toNumber() const gasUsed = receipt.gasUsed.toNumber()
console.log('Benchmark complete.')
expectApprox(gasUsed, 134_100, { expectApprox(gasUsed, 134_100, {
absoluteUpperDeviation: 500, absoluteUpperDeviation: 500,
// Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your // Assert a lower bound of 1% reduction on gas cost. If your tests are breaking because your
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment