core/vm: removed gasTable global (review change)

This commit is contained in:
Jeffrey Wilcke 2016-12-23 11:00:02 +01:00
parent 85921bbb85
commit f02b4012c9
2 changed files with 54 additions and 120 deletions

View file

@ -33,15 +33,15 @@ func memoryGasCost(mem *Memory, newMemSize *big.Int) *big.Int {
return gas 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 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 { func gasCalldataCopy(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
gas := memoryGasCost(mem, memorySize) gas := memoryGasCost(mem, memorySize)
gas.Add(gas, gasTable[CALLDATACOPY]) gas.Add(gas, GasFastestStep)
words := toWordSize(stack.Back(2)) words := toWordSize(stack.Back(2))
return gas.Add(gas, words.Mul(words, params.CopyGas)) 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 { func gasSha3(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
gas := memoryGasCost(mem, memorySize) gas := memoryGasCost(mem, memorySize)
gas.Add(gas, gasTable[SHA3]) gas.Add(gas, params.Sha3Gas)
words := toWordSize(stack.Back(1)) words := toWordSize(stack.Back(1))
return gas.Add(gas, words.Mul(words, params.Sha3WordGas)) 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 { func gasCodeCopy(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
gas := memoryGasCost(mem, memorySize) gas := memoryGasCost(mem, memorySize)
gas.Add(gas, gasTable[CODECOPY]) gas.Add(gas, GasFastestStep)
words := toWordSize(stack.Back(2)) words := toWordSize(stack.Back(2))
return gas.Add(gas, words.Mul(words, params.CopyGas)) 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 { 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 { 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 { 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 { 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 { 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) expByteLen := int64((stack.data[stack.len()-2].BitLen() + 7) / 8)
gas := big.NewInt(expByteLen) gas := big.NewInt(expByteLen)
gas.Mul(gas, gt.ExpByte) 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 { 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 { 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 { 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 { func gasDup(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
return GasFastestStep 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,
}

View file

@ -51,43 +51,43 @@ type operation struct {
var jumpTable = [256]operation{ var jumpTable = [256]operation{
ADD: operation{ ADD: operation{
execute: opAdd, execute: opAdd,
gasCost: makeGenericGasFunc(ADD), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
SUB: operation{ SUB: operation{
execute: opSub, execute: opSub,
gasCost: makeGenericGasFunc(SUB), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
MUL: operation{ MUL: operation{
execute: opMul, execute: opMul,
gasCost: makeGenericGasFunc(MUL), gasCost: constGasFunc(GasFastStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
DIV: operation{ DIV: operation{
execute: opDiv, execute: opDiv,
gasCost: makeGenericGasFunc(DIV), gasCost: constGasFunc(GasFastStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
SDIV: operation{ SDIV: operation{
execute: opSdiv, execute: opSdiv,
gasCost: makeGenericGasFunc(SDIV), gasCost: constGasFunc(GasFastStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
MOD: operation{ MOD: operation{
execute: opMod, execute: opMod,
gasCost: makeGenericGasFunc(MOD), gasCost: constGasFunc(GasFastStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
SMOD: operation{ SMOD: operation{
execute: opSmod, execute: opSmod,
gasCost: makeGenericGasFunc(SMOD), gasCost: constGasFunc(GasFastStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
@ -99,85 +99,85 @@ var jumpTable = [256]operation{
}, },
SIGNEXTEND: operation{ SIGNEXTEND: operation{
execute: opSignExtend, execute: opSignExtend,
gasCost: makeGenericGasFunc(SIGNEXTEND), gasCost: constGasFunc(GasFastStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
NOT: operation{ NOT: operation{
execute: opNot, execute: opNot,
gasCost: makeGenericGasFunc(NOT), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(1, 1), validateStack: makeStackFunc(1, 1),
valid: true, valid: true,
}, },
LT: operation{ LT: operation{
execute: opLt, execute: opLt,
gasCost: makeGenericGasFunc(LT), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
GT: operation{ GT: operation{
execute: opGt, execute: opGt,
gasCost: makeGenericGasFunc(GT), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
SLT: operation{ SLT: operation{
execute: opSlt, execute: opSlt,
gasCost: makeGenericGasFunc(SLT), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
SGT: operation{ SGT: operation{
execute: opSgt, execute: opSgt,
gasCost: makeGenericGasFunc(SGT), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
EQ: operation{ EQ: operation{
execute: opEq, execute: opEq,
gasCost: makeGenericGasFunc(EQ), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
ISZERO: operation{ ISZERO: operation{
execute: opIszero, execute: opIszero,
gasCost: makeGenericGasFunc(ISZERO), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(1, 1), validateStack: makeStackFunc(1, 1),
valid: true, valid: true,
}, },
AND: operation{ AND: operation{
execute: opAnd, execute: opAnd,
gasCost: makeGenericGasFunc(AND), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
OR: operation{ OR: operation{
execute: opOr, execute: opOr,
gasCost: makeGenericGasFunc(OR), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
XOR: operation{ XOR: operation{
execute: opXor, execute: opXor,
gasCost: makeGenericGasFunc(XOR), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
BYTE: operation{ BYTE: operation{
execute: opByte, execute: opByte,
gasCost: makeGenericGasFunc(BYTE), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 1), validateStack: makeStackFunc(2, 1),
valid: true, valid: true,
}, },
ADDMOD: operation{ ADDMOD: operation{
execute: opAddmod, execute: opAddmod,
gasCost: makeGenericGasFunc(ADDMOD), gasCost: constGasFunc(GasMidStep),
validateStack: makeStackFunc(3, 1), validateStack: makeStackFunc(3, 1),
valid: true, valid: true,
}, },
MULMOD: operation{ MULMOD: operation{
execute: opMulmod, execute: opMulmod,
gasCost: makeGenericGasFunc(MULMOD), gasCost: constGasFunc(GasMidStep),
validateStack: makeStackFunc(3, 1), validateStack: makeStackFunc(3, 1),
valid: true, valid: true,
}, },
@ -190,7 +190,7 @@ var jumpTable = [256]operation{
}, },
ADDRESS: operation{ ADDRESS: operation{
execute: opAddress, execute: opAddress,
gasCost: makeGenericGasFunc(ADDRESS), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
@ -202,31 +202,31 @@ var jumpTable = [256]operation{
}, },
ORIGIN: operation{ ORIGIN: operation{
execute: opOrigin, execute: opOrigin,
gasCost: makeGenericGasFunc(ORIGIN), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
CALLER: operation{ CALLER: operation{
execute: opCaller, execute: opCaller,
gasCost: makeGenericGasFunc(CALLER), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
CALLVALUE: operation{ CALLVALUE: operation{
execute: opCallValue, execute: opCallValue,
gasCost: makeGenericGasFunc(CALLVALUE), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
CALLDATALOAD: operation{ CALLDATALOAD: operation{
execute: opCalldataLoad, execute: opCalldataLoad,
gasCost: makeGenericGasFunc(CALLDATALOAD), gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(1, 1), validateStack: makeStackFunc(1, 1),
valid: true, valid: true,
}, },
CALLDATASIZE: operation{ CALLDATASIZE: operation{
execute: opCalldataSize, execute: opCalldataSize,
gasCost: makeGenericGasFunc(CALLDATASIZE), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
@ -239,7 +239,7 @@ var jumpTable = [256]operation{
}, },
CODESIZE: operation{ CODESIZE: operation{
execute: opCodeSize, execute: opCodeSize,
gasCost: makeGenericGasFunc(CODESIZE), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
@ -265,49 +265,49 @@ var jumpTable = [256]operation{
}, },
GASPRICE: operation{ GASPRICE: operation{
execute: opGasprice, execute: opGasprice,
gasCost: makeGenericGasFunc(GASPRICE), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
BLOCKHASH: operation{ BLOCKHASH: operation{
execute: opBlockhash, execute: opBlockhash,
gasCost: makeGenericGasFunc(BLOCKHASH), gasCost: constGasFunc(GasExtStep),
validateStack: makeStackFunc(1, 1), validateStack: makeStackFunc(1, 1),
valid: true, valid: true,
}, },
COINBASE: operation{ COINBASE: operation{
execute: opCoinbase, execute: opCoinbase,
gasCost: makeGenericGasFunc(COINBASE), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
TIMESTAMP: operation{ TIMESTAMP: operation{
execute: opTimestamp, execute: opTimestamp,
gasCost: makeGenericGasFunc(TIMESTAMP), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
NUMBER: operation{ NUMBER: operation{
execute: opNumber, execute: opNumber,
gasCost: makeGenericGasFunc(NUMBER), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
DIFFICULTY: operation{ DIFFICULTY: operation{
execute: opDifficulty, execute: opDifficulty,
gasCost: makeGenericGasFunc(DIFFICULTY), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
GASLIMIT: operation{ GASLIMIT: operation{
execute: opGasLimit, execute: opGasLimit,
gasCost: makeGenericGasFunc(GASLIMIT), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
POP: operation{ POP: operation{
execute: opPop, execute: opPop,
gasCost: makeGenericGasFunc(TIMESTAMP), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(1, 0), validateStack: makeStackFunc(1, 0),
valid: true, valid: true,
}, },
@ -347,25 +347,25 @@ var jumpTable = [256]operation{
}, },
JUMPDEST: operation{ JUMPDEST: operation{
execute: opJumpdest, execute: opJumpdest,
gasCost: makeGenericGasFunc(JUMPDEST), gasCost: constGasFunc(params.JumpdestGas),
validateStack: makeStackFunc(0, 0), validateStack: makeStackFunc(0, 0),
valid: true, valid: true,
}, },
PC: operation{ PC: operation{
execute: opPc, execute: opPc,
gasCost: makeGenericGasFunc(PC), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
MSIZE: operation{ MSIZE: operation{
execute: opMsize, execute: opMsize,
gasCost: makeGenericGasFunc(MSIZE), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
GAS: operation{ GAS: operation{
execute: opGas, execute: opGas,
gasCost: makeGenericGasFunc(GAS), gasCost: constGasFunc(GasQuickStep),
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
@ -414,21 +414,21 @@ var jumpTable = [256]operation{
}, },
JUMP: operation{ JUMP: operation{
execute: opJump, execute: opJump,
gasCost: makeGenericGasFunc(JUMP), gasCost: constGasFunc(GasMidStep),
validateStack: makeStackFunc(1, 0), validateStack: makeStackFunc(1, 0),
jumps: true, jumps: true,
valid: true, valid: true,
}, },
JUMPI: operation{ JUMPI: operation{
execute: opJumpi, execute: opJumpi,
gasCost: makeGenericGasFunc(JUMPI), gasCost: constGasFunc(GasSlowStep),
validateStack: makeStackFunc(2, 0), validateStack: makeStackFunc(2, 0),
jumps: true, jumps: true,
valid: true, valid: true,
}, },
STOP: operation{ STOP: operation{
execute: opStop, execute: opStop,
gasCost: makeGenericGasFunc(STOP), gasCost: constGasFunc(Zero),
validateStack: makeStackFunc(0, 0), validateStack: makeStackFunc(0, 0),
halts: true, halts: true,
valid: true, valid: true,