From 2c032ad1ab30b01185dfce7fc1ddc539e2a3a008 Mon Sep 17 00:00:00 2001 From: andri lim Date: Tue, 26 Feb 2019 14:04:12 +0700 Subject: [PATCH] refactor utils --- nimbus/db/db_chain.nim | 51 +++++++++++++------------- nimbus/db/state_db.nim | 8 ++-- nimbus/rpc/p2p.nim | 12 +++--- nimbus/tracer.nim | 13 +------ nimbus/transaction.nim | 9 ++++- nimbus/utils.nim | 11 +++++- nimbus/utils/addresses.nim | 4 -- nimbus/utils/header.nim | 6 --- nimbus/utils/keccak.nim | 19 ---------- nimbus/vm/interpreter/opcodes_impl.nim | 2 +- nimbus/vm_state.nim | 2 +- nimbus/vm_state_transactions.nim | 9 +++-- premix/.gitignore | 2 + premix/hunter.nim | 11 +++--- premix/premixcore.nim | 2 +- tests/macro_assembler.nim | 5 +-- tests/test_generalstate_json.nim | 3 +- tests/test_vm_json.nim | 6 +-- 18 files changed, 76 insertions(+), 99 deletions(-) delete mode 100644 nimbus/utils/addresses.nim delete mode 100644 nimbus/utils/keccak.nim diff --git a/nimbus/db/db_chain.nim b/nimbus/db/db_chain.nim index e2097c04a..bba3a3c6f 100644 --- a/nimbus/db/db_chain.nim +++ b/nimbus/db/db_chain.nim @@ -7,18 +7,20 @@ import tables, sequtils, algorithm, - ranges, state_db, nimcrypto, eth/trie/[hexary, db], eth/[common, rlp], byteutils, chronicles, - ../errors, ../block_types, ../utils/header, ../constants, ./storage_types + ranges, state_db, eth/trie/[hexary, db], + eth/[common, rlp], byteutils, chronicles, + ../errors, ../constants, ./storage_types, + ../utils type BaseChainDB* = ref object db* : TrieDatabaseRef pruneTrie*: bool - KeyType = enum - blockNumberToHash - blockHashToScore - + #KeyType = enum + # blockNumberToHash + # blockHashToScore + # TransactionKey = tuple blockNumber: BlockNumber index: int @@ -105,7 +107,7 @@ proc persistTransactions*(self: BaseChainDB, blockNumber: BlockNumber, transacti for idx, tx in transactions: let encodedTx = rlp.encode(tx).toRange - txHash = keccak256.digest(encodedTx.toOpenArray) + txHash = keccak(encodedTx.toOpenArray) txKey: TransactionKey = (blockNumber, idx) trie.put(rlp.encode(idx).toRange, encodedTx) self.db.put(transactionHashToBlockKey(txHash).toOpenArray, rlp.encode(txKey)) @@ -125,7 +127,7 @@ iterator getBlockTransactionHashes(self: BaseChainDB, blockHeader: BlockHeader): ## Returns an iterable of the transaction hashes from th block specified ## by the given block header. for encodedTx in self.getBlockTransactionData(blockHeader.txRoot): - yield keccak256.digest(encodedTx.toOpenArray) + yield keccak(encodedTx.toOpenArray) proc getBlockBody*(self: BaseChainDB, blockHash: Hash256, output: var BlockBody): bool = var header: BlockHeader @@ -245,24 +247,24 @@ proc persistUncles*(self: BaseChainDB, uncles: openarray[BlockHeader]): Hash256 ## Persists the list of uncles to the database. ## Returns the uncles hash. let enc = rlp.encode(uncles) - result = keccak256.digest(enc) + result = keccak(enc) self.db.put(genericHashKey(result).toOpenArray, enc) -proc persistBlockToDb*(self: BaseChainDB; blk: Block): ValidationResult = - ## Persist the given block's header and uncles. - ## Assumes all block transactions have been persisted already. - let newCanonicalHeaders = self.persistHeaderToDb(blk.header) - for header in newCanonicalHeaders: - var index = 0 - for txHash in self.getBlockTransactionHashes(header): - self.addTransactionToCanonicalChain(txHash, header, index) - inc index - - if blk.uncles.len != 0: - let ommersHash = self.persistUncles(blk.uncles) - if ommersHash != blk.header.ommersHash: - debug "ommersHash mismatch" - return ValidationResult.Error +#proc persistBlockToDb*(self: BaseChainDB; blk: Block): ValidationResult = +# ## Persist the given block's header and uncles. +# ## Assumes all block transactions have been persisted already. +# let newCanonicalHeaders = self.persistHeaderToDb(blk.header) +# for header in newCanonicalHeaders: +# var index = 0 +# for txHash in self.getBlockTransactionHashes(header): +# self.addTransactionToCanonicalChain(txHash, header, index) +# inc index +# +# if blk.uncles.len != 0: +# let ommersHash = self.persistUncles(blk.uncles) +# if ommersHash != blk.header.ommersHash: +# debug "ommersHash mismatch" +# return ValidationResult.Error # Deprecated: proc getBlockHeaderByHash*(self: BaseChainDB; blockHash: Hash256): BlockHeader {.deprecated.} = @@ -273,4 +275,3 @@ proc lookupBlockHash*(self: BaseChainDB; n: BlockNumber): Hash256 {.deprecated.} proc getCanonicalBlockHeaderByNumber*(self: BaseChainDB; n: BlockNumber): BlockHeader {.deprecated.} = self.getBlockHeader(n) - diff --git a/nimbus/db/state_db.nim b/nimbus/db/state_db.nim index d93433c59..c94f14e1a 100644 --- a/nimbus/db/state_db.nim +++ b/nimbus/db/state_db.nim @@ -7,8 +7,8 @@ import sequtils, strformat, tables, - chronicles, eth/[common, rlp], nimcrypto, eth/trie/[hexary, db], - ../constants, ../errors, ../validation, + chronicles, eth/[common, rlp], eth/trie/[hexary, db], + ../constants, ../errors, ../validation, ../utils, storage_types logScope: @@ -115,7 +115,7 @@ proc setStorage*(db: var AccountStateDB, var triedb = HexaryTrie(db.trie).db # slotHash can be obtained from accountTrie.put? - slotHash = keccak256.digest(slot.toByteArrayBE) + slotHash = keccak(slot.toByteArrayBE) triedb.put(slotHashToSlotKey(slotHash.data).toOpenArray, rlp.encode(slot)) account.storageRoot = accountTrie.rootHash @@ -162,7 +162,7 @@ proc setCode*(db: AccountStateDB, address: EthAddress, code: ByteRange) = # also use JournalDB to revert state trie let - newCodeHash = keccak256.digest code.toOpenArray + newCodeHash = keccak code.toOpenArray triedb = HexaryTrie(db.trie).db if code.len != 0: diff --git a/nimbus/rpc/p2p.nim b/nimbus/rpc/p2p.nim index 67db5acfe..1795fc8a6 100644 --- a/nimbus/rpc/p2p.nim +++ b/nimbus/rpc/p2p.nim @@ -9,10 +9,10 @@ import strutils, times, options, - nimcrypto, json_rpc/rpcserver, hexstrings, stint, byteutils, ranges/typedranges, - eth/[common, keys, rlp, p2p], eth/trie/db, - ../utils/header, ../transaction, ../config, ../vm_state, ../constants, ../vm_types, - ../vm_state_transactions, ../utils/addresses, + json_rpc/rpcserver, hexstrings, stint, byteutils, ranges/typedranges, + eth/[common, keys, rlp, p2p], eth/trie/db, nimcrypto, + ../transaction, ../config, ../vm_state, ../constants, ../vm_types, + ../vm_state_transactions, ../utils, ../db/[db_chain, state_db, storage_types], rpc_types, rpc_utils, ../vm/[message, computation, interpreter_dispatch] @@ -376,7 +376,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) = for i in 0 ..< blockBody.uncles.len: rawData[startIdx .. startIdx + 32] = blockBody.uncles[i].hash.data startIdx += 32 - result.sha3Uncles = keccak256.digest(rawData) + result.sha3Uncles = keccak(rawData) result.logsBloom = some(header.bloom) result.transactionsRoot = header.txRoot @@ -414,7 +414,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) = ## Returns BlockObject or nil when no block was found. let header = chain.headerFromTag(quantityTag) - + result = some(populateBlockObject(header, getBlockBody(header.hash))) proc populateTransactionObject(transaction: Transaction, txIndex: int64, blockHeader: BlockHeader, blockHash: Hash256): TransactionObject = diff --git a/nimbus/tracer.nim b/nimbus/tracer.nim index abf201fc2..ea7869fe1 100644 --- a/nimbus/tracer.nim +++ b/nimbus/tracer.nim @@ -1,7 +1,7 @@ import db/[db_chain, state_db, capturedb], eth/common, utils, json, constants, vm_state, vm_types, transaction, p2p/executor, - eth/trie/db, nimcrypto, strutils, ranges, ./utils/addresses, + eth/trie/db, nimcrypto, strutils, ranges, chronicles, rpc/hexstrings, launcher proc getParentHeader(self: BaseChainDB, header: BlockHeader): BlockHeader = @@ -32,17 +32,6 @@ proc toJson*(receipts: seq[Receipt]): JsonNode = for receipt in receipts: result.add receipt.toJson -proc getSender*(tx: Transaction): EthAddress = - if not tx.getSender(result): - raise newException(ValueError, "Could not get sender") - -proc getRecipient*(tx: Transaction): EthAddress = - if tx.isContractCreation: - let sender = tx.getSender() - result = generateAddress(sender, tx.accountNonce) - else: - result = tx.to - proc captureAccount(n: JsonNode, db: AccountStateDB, address: EthAddress, name: string) = var jaccount = newJObject() jaccount["name"] = %name diff --git a/nimbus/transaction.nim b/nimbus/transaction.nim index bc349ea30..9fc16c6d3 100644 --- a/nimbus/transaction.nim +++ b/nimbus/transaction.nim @@ -6,7 +6,7 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - constants, errors, eth/[common, rlp, keys], nimcrypto + constants, errors, eth/[common, rlp, keys], nimcrypto, utils proc initTransaction*(nonce: AccountNonce, gasPrice, gasLimit: GasInt, to: EthAddress, value: UInt256, payload: Blob, V: byte, R, S: UInt256, isContractCreation = false): Transaction = @@ -15,6 +15,7 @@ proc initTransaction*(nonce: AccountNonce, gasPrice, gasLimit: GasInt, to: EthAd result.gasLimit = gasLimit result.to = to result.value = value + result.payload = payload result.V = V result.R = R result.S = S @@ -115,3 +116,9 @@ proc getSender*(transaction: Transaction): EthAddress = if not transaction.getSender(result): raise newException(ValidationError, "Could not derive sender address from transaction") +proc getRecipient*(tx: Transaction): EthAddress = + if tx.isContractCreation: + let sender = tx.getSender() + result = generateAddress(sender, tx.accountNonce) + else: + result = tx.to diff --git a/nimbus/utils.nim b/nimbus/utils.nim index 8421f3335..35d9571e2 100644 --- a/nimbus/utils.nim +++ b/nimbus/utils.nim @@ -1,4 +1,4 @@ -import eth/trie/db, eth/[trie, rlp, common] +import eth/trie/db, eth/[trie, rlp, common], nimcrypto proc calcRootHash[T](items: openArray[T]): Hash256 = var tr = initHexaryTrie(newMemoryDB()) @@ -11,3 +11,12 @@ template calcTxRoot*(transactions: openArray[Transaction]): Hash256 = template calcReceiptRoot*(receipts: openArray[Receipt]): Hash256 = calcRootHash(receipts) + +func keccak*(value: openarray[byte]): Hash256 {.inline.} = + keccak256.digest value + +func generateAddress*(address: EthAddress, nonce: AccountNonce): EthAddress = + result[0..19] = keccak(rlp.encodeList(address, nonce)).data.toOpenArray(12, 31) + +func hash*(b: BlockHeader): Hash256 {.inline.} = + rlpHash(b) diff --git a/nimbus/utils/addresses.nim b/nimbus/utils/addresses.nim deleted file mode 100644 index 6bbb72433..000000000 --- a/nimbus/utils/addresses.nim +++ /dev/null @@ -1,4 +0,0 @@ -import nimcrypto, eth/[common, rlp] - -func generateAddress*(address: EthAddress, nonce: AccountNonce): EthAddress = - result[0..19] = keccak256.digest(rlp.encodeList(address, nonce)).data.toOpenArray(12, 31) diff --git a/nimbus/utils/header.nim b/nimbus/utils/header.nim index ce4f5315f..735e7d2e2 100644 --- a/nimbus/utils/header.nim +++ b/nimbus/utils/header.nim @@ -91,9 +91,3 @@ proc generateHeaderFromParentHeader*( coinbase: coinbase, # TODO: data: extraData, ) - -import nimcrypto -# TODO: required otherwise -# eth/common/rlp_serialization.nim(18, 12) template/generic instantiation from here -# nimcrypto/hash.nim(46, 6) Error: attempting to call undeclared routine: 'init' -proc hash*(b: BlockHeader): Hash256 {.inline.} = rlpHash(b) diff --git a/nimbus/utils/keccak.nim b/nimbus/utils/keccak.nim deleted file mode 100644 index ae96c0658..000000000 --- a/nimbus/utils/keccak.nim +++ /dev/null @@ -1,19 +0,0 @@ -# Nimbus -# Copyright (c) 2018 Status Research & Development GmbH -# Licensed under either of -# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) -# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) -# at your option. This file may not be copied, modified, or distributed except according to those terms. - -import - nimcrypto, strutils, eth/common - -proc keccak*(value: openarray[byte]): Hash256 {.inline.} = - keccak256.digest value - -proc keccak*(value: string): Hash256 {.inline.} = - keccak256.digest value - -proc keccak*(value: cstring): Hash256 {.inline.} = - # TODO: this is inefficient it allocates for the cstring -> string and then for string -> result - keccak $value diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index fdacc406c..b3dd5ed70 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -12,7 +12,7 @@ import ./gas_meter, ./gas_costs, ./opcode_values, ./vm_forks, ../memory, ../message, ../stack, ../code_stream, ../computation, ../../vm_state, ../../errors, ../../constants, ../../vm_types, - ../../db/[db_chain, state_db], ../../utils/addresses + ../../db/[db_chain, state_db], ../../utils logScope: topics = "opcode impl" diff --git a/nimbus/vm_state.nim b/nimbus/vm_state.nim index 5e1b457d1..d90d416d5 100644 --- a/nimbus/vm_state.nim +++ b/nimbus/vm_state.nim @@ -9,7 +9,7 @@ import macros, strformat, tables, sets, eth/common, eth/trie/db, ./constants, ./errors, ./transaction, ./db/[db_chain, state_db], - ./utils/header, json, vm_types, vm/transaction_tracer + ./utils, json, vm_types, vm/transaction_tracer proc newAccessLogs*: AccessLogs = AccessLogs(reads: initTable[string, string](), writes: initTable[string, string]()) diff --git a/nimbus/vm_state_transactions.nim b/nimbus/vm_state_transactions.nim index e41168fea..81a65af72 100644 --- a/nimbus/vm_state_transactions.nim +++ b/nimbus/vm_state_transactions.nim @@ -7,10 +7,9 @@ import ranges/typedranges, sequtils, strformat, tables, options, - eth/common, chronicles, - ./constants, ./errors, ./vm/computation, - ./transaction, ./vm_types, ./vm_state, ./block_types, ./db/[db_chain, state_db], ./utils/header, - ./vm/interpreter, ./vm/interpreter/gas_costs, ./utils/addresses + eth/common, chronicles, ./db/[db_chain, state_db], + constants, errors, transaction, vm_types, vm_state, utils, + ./vm/[computation, interpreter], ./vm/interpreter/gas_costs proc validateTransaction*(vmState: BaseVMState, transaction: Transaction, sender: EthAddress): bool = # XXX: https://github.com/status-im/nimbus/issues/35#issuecomment-391726518 @@ -124,6 +123,7 @@ proc applyCreateTransaction*(t: Transaction, vmState: BaseVMState, sender: EthAd vmState.clearLogs() return t.gasLimit.u256 * t.gasPrice.u256 +#[ method executeTransaction(vmState: BaseVMState, transaction: Transaction): (BaseComputation, BlockHeader) {.base.}= # Execute the transaction in the vm # TODO: introduced here: https://github.com/ethereum/py-evm/commit/21c57f2d56ab91bb62723c3f9ebe291d0b132dde @@ -184,3 +184,4 @@ method applyTransaction*( else: var (computation, blockHeader) = vmState.executeTransaction(transaction) return (computation, nil, initTable[string, string]()) +]# \ No newline at end of file diff --git a/premix/.gitignore b/premix/.gitignore index d291b3446..67e723053 100644 --- a/premix/.gitignore +++ b/premix/.gitignore @@ -2,5 +2,7 @@ *.db-lock /output /temp +/data +/nimcache *.json premixData.js diff --git a/premix/hunter.nim b/premix/hunter.nim index 3b099a5d4..f7b31ffca 100644 --- a/premix/hunter.nim +++ b/premix/hunter.nim @@ -1,10 +1,9 @@ import - json, downloader, stint, strutils, byteutils, parser, nimcrypto, - chronicles, ../nimbus/tracer, eth/trie/[trie_defs, db], ../nimbus/vm_state, + json, downloader, stint, strutils, byteutils, parser, + chronicles, ../nimbus/[tracer, vm_state, utils], eth/trie/[trie_defs, db], ../nimbus/db/[db_chain, state_db], ../nimbus/p2p/executor, premixcore, - eth/common, configuration, tables, ../nimbus/vm_types, hashes, - ../nimbus/utils/header - + eth/common, configuration, tables, ../nimbus/vm_types, hashes + const emptyCodeHash = blankStringHash emptyStorageHash = emptyRlpHash @@ -12,7 +11,7 @@ const proc store(memoryDB: TrieDatabaseRef, branch: JsonNode) = for p in branch: let rlp = hexToSeqByte(p.getStr) - let hash = keccak256.digest(rlp) + let hash = keccak(rlp) memoryDB.put(hash.data, rlp) proc parseAddress(address: string): EthAddress = diff --git a/premix/premixcore.nim b/premix/premixcore.nim index 80870d8e6..7b3dcaca3 100644 --- a/premix/premixcore.nim +++ b/premix/premixcore.nim @@ -1,7 +1,7 @@ import json, strutils, os, stint, chronicles, eth/common, - ../nimbus/tracer, ../nimbus/launcher, + ../nimbus/transaction, ../nimbus/launcher, ./js_tracer, ./parser, ./downloader proc fakeAlloc(n: JsonNode) = diff --git a/tests/macro_assembler.nim b/tests/macro_assembler.nim index 81c948119..9073bee81 100644 --- a/tests/macro_assembler.nim +++ b/tests/macro_assembler.nim @@ -6,11 +6,10 @@ import import options, json, os, eth/trie/[db, hexary], - ../nimbus/[vm_state, tracer, vm_types, transaction], + ../nimbus/[vm_state, tracer, vm_types, transaction, utils], ../nimbus/db/[db_chain, state_db], ../nimbus/vm_state_transactions, ../nimbus/vm/interpreter/[vm_forks, gas_costs], - ../nimbus/utils/addresses, ../nimbus/vm/[message, computation, memory] export opcode_values, byteutils @@ -223,7 +222,7 @@ proc initComputation(blockNumber: Uint256, chainDB: BaseChainDB, payload, data: var tx = body.transactions[0] - sender = tracer.getSender(tx) + sender = transaction.getSender(tx) tx.payload = payload tx.gasLimit = 500000000 diff --git a/tests/test_generalstate_json.nim b/tests/test_generalstate_json.nim index 17d3f443f..f8045b143 100644 --- a/tests/test_generalstate_json.nim +++ b/tests/test_generalstate_json.nim @@ -11,8 +11,7 @@ import eth/[rlp, common, keys], eth/trie/db, chronicles, ./test_helpers, ../nimbus/[constants, errors], - ../nimbus/[vm_state, vm_types, vm_state_transactions], - ../nimbus/utils/[header, addresses], + ../nimbus/[vm_state, vm_types, vm_state_transactions, utils], ../nimbus/vm/interpreter, ../nimbus/db/[db_chain, state_db] diff --git a/tests/test_vm_json.nim b/tests/test_vm_json.nim index b854e8f35..21d091c0d 100644 --- a/tests/test_vm_json.nim +++ b/tests/test_vm_json.nim @@ -12,12 +12,12 @@ import ./test_helpers, ../nimbus/[constants, errors], ../nimbus/[vm_state, vm_types], - ../nimbus/utils/header, + ../nimbus/utils, ../nimbus/vm/interpreter, ../nimbus/db/[db_chain, state_db] proc hashLogEntries(logs: seq[Log]): string = - toLowerAscii("0x" & $keccak256.digest(rlp.encode(logs))) + toLowerAscii("0x" & $keccak(rlp.encode(logs))) proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) @@ -32,7 +32,7 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) = break let fenv = fixture["env"] - var emptyRlpHash = keccak256.digest(rlp.encode("")) + var emptyRlpHash = keccak(rlp.encode("")) let header = BlockHeader( coinbase: fenv{"currentCoinbase"}.getStr.parseAddress, difficulty: fromHex(UInt256, fenv{"currentDifficulty"}.getStr),