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

maint(ct): clean up library tests (#2513)

Cleans up tests for all Library contracts using same techniques as
previous PRs.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 7b282676
/* External Imports */
import { ethers } from 'hardhat'
import { Contract } from 'ethers' import { Contract } from 'ethers'
/* Internal Imports */
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { NON_ZERO_ADDRESS } from '../../../helpers' import { NON_ZERO_ADDRESS, deploy } from '../../../helpers'
describe('Lib_OVMCodec', () => { describe('Lib_OVMCodec', () => {
let Lib_OVMCodec: Contract let Lib_OVMCodec: Contract
before(async () => { before(async () => {
Lib_OVMCodec = await ( Lib_OVMCodec = await deploy('TestLib_OVMCodec')
await ethers.getContractFactory('TestLib_OVMCodec')
).deploy()
}) })
describe('hashTransaction', () => { describe('hashTransaction', () => {
......
/* Internal Imports */
import { Lib_RLPReader_TEST_JSON } from '../../../data' import { Lib_RLPReader_TEST_JSON } from '../../../data'
import { runJsonTest } from '../../../helpers' import { runJsonTest } from '../../../helpers'
......
/* External Imports */
import { ethers } from 'hardhat'
import { Contract } from 'ethers' import { Contract } from 'ethers'
/* Internal Imports */
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { Lib_RLPWriter_TEST_JSON } from '../../../data' import { Lib_RLPWriter_TEST_JSON } from '../../../data'
import { deploy } from '../../../helpers'
const encode = async (Lib_RLPWriter: Contract, input: any): Promise<void> => { const encode = async (Lib_RLPWriter: Contract, input: any): Promise<void> => {
if (Array.isArray(input)) { if (Array.isArray(input)) {
const elements = await Promise.all( return Lib_RLPWriter.writeList(
await Promise.all(
input.map(async (el) => { input.map(async (el) => {
return encode(Lib_RLPWriter, el) return encode(Lib_RLPWriter, el)
}) })
) )
)
return Lib_RLPWriter.writeList(elements)
} else if (Number.isInteger(input)) { } else if (Number.isInteger(input)) {
return Lib_RLPWriter.writeUint(input) return Lib_RLPWriter.writeUint(input)
} else { } else {
...@@ -25,9 +23,7 @@ const encode = async (Lib_RLPWriter: Contract, input: any): Promise<void> => { ...@@ -25,9 +23,7 @@ const encode = async (Lib_RLPWriter: Contract, input: any): Promise<void> => {
describe('Lib_RLPWriter', () => { describe('Lib_RLPWriter', () => {
let Lib_RLPWriter: Contract let Lib_RLPWriter: Contract
before(async () => { before(async () => {
Lib_RLPWriter = await ( Lib_RLPWriter = await deploy('TestLib_RLPWriter')
await ethers.getContractFactory('TestLib_RLPWriter')
).deploy()
}) })
describe('Official Ethereum RLP Tests', () => { describe('Official Ethereum RLP Tests', () => {
...@@ -50,14 +46,11 @@ describe('Lib_RLPWriter', () => { ...@@ -50,14 +46,11 @@ describe('Lib_RLPWriter', () => {
describe('Use of library with other memory-modifying operations', () => { describe('Use of library with other memory-modifying operations', () => {
it('should allow creation of a contract beforehand and still work', async () => { it('should allow creation of a contract beforehand and still work', async () => {
const randomAddress = '0x1234123412341234123412341234123412341234' expect(
const rlpEncodedRandomAddress =
'0x941234123412341234123412341234123412341234'
const encoded =
await Lib_RLPWriter.callStatic.writeAddressWithTaintedMemory( await Lib_RLPWriter.callStatic.writeAddressWithTaintedMemory(
randomAddress '0x1234123412341234123412341234123412341234'
) )
expect(encoded).to.eq(rlpEncodedRandomAddress) ).to.eq('0x941234123412341234123412341234123412341234')
}) })
}) })
}) })
/* External Imports */
import { ethers } from 'hardhat'
import { Contract } from 'ethers' import { Contract } from 'ethers'
import { applyL1ToL2Alias, undoL1ToL2Alias } from '@eth-optimism/core-utils' import { applyL1ToL2Alias, undoL1ToL2Alias } from '@eth-optimism/core-utils'
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { deploy } from '../../../helpers'
describe('AddressAliasHelper', () => { describe('AddressAliasHelper', () => {
let AddressAliasHelper: Contract let AddressAliasHelper: Contract
before(async () => { before(async () => {
AddressAliasHelper = await ( AddressAliasHelper = await deploy('TestLib_AddressAliasHelper')
await ethers.getContractFactory('TestLib_AddressAliasHelper')
).deploy()
}) })
describe('applyL1ToL2Alias', () => { describe('applyL1ToL2Alias', () => {
......
/* External Imports */
import * as rlp from 'rlp' import * as rlp from 'rlp'
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
import { Contract } from 'ethers' import { Contract } from 'ethers'
import { toHexString } from '@eth-optimism/core-utils' import { toHexString } from '@eth-optimism/core-utils'
/* Internal Imports */
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { TrieTestGenerator } from '../../../helpers' import { deploy, TrieTestGenerator } from '../../../helpers'
const NODE_COUNTS = [1, 2, 32, 128] const NODE_COUNTS = [1, 2, 32, 128]
describe('Lib_MerkleTrie', () => { describe('Lib_MerkleTrie', () => {
let Lib_MerkleTrie: Contract let Lib_MerkleTrie: Contract
before(async () => { before(async () => {
Lib_MerkleTrie = await ( Lib_MerkleTrie = await deploy('TestLib_MerkleTrie')
await ethers.getContractFactory('TestLib_MerkleTrie')
).deploy()
}) })
// Eth-foundation tests: https://github.com/ethereum/tests/tree/develop/TrieTests // Eth-foundation tests: https://github.com/ethereum/tests/tree/develop/TrieTests
......
/* External Imports */
import { ethers } from 'hardhat'
import { Contract } from 'ethers' import { Contract } from 'ethers'
/* Internal Imports */
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { TrieTestGenerator } from '../../../helpers' import { deploy, TrieTestGenerator } from '../../../helpers'
const NODE_COUNTS = [1, 2, 128] const NODE_COUNTS = [1, 2, 128]
describe('Lib_SecureMerkleTrie', () => { describe('Lib_SecureMerkleTrie', () => {
let Lib_SecureMerkleTrie: Contract let Lib_SecureMerkleTrie: Contract
before(async () => { before(async () => {
Lib_SecureMerkleTrie = await ( Lib_SecureMerkleTrie = await deploy('TestLib_SecureMerkleTrie')
await ethers.getContractFactory('TestLib_SecureMerkleTrie')
).deploy()
}) })
describe('verifyInclusionProof', () => { describe('verifyInclusionProof', () => {
......
import hre from 'hardhat'
import { Contract, ethers } from 'ethers' import { Contract, ethers } from 'ethers'
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { deploy } from '../../../helpers'
describe('Lib_Buffer', () => { describe('Lib_Buffer', () => {
let Lib_Buffer: Contract let Lib_Buffer: Contract
beforeEach(async () => { beforeEach(async () => {
const Factory__Lib_Buffer = await hre.ethers.getContractFactory( Lib_Buffer = await deploy('TestLib_Buffer')
'TestLib_Buffer'
)
Lib_Buffer = await Factory__Lib_Buffer.deploy()
}) })
describe('push(bytes32,bytes27)', () => { describe('push(bytes32,bytes27)', () => {
......
/* Internal Imports */
import { Lib_Bytes32Utils_TEST_JSON } from '../../../data' import { Lib_Bytes32Utils_TEST_JSON } from '../../../data'
import { runJsonTest } from '../../../helpers' import { runJsonTest } from '../../../helpers'
......
/* Internal Imports */
import { ethers } from 'hardhat'
import { Contract } from 'ethers' import { Contract } from 'ethers'
/* External Imports */
import { Lib_BytesUtils_TEST_JSON } from '../../../data'
import { runJsonTest } from '../../../helpers'
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { Lib_BytesUtils_TEST_JSON } from '../../../data'
import { deploy, runJsonTest } from '../../../helpers'
describe('Lib_BytesUtils', () => { describe('Lib_BytesUtils', () => {
describe('JSON tests', () => { describe('JSON tests', () => {
...@@ -15,18 +12,17 @@ describe('Lib_BytesUtils', () => { ...@@ -15,18 +12,17 @@ describe('Lib_BytesUtils', () => {
describe('Use of library with other memory-modifying operations', () => { describe('Use of library with other memory-modifying operations', () => {
let TestLib_BytesUtils: Contract let TestLib_BytesUtils: Contract
before(async () => { before(async () => {
TestLib_BytesUtils = await ( TestLib_BytesUtils = await deploy('TestLib_BytesUtils')
await ethers.getContractFactory('TestLib_BytesUtils')
).deploy()
}) })
it('should allow creation of a contract beforehand and still work', async () => { it('should allow creation of a contract beforehand and still work', async () => {
const slice = await TestLib_BytesUtils.callStatic.sliceWithTaintedMemory( expect(
await TestLib_BytesUtils.callStatic.sliceWithTaintedMemory(
'0x123412341234', '0x123412341234',
0, 0,
0 0
) )
expect(slice).to.eq('0x') ).to.eq('0x')
}) })
}) })
}) })
/* External Imports */
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
import { Contract, BigNumber } from 'ethers' import { Contract, BigNumber } from 'ethers'
import { MerkleTree } from 'merkletreejs' import { MerkleTree } from 'merkletreejs'
import { fromHexString, toHexString } from '@eth-optimism/core-utils' import { fromHexString, toHexString } from '@eth-optimism/core-utils'
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 { NON_NULL_BYTES32 } from '../../../helpers' import { deploy, NON_NULL_BYTES32 } from '../../../helpers'
const NODE_COUNTS = [ const NODE_COUNTS = [
2, 3, 7, 9, 13, 63, 64, 123, 128, 129, 255, 1021, 1023, 1024, 2, 3, 7, 9, 13, 63, 64, 123, 128, 129, 255, 1021, 1023, 1024,
] ]
const hash = (el: Buffer | string): Buffer => { const makeMerkleTree = (elements: string[]): MerkleTree => {
return Buffer.from(ethers.utils.keccak256(el).slice(2), 'hex')
}
const fillDefaultHashes = (elements: string[]): string[] => {
const filled: string[] = [] const filled: string[] = []
for (let i = 0; i < Math.pow(2, Math.ceil(Math.log2(elements.length))); i++) { for (let i = 0; i < Math.pow(2, Math.ceil(Math.log2(elements.length))); i++) {
if (i < elements.length) { if (i < elements.length) {
filled.push(elements[i]) filled.push(elements[i])
...@@ -28,17 +21,19 @@ const fillDefaultHashes = (elements: string[]): string[] => { ...@@ -28,17 +21,19 @@ const fillDefaultHashes = (elements: string[]): string[] => {
} }
} }
return filled return new MerkleTree(
filled.map(fromHexString),
(el: Buffer | string): Buffer => {
return fromHexString(ethers.utils.keccak256(el))
}
)
} }
describe('Lib_MerkleTree', () => { describe('Lib_MerkleTree', () => {
let Lib_MerkleTree: Contract let Lib_MerkleTree: Contract
let Fake__LibMerkleTree: FakeContract<Contract> let Fake__LibMerkleTree: FakeContract<Contract>
before(async () => { before(async () => {
Lib_MerkleTree = await ( Lib_MerkleTree = await deploy('TestLib_MerkleTree')
await ethers.getContractFactory('TestLib_MerkleTree')
).deploy()
Fake__LibMerkleTree = await smock.fake('TestLib_MerkleTree') Fake__LibMerkleTree = await smock.fake('TestLib_MerkleTree')
}) })
...@@ -66,17 +61,13 @@ describe('Lib_MerkleTree', () => { ...@@ -66,17 +61,13 @@ describe('Lib_MerkleTree', () => {
describe('when more than one element is provided', () => { describe('when more than one element is provided', () => {
for (const size of NODE_COUNTS) { for (const size of NODE_COUNTS) {
it(`should generate the correct root when ${size} elements are provided`, async () => { it(`should generate the correct root when ${size} elements are provided`, async () => {
const elements = [...Array(size)].map((_, i) => { const tree = makeMerkleTree(
[...Array(size)].map((_, i) => {
return ethers.utils.keccak256(BigNumber.from(i).toHexString()) return ethers.utils.keccak256(BigNumber.from(i).toHexString())
}) })
)
const bufs = fillDefaultHashes(elements).map((element) => { expect(await Lib_MerkleTree.getMerkleRoot(tree.getLeaves())).to.equal(
return fromHexString(element)
})
const tree = new MerkleTree(bufs, hash)
expect(await Lib_MerkleTree.getMerkleRoot(bufs)).to.equal(
toHexString(tree.getRoot()) toHexString(tree.getRoot())
) )
}) })
...@@ -116,24 +107,6 @@ describe('Lib_MerkleTree', () => { ...@@ -116,24 +107,6 @@ describe('Lib_MerkleTree', () => {
}) })
}) })
describe('when total elements is zero', () => {
const totalLeaves = 0
it('should revert', async () => {
await expect(
Lib_MerkleTree.verify(
ethers.constants.HashZero,
ethers.constants.HashZero,
0,
[],
totalLeaves
)
).to.be.revertedWith(
'Lib_MerkleTree: Total leaves must be greater than zero.'
)
})
})
describe('when an index is out of bounds', () => { describe('when an index is out of bounds', () => {
const totalLeaves = 1 const totalLeaves = 1
const index = 2 const index = 2
...@@ -187,26 +160,24 @@ describe('Lib_MerkleTree', () => { ...@@ -187,26 +160,24 @@ describe('Lib_MerkleTree', () => {
describe('with valid proof for more than one element', () => { describe('with valid proof for more than one element', () => {
for (const size of NODE_COUNTS) { for (const size of NODE_COUNTS) {
describe(`for a tree with ${size} total elements`, () => { describe(`for a tree with ${size} total elements`, () => {
const elements = [...Array(size)].map((_, i) => { const tree = makeMerkleTree(
[...Array(size)].map((_, i) => {
return ethers.utils.keccak256(BigNumber.from(i).toHexString()) return ethers.utils.keccak256(BigNumber.from(i).toHexString())
}) })
)
const bufs = fillDefaultHashes(elements).map((element) => {
return fromHexString(element)
})
const tree = new MerkleTree(bufs, hash)
for (let i = 0; i < size; i += Math.ceil(size / 8)) { for (let i = 0; i < size; i += Math.ceil(size / 8)) {
it(`should verify a proof for the ${i}(th/st/rd, whatever) element`, async () => { it(`should verify a proof for the ${i}(th/st/rd, whatever) element`, async () => {
const proof = tree.getProof(bufs[i], i).map((element) => { const proof = tree
.getProof(tree.getLeaves()[i], i)
.map((element) => {
return element.data return element.data
}) })
expect( expect(
await Lib_MerkleTree.verify( await Lib_MerkleTree.verify(
tree.getRoot(), tree.getRoot(),
bufs[i], tree.getLeaves()[i],
i, i,
proof, proof,
size size
......
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