refactor utils

This commit is contained in:
andri lim 2019-02-26 14:04:12 +07:00 committed by zah
parent 091bbe22db
commit 2c032ad1ab
18 changed files with 76 additions and 99 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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 =

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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"

View File

@ -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]())

View File

@ -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]())
]#

2
premix/.gitignore vendored
View File

@ -2,5 +2,7 @@
*.db-lock
/output
/temp
/data
/nimcache
*.json
premixData.js

View File

@ -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 =

View File

@ -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) =

View File

@ -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

View File

@ -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]

View File

@ -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),