From f02b4012c945c71229c4ecfbe2d8d0e03c992b46 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Fri, 23 Dec 2016 11:00:02 +0100 Subject: [PATCH] core/vm: removed gasTable global (review change) --- core/vm/gas_table.go | 88 ++++++------------------------------------- core/vm/jump_table.go | 86 +++++++++++++++++++++--------------------- 2 files changed, 54 insertions(+), 120 deletions(-) diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index b57bb9cc80..4d2c4e94ae 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -33,15 +33,15 @@ func memoryGasCost(mem *Memory, newMemSize *big.Int) *big.Int { return gas } -func makeGenericGasFunc(op OpCode) gasFunc { +func constGasFunc(gas *big.Int) gasFunc { return func(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { - return gasTable[op] + return gas } } func gasCalldataCopy(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { gas := memoryGasCost(mem, memorySize) - gas.Add(gas, gasTable[CALLDATACOPY]) + gas.Add(gas, GasFastestStep) words := toWordSize(stack.Back(2)) return gas.Add(gas, words.Mul(words, params.CopyGas)) @@ -82,14 +82,14 @@ func makeGasLog(n uint) gasFunc { func gasSha3(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { gas := memoryGasCost(mem, memorySize) - gas.Add(gas, gasTable[SHA3]) + gas.Add(gas, params.Sha3Gas) words := toWordSize(stack.Back(1)) return gas.Add(gas, words.Mul(words, params.Sha3WordGas)) } func gasCodeCopy(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { gas := memoryGasCost(mem, memorySize) - gas.Add(gas, gasTable[CODECOPY]) + gas.Add(gas, GasFastestStep) words := toWordSize(stack.Back(2)) return gas.Add(gas, words.Mul(words, params.CopyGas)) @@ -104,19 +104,19 @@ func gasExtCodeCopy(gt params.GasTable, env *EVM, contract *Contract, stack *Sta } func gasMLoad(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { - return new(big.Int).Add(gasTable[MLOAD], memoryGasCost(mem, memorySize)) + return new(big.Int).Add(GasFastestStep, memoryGasCost(mem, memorySize)) } func gasMStore8(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { - return new(big.Int).Add(gasTable[MSTORE8], memoryGasCost(mem, memorySize)) + return new(big.Int).Add(GasFastestStep, memoryGasCost(mem, memorySize)) } func gasMStore(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { - return new(big.Int).Add(gasTable[MSTORE], memoryGasCost(mem, memorySize)) + return new(big.Int).Add(GasFastestStep, memoryGasCost(mem, memorySize)) } func gasCreate(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { - return new(big.Int).Add(gasTable[CREATE], memoryGasCost(mem, memorySize)) + return new(big.Int).Add(params.CreateGas, memoryGasCost(mem, memorySize)) } func gasBalance(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { @@ -135,7 +135,7 @@ func gasExp(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem expByteLen := int64((stack.data[stack.len()-2].BitLen() + 7) / 8) gas := big.NewInt(expByteLen) gas.Mul(gas, gt.ExpByte) - return gas.Add(gas, gasTable[EXP]) + return gas.Add(gas, GasSlowStep) } func gasCall(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { @@ -190,7 +190,7 @@ func gasCallCode(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, } func gasReturn(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { - return new(big.Int).Add(gasTable[RETURN], memoryGasCost(mem, memorySize)) + return memoryGasCost(mem, memorySize) } func gasSuicide(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { @@ -244,69 +244,3 @@ func gasSwap(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem func gasDup(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int { return GasFastestStep } - -var gasTable = [256]*big.Int{ - ADD: GasFastestStep, - LT: GasFastestStep, - GT: GasFastestStep, - SLT: GasFastestStep, - SGT: GasFastestStep, - EQ: GasFastestStep, - ISZERO: GasFastestStep, - SUB: GasFastestStep, - AND: GasFastestStep, - OR: GasFastestStep, - XOR: GasFastestStep, - NOT: GasFastestStep, - BYTE: GasFastestStep, - CALLDATALOAD: GasFastestStep, - CALLDATACOPY: GasFastestStep, - MLOAD: GasFastestStep, - MSTORE: GasFastestStep, - MSTORE8: GasFastestStep, - CODECOPY: GasFastestStep, - MUL: GasFastStep, - DIV: GasFastStep, - SDIV: GasFastStep, - MOD: GasFastStep, - SMOD: GasFastStep, - SIGNEXTEND: GasFastStep, - ADDMOD: GasMidStep, - MULMOD: GasMidStep, - JUMP: GasMidStep, - JUMPI: GasSlowStep, - EXP: GasSlowStep, - ADDRESS: GasQuickStep, - ORIGIN: GasQuickStep, - CALLER: GasQuickStep, - CALLVALUE: GasQuickStep, - CODESIZE: GasQuickStep, - GASPRICE: GasQuickStep, - COINBASE: GasQuickStep, - TIMESTAMP: GasQuickStep, - NUMBER: GasQuickStep, - CALLDATASIZE: GasQuickStep, - DIFFICULTY: GasQuickStep, - GASLIMIT: GasQuickStep, - POP: GasQuickStep, - PC: GasQuickStep, - MSIZE: GasQuickStep, - GAS: GasQuickStep, - BLOCKHASH: GasExtStep, - BALANCE: Zero, - EXTCODESIZE: Zero, - EXTCODECOPY: Zero, - SLOAD: params.SloadGas, - SSTORE: Zero, - SHA3: params.Sha3Gas, - CREATE: params.CreateGas, - CALL: Zero, - CALLCODE: Zero, - DELEGATECALL: Zero, - SUICIDE: Zero, - JUMPDEST: params.JumpdestGas, - RETURN: Zero, - PUSH1: GasFastestStep, - DUP1: Zero, - STOP: Zero, -} diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index 8e49e9b64b..ca15261a61 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -51,43 +51,43 @@ type operation struct { var jumpTable = [256]operation{ ADD: operation{ execute: opAdd, - gasCost: makeGenericGasFunc(ADD), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, SUB: operation{ execute: opSub, - gasCost: makeGenericGasFunc(SUB), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, MUL: operation{ execute: opMul, - gasCost: makeGenericGasFunc(MUL), + gasCost: constGasFunc(GasFastStep), validateStack: makeStackFunc(2, 1), valid: true, }, DIV: operation{ execute: opDiv, - gasCost: makeGenericGasFunc(DIV), + gasCost: constGasFunc(GasFastStep), validateStack: makeStackFunc(2, 1), valid: true, }, SDIV: operation{ execute: opSdiv, - gasCost: makeGenericGasFunc(SDIV), + gasCost: constGasFunc(GasFastStep), validateStack: makeStackFunc(2, 1), valid: true, }, MOD: operation{ execute: opMod, - gasCost: makeGenericGasFunc(MOD), + gasCost: constGasFunc(GasFastStep), validateStack: makeStackFunc(2, 1), valid: true, }, SMOD: operation{ execute: opSmod, - gasCost: makeGenericGasFunc(SMOD), + gasCost: constGasFunc(GasFastStep), validateStack: makeStackFunc(2, 1), valid: true, }, @@ -99,85 +99,85 @@ var jumpTable = [256]operation{ }, SIGNEXTEND: operation{ execute: opSignExtend, - gasCost: makeGenericGasFunc(SIGNEXTEND), + gasCost: constGasFunc(GasFastStep), validateStack: makeStackFunc(2, 1), valid: true, }, NOT: operation{ execute: opNot, - gasCost: makeGenericGasFunc(NOT), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(1, 1), valid: true, }, LT: operation{ execute: opLt, - gasCost: makeGenericGasFunc(LT), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, GT: operation{ execute: opGt, - gasCost: makeGenericGasFunc(GT), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, SLT: operation{ execute: opSlt, - gasCost: makeGenericGasFunc(SLT), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, SGT: operation{ execute: opSgt, - gasCost: makeGenericGasFunc(SGT), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, EQ: operation{ execute: opEq, - gasCost: makeGenericGasFunc(EQ), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, ISZERO: operation{ execute: opIszero, - gasCost: makeGenericGasFunc(ISZERO), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(1, 1), valid: true, }, AND: operation{ execute: opAnd, - gasCost: makeGenericGasFunc(AND), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, OR: operation{ execute: opOr, - gasCost: makeGenericGasFunc(OR), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, XOR: operation{ execute: opXor, - gasCost: makeGenericGasFunc(XOR), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, BYTE: operation{ execute: opByte, - gasCost: makeGenericGasFunc(BYTE), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(2, 1), valid: true, }, ADDMOD: operation{ execute: opAddmod, - gasCost: makeGenericGasFunc(ADDMOD), + gasCost: constGasFunc(GasMidStep), validateStack: makeStackFunc(3, 1), valid: true, }, MULMOD: operation{ execute: opMulmod, - gasCost: makeGenericGasFunc(MULMOD), + gasCost: constGasFunc(GasMidStep), validateStack: makeStackFunc(3, 1), valid: true, }, @@ -190,7 +190,7 @@ var jumpTable = [256]operation{ }, ADDRESS: operation{ execute: opAddress, - gasCost: makeGenericGasFunc(ADDRESS), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, @@ -202,31 +202,31 @@ var jumpTable = [256]operation{ }, ORIGIN: operation{ execute: opOrigin, - gasCost: makeGenericGasFunc(ORIGIN), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, CALLER: operation{ execute: opCaller, - gasCost: makeGenericGasFunc(CALLER), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, CALLVALUE: operation{ execute: opCallValue, - gasCost: makeGenericGasFunc(CALLVALUE), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, CALLDATALOAD: operation{ execute: opCalldataLoad, - gasCost: makeGenericGasFunc(CALLDATALOAD), + gasCost: constGasFunc(GasFastestStep), validateStack: makeStackFunc(1, 1), valid: true, }, CALLDATASIZE: operation{ execute: opCalldataSize, - gasCost: makeGenericGasFunc(CALLDATASIZE), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, @@ -239,7 +239,7 @@ var jumpTable = [256]operation{ }, CODESIZE: operation{ execute: opCodeSize, - gasCost: makeGenericGasFunc(CODESIZE), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, @@ -265,49 +265,49 @@ var jumpTable = [256]operation{ }, GASPRICE: operation{ execute: opGasprice, - gasCost: makeGenericGasFunc(GASPRICE), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, BLOCKHASH: operation{ execute: opBlockhash, - gasCost: makeGenericGasFunc(BLOCKHASH), + gasCost: constGasFunc(GasExtStep), validateStack: makeStackFunc(1, 1), valid: true, }, COINBASE: operation{ execute: opCoinbase, - gasCost: makeGenericGasFunc(COINBASE), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, TIMESTAMP: operation{ execute: opTimestamp, - gasCost: makeGenericGasFunc(TIMESTAMP), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, NUMBER: operation{ execute: opNumber, - gasCost: makeGenericGasFunc(NUMBER), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, DIFFICULTY: operation{ execute: opDifficulty, - gasCost: makeGenericGasFunc(DIFFICULTY), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, GASLIMIT: operation{ execute: opGasLimit, - gasCost: makeGenericGasFunc(GASLIMIT), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, POP: operation{ execute: opPop, - gasCost: makeGenericGasFunc(TIMESTAMP), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(1, 0), valid: true, }, @@ -347,25 +347,25 @@ var jumpTable = [256]operation{ }, JUMPDEST: operation{ execute: opJumpdest, - gasCost: makeGenericGasFunc(JUMPDEST), + gasCost: constGasFunc(params.JumpdestGas), validateStack: makeStackFunc(0, 0), valid: true, }, PC: operation{ execute: opPc, - gasCost: makeGenericGasFunc(PC), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, MSIZE: operation{ execute: opMsize, - gasCost: makeGenericGasFunc(MSIZE), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, GAS: operation{ execute: opGas, - gasCost: makeGenericGasFunc(GAS), + gasCost: constGasFunc(GasQuickStep), validateStack: makeStackFunc(0, 1), valid: true, }, @@ -414,21 +414,21 @@ var jumpTable = [256]operation{ }, JUMP: operation{ execute: opJump, - gasCost: makeGenericGasFunc(JUMP), + gasCost: constGasFunc(GasMidStep), validateStack: makeStackFunc(1, 0), jumps: true, valid: true, }, JUMPI: operation{ execute: opJumpi, - gasCost: makeGenericGasFunc(JUMPI), + gasCost: constGasFunc(GasSlowStep), validateStack: makeStackFunc(2, 0), jumps: true, valid: true, }, STOP: operation{ execute: opStop, - gasCost: makeGenericGasFunc(STOP), + gasCost: constGasFunc(Zero), validateStack: makeStackFunc(0, 0), halts: true, valid: true,