Commit 2493e925 authored by Antonios Kogias's avatar Antonios Kogias Committed by GitHub

maint(ct): clean up SCC tests (#2499)

Cleans up the StateCommitmentChain tests using the same techniques as
used in previous test cleanups.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 81f09f16
/* External Imports */
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
import { Signer, ContractFactory, Contract, constants } from 'ethers' import { Contract, constants } from 'ethers'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { smock, FakeContract } from '@defi-wonderland/smock' import { smock, FakeContract } from '@defi-wonderland/smock'
/* Internal Imports */
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { import {
makeAddressManager, deploy,
setProxyTarget, setProxyTarget,
NON_NULL_BYTES32, NON_NULL_BYTES32,
getEthTime, getEthTime,
...@@ -14,20 +13,19 @@ import { ...@@ -14,20 +13,19 @@ import {
} from '../../../helpers' } from '../../../helpers'
describe('StateCommitmentChain', () => { describe('StateCommitmentChain', () => {
let sequencer: Signer let sequencer: SignerWithAddress
let user: Signer let user: SignerWithAddress
const batch = [NON_NULL_BYTES32]
before(async () => { before(async () => {
;[sequencer, user] = await ethers.getSigners() ;[sequencer, user] = await ethers.getSigners()
}) })
let AddressManager: Contract let AddressManager: Contract
before(async () => {
AddressManager = await makeAddressManager()
})
let Fake__CanonicalTransactionChain: FakeContract let Fake__CanonicalTransactionChain: FakeContract
let Fake__BondManager: FakeContract let Fake__BondManager: FakeContract
before(async () => { before(async () => {
AddressManager = await deploy('Lib_AddressManager')
Fake__CanonicalTransactionChain = await smock.fake<Contract>( Fake__CanonicalTransactionChain = await smock.fake<Contract>(
'CanonicalTransactionChain' 'CanonicalTransactionChain'
) )
...@@ -44,36 +42,23 @@ describe('StateCommitmentChain', () => { ...@@ -44,36 +42,23 @@ describe('StateCommitmentChain', () => {
Fake__BondManager.isCollateralized.returns(true) Fake__BondManager.isCollateralized.returns(true)
await AddressManager.setAddress( await AddressManager.setAddress('OVM_Proposer', sequencer.address)
'OVM_Proposer',
await sequencer.getAddress()
)
})
let Factory__StateCommitmentChain: ContractFactory
let Factory__ChainStorageContainer: ContractFactory
before(async () => {
Factory__StateCommitmentChain = await ethers.getContractFactory(
'StateCommitmentChain'
)
Factory__ChainStorageContainer = await ethers.getContractFactory(
'ChainStorageContainer'
)
}) })
let StateCommitmentChain: Contract let StateCommitmentChain: Contract
beforeEach(async () => { beforeEach(async () => {
StateCommitmentChain = await Factory__StateCommitmentChain.deploy( StateCommitmentChain = await deploy('StateCommitmentChain', {
signer: sequencer,
args: [
AddressManager.address, AddressManager.address,
60 * 60 * 24 * 7, // 1 week fraud proof window 60 * 60 * 24 * 7, // 1 week fraud proof window
60 * 30 // 30 minute sequencer publish window 60 * 30, // 30 minute sequencer publish window
) ],
})
const batches = await Factory__ChainStorageContainer.deploy( const batches = await deploy('ChainStorageContainer', {
AddressManager.address, args: [AddressManager.address, 'StateCommitmentChain'],
'StateCommitmentChain' })
)
await AddressManager.setAddress( await AddressManager.setAddress(
'ChainStorageContainer-SCC-batches', 'ChainStorageContainer-SCC-batches',
...@@ -88,18 +73,14 @@ describe('StateCommitmentChain', () => { ...@@ -88,18 +73,14 @@ describe('StateCommitmentChain', () => {
describe('appendStateBatch', () => { describe('appendStateBatch', () => {
describe('when the provided batch is empty', () => { describe('when the provided batch is empty', () => {
const batch = []
it('should revert', async () => { it('should revert', async () => {
await expect( await expect(
StateCommitmentChain.appendStateBatch(batch, 0) StateCommitmentChain.appendStateBatch([], 0)
).to.be.revertedWith('Cannot submit an empty state batch.') ).to.be.revertedWith('Cannot submit an empty state batch.')
}) })
}) })
describe('when the provided batch is not empty', () => { describe('when the provided batch is not empty', () => {
const batch = [NON_NULL_BYTES32]
describe('when start index does not match total elements', () => { describe('when start index does not match total elements', () => {
it('should revert', async () => { it('should revert', async () => {
await expect( await expect(
...@@ -143,10 +124,7 @@ describe('StateCommitmentChain', () => { ...@@ -143,10 +124,7 @@ describe('StateCommitmentChain', () => {
batch.length * 2 batch.length * 2
) )
await StateCommitmentChain.connect(sequencer).appendStateBatch( await StateCommitmentChain.appendStateBatch(batch, 0)
batch,
0
)
}) })
describe('when inside sequencer publish window', () => { describe('when inside sequencer publish window', () => {
...@@ -193,7 +171,6 @@ describe('StateCommitmentChain', () => { ...@@ -193,7 +171,6 @@ describe('StateCommitmentChain', () => {
}) })
describe('deleteStateBatch', () => { describe('deleteStateBatch', () => {
const batch = [NON_NULL_BYTES32]
const batchHeader = { const batchHeader = {
batchIndex: 0, batchIndex: 0,
batchRoot: NON_NULL_BYTES32, batchRoot: NON_NULL_BYTES32,
...@@ -211,7 +188,7 @@ describe('StateCommitmentChain', () => { ...@@ -211,7 +188,7 @@ describe('StateCommitmentChain', () => {
await StateCommitmentChain.appendStateBatch(batch, 0) await StateCommitmentChain.appendStateBatch(batch, 0)
batchHeader.extraData = ethers.utils.defaultAbiCoder.encode( batchHeader.extraData = ethers.utils.defaultAbiCoder.encode(
['uint256', 'address'], ['uint256', 'address'],
[await getEthTime(ethers.provider), await sequencer.getAddress()] [await getEthTime(ethers.provider), sequencer.address]
) )
}) })
...@@ -234,10 +211,7 @@ describe('StateCommitmentChain', () => { ...@@ -234,10 +211,7 @@ describe('StateCommitmentChain', () => {
describe('when the sender is the OVM_FraudVerifier', () => { describe('when the sender is the OVM_FraudVerifier', () => {
before(async () => { before(async () => {
await AddressManager.setAddress( await AddressManager.setAddress('OVM_FraudVerifier', sequencer.address)
'OVM_FraudVerifier',
await sequencer.getAddress()
)
}) })
describe('when the provided batch index is greater than the total submitted', () => { describe('when the provided batch index is greater than the total submitted', () => {
...@@ -300,13 +274,14 @@ describe('StateCommitmentChain', () => { ...@@ -300,13 +274,14 @@ describe('StateCommitmentChain', () => {
prevTotalElements: 0, prevTotalElements: 0,
extraData: ethers.constants.HashZero, extraData: ethers.constants.HashZero,
} }
it('should revert when timestamp is zero', async () => { it('should revert when timestamp is zero', async () => {
await expect( await expect(
StateCommitmentChain.insideFraudProofWindow({ StateCommitmentChain.insideFraudProofWindow({
...batchHeader, ...batchHeader,
extraData: ethers.utils.defaultAbiCoder.encode( extraData: ethers.utils.defaultAbiCoder.encode(
['uint256', 'address'], ['uint256', 'address'],
[0, await sequencer.getAddress()] [0, sequencer.address]
), ),
}) })
).to.be.revertedWith('Batch header timestamp cannot be zero') ).to.be.revertedWith('Batch header timestamp cannot be zero')
...@@ -322,7 +297,6 @@ describe('StateCommitmentChain', () => { ...@@ -322,7 +297,6 @@ describe('StateCommitmentChain', () => {
describe('when one batch element has been inserted', () => { describe('when one batch element has been inserted', () => {
beforeEach(async () => { beforeEach(async () => {
const batch = [NON_NULL_BYTES32]
Fake__CanonicalTransactionChain.getTotalElements.returns(batch.length) Fake__CanonicalTransactionChain.getTotalElements.returns(batch.length)
await StateCommitmentChain.appendStateBatch(batch, 0) await StateCommitmentChain.appendStateBatch(batch, 0)
}) })
...@@ -334,9 +308,11 @@ describe('StateCommitmentChain', () => { ...@@ -334,9 +308,11 @@ describe('StateCommitmentChain', () => {
describe('when 64 batch elements have been inserted in one batch', () => { describe('when 64 batch elements have been inserted in one batch', () => {
beforeEach(async () => { beforeEach(async () => {
const batch = Array(64).fill(NON_NULL_BYTES32) const batchArray = Array(64).fill(NON_NULL_BYTES32)
Fake__CanonicalTransactionChain.getTotalElements.returns(batch.length) Fake__CanonicalTransactionChain.getTotalElements.returns(
await StateCommitmentChain.appendStateBatch(batch, 0) batchArray.length
)
await StateCommitmentChain.appendStateBatch(batchArray, 0)
}) })
it('should return the number of inserted batch elements', async () => { it('should return the number of inserted batch elements', async () => {
...@@ -346,12 +322,12 @@ describe('StateCommitmentChain', () => { ...@@ -346,12 +322,12 @@ describe('StateCommitmentChain', () => {
describe('when 32 batch elements have been inserted in each of two batches', () => { describe('when 32 batch elements have been inserted in each of two batches', () => {
beforeEach(async () => { beforeEach(async () => {
const batch = Array(32).fill(NON_NULL_BYTES32) const batchArray = Array(32).fill(NON_NULL_BYTES32)
Fake__CanonicalTransactionChain.getTotalElements.returns( Fake__CanonicalTransactionChain.getTotalElements.returns(
batch.length * 2 batchArray.length * 2
) )
await StateCommitmentChain.appendStateBatch(batch, 0) await StateCommitmentChain.appendStateBatch(batchArray, 0)
await StateCommitmentChain.appendStateBatch(batch, 32) await StateCommitmentChain.appendStateBatch(batchArray, 32)
}) })
it('should return the number of inserted batch elements', async () => { it('should return the number of inserted batch elements', async () => {
...@@ -369,7 +345,6 @@ describe('StateCommitmentChain', () => { ...@@ -369,7 +345,6 @@ describe('StateCommitmentChain', () => {
describe('when one batch has been inserted', () => { describe('when one batch has been inserted', () => {
beforeEach(async () => { beforeEach(async () => {
const batch = [NON_NULL_BYTES32]
Fake__CanonicalTransactionChain.getTotalElements.returns(batch.length) Fake__CanonicalTransactionChain.getTotalElements.returns(batch.length)
await StateCommitmentChain.appendStateBatch(batch, 0) await StateCommitmentChain.appendStateBatch(batch, 0)
}) })
...@@ -381,7 +356,6 @@ describe('StateCommitmentChain', () => { ...@@ -381,7 +356,6 @@ describe('StateCommitmentChain', () => {
describe('when 8 batches have been inserted', () => { describe('when 8 batches have been inserted', () => {
beforeEach(async () => { beforeEach(async () => {
const batch = [NON_NULL_BYTES32]
Fake__CanonicalTransactionChain.getTotalElements.returns( Fake__CanonicalTransactionChain.getTotalElements.returns(
batch.length * 8 batch.length * 8
) )
...@@ -409,7 +383,6 @@ describe('StateCommitmentChain', () => { ...@@ -409,7 +383,6 @@ describe('StateCommitmentChain', () => {
describe('when one batch element has been inserted', () => { describe('when one batch element has been inserted', () => {
let timestamp let timestamp
beforeEach(async () => { beforeEach(async () => {
const batch = [NON_NULL_BYTES32]
Fake__CanonicalTransactionChain.getTotalElements.returns(batch.length) Fake__CanonicalTransactionChain.getTotalElements.returns(batch.length)
await StateCommitmentChain.appendStateBatch(batch, 0) await StateCommitmentChain.appendStateBatch(batch, 0)
timestamp = await getEthTime(ethers.provider) timestamp = await getEthTime(ethers.provider)
...@@ -424,7 +397,6 @@ describe('StateCommitmentChain', () => { ...@@ -424,7 +397,6 @@ describe('StateCommitmentChain', () => {
}) })
describe('verifyStateCommitment()', () => { describe('verifyStateCommitment()', () => {
const batch = [NON_NULL_BYTES32]
const batchHeader = { const batchHeader = {
batchIndex: 0, batchIndex: 0,
batchRoot: NON_NULL_BYTES32, batchRoot: NON_NULL_BYTES32,
...@@ -449,7 +421,7 @@ describe('StateCommitmentChain', () => { ...@@ -449,7 +421,7 @@ describe('StateCommitmentChain', () => {
await StateCommitmentChain.appendStateBatch(batch, 0) await StateCommitmentChain.appendStateBatch(batch, 0)
batchHeader.extraData = ethers.utils.defaultAbiCoder.encode( batchHeader.extraData = ethers.utils.defaultAbiCoder.encode(
['uint256', 'address'], ['uint256', 'address'],
[await getEthTime(ethers.provider), await sequencer.getAddress()] [await getEthTime(ethers.provider), sequencer.address]
) )
}) })
......
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