fix CREATE opcode gas cost

This commit is contained in:
andri lim 2019-03-15 22:15:40 +07:00
parent 823dd95a53
commit 99d128ca26
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 25 additions and 12 deletions

View File

@ -175,8 +175,8 @@ proc writeContract*(computation: var BaseComputation, fork: Fork): bool =
let storageAddr = computation.msg.storageAddress
if computation.isSuicided(storageAddr): return
# tricky gasCost: 1,0,0 -> createCost. 0,0,x -> depositCost
let codeCost = computation.gasCosts[Create].m_handler(0, 0, contractCode.len)
let gasParams = GasParams(kind: Create, cr_memLength: contractCode.len)
let codeCost = computation.gasCosts[Create].c_handler(0.u256, gasParams).gasCost
if computation.gasMeter.gasRemaining >= codeCost:
computation.gasMeter.consumeGas(codeCost, reason = "Write contract code for CREATE")
computation.vmState.mutateStateDb:

View File

@ -72,6 +72,10 @@ type
c_memOffset*: Natural
c_memLength*: Natural
c_opCode*: Op
of Create:
cr_currentMemSize*: Natural
cr_memOffset*: Natural
cr_memLength*: Natural
else:
discard
@ -169,10 +173,15 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
if not value.isZero:
result += static(FeeSchedule[GasExpByte]) * (1 + log256(value))
func `prefix gasCreate`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} =
# tricky gasCost: 1,0,0 -> createCost. 0,0,x -> depositCost
result = currentMemSize * static(FeeSchedule[GasCreate]) +
static(FeeSchedule[GasCodeDeposit]) * memLength
func `prefix gasCreate`(value: Uint256, gasParams: GasParams): GasResult {.nimcall.} =
if value.isZero:
result.gasCost = static(FeeSchedule[GasCodeDeposit]) * gasParams.cr_memLength
else:
result.gasCost = static(FeeSchedule[GasCreate]) +
`prefix gasMemoryExpansion`(
gasParams.cr_currentMemSize,
gasParams.cr_memOffset,
gasParams.cr_memLength)
func `prefix gasSha3`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} =
@ -509,7 +518,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
Log4: memExpansion `prefix gasLog4`,
# f0s: System operations
Create: memExpansion `prefix gasCreate`, # TODO: Change to dynamic?
Create: complex `prefix gasCreate`,
Call: complex `prefix gasCall`,
CallCode: complex `prefix gasCall`,
Return: memExpansion `prefix gasHalt`,

View File

@ -506,13 +506,17 @@ genLog()
# ##########################################
# f0s: System operations.
proc canTransfer(computation: BaseComputation, memPos, len: int, value: Uint256): bool =
# tricky gasCost: 1,0,0 -> createCost. 0,0,x -> depositCost
let gasCost = computation.gasCosts[Create].m_handler(1, 0, 0)
let reason = &"CREATE: GasCreate + {len} * memory expansion"
proc canTransfer(computation: BaseComputation, memPos, memLen: int, value: Uint256): bool =
let gasParams = GasParams(kind: Create,
cr_currentMemSize: computation.memory.len,
cr_memOffset: memPos,
cr_memLength: memLen
)
let gasCost = computation.gasCosts[Create].c_handler(1.u256, gasParams).gasCost
let reason = &"CREATE: GasCreate + {memLen} * memory expansion"
computation.gasMeter.consumeGas(gasCost, reason = reason)
computation.memory.extend(memPos, len)
computation.memory.extend(memPos, memLen)
# the sender is childmsg sender, not parent msg sender
# perhaps we need to move this code somewhere else