Skip to content

Commit

Permalink
add typedoc for peek
Browse files Browse the repository at this point in the history
use underscore for unused peek params (and fix eslint config)
format some comment newlines for readability
switch from require to import for exceptions
  • Loading branch information
ryanio committed Jul 20, 2021
1 parent a9e58b3 commit b6cd21c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/vm/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ module.exports = {
rules: {
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-unnecessary-condition': 'off',
'@typescript-eslint/no-unused-vars': ["error", { argsIgnorePattern: "^_", varsIgnorePattern: "^_" }],
'no-unused-vars': ["error", { argsIgnorePattern: "^_", varsIgnorePattern: "^_" }],
'no-invalid-this': 'off',
'no-restricted-syntax': 'off',
'no-unused-vars': ["error", { "argsIgnorePattern": "^_" }]
},
overrides: [
{
Expand Down
5 changes: 3 additions & 2 deletions packages/vm/src/evm/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,13 @@ export default class Interpreter {
const opInfo = this.lookupOpInfo(this._runState.opCode)

const gas = new BN(opInfo.fee)
// clone the gas limit; call opcodes can add stipend, which makes it seem like the gas left increases
// clone the gas limit; call opcodes can add stipend,
// which makes it seem like the gas left increases
const gasLimitClone = this._eei.getGasLeft()

if (opInfo.dynamicGas) {
const dynamicGasHandler = dynamicGasHandlers.get(this._runState.opCode)!
// This function updates the gas BN by using `i*` methods
// This function updates the gas BN in-place using `i*` methods
// It needs the base fee, for correct gas limit calculation for the CALL opcodes
await dynamicGasHandler(this._runState, gas)
}
Expand Down
25 changes: 15 additions & 10 deletions packages/vm/src/evm/opcodes/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
/* CALLDATACOPY */
0x37,
async function (runState: RunState, gas: BN): Promise<void> {
const [memOffset /*dataOffset*/, , dataLength] = runState.stack.peek(3)
const [memOffset, _dataOffset, dataLength] = runState.stack.peek(3)

gas.iadd(subMemUsage(runState, memOffset, dataLength))
if (!dataLength.eqn(0)) {
Expand All @@ -67,7 +67,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
/* CODECOPY */
0x39,
async function (runState: RunState, gas: BN): Promise<void> {
const [memOffset /*codeOffset*/, , dataLength] = runState.stack.peek(3)
const [memOffset, _codeOffset, dataLength] = runState.stack.peek(3)

gas.iadd(subMemUsage(runState, memOffset, dataLength))
if (!dataLength.eqn(0)) {
Expand All @@ -90,7 +90,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
/* EXTCODECOPY */
0x3c,
async function (runState: RunState, gas: BN): Promise<void> {
const [addressBN, memOffset /*codeOffset*/, , dataLength] = runState.stack.peek(4)
const [addressBN, memOffset, _codeOffset, dataLength] = runState.stack.peek(4)

gas.iadd(subMemUsage(runState, memOffset, dataLength))
const address = new Address(addressToBuffer(addressBN))
Expand Down Expand Up @@ -194,7 +194,8 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
}

// We have to do this after the Istanbul (EIP2200) checks.
// Otherwise, we might run out of gas, due to "sentry check" of 2300 gas, if we deduct extra gas first.
// Otherwise, we might run out of gas, due to "sentry check" of 2300 gas,
// if we deduct extra gas first.
gas.iadd(accessStorageEIP2929(runState, keyBuf, true))
},
],
Expand Down Expand Up @@ -229,7 +230,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
if (runState.eei.isStatic()) {
trap(ERROR.STATIC_STATE_CHANGE)
}
const [, /*value*/ offset, length] = runState.stack.peek(3)
const [_value, offset, length] = runState.stack.peek(3)

gas.iadd(accessAddressEIP2929(runState, runState.eei.getAddress(), false))

Expand Down Expand Up @@ -268,7 +269,8 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
}
} else if (!(await runState.eei.accountExists(toAddress))) {
// We are before Spurious Dragon and the account does not exist.
// Call new account gas: account does not exist (it is not in the state trie, not even as an "empty" account)
// Call new account gas: account does not exist
// (it is not in the state trie, not even as an "empty" account)
gas.iadd(new BN(runState._common.param('gasPrices', 'callNewAccount')))
}

Expand All @@ -277,7 +279,8 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
runState.eei.getGasLeft().isub(gas),
runState
)
// note that TangerineWhistle or later this cannot happen (it could have ran out of gas prior to getting here though)
// note that TangerineWhistle or later this cannot happen
// (it could have ran out of gas prior to getting here though)
if (gasLimit.gt(runState.eei.getGasLeft().isub(gas))) {
trap(ERROR.OUT_OF_GAS)
}
Expand Down Expand Up @@ -315,7 +318,8 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
runState.eei.getGasLeft().isub(gas),
runState
)
// note that TangerineWhistle or later this cannot happen (it could have ran out of gas prior to getting here though)
// note that TangerineWhistle or later this cannot happen
// (it could have ran out of gas prior to getting here though)
if (gasLimit.gt(runState.eei.getGasLeft().isub(gas))) {
trap(ERROR.OUT_OF_GAS)
}
Expand Down Expand Up @@ -355,7 +359,8 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
runState.eei.getGasLeft().isub(gas),
runState
)
// note that TangerineWhistle or later this cannot happen (it could have ran out of gas prior to getting here though)
// note that TangerineWhistle or later this cannot happen
// (it could have ran out of gas prior to getting here though)
if (gasLimit.gt(runState.eei.getGasLeft().isub(gas))) {
trap(ERROR.OUT_OF_GAS)
}
Expand All @@ -371,7 +376,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler> = new Map([
trap(ERROR.STATIC_STATE_CHANGE)
}

const [, /*value*/ offset, length /*salt*/] = runState.stack.peek(4)
const [_value, offset, length, _salt] = runState.stack.peek(4)

gas.iadd(subMemUsage(runState, offset, length))
gas.iadd(accessAddressEIP2929(runState, runState.eei.getAddress(), false))
Expand Down
7 changes: 6 additions & 1 deletion packages/vm/src/evm/stack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BN, MAX_INTEGER } from 'ethereumjs-util'
const { ERROR, VmError } = require('../exceptions')
import { ERROR, VmError } from '../exceptions'

/**
* Implementation of the stack used in evm.
Expand Down Expand Up @@ -59,6 +59,11 @@ export default class Stack {
return this._store.splice(-1 * num).reverse()
}

/**
* Return items from the stack
* @param num Number of items to return
* @throws {@link ERROR.STACK_UNDERFLOW}
*/
peek(num: number = 1): BN[] {
const peekArray: BN[] = []

Expand Down

0 comments on commit b6cd21c

Please sign in to comment.