diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 2cdbb0a2b..23fef5d06 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -29,6 +29,10 @@ json-ipc-server = { git = "https://github.com/ethcore/json-ipc-server.git" } serde_codegen = { version = "0.7.0", optional = true } syntex = "^0.32.0" +[dev-dependencies] +ethjson = { path = "../json" } +ethcore-devtools = { path = "../devtools" } + [features] default = ["serde_codegen"] nightly = ["serde_macros"] diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 7d9818615..24d58819c 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -33,6 +33,11 @@ extern crate ethminer; extern crate transient_hashmap; extern crate json_ipc_server as ipc; +#[cfg(test)] +extern crate ethjson; +#[cfg(test)] +extern crate ethcore_devtools as devtools; + use std::sync::Arc; use std::net::SocketAddr; use self::jsonrpc_core::{IoHandler, IoDelegate}; diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs deleted file mode 100644 index e69de29bb..000000000 diff --git a/rpc/src/v1/tests/helpers/sync_provider.rs b/rpc/src/v1/tests/helpers/sync_provider.rs index fc81586dd..114b5b08f 100644 --- a/rpc/src/v1/tests/helpers/sync_provider.rs +++ b/rpc/src/v1/tests/helpers/sync_provider.rs @@ -16,9 +16,9 @@ //! Test implementation of SyncProvider. -use util::{U256}; +use util::U256; use ethsync::{SyncProvider, SyncStatus, SyncState}; -use std::sync::{RwLock}; +use std::sync::RwLock; /// TestSyncProvider config. pub struct Config { diff --git a/rpc/src/v1/tests/integration/eth.rs b/rpc/src/v1/tests/integration/eth.rs new file mode 100644 index 000000000..d402f2b08 --- /dev/null +++ b/rpc/src/v1/tests/integration/eth.rs @@ -0,0 +1,100 @@ +// Copyright 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! rpc integration tests. +use std::collections::HashMap; +use std::sync::Arc; + +use ethjson::blockchain::test::Test; +use ethcore::client::{BlockChainClient, Client, ClientConfig}; +use ethcore::spec::Genesis; +use ethcore::block::Block; +use ethcore::ethereum; +use ethminer::ExternalMiner; +use devtools::RandomTempPath; +use util::io::IoChannel; +use util::hash::{Address, FixedHash}; +use util::numbers::U256; +use util::keys::{TestAccount, TestAccountProvider}; +use jsonrpc_core::IoHandler; + +use v1::traits::eth::Eth; +use v1::impls::EthClient; +use v1::tests::helpers::{TestSyncProvider, Config, TestMinerService}; + +use super::RPC_CHAIN; + +#[test] +fn harness_works() { + eth_harness(|_| {}); +} + +fn account_provider() -> Arc { + let mut accounts = HashMap::new(); + accounts.insert(Address::from(1), TestAccount::new("test")); + let ap = TestAccountProvider::new(accounts); + Arc::new(ap) +} + +fn sync_provider() -> Arc { + Arc::new(TestSyncProvider::new(Config { + network_id: U256::from(3), + num_peers: 120, + })) +} + +fn miner_service() -> Arc { + Arc::new(TestMinerService::default()) +} + +// this harness will create a handler which tests can send specially-crafted +// JSONRPC requests to. +fn eth_harness(mut cb: F) -> U + where F: FnMut(&IoHandler) -> U { + let chains = Test::load(RPC_CHAIN).unwrap(); + let chain = chains.into_iter().next().unwrap().1; + let genesis = Genesis::from(chain.genesis()); + let mut spec = ethereum::new_frontier_test(); + let state = chain.pre_state.clone().into(); + spec.set_genesis_state(state); + spec.overwrite_genesis_params(genesis); + assert!(spec.is_state_root_valid()); + + let dir = RandomTempPath::new(); + let client = Client::new(ClientConfig::default(), spec, dir.as_path(), IoChannel::disconnected()).unwrap(); + let sync_provider = sync_provider(); + let miner_service = miner_service(); + let account_provider = account_provider(); + let external_miner = Arc::new(ExternalMiner::default()); + + for b in &chain.blocks_rlp() { + if Block::is_good(&b) { + let _ = client.import_block(b.clone()); + client.flush_queue(); + client.import_verified_blocks(&IoChannel::disconnected()); + } + } + + assert!(client.chain_info().best_block_hash == chain.best_block.into()); + + let eth_client = EthClient::new(&client, &sync_provider, &account_provider, + &miner_service, &external_miner); + + let handler = IoHandler::new(); + let delegate = eth_client.to_delegate(); + handler.add_delegate(delegate); + cb(&handler) +} diff --git a/rpc/src/v1/tests/integration/mod.rs b/rpc/src/v1/tests/integration/mod.rs new file mode 100644 index 000000000..8a67d78dd --- /dev/null +++ b/rpc/src/v1/tests/integration/mod.rs @@ -0,0 +1,21 @@ +// Copyright 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Integration tests for the JSONRPC APIs + +mod eth; + +const RPC_CHAIN: &'static [u8] = include_bytes!("../../../../../ethcore/res/ethereum/tests/BlockchainTests/bcRPC_API_Test.json"); \ No newline at end of file diff --git a/rpc/src/v1/tests/mod.rs b/rpc/src/v1/tests/mod.rs index f5e7d1404..78a6a674f 100644 --- a/rpc/src/v1/tests/mod.rs +++ b/rpc/src/v1/tests/mod.rs @@ -5,4 +5,4 @@ pub mod helpers; #[cfg(test)] mod mocked; #[cfg(test)] -mod eth; \ No newline at end of file +mod integration;