Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CHAINID opcode for istanbul (eip-1344) #572

Merged
merged 2 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/evm/eei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ export default class EEI {
return new BN(this._env.block.header.gasLimit)
}

/**
* Returns the chain ID for current chain. Introduced for the
* CHAINID opcode proposed in [EIP-1344](https://eips.ethereum.org/EIPS/eip-1344).
*/
getChainId(): BN {
return new BN(this._common.chainId())
}

/**
* Returns Gets the hash of one of the 256 most recent complete blocks.
* @param num - Number of block
Expand Down
7 changes: 7 additions & 0 deletions lib/evm/opFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ export const handlers: { [k: string]: OpHandler } = {
GASLIMIT: function(runState: RunState) {
runState.stack.push(runState.eei.getBlockGasLimit())
},
CHAINID: function(runState: RunState) {
if (!runState._common.gteHardfork('istanbul')) {
trap(ERROR.INVALID_OPCODE)
}

runState.stack.push(runState.eei.getChainId())
},
// 0x50 range - 'storage' and execution
POP: function(runState: RunState) {
runState.stack.pop()
Expand Down
1 change: 1 addition & 0 deletions lib/evm/opcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const codes: any = {
0x43: ['NUMBER', 2, true],
0x44: ['DIFFICULTY', 2, true],
0x45: ['GASLIMIT', 2, true],
0x46: ['CHAINID', 2, false],

// 0x50 range - 'storage' and execution
0x50: ['POP', 2, false],
Expand Down
38 changes: 38 additions & 0 deletions tests/api/istanbul/eip-1344.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const tape = require('tape')
const BN = require('bn.js')
const Common = require('ethereumjs-common').default
const VM = require('../../../dist/index').default
const { ERROR } = require('../../../dist/exceptions')

const testCases = [
{ chain: 'mainnet', hardfork: 'istanbul', chainId: new BN(1) },
{ chain: 'mainnet', hardfork: 'constantinople', err: ERROR.INVALID_OPCODE },
{ chain: 'ropsten', hardfork: 'istanbul', chainId: new BN(3) }
]

// CHAINID PUSH8 0x00 MSTORE8 PUSH8 0x01 PUSH8 0x00 RETURN
const code = ['46', '60', '00', '53', '60', '01', '60', '00', 'f3']
tape('Istanbul: EIP-1344 CHAINID', async (t) => {
const runCodeArgs = {
code: Buffer.from(code.join(''), 'hex'),
gasLimit: new BN(0xffff)
}

for (const testCase of testCases) {
const common = new Common(testCase.chain, testCase.hardfork)
const vm = new VM({ common })
try {
const res = await vm.runCode(runCodeArgs)
if (testCase.err) {
t.equal(res.exceptionError.error, testCase.err)
} else {
t.assert(res.exceptionError === undefined)
t.assert(testCase.chainId.eq(new BN(res.returnValue)))
}
} catch (e) {
t.fail(e.message)
}
}

t.end()
})