Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contract call and contract creation refactor #252

Merged
merged 12 commits into from
Feb 27, 2019
Merged
Next Next commit
refactor utils
  • Loading branch information
jangko committed Feb 27, 2019
commit 97d03ed8d37028881b0444b4df5f9f15b0070f1f
51 changes: 26 additions & 25 deletions nimbus/db/db_chain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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.} =
Expand All @@ -273,4 +275,3 @@ proc lookupBlockHash*(self: BaseChainDB; n: BlockNumber): Hash256 {.deprecated.}

proc getCanonicalBlockHeaderByNumber*(self: BaseChainDB; n: BlockNumber): BlockHeader {.deprecated.} =
self.getBlockHeader(n)

8 changes: 4 additions & 4 deletions nimbus/db/state_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions nimbus/rpc/p2p.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
13 changes: 1 addition & 12 deletions nimbus/tracer.nim
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion nimbus/transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand Down Expand Up @@ -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
11 changes: 10 additions & 1 deletion nimbus/utils.nim
Original file line number Diff line number Diff line change
@@ -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())
Expand All @@ -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)
4 changes: 0 additions & 4 deletions nimbus/utils/addresses.nim

This file was deleted.

6 changes: 0 additions & 6 deletions nimbus/utils/header.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 0 additions & 19 deletions nimbus/utils/keccak.nim

This file was deleted.

2 changes: 1 addition & 1 deletion nimbus/vm/interpreter/opcodes_impl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion nimbus/vm_state.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]())
Expand Down
9 changes: 5 additions & 4 deletions nimbus/vm_state_transactions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -184,3 +184,4 @@ method applyTransaction*(
else:
var (computation, blockHeader) = vmState.executeTransaction(transaction)
return (computation, nil, initTable[string, string]())
]#
2 changes: 2 additions & 0 deletions premix/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
*.db-lock
/output
/temp
/data
/nimcache
*.json
premixData.js
11 changes: 5 additions & 6 deletions premix/hunter.nim
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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

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 =
Expand Down
2 changes: 1 addition & 1 deletion premix/premixcore.nim
Original file line number Diff line number Diff line change
@@ -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) =
Expand Down
5 changes: 2 additions & 3 deletions tests/macro_assembler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/test_generalstate_json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Loading