From 17c4dd5255ae8df895fbe29ab5ed466cdd914fd5 Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Wed, 21 Oct 2020 00:16:06 -0700 Subject: [PATCH] update getPrecompile to type address --- packages/vm/lib/evm/evm.ts | 2 +- packages/vm/lib/evm/precompiles/index.ts | 14 ++++++++------ .../vm/tests/api/evm/precompiles/06-ecadd.spec.ts | 6 +++--- .../vm/tests/api/evm/precompiles/07-ecmul.spec.ts | 6 +++--- .../tests/api/evm/precompiles/08-ecpairing.spec.ts | 6 +++--- .../vm/tests/api/evm/precompiles/hardfork.spec.ts | 8 ++++---- packages/vm/tests/api/istanbul/eip-1108.spec.ts | 13 ++++++++----- 7 files changed, 30 insertions(+), 25 deletions(-) diff --git a/packages/vm/lib/evm/evm.ts b/packages/vm/lib/evm/evm.ts index 429b2330626..e0a85ebb0fe 100644 --- a/packages/vm/lib/evm/evm.ts +++ b/packages/vm/lib/evm/evm.ts @@ -387,7 +387,7 @@ export default class EVM { * if no such precompile exists. */ getPrecompile(address: Address): PrecompileFunc { - return getPrecompile(address.buf.toString('hex'), this._vm._common) + return getPrecompile(address, this._vm._common) } /** diff --git a/packages/vm/lib/evm/precompiles/index.ts b/packages/vm/lib/evm/precompiles/index.ts index 1639f840057..5bdf04875b7 100644 --- a/packages/vm/lib/evm/precompiles/index.ts +++ b/packages/vm/lib/evm/precompiles/index.ts @@ -1,3 +1,5 @@ +import { Address } from 'ethereumjs-util' +import Common from '@ethereumjs/common' import { PrecompileInput, PrecompileFunc } from './types' import { default as p1 } from './01-ecrecover' import { default as p2 } from './02-sha256' @@ -17,7 +19,6 @@ import { default as pf } from './0f-bls12-g2multiexp' import { default as p10 } from './10-bls12-pairing' import { default as p11 } from './11-bls12-map-fp-to-g1' import { default as p12 } from './12-bls12-map-fp2-to-g2' -import Common from '@ethereumjs/common' interface Precompiles { [key: string]: PrecompileFunc @@ -143,19 +144,20 @@ const precompileAvailability: PrecompileAvailability = { }, } -function getPrecompile(address: string, common: Common): PrecompileFunc { - if (precompiles[address]) { - const availability = precompileAvailability[address] +function getPrecompile(address: Address, common: Common): PrecompileFunc { + const addr = address.buf.toString('hex') + if (precompiles[addr]) { + const availability = precompileAvailability[addr] if ( availability.type == PrecompileAvailabilityCheck.Hardfork && common.gteHardfork(availability.param) ) { - return precompiles[address] + return precompiles[addr] } else if ( availability.type == PrecompileAvailabilityCheck.EIP && common.eips().includes(availability.param) ) { - return precompiles[address] + return precompiles[addr] } } return precompiles[''] diff --git a/packages/vm/tests/api/evm/precompiles/06-ecadd.spec.ts b/packages/vm/tests/api/evm/precompiles/06-ecadd.spec.ts index 73b2b733104..3b78e3f33bd 100644 --- a/packages/vm/tests/api/evm/precompiles/06-ecadd.spec.ts +++ b/packages/vm/tests/api/evm/precompiles/06-ecadd.spec.ts @@ -1,5 +1,5 @@ import tape from 'tape' -import { BN } from 'ethereumjs-util' +import { Address, BN } from 'ethereumjs-util' import Common from '@ethereumjs/common' import VM from '../../../../lib' import { getPrecompile } from '../../../../lib/evm/precompiles' @@ -8,8 +8,8 @@ tape('Precompiles: ECADD', (t) => { t.test('ECADD', async (st) => { const common = new Common({ chain: 'mainnet', hardfork: 'petersburg' }) const vm = new VM({ common: common }) - - const ECADD = getPrecompile('0000000000000000000000000000000000000006', common) + const address = new Address(Buffer.from('0000000000000000000000000000000000000006', 'hex')) + const ECADD = getPrecompile(address, common) const result = await ECADD({ data: Buffer.alloc(0), diff --git a/packages/vm/tests/api/evm/precompiles/07-ecmul.spec.ts b/packages/vm/tests/api/evm/precompiles/07-ecmul.spec.ts index 519119c9553..f8a6477bdd7 100644 --- a/packages/vm/tests/api/evm/precompiles/07-ecmul.spec.ts +++ b/packages/vm/tests/api/evm/precompiles/07-ecmul.spec.ts @@ -1,5 +1,5 @@ import tape from 'tape' -import { BN } from 'ethereumjs-util' +import { Address, BN } from 'ethereumjs-util' import Common from '@ethereumjs/common' import VM from '../../../../lib' import { getPrecompile } from '../../../../lib/evm/precompiles' @@ -8,8 +8,8 @@ tape('Precompiles: ECMUL', (t) => { t.test('ECMUL', async (st) => { const common = new Common({ chain: 'mainnet', hardfork: 'petersburg' }) const vm = new VM({ common: common }) - - const ECMUL = getPrecompile('0000000000000000000000000000000000000007', common) + const address = new Address(Buffer.from('0000000000000000000000000000000000000007', 'hex')) + const ECMUL = getPrecompile(address, common) const result = await ECMUL({ data: Buffer.alloc(0), diff --git a/packages/vm/tests/api/evm/precompiles/08-ecpairing.spec.ts b/packages/vm/tests/api/evm/precompiles/08-ecpairing.spec.ts index cddecca8f74..e8d10b4a894 100644 --- a/packages/vm/tests/api/evm/precompiles/08-ecpairing.spec.ts +++ b/packages/vm/tests/api/evm/precompiles/08-ecpairing.spec.ts @@ -1,5 +1,5 @@ import tape from 'tape' -import { BN } from 'ethereumjs-util' +import { Address, BN } from 'ethereumjs-util' import Common from '@ethereumjs/common' import VM from '../../../../lib' import { getPrecompile } from '../../../../lib/evm/precompiles' @@ -8,8 +8,8 @@ tape('Precompiles: ECPAIRING', (t) => { t.test('ECPAIRING', async (st) => { const common = new Common({ chain: 'mainnet', hardfork: 'petersburg' }) const vm = new VM({ common: common }) - - const ECPAIRING = getPrecompile('0000000000000000000000000000000000000008', common) + const address = new Address(Buffer.from('0000000000000000000000000000000000000008', 'hex')) + const ECPAIRING = getPrecompile(address, common) const result = await ECPAIRING({ data: Buffer.from( diff --git a/packages/vm/tests/api/evm/precompiles/hardfork.spec.ts b/packages/vm/tests/api/evm/precompiles/hardfork.spec.ts index 0ca5be8f1ef..26bf397bb4d 100644 --- a/packages/vm/tests/api/evm/precompiles/hardfork.spec.ts +++ b/packages/vm/tests/api/evm/precompiles/hardfork.spec.ts @@ -6,7 +6,7 @@ import { getPrecompile } from '../../../../lib/evm/precompiles' tape('Precompiles: hardfork availability', (t) => { t.test('Test ECPAIRING availability', async (st) => { - const ECPAIR_Address = '0000000000000000000000000000000000000008' + const ECPAIR_Address = new Address(Buffer.from('0000000000000000000000000000000000000008', 'hex')) // ECPAIR was introduced in Byzantium; check if available from Byzantium. const commonByzantium = new Common({ chain: 'mainnet', hardfork: 'byzantium' }) @@ -23,7 +23,7 @@ tape('Precompiles: hardfork availability', (t) => { let result = await vm.runCall({ caller: Address.zero(), gasLimit: new BN(0xffffffffff), - to: new Address(Buffer.from(ECPAIR_Address, 'hex')), + to: ECPAIR_Address, value: new BN(0), }) @@ -43,7 +43,7 @@ tape('Precompiles: hardfork availability', (t) => { result = await vm.runCall({ caller: Address.zero(), gasLimit: new BN(0xffffffffff), - to: new Address(Buffer.from(ECPAIR_Address, 'hex')), + to: ECPAIR_Address, value: new BN(0), }) @@ -64,7 +64,7 @@ tape('Precompiles: hardfork availability', (t) => { result = await vm.runCall({ caller: Address.zero(), gasLimit: new BN(0xffffffffff), - to: new Address(Buffer.from(ECPAIR_Address, 'hex')), + to: ECPAIR_Address, value: new BN(0), }) diff --git a/packages/vm/tests/api/istanbul/eip-1108.spec.ts b/packages/vm/tests/api/istanbul/eip-1108.spec.ts index aabc5016a46..085cd5c7430 100644 --- a/packages/vm/tests/api/istanbul/eip-1108.spec.ts +++ b/packages/vm/tests/api/istanbul/eip-1108.spec.ts @@ -1,5 +1,5 @@ import tape from 'tape' -import { BN } from 'ethereumjs-util' +import { Address, BN } from 'ethereumjs-util' import Common from '@ethereumjs/common' import VM from '../../../lib' import { getPrecompile } from '../../../lib/evm/precompiles' @@ -8,7 +8,8 @@ tape('Istanbul: EIP-1108 tests', (t) => { t.test('ECADD', async (st) => { const common = new Common({ chain: 'mainnet', hardfork: 'istanbul' }) const vm = new VM({ common }) - const ECADD = getPrecompile('0000000000000000000000000000000000000006', common) + const address = new Address(Buffer.from('0000000000000000000000000000000000000006', 'hex')) + const ECADD = getPrecompile(address, common) const result = await ECADD({ data: Buffer.alloc(0), @@ -23,8 +24,9 @@ tape('Istanbul: EIP-1108 tests', (t) => { t.test('ECMUL', async (st) => { const common = new Common({ chain: 'mainnet', hardfork: 'istanbul' }) - let vm = new VM({ common }) - let ECMUL = getPrecompile('0000000000000000000000000000000000000007', common) + const vm = new VM({ common }) + const address = new Address(Buffer.from('0000000000000000000000000000000000000007', 'hex')) + const ECMUL = getPrecompile(address, common) const result = await ECMUL({ data: Buffer.alloc(0), @@ -40,7 +42,8 @@ tape('Istanbul: EIP-1108 tests', (t) => { t.test('ECPAIRING', async (st) => { const common = new Common({ chain: 'mainnet', hardfork: 'istanbul' }) const vm = new VM({ common }) - const ECPAIRING = getPrecompile('0000000000000000000000000000000000000008', common) + const address = new Address(Buffer.from('0000000000000000000000000000000000000008', 'hex')) + const ECPAIRING = getPrecompile(address, common) const result = await ECPAIRING({ data: Buffer.from(