Merge branch 'lightrpc' into light-txq

This commit is contained in:
Robert Habermeier 2017-02-08 20:51:11 +01:00
commit 869d193d21
8 changed files with 41 additions and 23 deletions

View File

@ -211,7 +211,7 @@ impl HeaderChain {
/// Get a block header. In the case of query by number, only canonical blocks /// Get a block header. In the case of query by number, only canonical blocks
/// will be returned. /// will be returned.
pub fn get_header(&self, id: BlockId) -> Option<encoded::Header> { pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
match id { match id {
BlockId::Earliest | BlockId::Number(0) => Some(self.genesis_header.clone()), BlockId::Earliest | BlockId::Number(0) => Some(self.genesis_header.clone()),
BlockId::Hash(hash) => self.headers.read().get(&hash).cloned(), BlockId::Hash(hash) => self.headers.read().get(&hash).cloned(),
@ -238,7 +238,7 @@ impl HeaderChain {
/// Get the best block's header. /// Get the best block's header.
pub fn best_header(&self) -> encoded::Header { pub fn best_header(&self) -> encoded::Header {
self.get_header(BlockId::Latest).expect("Header for best block always stored; qed") self.block_header(BlockId::Latest).expect("Header for best block always stored; qed")
} }
/// Get the nth CHT root, if it's been computed. /// Get the nth CHT root, if it's been computed.
@ -324,8 +324,8 @@ mod tests {
rolling_timestamp += 10; rolling_timestamp += 10;
} }
assert!(chain.get_header(BlockId::Number(10)).is_none()); assert!(chain.block_header(BlockId::Number(10)).is_none());
assert!(chain.get_header(BlockId::Number(9000)).is_some()); assert!(chain.block_header(BlockId::Number(9000)).is_some());
assert!(chain.cht_root(2).is_some()); assert!(chain.cht_root(2).is_some());
assert!(chain.cht_root(3).is_none()); assert!(chain.cht_root(3).is_none());
} }
@ -394,7 +394,7 @@ mod tests {
assert_eq!(num, 12); assert_eq!(num, 12);
while num > 0 { while num > 0 {
let header = chain.get_header(BlockId::Number(num)).unwrap(); let header = chain.block_header(BlockId::Number(num)).unwrap();
assert_eq!(header.hash(), canon_hash); assert_eq!(header.hash(), canon_hash);
canon_hash = header.parent_hash(); canon_hash = header.parent_hash();
@ -409,8 +409,8 @@ mod tests {
let chain = HeaderChain::new(&::rlp::encode(&genesis_header)); let chain = HeaderChain::new(&::rlp::encode(&genesis_header));
assert!(chain.get_header(BlockId::Earliest).is_some()); assert!(chain.block_header(BlockId::Earliest).is_some());
assert!(chain.get_header(BlockId::Latest).is_some()); assert!(chain.block_header(BlockId::Latest).is_some());
assert!(chain.get_header(BlockId::Pending).is_some()); assert!(chain.block_header(BlockId::Pending).is_some());
} }
} }

View File

@ -155,8 +155,8 @@ impl Client {
} }
/// Get a block header by Id. /// Get a block header by Id.
pub fn get_header(&self, id: BlockId) -> Option<encoded::Header> { pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
self.chain.get_header(id) self.chain.block_header(id)
} }
/// Flush the header queue. /// Flush the header queue.
@ -252,7 +252,7 @@ impl Provider for Client {
} }
fn block_header(&self, id: BlockId) -> Option<encoded::Header> { fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
self.chain.get_header(id) Client::block_header(self, id)
} }
fn block_body(&self, _id: BlockId) -> Option<encoded::Body> { fn block_body(&self, _id: BlockId) -> Option<encoded::Body> {

View File

@ -28,6 +28,7 @@ use ethcore::miner::{Miner, ExternalMiner};
use ethcore::snapshot::SnapshotService; use ethcore::snapshot::SnapshotService;
use ethcore_rpc::{Metadata, NetworkSettings}; use ethcore_rpc::{Metadata, NetworkSettings};
use ethcore_rpc::informant::{Middleware, RpcStats, ClientNotifier}; use ethcore_rpc::informant::{Middleware, RpcStats, ClientNotifier};
use ethcore_rpc::dispatch::FullDispatcher;
use ethsync::{ManageNetwork, SyncProvider}; use ethsync::{ManageNetwork, SyncProvider};
use hash_fetch::fetch::Client as FetchClient; use hash_fetch::fetch::Client as FetchClient;
use jsonrpc_core::{MetaIoHandler}; use jsonrpc_core::{MetaIoHandler};
@ -176,10 +177,11 @@ macro_rules! add_signing_methods {
{ {
let handler = &mut $handler; let handler = &mut $handler;
let deps = &$deps; let deps = &$deps;
let dispatcher = FullDispatcher::new(Arc::downgrade(&deps.client), Arc::downgrade(&deps.miner));
if deps.signer_service.is_enabled() { if deps.signer_service.is_enabled() {
handler.extend_with($namespace::to_delegate(SigningQueueClient::new(&deps.signer_service, &deps.client, &deps.miner, &deps.secret_store))) handler.extend_with($namespace::to_delegate(SigningQueueClient::new(&deps.signer_service, dispatcher, &deps.secret_store)))
} else { } else {
handler.extend_with($namespace::to_delegate(SigningUnsafeClient::new(&deps.client, &deps.secret_store, &deps.miner))) handler.extend_with($namespace::to_delegate(SigningUnsafeClient::new(&deps.secret_store, dispatcher)))
} }
} }
} }
@ -194,6 +196,8 @@ pub fn setup_rpc(stats: Arc<RpcStats>, deps: Arc<Dependencies>, apis: ApiSet) ->
// it's turned into vector, cause ont of the cases requires &[] // it's turned into vector, cause ont of the cases requires &[]
let apis = apis.list_apis().into_iter().collect::<Vec<_>>(); let apis = apis.list_apis().into_iter().collect::<Vec<_>>();
let dispatcher = FullDispatcher::new(Arc::downgrade(&deps.client), Arc::downgrade(&deps.miner));
for api in &apis { for api in &apis {
match *api { match *api {
Api::Web3 => { Api::Web3 => {
@ -223,10 +227,10 @@ pub fn setup_rpc(stats: Arc<RpcStats>, deps: Arc<Dependencies>, apis: ApiSet) ->
add_signing_methods!(EthSigning, handler, deps); add_signing_methods!(EthSigning, handler, deps);
}, },
Api::Personal => { Api::Personal => {
handler.extend_with(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner, deps.geth_compatibility).to_delegate()); handler.extend_with(PersonalClient::new(&deps.secret_store, dispatcher.clone(), deps.geth_compatibility).to_delegate());
}, },
Api::Signer => { Api::Signer => {
handler.extend_with(SignerClient::new(&deps.secret_store, &deps.client, &deps.miner, &deps.signer_service).to_delegate()); handler.extend_with(SignerClient::new(&deps.secret_store, dispatcher.clone(), &deps.signer_service).to_delegate());
}, },
Api::Parity => { Api::Parity => {
let signer = match deps.signer_service.is_enabled() { let signer = match deps.signer_service.is_enabled() {

View File

@ -65,7 +65,7 @@ use jsonrpc_core::reactor::RpcHandler;
pub use ipc::{Server as IpcServer, Error as IpcServerError}; pub use ipc::{Server as IpcServer, Error as IpcServerError};
pub use jsonrpc_http_server::{ServerBuilder, Server, RpcServerError}; pub use jsonrpc_http_server::{ServerBuilder, Server, RpcServerError};
pub mod v1; pub mod v1;
pub use v1::{SigningQueue, SignerService, ConfirmationsQueue, NetworkSettings, Metadata, Origin, informant}; pub use v1::{SigningQueue, SignerService, ConfirmationsQueue, NetworkSettings, Metadata, Origin, informant, dispatch};
pub use v1::block_import::is_major_importing; pub use v1::block_import::is_major_importing;
/// Start http server asynchronously and returns result with `Server` handle on success or an error. /// Start http server asynchronously and returns result with `Server` handle on success or an error.

View File

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Utilities and helpers for transaction dispatch.
use std::fmt::Debug; use std::fmt::Debug;
use std::ops::Deref; use std::ops::Deref;
use std::sync::Weak; use std::sync::Weak;
@ -152,20 +154,29 @@ impl<C: MiningBlockChainClient, M: MinerService> Dispatcher for FullDispatcher<C
} }
} }
/// default MAC to use.
pub const DEFAULT_MAC: [u8; 2] = [0, 0]; pub const DEFAULT_MAC: [u8; 2] = [0, 0];
type AccountToken = String; /// Single-use account token.
pub type AccountToken = String;
/// Values used to unlock accounts for signing.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum SignWith { pub enum SignWith {
/// Nothing -- implies the account is already unlocked.
Nothing, Nothing,
/// Unlock with password.
Password(String), Password(String),
/// Unlock with single-use token.
Token(AccountToken), Token(AccountToken),
} }
/// A value, potentially accompanied by a signing token.
#[derive(Debug)] #[derive(Debug)]
pub enum WithToken<T: Debug> { pub enum WithToken<T: Debug> {
/// No token.
No(T), No(T),
/// With token.
Yes(T, AccountToken), Yes(T, AccountToken),
} }
@ -181,6 +192,7 @@ impl<T: Debug> Deref for WithToken<T> {
} }
impl<T: Debug> WithToken<T> { impl<T: Debug> WithToken<T> {
/// Map the value with the given closure, preserving the token.
pub fn map<S, F>(self, f: F) -> WithToken<S> where pub fn map<S, F>(self, f: F) -> WithToken<S> where
S: Debug, S: Debug,
F: FnOnce(T) -> S, F: FnOnce(T) -> S,
@ -191,6 +203,7 @@ impl<T: Debug> WithToken<T> {
} }
} }
/// Convert into inner value, ignoring possible token.
pub fn into_value(self) -> T { pub fn into_value(self) -> T {
match self { match self {
WithToken::No(v) => v, WithToken::No(v) => v,
@ -222,6 +235,7 @@ impl<T: Debug> From<(T, Option<AccountToken>)> for WithToken<T> {
} }
} }
/// Execute a confirmation payload.
pub fn execute<D: Dispatcher + 'static>( pub fn execute<D: Dispatcher + 'static>(
dispatcher: D, dispatcher: D,
accounts: &AccountProvider, accounts: &AccountProvider,

View File

@ -88,7 +88,7 @@ impl EthClient {
/// Get a block header from the on demand service or client, or error. /// Get a block header from the on demand service or client, or error.
fn header(&self, id: BlockId) -> BoxFuture<Option<encoded::Header>, Error> { fn header(&self, id: BlockId) -> BoxFuture<Option<encoded::Header>, Error> {
if let Some(h) = self.client.get_header(id) { if let Some(h) = self.client.block_header(id) {
return future::ok(Some(h)).boxed() return future::ok(Some(h)).boxed()
} }

View File

@ -38,5 +38,5 @@ pub mod types;
pub use self::traits::{Web3, Eth, EthFilter, EthSigning, Net, Parity, ParityAccounts, ParitySet, ParitySigning, Signer, Personal, Traces, Rpc}; pub use self::traits::{Web3, Eth, EthFilter, EthSigning, Net, Parity, ParityAccounts, ParitySet, ParitySigning, Signer, Personal, Traces, Rpc};
pub use self::impls::*; pub use self::impls::*;
pub use self::helpers::{SigningQueue, SignerService, ConfirmationsQueue, NetworkSettings, block_import, informant}; pub use self::helpers::{SigningQueue, SignerService, ConfirmationsQueue, NetworkSettings, block_import, informant, dispatch};
pub use self::metadata::{Metadata, Origin}; pub use self::metadata::{Metadata, Origin};

View File

@ -28,7 +28,7 @@ fn basic_sync() {
net.sync(); net.sync();
assert!(net.peer(0).light_chain().get_header(BlockId::Number(6000)).is_some()); assert!(net.peer(0).light_chain().block_header(BlockId::Number(6000)).is_some());
} }
#[test] #[test]
@ -49,15 +49,15 @@ fn fork_post_cht() {
let _ = light_chain.import_header(header); let _ = light_chain.import_header(header);
light_chain.flush_queue(); light_chain.flush_queue();
light_chain.import_verified(); light_chain.import_verified();
assert!(light_chain.get_header(id).is_some()); assert!(light_chain.block_header(id).is_some());
} }
net.sync(); net.sync();
for id in (0..CHAIN_LENGTH).map(|x| x + 1).map(BlockId::Number) { for id in (0..CHAIN_LENGTH).map(|x| x + 1).map(BlockId::Number) {
assert_eq!( assert_eq!(
net.peer(0).light_chain().get_header(id), net.peer(0).light_chain().block_header(id).unwrap(),
net.peer(2).chain().block_header(id).map(|h| h.into_inner()) net.peer(2).chain().block_header(id).unwrap()
); );
} }
} }