From d30f434f039ec615cbb16cb6069e87a55c4a5fe7 Mon Sep 17 00:00:00 2001 From: andri lim Date: Thu, 9 Jan 2020 17:40:39 +0700 Subject: [PATCH] get rid of computation child after execution --- nimbus/vm/computation.nim | 29 ++++++++++++++--------------- nimbus/vm_state.nim | 2 +- nimbus/vm_state_transactions.nim | 6 +----- nimbus/vm_types.nim | 3 +-- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/nimbus/vm/computation.nim b/nimbus/vm/computation.nim index dbfa45861..1f89e6cb5 100644 --- a/nimbus/vm/computation.nim +++ b/nimbus/vm/computation.nim @@ -24,7 +24,6 @@ proc newBaseComputation*(vmState: BaseVMState, blockNumber: BlockNumber, message result.memory = Memory() result.stack = newStack() result.gasMeter.init(message.gas) - result.children = @[] result.touchedAccounts = initHashSet[EthAddress]() result.suicides = initHashSet[EthAddress]() result.code = newCodeStream(message.code) @@ -200,6 +199,11 @@ proc applyMessage*(computation: BaseComputation, opCode: static[Op]) = executeOpcodes(computation) proc addChildComputation*(computation: BaseComputation, child: BaseComputation) = + if child.isError or computation.getFork == FKIstanbul: + if not child.msg.isCreate: + if child.msg.contractAddress == ripemdAddr: + child.vmState.touchedAccounts.incl child.msg.contractAddress + if child.isError: if child.shouldBurnGas: computation.returnData = @[] @@ -218,9 +222,9 @@ proc addChildComputation*(computation: BaseComputation, child: BaseComputation) if not child.shouldBurnGas: computation.gasMeter.returnGas(child.gasMeter.gasRemaining) - computation.children.add(child) proc registerAccountForDeletion*(c: BaseComputation, beneficiary: EthAddress) = + c.touchedAccounts.incl beneficiary c.touchedAccounts.incl beneficiary c.suicides.incl(c.msg.contractAddress) @@ -248,26 +252,21 @@ proc getGasRemaining*(c: BaseComputation): GasInt = else: result = c.gasMeter.gasRemaining -# TODO: collecting touched accounts should be done in account state db -proc collectTouchedAccounts*(c: BaseComputation, output: var HashSet[EthAddress], ancestorHadError: bool = false) = +proc collectTouchedAccounts*(c: BaseComputation) = ## Collect all of the accounts that *may* need to be deleted based on EIP161: ## https://github.com/ethereum/EIPs/blob/master/EIPS/eip-161.md ## also see: https://github.com/ethereum/EIPs/issues/716 - let isIstanbul = c.getFork >= FkIstanbul - let condition = c.isError or ancestorHadError - - if not c.msg.isCreate: - if condition: + if c.isSuccess: + if not c.msg.isCreate: + c.touchedAccounts.incl c.msg.contractAddress + c.vmState.touchedAccounts.incl c.touchedAccounts + else: + if not c.msg.isCreate: # Special case to account for geth+parity bug # https://github.com/ethereum/EIPs/issues/716 if c.msg.contractAddress == ripemdAddr: - output.incl c.msg.contractAddress - - if c.isSuccess or isIstanbul: - # recurse into nested computations (even errored ones, since looking for RIPEMD160) - for child in c.children: - child.collectTouchedAccounts(output, c.isError or ancestorHadError) + c.vmState.touchedAccounts.incl c.msg.contractAddress proc tracingEnabled*(c: BaseComputation): bool = c.vmState.tracingEnabled diff --git a/nimbus/vm_state.nim b/nimbus/vm_state.nim index fce3e6f78..5eb9b0657 100644 --- a/nimbus/vm_state.nim +++ b/nimbus/vm_state.nim @@ -35,7 +35,7 @@ proc init*(self: BaseVMState, prevStateRoot: Hash256, header: BlockHeader, self.tracingEnabled = TracerFlags.EnableTracing in tracerFlags self.logEntries = @[] self.accountDb = newAccountStateDB(chainDB.db, prevStateRoot, chainDB.pruneTrie) - self.touchedAccounts = initHashSet[EthAddress]() + self.touchedAccounts = initHashSet[EthAddress]() proc newBaseVMState*(prevStateRoot: Hash256, header: BlockHeader, chainDB: BaseChainDB, tracerFlags: set[TracerFlags] = {}): BaseVMState = diff --git a/nimbus/vm_state_transactions.nim b/nimbus/vm_state_transactions.nim index b6561f9a2..f386211f4 100644 --- a/nimbus/vm_state_transactions.nim +++ b/nimbus/vm_state_transactions.nim @@ -78,11 +78,7 @@ proc execComputation*(computation: var BaseComputation) = computation.gasMeter.refundGas(RefundSelfDestruct * suicidedCount) if computation.getFork >= FkSpurious: - computation.collectTouchedAccounts(computation.vmState.touchedAccounts) - if computation.isSuccess: - if not computation.msg.isCreate: - computation.touchedAccounts.incl computation.msg.contractAddress - computation.vmState.touchedAccounts.incl computation.touchedAccounts + computation.collectTouchedAccounts() computation.vmstate.status = computation.isSuccess if computation.isSuccess: diff --git a/nimbus/vm_types.nim b/nimbus/vm_types.nim index 4fc0ef144..941176a6f 100644 --- a/nimbus/vm_types.nim +++ b/nimbus/vm_types.nim @@ -59,11 +59,10 @@ type stack*: Stack gasMeter*: GasMeter code*: CodeStream - children*: seq[BaseComputation] rawOutput*: seq[byte] returnData*: seq[byte] error*: Error - touchedAccounts*: HashSet[EthAddress] + touchedAccounts*: HashSet[EthAddress] suicides*: HashSet[EthAddress] gasCosts*: GasCosts # TODO - will be hidden at a lower layer forkOverride*: Option[Fork]