Commit 9d25e0a5 authored by Kelvin Fichter's avatar Kelvin Fichter Committed by GitHub

dev: Use eth-optimism/core-utils where applicable (#281)

* Use core-utils where applicable

* Further reduce utils

* Update ovmCREATEEOA.spec.ts
Co-authored-by: default avatarMark Tyneway <mark.tyneway@gmail.com>
parent e7b0d3de
......@@ -33,6 +33,7 @@
"serve": "./bin/serve_dump.sh"
},
"dependencies": {
"@eth-optimism/core-utils": "^0.1.8",
"@eth-optimism/dev": "^1.1.1",
"@ethersproject/abstract-provider": "^5.0.8",
"@ethersproject/contracts": "^5.0.5",
......@@ -42,7 +43,7 @@
"glob": "^7.1.6"
},
"devDependencies": {
"@eth-optimism/plugins": "^0.0.16",
"@eth-optimism/plugins": "^0.0.17",
"@eth-optimism/smock": "0.2.1-alpha.0",
"@nomiclabs/hardhat-ethers": "^2.0.1",
"@nomiclabs/hardhat-waffle": "^2.0.1",
......
......@@ -6,7 +6,7 @@ import { Interface } from 'ethers/lib/utils'
export const getContractDefinition = (name: string, ovm?: boolean): any => {
const match = glob.sync(
path.resolve(__dirname, `../artifacts`) +
`/**/${name}${ovm ? '.ovm' : ''}.json`
`/**/${name}${ovm ? '-ovm' : ''}.json`
)
if (match.length > 0) {
......
......@@ -3,10 +3,10 @@ import * as path from 'path'
import { ethers } from 'ethers'
import * as Ganache from 'ganache-core'
import { keccak256 } from 'ethers/lib/utils'
import { fromHexString, toHexString, remove0x } from '@eth-optimism/core-utils'
/* Internal Imports */
import { deploy, RollupDeployConfig } from './contract-deployment'
import { fromHexString, toHexString, remove0x } from './utils'
import { getContractDefinition } from './contract-defs'
interface StorageDump {
......
/* External Imports */
import { BigNumber } from 'ethers'
/**
* Converts a string or buffer to a '0x'-prefixed hex string.
* @param buf String or buffer to convert.
* @returns '0x'-prefixed string.
*/
export const toHexString = (buf: Buffer | string): string => {
return '0x' + fromHexString(buf).toString('hex')
}
/**
* Converts a '0x'-prefixed string to a buffer.
* @param str '0x'-prefixed string to convert.
* @returns Hex buffer.
*/
export const fromHexString = (str: string | Buffer): Buffer => {
if (typeof str === 'string' && str.startsWith('0x')) {
return Buffer.from(str.slice(2), 'hex')
}
return Buffer.from(str)
}
export const toHexString32 = (
input: Buffer | string | number,
padRight = false
): string => {
if (typeof input === 'number') {
input = BigNumber.from(input).toHexString()
}
input = toHexString(input).slice(2)
return '0x' + (padRight ? input.padEnd(64, '0') : input.padStart(64, '0'))
}
export const getHexSlice = (
input: Buffer | string,
start: number,
length: number
): string => {
return toHexString(fromHexString(input).slice(start, start + length))
}
const hexRegex = /^(0x)?[0-9a-fA-F]*$/
/**
* Generates a hex string of repeated bytes.
* @param byte Byte to repeat.
* @param len Number of times to repeat the byte.
* @return '0x'-prefixed hex string filled with the provided byte.
*/
export const makeHexString = (byte: string, len: number): string => {
return '0x' + byte.repeat(len)
}
/**
* Genereates an address with a repeated byte.
* @param byte Byte to repeat in the address.
* @return Address filled with the repeated byte.
*/
export const makeAddress = (byte: string): string => {
return makeHexString(byte, 20)
}
/**
* Removes '0x' from a hex string.
* @param str Hex string to remove '0x' from.
* @returns String without the '0x' prefix.
*/
export const remove0x = (str: string): string => {
if (str.startsWith('0x')) {
return str.slice(2)
} else {
return str
}
}
/**
* Returns whether or not the provided string is a hex string.
*
* @param str The string to test.
* @returns True if the provided string is a hex string, false otherwise.
*/
export const isHexString = (str: string): boolean => {
return hexRegex.test(str)
}
/**
* Converts a hex string to a buffer
* @param hexString the hex string to be converted
* @returns the hexString as a buffer.
*/
export const hexStrToBuf = (hexString: string): Buffer => {
if (!isHexString(hexString)) {
throw new RangeError(`Invalid hex string [${hexString}]`)
}
if (hexString.length % 2 !== 0) {
throw new RangeError(
`Invalid hex string -- odd number of characters: [${hexString}]`
)
}
return Buffer.from(remove0x(hexString), 'hex')
}
/**
* Converts a JavaScript number to a big-endian hex string.
* @param number the JavaScript number to be converted.
* @param padToBytes the number of numeric bytes the resulting string should be, -1 if no padding should be done.
* @returns the JavaScript number as a string.
*/
export const numberToHexString = (
number: number,
padToBytes: number = -1
): string => {
let str = number.toString(16)
if (padToBytes > 0 || str.length < padToBytes * 2) {
str = `${'0'.repeat(padToBytes * 2 - str.length)}${str}`
}
return add0x(str)
}
/**
* Adds "0x" to the start of a string if necessary.
* @param str String to modify.
* @returns the string with "0x".
*/
export const add0x = (str: string): string => {
if (str === undefined) {
return str
}
return str.startsWith('0x') ? str : '0x' + str
}
export * from './buffer-utils'
export * from './byte-utils'
......@@ -4,9 +4,10 @@ import { expect } from '../../../setup'
import { ethers, waffle } from 'hardhat'
import { ContractFactory, Contract, Wallet } from 'ethers'
import { MockContract, smockit } from '@eth-optimism/smock'
import { remove0x } from '@eth-optimism/core-utils'
/* Internal Imports */
import { remove0x, decodeSolidityError } from '../../../helpers'
import { decodeSolidityError } from '../../../helpers'
const callPrecompile = async (
Helper_PrecompileCaller: Contract,
......
......@@ -4,6 +4,7 @@ import { expect } from '../../../../setup'
import { ethers } from 'hardhat'
import { Signer, ContractFactory, Contract, BigNumber } from 'ethers'
import { smockit, MockContract } from '@eth-optimism/smock'
import { remove0x, toHexString } from '@eth-optimism/core-utils'
/* Internal Imports */
import {
......@@ -15,9 +16,7 @@ import {
DUMMY_BATCH_HEADERS,
DUMMY_BATCH_PROOFS,
TrieTestGenerator,
toHexString,
getNextBlockNumber,
remove0x,
getXDomainCalldata,
} from '../../../../helpers'
import { keccak256 } from 'ethers/lib/utils'
......
......@@ -4,6 +4,7 @@ import { expect } from '../../../../setup'
import { ethers } from 'hardhat'
import { Signer, ContractFactory, Contract } from 'ethers'
import { smockit, MockContract } from '@eth-optimism/smock'
import { toHexString } from '@eth-optimism/core-utils'
/* Internal Imports */
import {
......@@ -12,7 +13,6 @@ import {
NON_NULL_BYTES32,
DUMMY_BATCH_HEADERS,
DUMMY_BATCH_PROOFS,
toHexString,
} from '../../../../helpers'
describe('OVM_L1MultiMessageRelayer', () => {
......
......@@ -5,6 +5,7 @@ import { ethers } from 'hardhat'
import { Signer, ContractFactory, Contract, BigNumber } from 'ethers'
import { TransactionResponse } from '@ethersproject/abstract-provider'
import { smockit, MockContract } from '@eth-optimism/smock'
import { remove0x } from '@eth-optimism/core-utils'
import _ from 'lodash'
/* Internal Imports */
......@@ -15,7 +16,6 @@ import {
FORCE_INCLUSION_PERIOD_BLOCKS,
setEthTime,
NON_ZERO_ADDRESS,
remove0x,
getEthTime,
getNextBlockNumber,
increaseEthTime,
......
/* External Imports */
import { fromHexString } from '@eth-optimism/core-utils'
/* Internal Imports */
import {
ExecutionManagerTestRunner,
......@@ -5,7 +8,6 @@ import {
OVM_TX_GAS_LIMIT,
NON_NULL_BYTES32,
VERIFIED_EMPTY_CONTRACT_HASH,
fromHexString,
} from '../../../../helpers'
import { getContractDefinition } from '../../../../../src'
......
......@@ -4,9 +4,11 @@ import { expect } from '../../../setup'
import { ethers } from 'hardhat'
import { ContractFactory, Contract } from 'ethers'
import { MockContract, smockit } from '@eth-optimism/smock'
import { NON_ZERO_ADDRESS } from '../../../helpers/constants'
import { remove0x } from '@eth-optimism/core-utils'
import { keccak256 } from 'ethers/lib/utils'
import { remove0x } from '../../../helpers'
/* Internal Imports */
import { NON_ZERO_ADDRESS } from '../../../helpers/constants'
const ELEMENT_TEST_SIZES = [1, 2, 4, 8, 16]
......
......@@ -4,9 +4,10 @@ import { expect } from '../../../setup'
import { ethers, waffle } from 'hardhat'
import { ContractFactory, Contract, Wallet } from 'ethers'
import { MockContract, smockit } from '@eth-optimism/smock'
import { ZERO_ADDRESS, remove0x } from '@eth-optimism/core-utils'
/* Internal Imports */
import { decodeSolidityError, ZERO_ADDRESS, remove0x } from '../../../helpers'
import { decodeSolidityError } from '../../../helpers'
const callPrecompile = async (
Helper_PrecompileCaller: Contract,
......
......@@ -15,7 +15,6 @@ import {
setProxyTarget,
TrieTestGenerator,
ZERO_ADDRESS,
numberToHexString,
} from '../../../helpers'
import {
MockContract,
......@@ -24,6 +23,7 @@ import {
smoddit,
ModifiableContractFactory,
} from '@eth-optimism/smock'
import { toHexString } from '@eth-optimism/core-utils'
describe('OVM_StateTransitioner', () => {
let AddressManager: Contract
......@@ -279,7 +279,7 @@ describe('OVM_StateTransitioner', () => {
l1QueueOrigin: '0x00',
l1TxOrigin: ZERO_ADDRESS,
entrypoint: ZERO_ADDRESS,
gasLimit: numberToHexString(gasLimit),
gasLimit: toHexString(gasLimit),
data: '0x1234',
}
......
......@@ -4,9 +4,10 @@ import { expect } from '../../../setup'
import * as rlp from 'rlp'
import { ethers } from 'hardhat'
import { Contract } from 'ethers'
import { toHexString } from '@eth-optimism/core-utils'
/* Internal Imports */
import { toHexString, TrieTestGenerator } from '../../../helpers'
import { TrieTestGenerator } from '../../../helpers'
const NODE_COUNTS = [1, 2, 128]
......
......@@ -4,14 +4,19 @@ import { expect } from '../../../setup'
/* External Imports */
import { ethers } from 'hardhat'
import { Contract, Signer } from 'ethers'
import { fromHexString, toHexString } from '@eth-optimism/core-utils'
/* Internal Imports */
import {
ZERO_ADDRESS,
makeHexString,
fromHexString,
getHexSlice,
} from '../../../helpers'
import { ZERO_ADDRESS } from '../../../helpers'
// Leaving this here for now. If it's sufficiently useful we can throw it in core-utils.
const getHexSlice = (
input: Buffer | string,
start: number,
length: number
): string => {
return toHexString(fromHexString(input).slice(start, start + length))
}
describe('Lib_EthUtils', () => {
let signer: Signer
......@@ -42,7 +47,7 @@ describe('Lib_EthUtils', () => {
offset,
length
)
).to.equal(makeHexString('00', length))
).to.equal('0x' + '00'.repeat(length))
})
})
......@@ -58,7 +63,7 @@ describe('Lib_EthUtils', () => {
offset,
length
)
).to.equal(makeHexString('00', length))
).to.equal('0x' + '00'.repeat(length))
})
})
})
......@@ -81,7 +86,7 @@ describe('Lib_EthUtils', () => {
offset,
length
)
).to.equal(makeHexString('00', length))
).to.equal('0x' + '00'.repeat(length))
})
})
......@@ -97,7 +102,7 @@ describe('Lib_EthUtils', () => {
offset,
length
)
).to.equal(makeHexString('00', length))
).to.equal('0x' + '00'.repeat(length))
})
})
})
......@@ -291,7 +296,7 @@ describe('Lib_EthUtils', () => {
offset,
length
)
).to.equal(makeHexString('00', length))
).to.equal('0x' + '00'.repeat(length))
})
})
})
......
......@@ -4,14 +4,10 @@ import { expect } from '../../../setup'
import { ethers } from 'hardhat'
import { Contract, BigNumber } from 'ethers'
import { MerkleTree } from 'merkletreejs'
import { fromHexString, toHexString } from '@eth-optimism/core-utils'
/* Internal Imports */
import {
fromHexString,
NON_NULL_BYTES32,
NULL_BYTES32,
toHexString,
} from '../../../helpers'
import { NON_NULL_BYTES32, NULL_BYTES32 } from '../../../helpers'
const NODE_COUNTS = [
2,
......
......@@ -3,7 +3,7 @@ import { ethers } from 'hardhat'
import { Wallet } from 'ethers'
/* Internal Imports */
import { remove0x, hexStrToBuf } from '../'
import { remove0x, fromHexString } from '@eth-optimism/core-utils'
import { ZERO_ADDRESS } from '../constants'
export interface EIP155Transaction {
......@@ -47,9 +47,9 @@ export const encodeCompactTransaction = (transaction: any): string => {
const compressedGasPrice: any = transaction.gasPrice / 1000000
const gasPrice = ethers.utils.zeroPad(compressedGasPrice, 3)
const to = !transaction.to.length
? hexStrToBuf(ZERO_ADDRESS)
: hexStrToBuf(transaction.to)
const data = hexStrToBuf(transaction.data)
? fromHexString(ZERO_ADDRESS)
: fromHexString(transaction.to)
const data = fromHexString(transaction.data)
return Buffer.concat([
Buffer.from(gasLimit),
......
/* External Imports */
import { ethers } from 'ethers'
import { defaultAccounts } from 'ethereum-waffle'
import { fromHexString, toHexString } from '@eth-optimism/core-utils'
import xor from 'buffer-xor'
/* Internal Imports */
import { makeHexString, makeAddress, fromHexString, toHexString } from './utils'
export const DEFAULT_ACCOUNTS = defaultAccounts
export const DEFAULT_ACCOUNTS_HARDHAT = defaultAccounts.map((account) => {
return {
......@@ -19,10 +17,12 @@ export const RUN_OVM_TEST_GAS = 20_000_000
export const FORCE_INCLUSION_PERIOD_SECONDS = 600
export const FORCE_INCLUSION_PERIOD_BLOCKS = 600 / 12
export const NULL_BYTES32 = makeHexString('00', 32)
export const NON_NULL_BYTES32 = makeHexString('11', 32)
export const ZERO_ADDRESS = makeAddress('00')
export const NON_ZERO_ADDRESS = makeAddress('11')
export const NULL_BYTES32 =
'0x0000000000000000000000000000000000000000000000000000000000000000'
export const NON_NULL_BYTES32 =
'0x1111111111111111111111111111111111111111111111111111111111111111'
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
export const NON_ZERO_ADDRESS = '0x1111111111111111111111111111111111111111'
export const VERIFIED_EMPTY_CONTRACT_HASH =
'0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
......
......@@ -4,7 +4,6 @@ import { BigNumber } from 'ethers'
/* Internal Imports */
import { DUMMY_BYTES32 } from './bytes32'
import { ZERO_ADDRESS, NON_ZERO_ADDRESS } from '../constants'
import { makeAddress } from '../utils'
import { OVMAccount } from '../types/ovm-types'
export const DUMMY_ACCOUNTS: Array<{
......@@ -12,7 +11,7 @@ export const DUMMY_ACCOUNTS: Array<{
data: OVMAccount
}> = [
{
address: makeAddress('12'),
address: '0x1212121212121212121212121212121212121212',
data: {
nonce: BigNumber.from(123),
balance: BigNumber.from(456),
......@@ -22,7 +21,7 @@ export const DUMMY_ACCOUNTS: Array<{
},
},
{
address: makeAddress('21'),
address: '0x2121212121212121212121212121212121212121',
data: {
nonce: BigNumber.from(321),
balance: BigNumber.from(654),
......
......@@ -2,9 +2,9 @@
import * as rlp from 'rlp'
import { default as seedbytes } from 'random-bytes-seed'
import { SecureTrie, BaseTrie } from 'merkle-patricia-tree'
import { fromHexString, toHexString } from '@eth-optimism/core-utils'
/* Internal Imports */
import { fromHexString, toHexString } from '../utils'
import { NULL_BYTES32 } from '../constants'
export interface TrieNode {
......
/* External Imports */
import { Signer } from 'ethers'
import { toHexString } from '../../../src/utils'
import { toHexString } from '@eth-optimism/core-utils'
export const deployContractCode = async (
code: string,
......
export * from '../../../src/utils'
export * from './eth-time'
export * from './sol-utils'
export * from './custom-deployer'
This diff is collapsed.
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