mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-25 18:35:32 +00:00
remove explicit return value from VM
This commit is contained in:
parent
4d958acf15
commit
0f8affb7c9
@ -111,6 +111,14 @@ proc commit*(comp: BaseComputation) =
|
|||||||
proc dispose*(comp: BaseComputation) {.inline.} =
|
proc dispose*(comp: BaseComputation) {.inline.} =
|
||||||
comp.dbsnapshot.transaction.dispose()
|
comp.dbsnapshot.transaction.dispose()
|
||||||
|
|
||||||
|
proc rollback*(comp: BaseComputation) =
|
||||||
|
comp.dbsnapshot.transaction.rollback()
|
||||||
|
comp.vmState.accountDb.rootHash = comp.dbsnapshot.intermediateRoot
|
||||||
|
comp.vmState.blockHeader.stateRoot = comp.dbsnapshot.intermediateRoot
|
||||||
|
|
||||||
|
proc setError*(comp: BaseComputation, msg: string, burnsGas = false) {.inline.} =
|
||||||
|
comp.error = Error(info: msg, burnsGas: burnsGas)
|
||||||
|
|
||||||
proc getFork*(computation: BaseComputation): Fork =
|
proc getFork*(computation: BaseComputation): Fork =
|
||||||
result =
|
result =
|
||||||
if computation.forkOverride.isSome:
|
if computation.forkOverride.isSome:
|
||||||
@ -142,34 +150,33 @@ proc writeContract*(computation: var BaseComputation, fork: Fork): bool =
|
|||||||
if fork < FkHomestead: computation.output = @[]
|
if fork < FkHomestead: computation.output = @[]
|
||||||
result = false
|
result = false
|
||||||
|
|
||||||
proc transferBalance(computation: var BaseComputation, opCode: static[Op]): bool =
|
proc transferBalance(computation: var BaseComputation, opCode: static[Op]) =
|
||||||
if computation.msg.depth > MaxCallDepth:
|
if computation.msg.depth > MaxCallDepth:
|
||||||
debug "Stack depth limit reached", depth=computation.msg.depth
|
computation.setError(&"Stack depth limit reached depth={computation.msg.depth}")
|
||||||
return false
|
return
|
||||||
|
|
||||||
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:
|
||||||
debug "insufficient funds", available=senderBalance, needed=computation.msg.value
|
computation.setError(&"insufficient funds available={senderBalance}, needed={computation.msg.value}")
|
||||||
return false
|
return
|
||||||
|
|
||||||
when opCode in {Call, Create}:
|
when opCode in {Call, Create}:
|
||||||
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)
|
||||||
|
|
||||||
result = true
|
|
||||||
|
|
||||||
proc executeOpcodes*(computation: var BaseComputation) {.gcsafe.}
|
proc executeOpcodes*(computation: var BaseComputation) {.gcsafe.}
|
||||||
|
|
||||||
proc applyMessage*(computation: var BaseComputation, opCode: static[Op]): bool =
|
proc applyMessage*(computation: var BaseComputation, opCode: static[Op]) =
|
||||||
computation.snapshot()
|
computation.snapshot()
|
||||||
defer: computation.dispose()
|
defer: computation.dispose()
|
||||||
|
|
||||||
when opCode in {CallCode, Call, Create}:
|
when opCode in {CallCode, Call, Create}:
|
||||||
if not computation.transferBalance(opCode):
|
computation.transferBalance(opCode)
|
||||||
computation.revert()
|
if computation.isError():
|
||||||
|
computation.rollback()
|
||||||
return
|
return
|
||||||
|
|
||||||
if computation.gasMeter.gasRemaining < 0:
|
if computation.gasMeter.gasRemaining < 0:
|
||||||
@ -178,27 +185,20 @@ proc applyMessage*(computation: var BaseComputation, opCode: static[Op]): bool =
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
executeOpcodes(computation)
|
executeOpcodes(computation)
|
||||||
result = not computation.isError
|
except:
|
||||||
except VMError:
|
let msg = getCurrentExceptionMsg()
|
||||||
result = false
|
computation.setError(&"applyMessage Error msg={msg}, depth={computation.msg.depth}", true)
|
||||||
debug "applyMessage VM Error",
|
|
||||||
msg = getCurrentExceptionMsg(),
|
|
||||||
depth = computation.msg.depth
|
|
||||||
except ValueError:
|
|
||||||
result = false
|
|
||||||
debug "applyMessage Value Error",
|
|
||||||
msg = getCurrentExceptionMsg(),
|
|
||||||
depth = computation.msg.depth
|
|
||||||
|
|
||||||
if result and computation.msg.isCreate:
|
if computation.isSuccess and computation.msg.isCreate:
|
||||||
var fork = computation.getFork
|
let fork = computation.getFork
|
||||||
let contractFailed = not computation.writeContract(fork)
|
let contractFailed = not computation.writeContract(fork)
|
||||||
result = not(contractFailed and fork == FkHomestead)
|
if contractFailed and fork == FkHomestead:
|
||||||
|
computation.setError(&"writeContract failed, depth={computation.msg.depth}", true)
|
||||||
|
|
||||||
if result:
|
if computation.isSuccess:
|
||||||
computation.commit()
|
computation.commit()
|
||||||
else:
|
else:
|
||||||
computation.revert(true)
|
computation.rollback()
|
||||||
|
|
||||||
proc addChildComputation(computation: var BaseComputation, child: BaseComputation) =
|
proc addChildComputation(computation: var BaseComputation, child: BaseComputation) =
|
||||||
if child.isError:
|
if child.isError:
|
||||||
@ -220,7 +220,7 @@ proc addChildComputation(computation: var BaseComputation, child: BaseComputatio
|
|||||||
|
|
||||||
proc applyChildComputation*(parentComp, childComp: var BaseComputation, opCode: static[Op]) =
|
proc applyChildComputation*(parentComp, childComp: var BaseComputation, opCode: static[Op]) =
|
||||||
## Apply the vm message childMsg as a child computation.
|
## Apply the vm message childMsg as a child computation.
|
||||||
discard childComp.applyMessage(opCode)
|
childComp.applyMessage(opCode)
|
||||||
parentComp.addChildComputation(childComp)
|
parentComp.addChildComputation(childComp)
|
||||||
|
|
||||||
proc registerAccountForDeletion*(c: var BaseComputation, beneficiary: EthAddress) =
|
proc registerAccountForDeletion*(c: var BaseComputation, beneficiary: EthAddress) =
|
||||||
|
@ -65,9 +65,9 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender, recipient:
|
|||||||
|
|
||||||
proc execComputation*(computation: var BaseComputation): bool =
|
proc execComputation*(computation: var BaseComputation): bool =
|
||||||
if computation.msg.isCreate:
|
if computation.msg.isCreate:
|
||||||
result = computation.applyMessage(Create)
|
computation.applyMessage(Create)
|
||||||
else:
|
else:
|
||||||
result = computation.applyMessage(Call)
|
computation.applyMessage(Call)
|
||||||
|
|
||||||
computation.vmState.mutateStateDB:
|
computation.vmState.mutateStateDB:
|
||||||
var suicidedCount = 0
|
var suicidedCount = 0
|
||||||
@ -79,6 +79,7 @@ proc execComputation*(computation: var BaseComputation): bool =
|
|||||||
const RefundSelfDestruct = 24_000
|
const RefundSelfDestruct = 24_000
|
||||||
computation.gasMeter.refundGas(RefundSelfDestruct * suicidedCount)
|
computation.gasMeter.refundGas(RefundSelfDestruct * suicidedCount)
|
||||||
|
|
||||||
|
result = computation.isSuccess
|
||||||
if result:
|
if result:
|
||||||
computation.vmState.addLogs(computation.logEntries)
|
computation.vmState.addLogs(computation.logEntries)
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user