Next nonce RPC (#2917)

This commit is contained in:
Tomasz Drwięga 2016-10-27 19:29:55 +02:00 committed by Gav Wood
parent dedc4d5cfc
commit 07fca24b44
3 changed files with 40 additions and 1 deletions

View File

@ -334,4 +334,17 @@ impl<C, M, S: ?Sized, F> Ethcore for EthcoreClient<C, M, S, F> where
self.dapps_port
.ok_or_else(|| errors::dapps_disabled())
}
fn next_nonce(&self, address: H160) -> Result<U256, Error> {
try!(self.active());
let address: Address = address.into();
let miner = take_weak!(self.miner);
let client = take_weak!(self.client);
Ok(miner.last_nonce(&address)
.map(|n| n + 1.into())
.unwrap_or_else(|| client.latest_nonce(&address))
.into()
)
}
}

View File

@ -16,7 +16,7 @@
use std::sync::Arc;
use util::log::RotatingLogger;
use util::U256;
use util::{U256, Address};
use ethsync::ManageNetwork;
use ethcore::client::{TestBlockChainClient};
use ethstore::ethkey::{Generator, Random};
@ -320,3 +320,25 @@ fn rpc_ethcore_dapps_port() {
assert_eq!(io1.handle_request_sync(request), Some(response1.to_owned()));
assert_eq!(io2.handle_request_sync(request), Some(response2.to_owned()));
}
#[test]
fn rpc_ethcore_next_nonce() {
let deps = Dependencies::new();
let address = Address::default();
let io1 = deps.default_client();
let deps = Dependencies::new();
deps.miner.last_nonces.write().insert(address.clone(), 2.into());
let io2 = deps.default_client();
let request = r#"{
"jsonrpc": "2.0",
"method": "ethcore_nextNonce",
"params": [""#.to_owned() + &format!("0x{:?}", address) + r#""],
"id": 1
}"#;
let response1 = r#"{"jsonrpc":"2.0","result":"0x0","id":1}"#;
let response2 = r#"{"jsonrpc":"2.0","result":"0x3","id":1}"#;
assert_eq!(io1.handle_request_sync(&request), Some(response1.to_owned()));
assert_eq!(io2.handle_request_sync(&request), Some(response2.to_owned()));
}

View File

@ -125,5 +125,9 @@ build_rpc_trait! {
/// Returns current Dapps Server port or an error if dapps server is disabled.
#[rpc(name = "ethcore_dappsPort")]
fn dapps_port(&self) -> Result<u16, Error>;
/// Returns next nonce for particular sender. Should include all transactions in the queue.
#[rpc(name = "ethcore_nextNonce")]
fn next_nonce(&self, H160) -> Result<U256, Error>;
}
}