Skip to content

Commit

Permalink
[VM] address requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Aug 12, 2020
1 parent b303a21 commit 582ce78
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
31 changes: 8 additions & 23 deletions packages/vm/lib/evm/opFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,7 @@ export const handlers: { [k: string]: OpHandler } = {

subMemUsage(runState, offset, length)
let gasLimit = new BN(runState.eei.getGasLeft())
gasLimit = maxCallGas(
gasLimit,
runState.eei.getGasLeft(),
runState._common.gteHardfork('tangerineWhistle'),
)
gasLimit = maxCallGas(gasLimit, runState.eei.getGasLeft(), runState)

let data = Buffer.alloc(0)
if (!length.isZero()) {
Expand All @@ -634,7 +630,7 @@ export const handlers: { [k: string]: OpHandler } = {
new BN(runState._common.param('gasPrices', 'sha3Word')).imul(divCeil(length, new BN(32))),
)
let gasLimit = new BN(runState.eei.getGasLeft())
gasLimit = maxCallGas(gasLimit, runState.eei.getGasLeft(), true) // CREATE2 is only available after TW (Constantinople introduced this opcode)
gasLimit = maxCallGas(gasLimit, runState.eei.getGasLeft(), runState) // CREATE2 is only available after TW (Constantinople introduced this opcode)

let data = Buffer.alloc(0)
if (!length.isZero()) {
Expand Down Expand Up @@ -688,11 +684,7 @@ export const handlers: { [k: string]: OpHandler } = {
runState.eei.useGas(new BN(runState._common.param('gasPrices', 'callNewAccount')))
}

gasLimit = maxCallGas(
gasLimit,
runState.eei.getGasLeft(),
runState._common.gteHardfork('tangerineWhistle'),
)
gasLimit = maxCallGas(gasLimit, runState.eei.getGasLeft(), runState)
// note that TW or later this cannot happen (it could have ran out of gas prior to getting here though)
if (gasLimit.gt(runState.eei.getGasLeft())) {
trap(ERROR.OUT_OF_GAS)
Expand Down Expand Up @@ -726,11 +718,7 @@ export const handlers: { [k: string]: OpHandler } = {
if (!value.isZero()) {
runState.eei.useGas(new BN(runState._common.param('gasPrices', 'callValueTransfer')))
}
gasLimit = maxCallGas(
gasLimit,
runState.eei.getGasLeft(),
runState._common.gteHardfork('tangerineWhistle'),
)
gasLimit = maxCallGas(gasLimit, runState.eei.getGasLeft(), runState)
// note that TW or later this cannot happen (it could have ran out of gas prior to getting here though)
if (gasLimit.gt(runState.eei.getGasLeft())) {
trap(ERROR.OUT_OF_GAS)
Expand Down Expand Up @@ -758,11 +746,7 @@ export const handlers: { [k: string]: OpHandler } = {

subMemUsage(runState, inOffset, inLength)
subMemUsage(runState, outOffset, outLength)
gasLimit = maxCallGas(
gasLimit,
runState.eei.getGasLeft(),
runState._common.gteHardfork('tangerineWhistle'),
)
gasLimit = maxCallGas(gasLimit, runState.eei.getGasLeft(), runState)
// note that TW or later this cannot happen (it could have ran out of gas prior to getting here though)
if (gasLimit.gt(runState.eei.getGasLeft())) {
trap(ERROR.OUT_OF_GAS)
Expand All @@ -785,7 +769,7 @@ export const handlers: { [k: string]: OpHandler } = {

subMemUsage(runState, inOffset, inLength)
subMemUsage(runState, outOffset, outLength)
gasLimit = maxCallGas(gasLimit, runState.eei.getGasLeft(), true) // we set TW or later to true here, as STATICCALL was available from Byzantium (which is after TW)
gasLimit = maxCallGas(gasLimit, runState.eei.getGasLeft(), runState) // we set TW or later to true here, as STATICCALL was available from Byzantium (which is after TW)

let data = Buffer.alloc(0)
if (!inLength.isZero()) {
Expand Down Expand Up @@ -933,7 +917,8 @@ function jumpIsValid(runState: RunState, dest: number): boolean {
* @param {BN} gasLeft - current gas left
* @param {boolean} isTangerineWhistleOrLater - set to true if we are on TW or later (this converts this to the identify function if it is false)
*/
function maxCallGas(gasLimit: BN, gasLeft: BN, isTangerineWhistleOrLater: boolean): BN {
function maxCallGas(gasLimit: BN, gasLeft: BN, runState: RunState): BN {
const isTangerineWhistleOrLater = runState._common.gteHardfork('tangerineWhistle')
if (isTangerineWhistleOrLater) {
const gasAllowed = gasLeft.sub(gasLeft.divn(64))
return gasLimit.gt(gasAllowed) ? gasAllowed : gasLimit
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/lib/state/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface StateManager {
checkpoint(): Promise<void>
commit(): Promise<void>
revert(): Promise<void>
getStateRoot(force: boolean): Promise<Buffer>
getStateRoot(force?: boolean): Promise<Buffer>
setStateRoot(stateRoot: Buffer): Promise<void>
dumpStorage(address: Buffer): Promise<StorageDump>
hasGenesisState(): Promise<boolean>
Expand Down
6 changes: 3 additions & 3 deletions packages/vm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"coverage:test": "tape './tests/api/**/*.js' ./tests/tester.js --state --dist",
"docs:build": "typedoc --options typedoc.js",
"test:state": "ts-node ./tests/tester --state",
"test:state:allForks": "npm run test:state -- --fork=Homestead && npm run test:state -- --fork=TangerineWhistle && npm run test:state -- --fork=SpuriousDragon && npm run test:state -- --fork=Byzantium && npm run test:state -- --fork=Constantinople && npm run test:state -- --fork=Petersburg && npm run test:state -- --fork=Istanbul && npm run test:state -- --fork=MuirGlacier",
"test:state:selectedForks": "npm run test:state -- --fork=Petersburg && npm run test:state -- --fork=Homestead && npm run test:state -- --fork=TangerineWhistle && npm run test:state -- --fork=SpuriousDragon",
"test:state:allForks": "echo {'Homestead','TangerineWhistle','SpuriousDragon','Byzantium','Constantinople','Petersburg','Istanbul','MuirGlacier'} | xargs -n1 | xargs -I v1 npm run test:state -- --fork=v1",
"test:state:selectedForks": "echo {'Homestead','TangerineWhistle','SpuriousDragon','Petersburg',} | xargs -n1 | xargs -I v1 npm run test:state -- --fork=v1",
"test:state:slow": "npm run test:state -- --runSkipped=slow",
"test:buildIntegrity": "npm run test:state -- --test='stackOverflow'",
"test:blockchain": "node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain",
"test:blockchain:allForks": "node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain --fork=Homestead && node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain --fork=TangerineWhistle && node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain --fork=SpuriousDragon && node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain --fork=Byzantium && node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain --fork=Constantinople && node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain --fork=Petersburg && node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain --fork=Istanbul && node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain --fork=MuirGlacier",
"test:blockchain:allForks": "echo {'Homestead','TangerineWhistle','SpuriousDragon','Byzantium','Constantinople','Petersburg','Istanbul','MuirGlacier'} | xargs -n1 | xargs -I v1 node -r ts-node/register --stack-size=1500 ./tests/tester --blockchain --fork=v1",
"test:API": "tape -r ts-node/register --stack-size=1500 ./tests/api/**/*.js",
"test:API:browser": "npm run build && karma start karma.conf.js",
"test": "echo \"[INFO] Generic test cmd not used. See package.json for more specific test run cmds.\"",
Expand Down

0 comments on commit 582ce78

Please sign in to comment.