some cleanup

This commit is contained in:
andri lim 2018-12-14 14:32:45 +07:00 committed by zah
parent 9db4e9296a
commit a6bf970b1b
6 changed files with 59 additions and 82 deletions

View File

@ -149,6 +149,10 @@ proc getBlockBody*(self: BaseChainDB, blockHash: Hash256, output: var BlockBody)
else: else:
result = false result = false
proc getBlockBody*(self: BaseChainDB, hash: Hash256): BlockBody =
if not self.getBlockBody(hash, result):
raise newException(ValueError, "Error when retrieving block body")
proc getTransactionKey*(self: BaseChainDB, transactionHash: Hash256): tuple[blockNumber: BlockNumber, index: int] {.inline.} = proc getTransactionKey*(self: BaseChainDB, transactionHash: Hash256): tuple[blockNumber: BlockNumber, index: int] {.inline.} =
let let
tx = self.db.get(transactionHashToBlockKey(transactionHash).toOpenArray).toRange tx = self.db.get(transactionHashToBlockKey(transactionHash).toOpenArray).toRange

View File

@ -1,41 +1,6 @@
import ../db/[db_chain, state_db], eth_common, chronicles, ../vm_state, ../vm_types, ../transaction, ranges, import ../db/[db_chain, state_db], eth_common, chronicles, ../vm_state, ../vm_types, ../transaction, ranges,
../vm/[computation, interpreter_dispatch, message, interpreter/vm_forks], ../constants, stint, nimcrypto, ../vm/[computation, interpreter_dispatch, message], ../constants, stint, nimcrypto,
../vm_state_transactions, sugar, ../utils, eth_trie/db, ../tracer, ./executor, json, ../vm_state_transactions, sugar, ../utils, eth_trie/db, ../tracer, ./executor
eth_bloom, strutils
type
# TODO: these types need to be removed
# once eth_bloom and eth_common sync'ed
Bloom = eth_common.BloomFilter
LogsBloom = eth_bloom.BloomFilter
# TODO: move these three receipt procs below somewhere else more appropriate
func logsBloom(logs: openArray[Log]): LogsBloom =
for log in logs:
result.incl log.address
for topic in log.topics:
result.incl topic
func createBloom*(receipts: openArray[Receipt]): Bloom =
var bloom: LogsBloom
for receipt in receipts:
bloom.value = bloom.value or logsBloom(receipt.logs).value
result = bloom.value.toByteArrayBE
proc makeReceipt(vmState: BaseVMState, stateRoot: Hash256, cumulativeGasUsed: GasInt, fork = FkFrontier): Receipt =
if fork < FkByzantium:
# TODO: which one: vmState.blockHeader.stateRoot or stateDb.rootHash?
# currently, vmState.blockHeader.stateRoot vs stateDb.rootHash can be different
# need to wait #188 solved
result.stateRootOrStatus = hashOrStatus(stateRoot)
else:
# TODO: post byzantium fork use status instead of rootHash
let vmStatus = true # success or failure
result.stateRootOrStatus = hashOrStatus(vmStatus)
result.cumulativeGasUsed = cumulativeGasUsed
result.logs = vmState.getAndClearLogEntries()
result.bloom = logsBloom(result.logs).value.toByteArrayBE
type type
Chain* = ref object of AbstractChainDB Chain* = ref object of AbstractChainDB

View File

@ -1,8 +1,8 @@
import ../db/[db_chain, state_db], ../transaction, eth_common, import eth_common, ranges, chronicles,
../vm_state, ../vm_types, ../vm_state_transactions, ranges, ../db/[db_chain, state_db],
chronicles, ../vm/[computation, interpreter_dispatch, message], ../utils, ../constants, ../transaction,
../rpc/hexstrings, byteutils, nimcrypto, ../vm_state, ../vm_types, ../vm_state_transactions,
../utils, ../constants ../vm/[computation, interpreter_dispatch, message]
proc processTransaction*(db: var AccountStateDB, t: Transaction, sender: EthAddress, vmState: BaseVMState): UInt256 = proc processTransaction*(db: var AccountStateDB, t: Transaction, sender: EthAddress, vmState: BaseVMState): UInt256 =
## Process the transaction, write the results to db. ## Process the transaction, write the results to db.
@ -68,6 +68,39 @@ proc processTransaction*(db: var AccountStateDB, t: Transaction, sender: EthAddr
db.addBalance(sender, refund) db.addBalance(sender, refund)
return gasUsed.u256 * t.gasPrice.u256 return gasUsed.u256 * t.gasPrice.u256
type
# TODO: these types need to be removed
# once eth_bloom and eth_common sync'ed
Bloom = eth_common.BloomFilter
LogsBloom = eth_bloom.BloomFilter
# TODO: move these three receipt procs below somewhere else more appropriate
func logsBloom(logs: openArray[Log]): LogsBloom =
for log in logs:
result.incl log.address
for topic in log.topics:
result.incl topic
func createBloom*(receipts: openArray[Receipt]): Bloom =
var bloom: LogsBloom
for receipt in receipts:
bloom.value = bloom.value or logsBloom(receipt.logs).value
result = bloom.value.toByteArrayBE
proc makeReceipt(vmState: BaseVMState, stateRoot: Hash256, cumulativeGasUsed: GasInt, fork = FkFrontier): Receipt =
if fork < FkByzantium:
# TODO: which one: vmState.blockHeader.stateRoot or stateDb.rootHash?
# currently, vmState.blockHeader.stateRoot vs stateDb.rootHash can be different
# need to wait #188 solved
result.stateRootOrStatus = hashOrStatus(stateRoot)
else:
# TODO: post byzantium fork use status instead of rootHash
let vmStatus = true # success or failure
result.stateRootOrStatus = hashOrStatus(vmStatus)
result.cumulativeGasUsed = cumulativeGasUsed
result.logs = vmState.getAndClearLogEntries()
result.bloom = logsBloom(result.logs).value.toByteArrayBE
proc processBlock*(chainDB: BaseChainDB, head, header: BlockHeader, body: BlockBody, vmState: BaseVMState): bool = proc processBlock*(chainDB: BaseChainDB, head, header: BlockHeader, body: BlockBody, vmState: BaseVMState): bool =
let blockReward = 5.u256 * pow(10.u256, 18) # 5 ETH let blockReward = 5.u256 * pow(10.u256, 18) # 5 ETH

View File

@ -8,10 +8,10 @@
# those terms. # those terms.
import import
strutils, hexstrings, eth_p2p, options, strutils, json, options,
../db/[db_chain, state_db, storage_types], json_rpc/rpcserver, rpc_utils, eth_common,
json_rpc/rpcserver, json, macros, rpc_utils, hexstrings, ../tracer, ../vm_state, ../vm_types,
eth_common, ../tracer, ../vm_state, ../vm_types ../db/[db_chain, storage_types]
type type
TraceOptions = object TraceOptions = object
@ -33,10 +33,6 @@ proc traceOptionsToFlags(options: Option[TraceOptions]): set[TracerFlags] =
proc setupDebugRpc*(chainDB: BaseChainDB, rpcsrv: RpcServer) = proc setupDebugRpc*(chainDB: BaseChainDB, rpcsrv: RpcServer) =
proc getBlockBody(hash: Hash256): BlockBody =
if not chainDB.getBlockBody(hash, result):
raise newException(ValueError, "Error when retrieving block body")
rpcsrv.rpc("debug_traceTransaction") do(data: EthHashStr, options: Option[TraceOptions]) -> JsonNode: rpcsrv.rpc("debug_traceTransaction") do(data: EthHashStr, options: Option[TraceOptions]) -> JsonNode:
## The traceTransaction debugging method will attempt to run the transaction in the exact ## The traceTransaction debugging method will attempt to run the transaction in the exact
## same manner as it was executed on the network. It will replay any transaction that may ## same manner as it was executed on the network. It will replay any transaction that may
@ -55,7 +51,7 @@ proc setupDebugRpc*(chainDB: BaseChainDB, rpcsrv: RpcServer) =
txDetails = chainDB.getTransactionKey(txHash) txDetails = chainDB.getTransactionKey(txHash)
blockHeader = chainDB.getBlockHeader(txDetails.blockNumber) blockHeader = chainDB.getBlockHeader(txDetails.blockNumber)
blockHash = chainDB.getBlockHash(txDetails.blockNumber) blockHash = chainDB.getBlockHash(txDetails.blockNumber)
blockBody = getBlockBody(blockHash) blockBody = chainDB.getBlockBody(blockHash)
flags = traceOptionsToFlags(options) flags = traceOptionsToFlags(options)
traceTransaction(chainDB, blockHeader, blockBody, txDetails.index, flags) traceTransaction(chainDB, blockHeader, blockBody, txDetails.index, flags)
@ -69,7 +65,7 @@ proc setupDebugRpc*(chainDB: BaseChainDB, rpcsrv: RpcServer) =
let let
header = chainDB.headerFromTag(quantityTag) header = chainDB.headerFromTag(quantityTag)
blockHash = chainDB.getBlockHash(header.blockNumber) blockHash = chainDB.getBlockHash(header.blockNumber)
body = getBlockBody(blockHash) body = chainDB.getBlockBody(blockHash)
dumpBlockState(chainDB, header, body) dumpBlockState(chainDB, header, body)
@ -82,7 +78,7 @@ proc setupDebugRpc*(chainDB: BaseChainDB, rpcsrv: RpcServer) =
h = data.toHash h = data.toHash
header = chainDB.getBlockHeader(h) header = chainDB.getBlockHeader(h)
blockHash = chainDB.getBlockHash(header.blockNumber) blockHash = chainDB.getBlockHash(header.blockNumber)
body = getBlockBody(blockHash) body = chainDB.getBlockBody(blockHash)
dumpBlockState(chainDB, header, body) dumpBlockState(chainDB, header, body)
@ -96,7 +92,7 @@ proc setupDebugRpc*(chainDB: BaseChainDB, rpcsrv: RpcServer) =
let let
header = chainDB.headerFromTag(quantityTag) header = chainDB.headerFromTag(quantityTag)
blockHash = chainDB.getBlockHash(header.blockNumber) blockHash = chainDB.getBlockHash(header.blockNumber)
body = getBlockBody(blockHash) body = chainDB.getBlockBody(blockHash)
flags = traceOptionsToFlags(options) flags = traceOptionsToFlags(options)
traceBlock(chainDB, header, body, flags) traceBlock(chainDB, header, body, flags)
@ -111,7 +107,7 @@ proc setupDebugRpc*(chainDB: BaseChainDB, rpcsrv: RpcServer) =
h = data.toHash h = data.toHash
header = chainDB.getBlockHeader(h) header = chainDB.getBlockHeader(h)
blockHash = chainDB.getBlockHash(header.blockNumber) blockHash = chainDB.getBlockHash(header.blockNumber)
body = getBlockBody(blockHash) body = chainDB.getBlockBody(blockHash)
flags = traceOptionsToFlags(options) flags = traceOptionsToFlags(options)
traceBlock(chainDB, header, body, flags) traceBlock(chainDB, header, body, flags)

View File

@ -6,25 +6,15 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import import
unittest, json, os, tables, strutils, times, strformat, unittest, json, os, tables, strformat,
eth_common, byteutils, nimcrypto, rlp, eth_common, byteutils, eth_trie/db,
eth_trie/[hexary, db, defs] ./test_helpers, ../nimbus/db/db_chain, ../nimbus/[tracer, vm_types]
import
./test_helpers,
../nimbus/db/[storage_types, db_chain, state_db, capturedb],
../nimbus/[transaction, rpc/hexstrings],
../nimbus/[tracer, vm_types, utils]
proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus) proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus)
suite "tracer json tests": suite "tracer json tests":
jsonTest("TracerTests", testFixture) jsonTest("TracerTests", testFixture)
proc getBlockBody(chainDB: BaseChainDB, hash: Hash256): BlockBody =
if not chainDB.getBlockBody(hash, result):
raise newException(ValueError, "Error when retrieving block body")
# use tracerTestGen.nim to generate additional test data # use tracerTestGen.nim to generate additional test data
proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus) = proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus) =

