This repository was archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #691 from ethcore/rpc_tests
eth_accounts, eth_getBalance rpc functions && tests
- Loading branch information
Showing
10 changed files
with
210 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2015, 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 <http://www.gnu.org/licenses/>. | ||
|
||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use jsonrpc_core::IoHandler; | ||
use util::hash::Address; | ||
use util::numbers::U256; | ||
use ethcore::client::{TestBlockChainClient, EachBlockWith}; | ||
use v1::{Eth, EthClient}; | ||
use v1::tests::helpers::{TestAccount, TestAccountProvider, TestSyncProvider, Config}; | ||
|
||
fn blockchain_client() -> Arc<TestBlockChainClient> { | ||
let mut client = TestBlockChainClient::new(); | ||
client.add_blocks(10, EachBlockWith::Nothing); | ||
client.set_balance(Address::from(1), U256::from(5)); | ||
Arc::new(client) | ||
} | ||
|
||
fn accounts_provider() -> Arc<TestAccountProvider> { | ||
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<TestSyncProvider> { | ||
Arc::new(TestSyncProvider::new(Config { | ||
protocol_version: 65, | ||
num_peers: 120, | ||
})) | ||
} | ||
|
||
struct EthTester { | ||
client: Arc<TestBlockChainClient>, | ||
sync: Arc<TestSyncProvider>, | ||
accounts_provider: Arc<TestAccountProvider>, | ||
pub io: IoHandler, | ||
} | ||
|
||
impl Default for EthTester { | ||
fn default() -> Self { | ||
let client = blockchain_client(); | ||
let sync = sync_provider(); | ||
let ap = accounts_provider(); | ||
let eth = EthClient::new(&client, &sync, &ap).to_delegate(); | ||
let io = IoHandler::new(); | ||
io.add_delegate(eth); | ||
EthTester { | ||
client: client, | ||
sync: sync, | ||
accounts_provider: ap, | ||
io: io | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn rpc_eth_accounts() { | ||
let request = r#"{"jsonrpc": "2.0", "method": "eth_accounts", "params": [], "id": 1}"#; | ||
let response = r#"{"jsonrpc":"2.0","result":["0x0000000000000000000000000000000000000001"],"id":1}"#; | ||
|
||
assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); | ||
} | ||
|
||
#[test] | ||
fn rpc_eth_balance() { | ||
let request = r#"{ | ||
"jsonrpc": "2.0", | ||
"method": "eth_getBalance", | ||
"params": ["0x0000000000000000000000000000000000000001", "latest"], | ||
"id": 1 | ||
}"#; | ||
let response = r#"{"jsonrpc":"2.0","result":"0x05","id":1}"#; | ||
|
||
assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2015, 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 <http://www.gnu.org/licenses/>. | ||
|
||
use std::sync::RwLock; | ||
use std::collections::HashMap; | ||
use std::io; | ||
use util::hash::{Address, H256}; | ||
use util::crypto::{Secret, Signature}; | ||
use util::keys::store::{AccountProvider, SigningError, EncryptedHashMapError}; | ||
|
||
/// Account mock. | ||
#[derive(Clone)] | ||
pub struct TestAccount { | ||
/// True if account is unlocked. | ||
pub unlocked: bool, | ||
/// Account's password. | ||
pub password: String, | ||
} | ||
|
||
impl TestAccount { | ||
pub fn new(password: &str) -> Self { | ||
TestAccount { | ||
unlocked: false, | ||
password: password.to_owned(), | ||
} | ||
} | ||
} | ||
|
||
/// Test account provider. | ||
pub struct TestAccountProvider { | ||
accounts: RwLock<HashMap<Address, TestAccount>>, | ||
} | ||
|
||
impl TestAccountProvider { | ||
/// Basic constructor. | ||
pub fn new(accounts: HashMap<Address, TestAccount>) -> Self { | ||
TestAccountProvider { | ||
accounts: RwLock::new(accounts), | ||
} | ||
} | ||
} | ||
|
||
impl AccountProvider for TestAccountProvider { | ||
fn accounts(&self) -> Result<Vec<Address>, io::Error> { | ||
Ok(self.accounts.read().unwrap().keys().cloned().collect()) | ||
} | ||
|
||
fn unlock_account(&self, account: &Address, pass: &str) -> Result<(), EncryptedHashMapError> { | ||
match self.accounts.write().unwrap().get_mut(account) { | ||
Some(ref mut acc) if acc.password == pass => { | ||
acc.unlocked = true; | ||
Ok(()) | ||
}, | ||
Some(_) => Err(EncryptedHashMapError::InvalidPassword), | ||
None => Err(EncryptedHashMapError::UnknownIdentifier), | ||
} | ||
} | ||
|
||
fn new_account(&self, _pass: &str) -> Result<Address, io::Error> { | ||
unimplemented!() | ||
} | ||
fn account_secret(&self, _account: &Address) -> Result<Secret, SigningError> { | ||
unimplemented!() | ||
} | ||
|
||
fn sign(&self, _account: &Address, _message: &H256) -> Result<Signature, SigningError> { | ||
unimplemented!() | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
//!TODO: load custom blockchain state and test | ||
mod eth; | ||
mod net; | ||
mod web3; | ||
mod helpers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters