diff --git a/nimbus/rpc/p2p.nim b/nimbus/rpc/p2p.nim index 87d6270fb..b0493bd75 100644 --- a/nimbus/rpc/p2p.nim +++ b/nimbus/rpc/p2p.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2018 Status Research & Development GmbH +# Copyright (c) 2018-2019 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) # * MIT license ([LICENSE-MIT](LICENSE-MIT)) @@ -13,7 +13,7 @@ import eth_common, eth_p2p, eth_keys, eth_trie/db, rlp, ../utils/header, ../transaction, ../config, ../vm_state, ../constants, ../vm_types, ../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] #[ @@ -37,11 +37,10 @@ proc binarySearchGas(vmState: var BaseVMState, transaction: Transaction, sender: 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, @@ -54,53 +53,7 @@ proc binarySearchGas(vmState: var BaseVMState, transaction: Transaction, sender: hiGas = vmState.gasLimit loGas = transaction.intrinsicGas 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 = var spoofTransaction = dummyTransaction(gasLimit, gasPrice, transaction.to, transaction.value) @@ -192,7 +145,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) = let accountDb = accountDbFromTag(quantityTag) addrBytes = data.toAddress - balance = accountDb.get_balance(addrBytes) + balance = accountDb.getBalance(addrBytes) result = balance @@ -366,7 +319,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) = discard comp.execComputation result = ("0x" & nimcrypto.toHex(comp.output)).HexDataStr - + 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. ## 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 call.to.isSome: call.to.get.toAddress else: ZERO_ADDRESS - curState = chain.getStateDb(header.stateRoot, true) + curState = vmState.readOnlyStateDb() nonce = curState.getNonce(sender) value = if call.value.isSome: call.value.get diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index a1623f152..257d524e4 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2018 Status Research & Development GmbH +# 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) @@ -781,8 +781,8 @@ op selfDestruct, inline = false: computation.vmState.mutateStateDB: let - local_balance = db.get_balance(computation.msg.storage_address) - beneficiary_balance = db.get_balance(beneficiary) + local_balance = db.getBalance(computation.msg.storage_address) + beneficiary_balance = db.getBalance(beneficiary) # Transfer to beneficiary db.setBalance(beneficiary, local_balance + beneficiary_balance) diff --git a/tests/all_tests.nim b/tests/all_tests.nim index a481896e6..bfc409964 100644 --- a/tests/all_tests.nim +++ b/tests/all_tests.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2018 Status Research & Development GmbH +# 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) @@ -17,4 +17,5 @@ import ./test_code_stream, ./test_precompiles, ./test_generalstate_json, ./test_tracer_json, - ./test_persistblock_json + ./test_persistblock_json, + ./test_rpc diff --git a/tests/rpcclient/ethcallsigs.nim b/tests/rpcclient/ethcallsigs.nim index 41725de85..a5ad0edf0 100644 --- a/tests/rpcclient/ethcallsigs.nim +++ b/tests/rpcclient/ethcallsigs.nim @@ -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. ## 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. -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_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_getFilterChanges(id: int): seq[WhisperMessage] proc shh_getMessages(id: int): seq[WhisperMessage] -]# \ No newline at end of file +]# diff --git a/tests/rpcclient/test_hexstrings.nim b/tests/rpcclient/test_hexstrings.nim index b5efd4014..1d07dc7ae 100644 --- a/tests/rpcclient/test_hexstrings.nim +++ b/tests/rpcclient/test_hexstrings.nim @@ -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 import unittest, ../../nimbus/rpc/hexstrings, json diff --git a/tests/test_rpc.nim b/tests/test_rpc.nim index 08064984e..cb9bcf1e3 100644 --- a/tests/test_rpc.nim +++ b/tests/test_rpc.nim @@ -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 - unittest, json, strformat, nimcrypto, rlp, options, + unittest, json, strformat, options, + nimcrypto, rlp, eth_trie/db, eth_p2p, eth_keys, json_rpc/[rpcserver, rpcclient], ../nimbus/rpc/[common, p2p, hexstrings, rpc_types], ../nimbus/constants, - ../nimbus/nimbus/[vm_state, config], + ../nimbus/[vm_state, config], ../nimbus/db/[state_db, db_chain, storage_types], eth_common, byteutils, ../nimbus/p2p/chain, - ../nimbus/genesis, - eth_trie/db, - eth_p2p, eth_keys -import rpcclient/test_hexstrings + ../nimbus/genesis, + ./rpcclient/test_hexstrings # Perform checks for hex string validation doHexStrTests() @@ -58,41 +64,41 @@ proc doTests = defaultGenesisBlockForNetwork(conf.net.networkId.toPublicNetwork()).commit(chain) state.mutateStateDB: db.setBalance(address, balance) - assert(canonicalHeadHashKey().toOpenArray in state.chainDb.db) + doAssert(canonicalHeadHashKey().toOpenArray in state.chainDb.db) # Create Ethereum RPCs + let RPC_PORT = 8545 var - rpcServer = newRpcSocketServer(["localhost:8545"]) + rpcServer = newRpcSocketServer(["localhost:" & $RPC_PORT]) client = newRpcSocketClient() setupCommonRpc(rpcServer) setupEthRpc(ethNode, chain, rpcServer) # Begin tests rpcServer.start() - waitFor client.connect("localhost", Port(8545)) + waitFor client.connect("localhost", Port(RPC_PORT)) + # TODO: add more tests here suite "Remote Procedure Calls": - # TODO: Currently returning 'block not found' when fetching header in p2p, so cannot perform tests test "eth_call": let blockNum = state.blockheader.blockNumber callParams = EthCall(value: some(100.u256)) - var r = waitFor client.eth_call(callParams, "0x" & blockNum.toHex) - echo r + r1 = waitFor client.eth_call(callParams, "0x" & blockNum.toHex) + check r1 == "0x" test "eth_getBalance": - expect ValueError: - # check error is raised on null address - var r = waitFor client.eth_getBalance(ZERO_ADDRESS.toEthAddressStr, "0x0") + let r2 = waitFor client.eth_getBalance(ZERO_ADDRESS.toEthAddressStr, "0x0") + check r2 == 0 let blockNum = state.blockheader.blockNumber - var r = waitFor client.eth_getBalance(address.toEthAddressStr, "0x" & blockNum.toHex) - echo r + let r3 = waitFor client.eth_getBalance(address.toEthAddressStr, "0x" & blockNum.toHex) + check r3 == 0 test "eth_estimateGas": let call = EthCall() blockNum = state.blockheader.blockNumber - var r = waitFor client.eth_estimateGas(call, "0x" & blockNum.toHex) - check r == 21_000 + r4 = waitFor client.eth_estimateGas(call, "0x" & blockNum.toHex) + check r4 == 21_000 rpcServer.stop() rpcServer.close()