refactor recipient transfer

This commit is contained in:
andri lim 2019-02-27 12:09:46 +07:00 committed by zah
parent 86de4faa81
commit 26d8ab84fe
3 changed files with 8 additions and 15 deletions

View File

@ -18,9 +18,6 @@ proc contractCall(tx: Transaction, vmState: BaseVMState, sender: EthAddress, for
let storageRoot = db.getStorageRoot(tx.to) let storageRoot = db.getStorageRoot(tx.to)
var computation = setupComputation(vmState, tx, sender, forkOverride) var computation = setupComputation(vmState, tx, sender, forkOverride)
# contract creation transaction.to == 0, so ensure happens after
db.addBalance(tx.to, tx.value)
if execComputation(computation): if execComputation(computation):
let let
gasRemaining = computation.gasMeter.gasRemaining gasRemaining = computation.gasMeter.gasRemaining
@ -33,7 +30,6 @@ proc contractCall(tx: Transaction, vmState: BaseVMState, sender: EthAddress, for
return (tx.gasLimit - gasRemaining - gasRefund) return (tx.gasLimit - gasRemaining - gasRefund)
else: else:
db.subBalance(tx.to, tx.value)
db.addBalance(sender, tx.value) db.addBalance(sender, tx.value)
db.setStorageRoot(tx.to, storageRoot) db.setStorageRoot(tx.to, storageRoot)
if computation.tracingEnabled: computation.traceError() if computation.tracingEnabled: computation.traceError()

View File

@ -16,13 +16,13 @@ proc validateTransaction*(vmState: BaseVMState, transaction: Transaction, sender
# XXX: lots of avoidable u256 construction # XXX: lots of avoidable u256 construction
var readOnlyDB = vmState.readOnlyStateDB var readOnlyDB = vmState.readOnlyStateDB
let limitAndValue = transaction.gasLimit.u256 + transaction.value let limitAndValue = transaction.gasLimit.u256 + transaction.value
let gas_cost = transaction.gasLimit.u256 * transaction.gasPrice.u256 let gasCost = transaction.gasLimit.u256 * transaction.gasPrice.u256
transaction.gasLimit >= transaction.payload.intrinsicGas and transaction.gasLimit >= transaction.intrinsicGas and
transaction.gasPrice <= (1 shl 34) and transaction.gasPrice <= (1 shl 34) and
limitAndValue <= readOnlyDB.getBalance(sender) and limitAndValue <= readOnlyDB.getBalance(sender) and
transaction.accountNonce == readOnlyDB.getNonce(sender) and transaction.accountNonce == readOnlyDB.getNonce(sender) and
readOnlyDB.getBalance(sender) >= gas_cost readOnlyDB.getBalance(sender) >= gasCost
proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender: EthAddress, forkOverride=none(Fork)) : BaseComputation = proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender: EthAddress, forkOverride=none(Fork)) : BaseComputation =
let fork = let fork =
@ -66,6 +66,9 @@ proc execComputation*(computation: var BaseComputation): bool =
var snapshot = computation.snapshot() var snapshot = computation.snapshot()
defer: snapshot.dispose() defer: snapshot.dispose()
computation.vmState.mutateStateDB:
db.addBalance(computation.msg.storageAddress, computation.msg.value)
try: try:
computation.executeOpcodes() computation.executeOpcodes()
computation.vmState.mutateStateDB: computation.vmState.mutateStateDB:
@ -74,6 +77,7 @@ proc execComputation*(computation: var BaseComputation): bool =
result = not computation.isError result = not computation.isError
except ValueError: except ValueError:
result = false result = false
debug "execComputation() error", msg = getCurrentExceptionMsg()
if result: if result:
snapshot.commit() snapshot.commit()
@ -86,7 +90,6 @@ proc applyCreateTransaction*(tx: Transaction, vmState: BaseVMState, sender: EthA
trace "Contract creation" trace "Contract creation"
var c = setupComputation(vmState, tx, sender, forkOverride) var c = setupComputation(vmState, tx, sender, forkOverride)
let contractAddress = c.msg.storageAddress
if execComputation(c): if execComputation(c):
var db = vmState.accountDb var db = vmState.accountDb
@ -107,9 +110,9 @@ proc applyCreateTransaction*(tx: Transaction, vmState: BaseVMState, sender: EthA
# for purposes of accounting. Py-EVM apparently does consume the gas, but it is # for purposes of accounting. Py-EVM apparently does consume the gas, but it is
# not matching observed blockchain balances if consumeGas is called. # not matching observed blockchain balances if consumeGas is called.
let contractAddress = c.msg.storageAddress
if not c.isSuicided(contractAddress): if not c.isSuicided(contractAddress):
# make changes only if it not selfdestructed # make changes only if it not selfdestructed
db.addBalance(contractAddress, tx.value)
if gasRemaining >= codeCost: if gasRemaining >= codeCost:
db.setCode(contractAddress, c.output.toRange) db.setCode(contractAddress, c.output.toRange)
else: else:
@ -126,7 +129,6 @@ proc applyCreateTransaction*(tx: Transaction, vmState: BaseVMState, sender: EthA
# especially when it's cross-function, it's ugly/fragile # especially when it's cross-function, it's ugly/fragile
var db = vmState.accountDb var db = vmState.accountDb
db.addBalance(sender, tx.value) db.addBalance(sender, tx.value)
debug "execComputation() error", isError = c.isError
if c.tracingEnabled: if c.tracingEnabled:
c.traceError() c.traceError()
vmState.clearLogs() vmState.clearLogs()

View File

@ -64,10 +64,6 @@ proc testFixtureIndexes(prevStateRoot: Hash256, header: BlockHeader, pre: JsonNo
return return
var computation = setupComputation(vmState, transaction, sender, some(fork)) var computation = setupComputation(vmState, transaction, sender, some(fork))
vmState.mutateStateDB:
# contract creation transaction.to == 0, so ensure happens after
db.addBalance(transaction.to, transaction.value)
# What remains is call and/or value transfer # What remains is call and/or value transfer
if execComputation(computation): if execComputation(computation):
let let
@ -88,7 +84,6 @@ proc testFixtureIndexes(prevStateRoot: Hash256, header: BlockHeader, pre: JsonNo
else: else:
vmState.mutateStateDB: vmState.mutateStateDB:
# XXX: the coinbase has to be committed; the rest are basically reverts # XXX: the coinbase has to be committed; the rest are basically reverts
db.subBalance(transaction.to, transaction.value)
db.addBalance(sender, transaction.value) db.addBalance(sender, transaction.value)
db.setStorageRoot(transaction.to, storageRoot) db.setStorageRoot(transaction.to, storageRoot)
db.addBalance(header.coinbase, gasCost) db.addBalance(header.coinbase, gasCost)