simplify transferBalance again

This commit is contained in:
andri lim 2019-03-13 22:06:32 +07:00
parent 2f9f708f1b
commit 44688259f1
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
1 changed files with 11 additions and 20 deletions

View File

@ -109,40 +109,31 @@ proc commit*(snapshot: var ComputationSnapshot) {.inline.} =
proc dispose*(snapshot: var ComputationSnapshot) {.inline.} = proc dispose*(snapshot: var ComputationSnapshot) {.inline.} =
snapshot.snapshot.dispose() snapshot.snapshot.dispose()
proc applyMessageAux(computation: var BaseComputation, opCode: static[Op]) = proc transferBalance(computation: var BaseComputation, opCode: static[Op]) =
if computation.msg.depth >= MaxCallDepth: if computation.msg.depth >= MaxCallDepth:
raise newException(StackDepthError, "Stack depth limit reached") raise newException(StackDepthError, "Stack depth limit reached")
if computation.msg.value != 0: let senderBalance = computation.vmState.readOnlyStateDb().
let senderBalance =
computation.vmState.readOnlyStateDb().
getBalance(computation.msg.sender) getBalance(computation.msg.sender)
if senderBalance < computation.msg.value: if senderBalance < computation.msg.value:
raise newException(InsufficientFunds, raise newException(InsufficientFunds,
&"Insufficient funds: {senderBalance} < {computation.msg.value}" &"Insufficient funds: {senderBalance} < {computation.msg.value}")
)
computation.vmState.mutateStateDb: computation.vmState.mutateStateDb:
db.subBalance(computation.msg.sender, computation.msg.value) db.subBalance(computation.msg.sender, computation.msg.value)
db.addBalance(computation.msg.storageAddress, computation.msg.value) db.addBalance(computation.msg.storageAddress, computation.msg.value)
else:
# even though the value is zero, the account
# should be exist.
computation.vmState.mutateStateDb:
db.addBalance(computation.msg.storageAddress, computation.msg.value)
proc applyMessage(computation: var BaseComputation, opCode: static[Op]): bool = proc applyMessage(computation: var BaseComputation, opCode: static[Op]): bool =
var snapshot = computation.snapshot() var snapshot = computation.snapshot()
defer: snapshot.dispose() defer: snapshot.dispose()
when opCode in {Call, Create}: when opCode in {Call, Create}:
try: try:
computation.applyMessageAux(opCode) computation.transferBalance(opCode)
except VMError: except VMError:
snapshot.revert() snapshot.revert()
debug "applyMessageAux failed", msg = computation.error.info debug "transferBalance failed", msg = computation.error.info
return return
if computation.gasMeter.gasRemaining < 0: if computation.gasMeter.gasRemaining < 0: