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

Refactor stack manipulation in evm #460

Merged
merged 1 commit into from
Mar 5, 2019
Merged

Refactor stack manipulation in evm #460

merged 1 commit into from
Mar 5, 2019

Conversation

s1na
Copy link
Contributor

@s1na s1na commented Mar 4, 2019

This PR introduces a new Stack class for the evm, and moves all the boundary-checking logic, and dup and swap methods to it. it also removes the number of inputs off stack, and results on stack from the opcodes list as they're no longer necessary.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.4%) to 94.657% when pulling 058a877 on evm/stack into ea0e29e on master.

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

Could you explain the changes in the LOG opcode?

I am otherwise through and would actually give this a relatively quick go, all looks good and transparently traceable to me and all tests are passing.

Since changes here are pretty extensive I will leave this open a bit more for others to comment.

@@ -158,7 +159,7 @@ module.exports = function (opts, cb) {
pc: runState.programCounter,
gasLeft: runState.gasLeft,
opcode: lookupOpInfo(opCode, true, self.emitFreeLogs),
stack: runState.stack,
Copy link
Member

Choose a reason for hiding this comment

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

Changed for backwards compatibility, ok.

// check for stack underflows
if (runState.stack.length < opInfo.in) {
return cb(new VmError(ERROR.STACK_UNDERFLOW))
}
Copy link
Member

Choose a reason for hiding this comment

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

This has been moved to the respective push method, ok.


if ((runState.stack.length - opInfo.in + opInfo.out) > 1024) {
return cb(new VmError(ERROR.STACK_OVERFLOW))
}
Copy link
Member

Choose a reason for hiding this comment

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

Moved to pull, ok.

@@ -192,5 +192,5 @@ module.exports = function (op, full, freeLogs) {
}
}

return {name: opcode, opcode: op, fee: fee, in: code[2], out: code[3], dynamic: code[4], async: code[5]}
return {name: opcode, opcode: op, fee: fee, dynamic: code[2], async: code[3]}
Copy link
Member

Choose a reason for hiding this comment

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

Code changes in file ok.

}

this._store.push(value)
}
Copy link
Member

Choose a reason for hiding this comment

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

Ok.

@@ -272,9 +351,11 @@ module.exports = {
runState.memory.write(memOffset, dataLength, data)
},
Copy link
Member

Choose a reason for hiding this comment

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

Ok.

@@ -354,76 +445,90 @@ module.exports = {
runState.memory.write(memOffset, length, data)
},
Copy link
Member

Choose a reason for hiding this comment

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

Ok.

@@ -454,7 +559,8 @@ module.exports = {
})
})
},
Copy link
Member

Choose a reason for hiding this comment

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

Ok.

var topIndex = runState.stack.length - 1
var tmp = runState.stack[topIndex]
runState.stack[topIndex] = runState.stack[swapIndex]
runState.stack[swapIndex] = tmp
Copy link
Member

Choose a reason for hiding this comment

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

These two have dedicated functions in the new stack class, ok.

@@ -1091,7 +1199,8 @@ function makeCall (runState, callOptions, localOpts, cb) {
runState.contract.nonce = new BN(runState.contract.nonce).subn(1)
}

cb(null, new BN(results.vm.exception))
runState.stack.push(new BN(results.vm.exception))
cb(null)
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Ok.

@s1na
Copy link
Contributor Author

s1na commented Mar 5, 2019

Previously, it was specified in the opcodes file, how many items should be popped off the stack given each variant of LOG (e.g. LOG1), and all were passed as arguments. Now, the same thing is being done in the opFn dynamically (determine number of items from opcode number).

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants