rebase and various fixes

- port to new APIs
- added copyright headers
- bumped the copyright interval in modified files
- fixed tests
- reorganised imports
- normalised function names
- deleted some trailing space
- added test_rpc to all_tests
- assert() -> doAssert()
- moved the RPC port in a constant for the test suite
This commit is contained in:
Ștefan Talpalaru 2019-01-06 20:19:48 +01:00
parent d19a7f7c04
commit 7c7260552d
No known key found for this signature in database
GPG Key ID: CBF7934204F1B6F9
6 changed files with 56 additions and 79 deletions

View File

@ -1,5 +1,5 @@
# Nimbus # Nimbus
# Copyright (c) 2018 Status Research & Development GmbH # Copyright (c) 2018-2019 Status Research & Development GmbH
# Licensed under either of # Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT)) # * MIT license ([LICENSE-MIT](LICENSE-MIT))
@ -13,7 +13,7 @@ import
eth_common, eth_p2p, eth_keys, eth_trie/db, rlp, eth_common, eth_p2p, eth_keys, eth_trie/db, rlp,
../utils/header, ../transaction, ../config, ../vm_state, ../constants, ../vm_types, ../utils/header, ../transaction, ../config, ../vm_state, ../constants, ../vm_types,
../vm_state_transactions, ../utils/addresses, ../vm_state_transactions, ../utils/addresses,
../vm/[interpreter_dispatch, computation], ../db/[db_chain, state_db, storage_types],
rpc_types, rpc_utils, ../vm/[message, computation, interpreter_dispatch] rpc_types, rpc_utils, ../vm/[message, computation, interpreter_dispatch]
#[ #[
@ -37,11 +37,10 @@ proc binarySearchGas(vmState: var BaseVMState, transaction: Transaction, sender:
proc dummyComputation(vmState: var BaseVMState, transaction: Transaction, sender: EthAddress): BaseComputation = proc dummyComputation(vmState: var BaseVMState, transaction: Transaction, sender: EthAddress): BaseComputation =
# Note that vmState may be altered # Note that vmState may be altered
setupComputation( setupComputation(
vmState.blockHeader,
vmState, vmState,
transaction, transaction,
sender) sender)
proc dummyTransaction(gasLimit, gasPrice: GasInt, destination: EthAddress, value: UInt256): Transaction = proc dummyTransaction(gasLimit, gasPrice: GasInt, destination: EthAddress, value: UInt256): Transaction =
Transaction( Transaction(
accountNonce: 0.AccountNonce, accountNonce: 0.AccountNonce,
@ -54,53 +53,7 @@ proc binarySearchGas(vmState: var BaseVMState, transaction: Transaction, sender:
hiGas = vmState.gasLimit hiGas = vmState.gasLimit
loGas = transaction.intrinsicGas loGas = transaction.intrinsicGas
gasPrice = transaction.gasPrice # TODO: Or zero? gasPrice = transaction.gasPrice # TODO: Or zero?
proc tryTransaction(vmState: var BaseVMState, gasLimit: GasInt): bool =
var
spoofTransaction = dummyTransaction(gasLimit, gasPrice, transaction.to, transaction.value)
computation = vmState.dummyComputation(spoofTransaction, sender)
computation.executeOpcodes
if not computation.isError:
return true
if vmState.tryTransaction(loGas):
return loGas
if not vmState.tryTransaction(hiGas):
return 0.GasInt # TODO: Reraise error from computation
var
minVal = vmState.gasLimit
maxVal = transaction.intrinsicGas
while loGas - hiGas > tolerance:
let midPoint = (loGas + hiGas) div 2
if vmState.tryTransaction(midPoint):
minVal = midPoint
else:
maxVal = midPoint
result = minVal
proc binarySearchGas(vmState: var BaseVMState, transaction: Transaction, sender: EthAddress, gasPrice: GasInt, tolerance = 1): GasInt =
proc dummyComputation(vmState: var BaseVMState, transaction: Transaction, sender: EthAddress): BaseComputation =
# Note that vmState may be altered
setupComputation(
vmState.blockHeader,
vmState,
transaction,
sender)
proc dummyTransaction(gasLimit, gasPrice: GasInt, destination: EthAddress, value: UInt256): Transaction =
Transaction(
accountNonce: 0.AccountNonce,
gasPrice: gasPrice,
gasLimit: gasLimit,
to: destination,
value: value
)
var
hiGas = vmState.gasLimit
loGas = transaction.intrinsicGas
gasPrice = transaction.gasPrice # TODO: Or zero?
proc tryTransaction(vmState: var BaseVMState, gasLimit: GasInt): bool = proc tryTransaction(vmState: var BaseVMState, gasLimit: GasInt): bool =
var var
spoofTransaction = dummyTransaction(gasLimit, gasPrice, transaction.to, transaction.value) spoofTransaction = dummyTransaction(gasLimit, gasPrice, transaction.to, transaction.value)
@ -192,7 +145,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
let let
accountDb = accountDbFromTag(quantityTag) accountDb = accountDbFromTag(quantityTag)
addrBytes = data.toAddress addrBytes = data.toAddress
balance = accountDb.get_balance(addrBytes) balance = accountDb.getBalance(addrBytes)
result = balance result = balance
@ -366,7 +319,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
discard comp.execComputation discard comp.execComputation
result = ("0x" & nimcrypto.toHex(comp.output)).HexDataStr result = ("0x" & nimcrypto.toHex(comp.output)).HexDataStr
rpcsrv.rpc("eth_estimateGas") do(call: EthCall, quantityTag: string) -> GasInt: rpcsrv.rpc("eth_estimateGas") do(call: EthCall, quantityTag: string) -> GasInt:
## Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. ## Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
## The transaction will not be added to the blockchain. Note that the estimate may be significantly more than ## The transaction will not be added to the blockchain. Note that the estimate may be significantly more than
@ -391,7 +344,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
destination = if destination = if
call.to.isSome: call.to.get.toAddress call.to.isSome: call.to.get.toAddress
else: ZERO_ADDRESS else: ZERO_ADDRESS
curState = chain.getStateDb(header.stateRoot, true) curState = vmState.readOnlyStateDb()
nonce = curState.getNonce(sender) nonce = curState.getNonce(sender)
value = if value = if
call.value.isSome: call.value.get call.value.isSome: call.value.get

View File

@ -1,5 +1,5 @@
# Nimbus # Nimbus
# Copyright (c) 2018 Status Research & Development GmbH # Copyright (c) 2018-2019 Status Research & Development GmbH
# Licensed under either of # Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) # * 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) # * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
@ -781,8 +781,8 @@ op selfDestruct, inline = false:
computation.vmState.mutateStateDB: computation.vmState.mutateStateDB:
let let
local_balance = db.get_balance(computation.msg.storage_address) local_balance = db.getBalance(computation.msg.storage_address)
beneficiary_balance = db.get_balance(beneficiary) beneficiary_balance = db.getBalance(beneficiary)
# Transfer to beneficiary # Transfer to beneficiary
db.setBalance(beneficiary, local_balance + beneficiary_balance) db.setBalance(beneficiary, local_balance + beneficiary_balance)

View File

@ -1,5 +1,5 @@
# Nimbus # Nimbus
# Copyright (c) 2018 Status Research & Development GmbH # Copyright (c) 2018-2019 Status Research & Development GmbH
# Licensed under either of # Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) # * 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) # * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
@ -17,4 +17,5 @@ import ./test_code_stream,
./test_precompiles, ./test_precompiles,
./test_generalstate_json, ./test_generalstate_json,
./test_tracer_json, ./test_tracer_json,
./test_persistblock_json ./test_persistblock_json,
./test_rpc

View File

@ -1,7 +1,17 @@
# Nimbus
# Copyright (c) 2018-2019 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.
## This module contains signatures for the Ethereum client RPCs. ## This module contains signatures for the Ethereum client RPCs.
## The signatures are not imported directly, but read and processed with parseStmt, ## The signatures are not imported directly, but read and processed with parseStmt,
## then a procedure body is generated to marshal native Nim parameters to json and visa versa. ## then a procedure body is generated to marshal native Nim parameters to json and visa versa.
import json, stint, eth_common, ../../nimbus/rpc/hexstrings, ../../nimbus/rpc/rpc_types import
json,
stint, eth_common,
../../nimbus/rpc/hexstrings, ../../nimbus/rpc/rpc_types
proc web3_clientVersion(): string proc web3_clientVersion(): string
proc web3_sha3(data: string): string proc web3_sha3(data: string): string
@ -64,4 +74,4 @@ proc shh_newFilter(filterOptions: FilterOptions, to: array[60, byte], topics: se
proc shh_uninstallFilter(id: int): bool proc shh_uninstallFilter(id: int): bool
proc shh_getFilterChanges(id: int): seq[WhisperMessage] proc shh_getFilterChanges(id: int): seq[WhisperMessage]
proc shh_getMessages(id: int): seq[WhisperMessage] proc shh_getMessages(id: int): seq[WhisperMessage]
]# ]#

View File

@ -1,3 +1,10 @@
# Nimbus
# Copyright (c) 2018-2019 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.
# Separated from main tests for brevity # Separated from main tests for brevity
import unittest, ../../nimbus/rpc/hexstrings, json import unittest, ../../nimbus/rpc/hexstrings, json

View File

@ -1,15 +1,21 @@
# Nimbus
# Copyright (c) 2018-2019 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 import
unittest, json, strformat, nimcrypto, rlp, options, unittest, json, strformat, options,
nimcrypto, rlp, eth_trie/db, eth_p2p, eth_keys,
json_rpc/[rpcserver, rpcclient], json_rpc/[rpcserver, rpcclient],
../nimbus/rpc/[common, p2p, hexstrings, rpc_types], ../nimbus/rpc/[common, p2p, hexstrings, rpc_types],
../nimbus/constants, ../nimbus/constants,
../nimbus/nimbus/[vm_state, config], ../nimbus/[vm_state, config],
../nimbus/db/[state_db, db_chain, storage_types], eth_common, byteutils, ../nimbus/db/[state_db, db_chain, storage_types], eth_common, byteutils,
../nimbus/p2p/chain, ../nimbus/p2p/chain,
../nimbus/genesis, ../nimbus/genesis,
eth_trie/db, ./rpcclient/test_hexstrings
eth_p2p, eth_keys
import rpcclient/test_hexstrings
# Perform checks for hex string validation # Perform checks for hex string validation
doHexStrTests() doHexStrTests()
@ -58,41 +64,41 @@ proc doTests =
defaultGenesisBlockForNetwork(conf.net.networkId.toPublicNetwork()).commit(chain) defaultGenesisBlockForNetwork(conf.net.networkId.toPublicNetwork()).commit(chain)
state.mutateStateDB: state.mutateStateDB:
db.setBalance(address, balance) db.setBalance(address, balance)
assert(canonicalHeadHashKey().toOpenArray in state.chainDb.db) doAssert(canonicalHeadHashKey().toOpenArray in state.chainDb.db)
# Create Ethereum RPCs # Create Ethereum RPCs
let RPC_PORT = 8545
var var
rpcServer = newRpcSocketServer(["localhost:8545"]) rpcServer = newRpcSocketServer(["localhost:" & $RPC_PORT])
client = newRpcSocketClient() client = newRpcSocketClient()
setupCommonRpc(rpcServer) setupCommonRpc(rpcServer)
setupEthRpc(ethNode, chain, rpcServer) setupEthRpc(ethNode, chain, rpcServer)
# Begin tests # Begin tests
rpcServer.start() rpcServer.start()
waitFor client.connect("localhost", Port(8545)) waitFor client.connect("localhost", Port(RPC_PORT))
# TODO: add more tests here
suite "Remote Procedure Calls": suite "Remote Procedure Calls":
# TODO: Currently returning 'block not found' when fetching header in p2p, so cannot perform tests
test "eth_call": test "eth_call":
let let
blockNum = state.blockheader.blockNumber blockNum = state.blockheader.blockNumber
callParams = EthCall(value: some(100.u256)) callParams = EthCall(value: some(100.u256))
var r = waitFor client.eth_call(callParams, "0x" & blockNum.toHex) r1 = waitFor client.eth_call(callParams, "0x" & blockNum.toHex)
echo r check r1 == "0x"
test "eth_getBalance": test "eth_getBalance":
expect ValueError: let r2 = waitFor client.eth_getBalance(ZERO_ADDRESS.toEthAddressStr, "0x0")
# check error is raised on null address check r2 == 0
var r = waitFor client.eth_getBalance(ZERO_ADDRESS.toEthAddressStr, "0x0")
let blockNum = state.blockheader.blockNumber let blockNum = state.blockheader.blockNumber
var r = waitFor client.eth_getBalance(address.toEthAddressStr, "0x" & blockNum.toHex) let r3 = waitFor client.eth_getBalance(address.toEthAddressStr, "0x" & blockNum.toHex)
echo r check r3 == 0
test "eth_estimateGas": test "eth_estimateGas":
let let
call = EthCall() call = EthCall()
blockNum = state.blockheader.blockNumber blockNum = state.blockheader.blockNumber
var r = waitFor client.eth_estimateGas(call, "0x" & blockNum.toHex) r4 = waitFor client.eth_estimateGas(call, "0x" & blockNum.toHex)
check r == 21_000 check r4 == 21_000
rpcServer.stop() rpcServer.stop()
rpcServer.close() rpcServer.close()