core/vm, params: deprecate gastable, use both constant and dynamic gas

This commit is contained in:
Martin Holst Swende 2019-06-18 21:55:30 +02:00
parent 7ef08f1166
commit be2827213a
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
4 changed files with 109 additions and 140 deletions

View file

@ -59,11 +59,6 @@ func gasCallDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *St
return 0, err
}
var overflow bool
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow
}
words, overflow := bigUint64(stack.Back(2))
if overflow {
return 0, errGasUintOverflow
@ -85,11 +80,6 @@ func gasReturnDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *
return 0, err
}
var overflow bool
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow
}
words, overflow := bigUint64(stack.Back(2))
if overflow {
return 0, errGasUintOverflow
@ -211,10 +201,6 @@ func gasSha3(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem
return 0, err
}
if gas, overflow = math.SafeAdd(gas, params.Sha3Gas); overflow {
return 0, errGasUintOverflow
}
wordGas, overflow := bigUint64(stack.Back(1))
if overflow {
return 0, errGasUintOverflow
@ -235,10 +221,6 @@ func gasCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
}
var overflow bool
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow
}
wordGas, overflow := bigUint64(stack.Back(2))
if overflow {
return 0, errGasUintOverflow
@ -259,9 +241,6 @@ func gasExtCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Sta
}
var overflow bool
if gas, overflow = math.SafeAdd(gas, gt.ExtcodeCopy); overflow {
return 0, errGasUintOverflow
}
wordGas, overflow := bigUint64(stack.Back(3))
if overflow {
@ -279,62 +258,26 @@ func gasExtCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Sta
}
func gasMLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, errGasUintOverflow
}
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow
}
return gas, nil
return memoryGasCost(mem, memorySize)
}
func gasMStore8(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, errGasUintOverflow
}
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow
}
return gas, nil
return memoryGasCost(mem, memorySize)
}
func gasMStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, errGasUintOverflow
}
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow
}
return gas, nil
return memoryGasCost(mem, memorySize)
}
func gasCreate(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
if gas, overflow = math.SafeAdd(gas, params.CreateGas); overflow {
return 0, errGasUintOverflow
}
return gas, nil
return memoryGasCost(mem, memorySize)
}
func gasCreate2(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
if gas, overflow = math.SafeAdd(gas, params.Create2Gas); overflow {
return 0, errGasUintOverflow
}
wordGas, overflow := bigUint64(stack.Back(2))
if overflow {
return 0, errGasUintOverflow
@ -349,11 +292,24 @@ func gasCreate2(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
return gas, nil
}
func gasExp(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
func gasExpFrontier(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
var (
gas = expByteLen * gt.ExpByte // no overflow check required. Max is 256 * ExpByte gas
gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas
overflow bool
)
if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {
return 0, errGasUintOverflow
}
return gas, nil
}
func gasExpEip158(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
var (
gas = expByteLen * params.ExpByteEip158 // no overflow check required. Max is 256 * ExpByte gas
overflow bool
)
if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {

View file

@ -97,6 +97,7 @@ func newConstantinopleInstructionSet() [256]operation {
}
instructionSet[CREATE2] = operation{
execute: opCreate2,
constantGas: params.Create2Gas,
dynamicGas: gasCreate2,
minStack: minStack(4, 1),
maxStack: maxStack(4, 1),
@ -131,6 +132,7 @@ func newByzantiumInstructionSet() [256]operation {
}
instructionSet[RETURNDATACOPY] = operation{
execute: opReturnDataCopy,
constantGas: GasFastestStep,
dynamicGas: gasReturnDataCopy,
minStack: minStack(3, 0),
maxStack: maxStack(3, 0),
@ -152,6 +154,7 @@ func newByzantiumInstructionSet() [256]operation {
func newSpuriousDragonInstructionSet() [256]operation {
instructionSet := newTangerineWhistleInstructionSet()
instructionSet[EXP].dynamicGas = gasExpEip158
return instructionSet
}
@ -161,6 +164,7 @@ func newTangerineWhistleInstructionSet() [256]operation {
instructionSet[BALANCE].constantGas = params.BalanceGasEip150
instructionSet[EXTCODESIZE].constantGas = params.ExtcodeSizeGasEip150
instructionSet[SLOAD].constantGas = params.SloadGasEip150
instructionSet[EXTCODECOPY].constantGas = params.ExtcodeCopyBaseEip150
return instructionSet
}
@ -257,7 +261,7 @@ func newFrontierInstructionSet() [256]operation {
},
EXP: {
execute: opExp,
dynamicGas: gasExp,
dynamicGas: gasExpFrontier,
minStack: minStack(2, 1),
maxStack: maxStack(2, 1),
valid: true,
@ -348,6 +352,7 @@ func newFrontierInstructionSet() [256]operation {
},
SHA3: {
execute: opSha3,
constantGas: params.Sha3Gas,
dynamicGas: gasSha3,
minStack: minStack(2, 1),
maxStack: maxStack(2, 1),
@ -405,6 +410,7 @@ func newFrontierInstructionSet() [256]operation {
},
CALLDATACOPY: {
execute: opCallDataCopy,
constantGas: GasFastestStep,
dynamicGas: gasCallDataCopy,
minStack: minStack(3, 0),
maxStack: maxStack(3, 0),
@ -420,6 +426,7 @@ func newFrontierInstructionSet() [256]operation {
},
CODECOPY: {
execute: opCodeCopy,
constantGas: GasFastestStep,
dynamicGas: gasCodeCopy,
minStack: minStack(3, 0),
maxStack: maxStack(3, 0),
@ -442,6 +449,7 @@ func newFrontierInstructionSet() [256]operation {
},
EXTCODECOPY: {
execute: opExtCodeCopy,
constantGas: params.ExtcodeCopyBaseFrontier,
dynamicGas: gasExtCodeCopy,
minStack: minStack(4, 0),
maxStack: maxStack(4, 0),
@ -499,6 +507,7 @@ func newFrontierInstructionSet() [256]operation {
},
MLOAD: {
execute: opMload,
constantGas: GasFastestStep,
dynamicGas: gasMLoad,
minStack: minStack(1, 1),
maxStack: maxStack(1, 1),
@ -507,6 +516,7 @@ func newFrontierInstructionSet() [256]operation {
},
MSTORE: {
execute: opMstore,
constantGas: GasFastestStep,
dynamicGas: gasMStore,
minStack: minStack(2, 0),
maxStack: maxStack(2, 0),
@ -515,6 +525,7 @@ func newFrontierInstructionSet() [256]operation {
},
MSTORE8: {
execute: opMstore8,
constantGas: GasFastestStep,
dynamicGas: gasMStore8,
memorySize: memoryMStore8,
minStack: minStack(2, 0),
@ -1076,6 +1087,7 @@ func newFrontierInstructionSet() [256]operation {
},
CREATE: {
execute: opCreate,
constantGas: params.CreateGas,
dynamicGas: gasCreate,
minStack: minStack(3, 1),
maxStack: maxStack(3, 1),

View file

@ -18,10 +18,7 @@ package params
// GasTable organizes gas prices for different ethereum phases.
type GasTable struct {
ExtcodeCopy uint64
Calls uint64
ExpByte uint64
}
// Variables containing gas prices for different ethereum phases.
@ -29,23 +26,17 @@ var (
// GasTableHomestead contain the gas prices for
// the homestead phase.
GasTableHomestead = GasTable{
ExtcodeCopy: 20,
Calls: 40,
ExpByte: 10,
}
// GasTableEIP150 contain the gas re-prices for
// the EIP150 phase (a.k.a TangerineWhistle).
GasTableEIP150 = GasTable{
ExtcodeCopy: 700,
Calls: 700,
ExpByte: 10,
}
// GasTableEIP158 contain the gas re-prices for
// the EIP155/EIP158 phase (a.k.a Spurious Dragon).
GasTableEIP158 = GasTable{
ExtcodeCopy: 700,
Calls: 700,
ExpByte: 50,
}
)

View file

@ -78,6 +78,16 @@ const (
SloadGasEip150 uint64 = 200
ExtcodeHashGas uint64 = 400 // Cost of EXTCODEHASH (introduced in Constantinople)
SuicideGasEip150 uint64 = 5000 // Cost of SELFDESTRUCT post T.W
// EXP has a dynamic portion depending on the size of the exponent
ExpByteFrontier uint64 = 10 // was set to 10 in Frontier
ExpByteEip158 uint64 = 50 // was raised to 50 during Eip158 (Spurious Dragon)
// Extcodecopy has a dynamic AND a static cost. This represents only the
// static portion of the gas. It was changed during EIP 150 (Tangerine Whistle)
ExtcodeCopyBaseFrontier uint64 = 20
ExtcodeCopyBaseEip150 uint64 = 700
// CreateBySuicide occurs when the refunded account is one that does
// not exist. This logic is similar to call.
// Introduced in Tangerine Whistle (Eip 150)