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