View File

@ -1,14 +1,7 @@
import import
json, eth_common, json, os, eth_common, stint, chronicles,
stint, strutils, times, byteutils, nimcrypto, eth_trie/[db], ../nimbus/db/[db_chain, capturedb],
chronicles, options, os, rlp ../nimbus/[tracer, vm_types, config]
import
eth_trie/[hexary, db, defs],
../nimbus/db/[storage_types, db_chain, state_db, capturedb],
../nimbus/[genesis, utils, config],
../nimbus/[p2p/chain, transaction, rpc/hexstrings],
../nimbus/[tracer, vm_types]
const backEnd {.strdefine.} = "lmdb" const backEnd {.strdefine.} = "lmdb"
@ -19,10 +12,6 @@ elif backEnd == "rocksdb":
else: else:
import ../nimbus/db/backends/lmdb_backend import ../nimbus/db/backends/lmdb_backend
proc getBlockBody(chainDB: BaseChainDB, hash: Hash256): BlockBody =
if not chainDB.getBlockBody(hash, result):
raise newException(ValueError, "Error when retrieving block body")
proc dumpTest(chainDB: BaseChainDB, blockNumber: int) = proc dumpTest(chainDB: BaseChainDB, blockNumber: int) =
var var
memoryDB = newMemoryDB() memoryDB = newMemoryDB()