added missing eth_getBalance rpc method and tests for it
This commit is contained in:
parent
a9a1c80fac
commit
e09de6ea3d
@ -449,6 +449,10 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
|
|||||||
self.state().code(address)
|
self.state().code(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn balance(&self, address: &Address) -> U256 {
|
||||||
|
self.state().balance(address)
|
||||||
|
}
|
||||||
|
|
||||||
fn transaction(&self, id: TransactionId) -> Option<LocalizedTransaction> {
|
fn transaction(&self, id: TransactionId) -> Option<LocalizedTransaction> {
|
||||||
match id {
|
match id {
|
||||||
TransactionId::Hash(ref hash) => self.chain.transaction_address(hash),
|
TransactionId::Hash(ref hash) => self.chain.transaction_address(hash),
|
||||||
|
@ -66,6 +66,9 @@ pub trait BlockChainClient : Sync + Send {
|
|||||||
/// Get address code.
|
/// Get address code.
|
||||||
fn code(&self, address: &Address) -> Option<Bytes>;
|
fn code(&self, address: &Address) -> Option<Bytes>;
|
||||||
|
|
||||||
|
/// Get address balance.
|
||||||
|
fn balance(&self, address: &Address) -> U256;
|
||||||
|
|
||||||
/// Get transaction with given hash.
|
/// Get transaction with given hash.
|
||||||
fn transaction(&self, id: TransactionId) -> Option<LocalizedTransaction>;
|
fn transaction(&self, id: TransactionId) -> Option<LocalizedTransaction>;
|
||||||
|
|
||||||
|
@ -40,6 +40,8 @@ pub struct TestBlockChainClient {
|
|||||||
pub last_hash: RwLock<H256>,
|
pub last_hash: RwLock<H256>,
|
||||||
/// Difficulty.
|
/// Difficulty.
|
||||||
pub difficulty: RwLock<U256>,
|
pub difficulty: RwLock<U256>,
|
||||||
|
/// Balances.
|
||||||
|
pub balances: RwLock<HashMap<Address, U256>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -65,12 +67,17 @@ impl TestBlockChainClient {
|
|||||||
genesis_hash: H256::new(),
|
genesis_hash: H256::new(),
|
||||||
last_hash: RwLock::new(H256::new()),
|
last_hash: RwLock::new(H256::new()),
|
||||||
difficulty: RwLock::new(From::from(0)),
|
difficulty: RwLock::new(From::from(0)),
|
||||||
|
balances: RwLock::new(HashMap::new()),
|
||||||
};
|
};
|
||||||
client.add_blocks(1, EachBlockWith::Nothing); // add genesis block
|
client.add_blocks(1, EachBlockWith::Nothing); // add genesis block
|
||||||
client.genesis_hash = client.last_hash.read().unwrap().clone();
|
client.genesis_hash = client.last_hash.read().unwrap().clone();
|
||||||
client
|
client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_balance(&mut self, address: Address, balance: U256) {
|
||||||
|
self.balances.write().unwrap().insert(address, balance);
|
||||||
|
}
|
||||||
|
|
||||||
/// Add blocks to test client.
|
/// Add blocks to test client.
|
||||||
pub fn add_blocks(&mut self, count: usize, with: EachBlockWith) {
|
pub fn add_blocks(&mut self, count: usize, with: EachBlockWith) {
|
||||||
let len = self.numbers.read().unwrap().len();
|
let len = self.numbers.read().unwrap().len();
|
||||||
@ -165,6 +172,10 @@ impl BlockChainClient for TestBlockChainClient {
|
|||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn balance(&self, address: &Address) -> U256 {
|
||||||
|
self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)
|
||||||
|
}
|
||||||
|
|
||||||
fn transaction(&self, _id: TransactionId) -> Option<LocalizedTransaction> {
|
fn transaction(&self, _id: TransactionId) -> Option<LocalizedTransaction> {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
|
@ -170,6 +170,11 @@ impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn balance(&self, params: Params) -> Result<Value, Error> {
|
||||||
|
from_params::<(Address, BlockNumber)>(params)
|
||||||
|
.and_then(|(address, _block_number)| to_value(&take_weak!(self.client).balance(&address)))
|
||||||
|
}
|
||||||
|
|
||||||
fn block_transaction_count_by_hash(&self, params: Params) -> Result<Value, Error> {
|
fn block_transaction_count_by_hash(&self, params: Params) -> Result<Value, Error> {
|
||||||
from_params::<(H256,)>(params)
|
from_params::<(H256,)>(params)
|
||||||
.and_then(|(hash,)| match take_weak!(self.client).block(BlockId::Hash(hash)) {
|
.and_then(|(hash,)| match take_weak!(self.client).block(BlockId::Hash(hash)) {
|
||||||
|
@ -17,7 +17,8 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use jsonrpc_core::IoHandler;
|
use jsonrpc_core::IoHandler;
|
||||||
use util::hash::{Address};
|
use util::hash::Address;
|
||||||
|
use util::numbers::U256;
|
||||||
use ethcore::client::{TestBlockChainClient, EachBlockWith};
|
use ethcore::client::{TestBlockChainClient, EachBlockWith};
|
||||||
use v1::{Eth, EthClient};
|
use v1::{Eth, EthClient};
|
||||||
use v1::tests::helpers::{TestAccount, TestAccountProvider, TestSyncProvider, Config};
|
use v1::tests::helpers::{TestAccount, TestAccountProvider, TestSyncProvider, Config};
|
||||||
@ -25,6 +26,7 @@ use v1::tests::helpers::{TestAccount, TestAccountProvider, TestSyncProvider, Con
|
|||||||
fn blockchain_client() -> Arc<TestBlockChainClient> {
|
fn blockchain_client() -> Arc<TestBlockChainClient> {
|
||||||
let mut client = TestBlockChainClient::new();
|
let mut client = TestBlockChainClient::new();
|
||||||
client.add_blocks(10, EachBlockWith::Nothing);
|
client.add_blocks(10, EachBlockWith::Nothing);
|
||||||
|
client.set_balance(Address::from(1), U256::from(5));
|
||||||
Arc::new(client)
|
Arc::new(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,3 +59,24 @@ fn rpc_eth_accounts() {
|
|||||||
|
|
||||||
assert_eq!(io.handle_request(request), Some(response.to_owned()));
|
assert_eq!(io.handle_request(request), Some(response.to_owned()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rpc_eth_balance() {
|
||||||
|
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);
|
||||||
|
|
||||||
|
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!(io.handle_request(request), Some(response.to_owned()));
|
||||||
|
}
|
||||||
|
@ -130,7 +130,7 @@ pub trait Eth: Sized + Send + Sync + 'static {
|
|||||||
delegate.add_method("eth_gasPrice", Eth::gas_price);
|
delegate.add_method("eth_gasPrice", Eth::gas_price);
|
||||||
delegate.add_method("eth_accounts", Eth::accounts);
|
delegate.add_method("eth_accounts", Eth::accounts);
|
||||||
delegate.add_method("eth_blockNumber", Eth::block_number);
|
delegate.add_method("eth_blockNumber", Eth::block_number);
|
||||||
delegate.add_method("eth_balance", Eth::balance);
|
delegate.add_method("eth_getBalance", Eth::balance);
|
||||||
delegate.add_method("eth_getStorageAt", Eth::storage_at);
|
delegate.add_method("eth_getStorageAt", Eth::storage_at);
|
||||||
delegate.add_method("eth_getTransactionCount", Eth::transaction_count);
|
delegate.add_method("eth_getTransactionCount", Eth::transaction_count);
|
||||||
delegate.add_method("eth_getBlockTransactionCountByHash", Eth::block_transaction_count_by_hash);
|
delegate.add_method("eth_getBlockTransactionCountByHash", Eth::block_transaction_count_by_hash);
|
||||||
|
Loading…
Reference in New Issue
Block a user