From 823dd95a53e5a601c141c675012550fd1ead3f14 Mon Sep 17 00:00:00 2001 From: andri lim Date: Fri, 15 Mar 2019 18:16:47 +0700 Subject: [PATCH] write contract unification --- nimbus/p2p/executor.nim | 2 +- nimbus/vm/computation.nim | 10 +++++----- nimbus/vm_state_transactions.nim | 16 ---------------- 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/nimbus/p2p/executor.nim b/nimbus/p2p/executor.nim index 7bb1dc4c8..1f1b15872 100644 --- a/nimbus/p2p/executor.nim +++ b/nimbus/p2p/executor.nim @@ -42,7 +42,7 @@ proc processTransaction*(tx: Transaction, sender: EthAddress, vmState: BaseVMSta if execComputation(computation): if tx.isContractCreation: - contractOK = computation.writeContract() + contractOK = computation.writeContract(fork) result = computation.refundGas(tx, sender) if not contractOK and fork == FkHomestead: diff --git a/nimbus/vm/computation.nim b/nimbus/vm/computation.nim index b5641e615..2fc69da8a 100644 --- a/nimbus/vm/computation.nim +++ b/nimbus/vm/computation.nim @@ -69,6 +69,9 @@ proc outputHex*(c: BaseComputation): string = return "0x" c.rawOutput.bytesToHex +proc isSuicided*(c: var BaseComputation, address: EthAddress): bool = + result = address in c.accountsToDelete + proc prepareChildMessage*( c: var BaseComputation, gas: GasInt, @@ -159,7 +162,7 @@ proc applyMessage(computation: var BaseComputation, opCode: static[Op]): bool = 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 let contractCode = computation.output @@ -230,7 +233,7 @@ proc applyChildComputation*(parentComp, childComp: var BaseComputation, opCode: if applyMessage(childComp, opCode): if childComp.msg.isCreate: - contractOK = fork.writeContract(childComp, opCode) + contractOK = childComp.writeContract(fork) if not contractOK and fork == FkHomestead: # consume all gas @@ -247,9 +250,6 @@ proc registerAccountForDeletion*(c: var BaseComputation, beneficiary: EthAddress "registered for deletion multiple times") 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.} = c.logEntries.add(log) diff --git a/nimbus/vm_state_transactions.nim b/nimbus/vm_state_transactions.nim index 87c60b541..0b9d851e7 100644 --- a/nimbus/vm_state_transactions.nim +++ b/nimbus/vm_state_transactions.nim @@ -99,22 +99,6 @@ proc refundGas*(computation: BaseComputation, tx: Transaction, sender: EthAddres 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.}= # Execute the transaction in the vm