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

runCode.js: add pc to opt args | initial API tests #406

Merged
merged 6 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Runs EVM code
- `opts.gasLimit` **[Buffer][42]** the gas limit for the code
- `opts.origin` **[Buffer][42]** the address where the call originated from. The address should be a `Buffer` of 20bits. Defaults to `0`
- `opts.value` **[Buffer][42]** the value in ether that is being sent to `opt.address`. Defaults to `0`
- `opts.pc` **[Number][32]** the initial program counter. Defaults to `0`
- `cb` **[runCode~callback][44]** callback

## Event: beforeBlock
Expand Down
9 changes: 8 additions & 1 deletion lib/runCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const VmError = exceptions.VmError
* @param {Buffer} opts.gasLimit the gas limit for the code
* @param {Buffer} opts.origin the address where the call originated from. The address should be a `Buffer` of 20bits. Defaults to `0`
* @param {Buffer} opts.value the value in ether that is being sent to `opt.address`. Defaults to `0`
* @param {Number} opts.pc the initial program counter. Defaults to `0`
* @param {runCode~callback} cb callback
*/

Expand Down Expand Up @@ -69,7 +70,7 @@ module.exports = function (opts, cb) {
returnValue: false,
stopped: false,
vmError: false,
programCounter: 0,
programCounter: opts.pc | 0,
opCode: undefined,
opName: undefined,
gasLeft: new BN(opts.gasLimit),
Expand Down Expand Up @@ -108,6 +109,12 @@ module.exports = function (opts, cb) {

// iterate through the given ops until something breaks or we hit STOP
function runVm (err) {
// Check that the programCounter is in range. Does not overwrite the previous err, if any.
const pc = runState.programCounter
if (!err && pc !== 0 && (pc < 0 || pc >= runState.code.length)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Condition looks good.

err = new VmError(ERROR.INVALID_OPCODE)
}

if (err) {
return parseVmResults(err)
}
Expand Down
58 changes: 58 additions & 0 deletions tests/programCounter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const tape = require('tape')
const async = require('async')
const VM = require('../')

const STOP = '00'
const JUMP = '56'
const JUMPDEST = '5b'
const PUSH1 = '60'

const testCases = [
{ code: [STOP, JUMPDEST, PUSH1, '05', JUMP, JUMPDEST], pc: 1, resultPC: 6 },
{ code: [STOP, JUMPDEST, PUSH1, '05', JUMP, JUMPDEST], pc: -1, error: 'invalid opcode' },
{ code: [STOP], pc: 3, error: 'invalid opcode' },
{ code: [STOP], resultPC: 1 }
]

tape('VM.runcode: initial program counter', function (t) {
const vm = new VM()

testCases.forEach(function (testData, i) {
t.test('should start the execution at the specified pc or 0 #' + i, function (st) {
const runCodeArgs = {
code: Buffer.from(testData.code.join(''), 'hex'),
pc: testData.pc,
gasLimit: 0xffff
}
let result

async.series([
function (done) {
vm.runCode(runCodeArgs, function (err, res) {
if (res) {
result = res
}

done(err)
})
},
function (done) {
if (testData.resultPC !== undefined) {
t.equals(result.runState.programCounter, testData.resultPC, 'runstate.programCounter')
}

done()
}
], function (err) {
if (testData.error) {
err = err ? err.error : 'no error thrown'
t.equals(err, testData.error, 'error message should match')
err = false
}

t.assert(!err)
st.end()
})
})
})
})
1 change: 1 addition & 0 deletions tests/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ function runAll () {
require('./tester.js')
require('./genesishashes.js')
require('./constantinopleSstoreTest.js')
require('./programCounter.js')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this is really completely our fault and I have directly created a test cleanup PR #437 on this to avoid future confusion:

The runAll() method is not used for quite some time and is also not integrated into CI, so currently your tests are not run.

Can you just add a file to the tests/api folder instead? Then the tests are run along the npm run testAPI command.

Also: the naming with programCounter.js is far too specific. Can we change this to some to-be-expanded entry point to generally test runCode functionality and can you therefore change the filename to runCode.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ;)

async.series([
// runTests.bind(this, 'VMTests', {}), // VM tests disabled since we don't support Frontier gas costs
runTests.bind(this, 'GeneralStateTests', {}),
Expand Down