write contract unification

This commit is contained in:
andri lim 2019-03-15 18:16:47 +07:00
parent 0654a232a3
commit 823dd95a53
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 6 additions and 22 deletions

View File

@ -42,7 +42,7 @@ proc processTransaction*(tx: Transaction, sender: EthAddress, vmState: BaseVMSta
if execComputation(computation): if execComputation(computation):
if tx.isContractCreation: if tx.isContractCreation:
contractOK = computation.writeContract() contractOK = computation.writeContract(fork)
result = computation.refundGas(tx, sender) result = computation.refundGas(tx, sender)
if not contractOK and fork == FkHomestead: if not contractOK and fork == FkHomestead:

View File

@ -69,6 +69,9 @@ proc outputHex*(c: BaseComputation): string =
return "0x" return "0x"
c.rawOutput.bytesToHex c.rawOutput.bytesToHex
proc isSuicided*(c: var BaseComputation, address: EthAddress): bool =
result = address in c.accountsToDelete
proc prepareChildMessage*( proc prepareChildMessage*(
c: var BaseComputation, c: var BaseComputation,
gas: GasInt, gas: GasInt,
@ -159,7 +162,7 @@ proc applyMessage(computation: var BaseComputation, opCode: static[Op]): bool =
result = not computation.isError result = not computation.isError
proc writeContract(fork: Fork, computation: var BaseComputation, opCode: static[Op]): bool = proc writeContract*(computation: var BaseComputation, fork: Fork): bool =
result = true result = true
let contractCode = computation.output let contractCode = computation.output
@ -230,7 +233,7 @@ proc applyChildComputation*(parentComp, childComp: var BaseComputation, opCode:
if applyMessage(childComp, opCode): if applyMessage(childComp, opCode):
if childComp.msg.isCreate: if childComp.msg.isCreate:
contractOK = fork.writeContract(childComp, opCode) contractOK = childComp.writeContract(fork)
if not contractOK and fork == FkHomestead: if not contractOK and fork == FkHomestead:
# consume all gas # consume all gas
@ -247,9 +250,6 @@ proc registerAccountForDeletion*(c: var BaseComputation, beneficiary: EthAddress
"registered for deletion multiple times") "registered for deletion multiple times")
c.accountsToDelete[c.msg.storageAddress] = beneficiary c.accountsToDelete[c.msg.storageAddress] = beneficiary
proc isSuicided*(c: var BaseComputation, address: EthAddress): bool =
result = address in c.accountsToDelete
proc addLogEntry*(c: var BaseComputation, log: Log) {.inline.} = proc addLogEntry*(c: var BaseComputation, log: Log) {.inline.} =
c.logEntries.add(log) c.logEntries.add(log)

View File

@ -99,22 +99,6 @@ proc refundGas*(computation: BaseComputation, tx: Transaction, sender: EthAddres
result = gasUsed - gasRefund result = gasUsed - gasRefund
proc writeContract*(computation: var BaseComputation): bool =
result = true
let contractAddress = computation.msg.storageAddress
if computation.isSuicided(contractAddress): return
let codeCost = computation.gasCosts[Create].m_handler(0, 0, computation.output.len)
if computation.gasMeter.gasRemaining >= codeCost:
computation.gasMeter.consumeGas(codeCost, reason = "Write contract code for CREATE")
computation.vmState.mutateStateDB:
db.setCode(contractAddress, computation.output.toRange)
result = true
else:
computation.vmState.mutateStateDB:
db.setCode(contractAddress, ByteRange())
result = false
#[ #[
method executeTransaction(vmState: BaseVMState, transaction: Transaction): (BaseComputation, BlockHeader) {.base.}= method executeTransaction(vmState: BaseVMState, transaction: Transaction): (BaseComputation, BlockHeader) {.base.}=
# Execute the transaction in the vm # Execute the transaction in the vm