mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-13 13:55:45 +00:00
refactor sender transfer
This commit is contained in:
parent
26d8ab84fe
commit
5ab7c02dcf
@ -7,16 +7,7 @@ import options,
|
|||||||
../vm/interpreter/vm_forks
|
../vm/interpreter/vm_forks
|
||||||
|
|
||||||
proc contractCall(tx: Transaction, vmState: BaseVMState, sender: EthAddress, forkOverride=none(Fork)): GasInt =
|
proc contractCall(tx: Transaction, vmState: BaseVMState, sender: EthAddress, forkOverride=none(Fork)): GasInt =
|
||||||
# TODO: this function body was copied from GST with it's comments and TODOs.
|
|
||||||
# Right now it's main purpose is to produce VM tracing when syncing block with
|
|
||||||
# contract call. At later stage, this proc together with applyCreateTransaction
|
|
||||||
# and processTransaction need to be restructured.
|
|
||||||
|
|
||||||
# TODO: replace with cachingDb or similar approach; necessary
|
|
||||||
# when calls/subcalls/etc come in, too.
|
|
||||||
var db = vmState.accountDb
|
var db = vmState.accountDb
|
||||||
let storageRoot = db.getStorageRoot(tx.to)
|
|
||||||
|
|
||||||
var computation = setupComputation(vmState, tx, sender, forkOverride)
|
var computation = setupComputation(vmState, tx, sender, forkOverride)
|
||||||
if execComputation(computation):
|
if execComputation(computation):
|
||||||
let
|
let
|
||||||
@ -27,11 +18,8 @@ proc contractCall(tx: Transaction, vmState: BaseVMState, sender: EthAddress, for
|
|||||||
gasRefundAmount = (gasRefund + gasRemaining).u256 * tx.gasPrice.u256
|
gasRefundAmount = (gasRefund + gasRemaining).u256 * tx.gasPrice.u256
|
||||||
|
|
||||||
db.addBalance(sender, gasRefundAmount)
|
db.addBalance(sender, gasRefundAmount)
|
||||||
|
|
||||||
return (tx.gasLimit - gasRemaining - gasRefund)
|
return (tx.gasLimit - gasRemaining - gasRefund)
|
||||||
else:
|
else:
|
||||||
db.addBalance(sender, tx.value)
|
|
||||||
db.setStorageRoot(tx.to, storageRoot)
|
|
||||||
if computation.tracingEnabled: computation.traceError()
|
if computation.tracingEnabled: computation.traceError()
|
||||||
vmState.clearLogs()
|
vmState.clearLogs()
|
||||||
return tx.gasLimit
|
return tx.gasLimit
|
||||||
@ -68,7 +56,7 @@ proc processTransaction*(t: Transaction, sender: EthAddress, vmState: BaseVMStat
|
|||||||
balance -= upfrontGasCost
|
balance -= upfrontGasCost
|
||||||
transactionFailed = true
|
transactionFailed = true
|
||||||
else:
|
else:
|
||||||
balance -= upfrontCost
|
balance -= upfrontGasCost
|
||||||
|
|
||||||
db.setBalance(sender, balance)
|
db.setBalance(sender, balance)
|
||||||
if transactionFailed:
|
if transactionFailed:
|
||||||
@ -89,7 +77,7 @@ proc processTransaction*(t: Transaction, sender: EthAddress, vmState: BaseVMStat
|
|||||||
if code.len == 0 and not isPrecompiles(t.to):
|
if code.len == 0 and not isPrecompiles(t.to):
|
||||||
# Value transfer
|
# Value transfer
|
||||||
trace "Transfer", value = t.value, sender, to = t.to
|
trace "Transfer", value = t.value, sender, to = t.to
|
||||||
|
db.subBalance(sender, t.value)
|
||||||
db.addBalance(t.to, t.value)
|
db.addBalance(t.to, t.value)
|
||||||
else:
|
else:
|
||||||
# Contract call
|
# Contract call
|
||||||
@ -99,13 +87,9 @@ proc processTransaction*(t: Transaction, sender: EthAddress, vmState: BaseVMStat
|
|||||||
# TODO: Run the vm with proper fork
|
# TODO: Run the vm with proper fork
|
||||||
return contractCall(t, vmState, sender)
|
return contractCall(t, vmState, sender)
|
||||||
|
|
||||||
if gasUsed > t.gasLimit:
|
if gasUsed > t.gasLimit: gasUsed = t.gasLimit
|
||||||
gasUsed = t.gasLimit
|
|
||||||
|
|
||||||
var refund = (t.gasLimit - gasUsed).u256 * t.gasPrice.u256
|
var refund = (t.gasLimit - gasUsed).u256 * t.gasPrice.u256
|
||||||
if transactionFailed:
|
if transactionFailed: refund += t.value
|
||||||
refund += t.value
|
|
||||||
|
|
||||||
db.addBalance(sender, refund)
|
db.addBalance(sender, refund)
|
||||||
return gasUsed
|
return gasUsed
|
||||||
|
|
||||||
|
@ -67,6 +67,7 @@ proc execComputation*(computation: var BaseComputation): bool =
|
|||||||
defer: snapshot.dispose()
|
defer: snapshot.dispose()
|
||||||
|
|
||||||
computation.vmState.mutateStateDB:
|
computation.vmState.mutateStateDB:
|
||||||
|
db.subBalance(computation.msg.origin, computation.msg.value)
|
||||||
db.addBalance(computation.msg.storageAddress, computation.msg.value)
|
db.addBalance(computation.msg.storageAddress, computation.msg.value)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -124,13 +125,7 @@ proc applyCreateTransaction*(tx: Transaction, vmState: BaseVMState, sender: EthA
|
|||||||
db.addBalance(sender, (tx.gasLimit - gasUsed - codeCost + gasRefund).u256 * tx.gasPrice.u256)
|
db.addBalance(sender, (tx.gasLimit - gasUsed - codeCost + gasRefund).u256 * tx.gasPrice.u256)
|
||||||
return (gasUsed + codeCost - gasRefund)
|
return (gasUsed + codeCost - gasRefund)
|
||||||
else:
|
else:
|
||||||
# FIXME: don't do this revert, but rather only subBalance correctly
|
if c.tracingEnabled: c.traceError()
|
||||||
# the if transactionfailed at end is what is supposed to pick it up
|
|
||||||
# especially when it's cross-function, it's ugly/fragile
|
|
||||||
var db = vmState.accountDb
|
|
||||||
db.addBalance(sender, tx.value)
|
|
||||||
if c.tracingEnabled:
|
|
||||||
c.traceError()
|
|
||||||
vmState.clearLogs()
|
vmState.clearLogs()
|
||||||
return tx.gasLimit
|
return tx.gasLimit
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ proc testFixtureIndexes(prevStateRoot: Hash256, header: BlockHeader, pre: JsonNo
|
|||||||
let gasCost = transaction.gasLimit.u256 * transaction.gasPrice.u256
|
let gasCost = transaction.gasLimit.u256 * transaction.gasPrice.u256
|
||||||
vmState.mutateStateDB:
|
vmState.mutateStateDB:
|
||||||
db.incNonce(sender)
|
db.incNonce(sender)
|
||||||
db.subBalance(sender, transaction.value + gasCost)
|
db.subBalance(sender, gasCost)
|
||||||
|
|
||||||
if transaction.isContractCreation and transaction.payload.len > 0:
|
if transaction.isContractCreation and transaction.payload.len > 0:
|
||||||
vmState.mutateStateDB:
|
vmState.mutateStateDB:
|
||||||
@ -83,9 +83,6 @@ proc testFixtureIndexes(prevStateRoot: Hash256, header: BlockHeader, pre: JsonNo
|
|||||||
# TODO: only here does one commit, with some nuance/caveat
|
# TODO: only here does one commit, with some nuance/caveat
|
||||||
else:
|
else:
|
||||||
vmState.mutateStateDB:
|
vmState.mutateStateDB:
|
||||||
# XXX: the coinbase has to be committed; the rest are basically reverts
|
|
||||||
db.addBalance(sender, transaction.value)
|
|
||||||
db.setStorageRoot(transaction.to, storageRoot)
|
|
||||||
db.addBalance(header.coinbase, gasCost)
|
db.addBalance(header.coinbase, gasCost)
|
||||||
|
|
||||||
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
|
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user