Skip to content

Commit

Permalink
update getPrecompile to type address
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Oct 21, 2020
1 parent b5d3026 commit 17c4dd5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/vm/lib/evm/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
14 changes: 8 additions & 6 deletions packages/vm/lib/evm/precompiles/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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['']
Expand Down
6 changes: 3 additions & 3 deletions packages/vm/tests/api/evm/precompiles/06-ecadd.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions packages/vm/tests/api/evm/precompiles/07-ecmul.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions packages/vm/tests/api/evm/precompiles/08-ecpairing.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions packages/vm/tests/api/evm/precompiles/hardfork.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand All @@ -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),
})

Expand All @@ -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),
})

Expand All @@ -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),
})

Expand Down
13 changes: 8 additions & 5 deletions packages/vm/tests/api/istanbul/eip-1108.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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(
Expand Down

0 comments on commit 17c4dd5

Please sign in to comment.