Client::info() no longer returns a Result (#2776)

This commit is contained in:
Pierre Krieger
2019-06-04 16:09:46 +02:00
committed by Bastian Köcher
parent 53e8ad8728
commit 5df89a8a6f
33 changed files with 235 additions and 274 deletions
+2 -2
View File
@@ -28,7 +28,7 @@ use primitives::{H256, Blake2Hasher, storage::StorageKey};
/// Local client abstraction for the network.
pub trait Client<Block: BlockT>: Send + Sync {
/// Get blockchain info.
fn info(&self) -> Result<ClientInfo<Block>, Error>;
fn info(&self) -> ClientInfo<Block>;
/// Get block status.
fn block_status(&self, id: &BlockId<Block>) -> Result<BlockStatus, Error>;
@@ -81,7 +81,7 @@ impl<B, E, Block, RA> Client<Block> for SubstrateClient<B, E, Block, RA> where
Block: BlockT<Hash=H256>,
RA: Send + Sync
{
fn info(&self) -> Result<ClientInfo<Block>, Error> {
fn info(&self) -> ClientInfo<Block> {
(self as &SubstrateClient<B, E, Block, RA>).info()
}
+14 -15
View File
@@ -399,7 +399,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
checker: Arc<dyn FetchChecker<B>>,
specialization: S,
) -> error::Result<Protocol<B, S, H>> {
let info = chain.info()?;
let info = chain.info();
let sync = ChainSync::new(config.roles, &info);
Ok(Protocol {
tick_timeout: tokio_timer::Interval::new_interval(TICK_TIMEOUT),
@@ -861,8 +861,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
.context_data
.chain
.info()
.ok()
.and_then(|info| info.best_queued_number)
.best_queued_number
.unwrap_or_else(|| Zero::zero());
let blocks_difference = self_best_block
.checked_sub(&status.best_number)
@@ -1008,18 +1007,18 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
/// Send Status message
fn send_status(&mut self, network_out: &mut dyn NetworkOut<B>, who: PeerId) {
if let Ok(info) = self.context_data.chain.info() {
let status = message::generic::Status {
version: CURRENT_VERSION,
min_supported_version: MIN_VERSION,
genesis_hash: info.chain.genesis_hash,
roles: self.config.roles.into(),
best_number: info.chain.best_number,
best_hash: info.chain.best_hash,
chain_status: self.specialization.status(),
};
self.send_message(network_out, who, GenericMessage::Status(status))
}
let info = self.context_data.chain.info();
let status = message::generic::Status {
version: CURRENT_VERSION,
min_supported_version: MIN_VERSION,
genesis_hash: info.chain.genesis_hash,
roles: self.config.roles.into(),
best_number: info.chain.best_number,
best_hash: info.chain.best_hash,
chain_status: self.specialization.status(),
};
self.send_message(network_out, who, GenericMessage::Status(status))
}
fn on_block_announce(
+4 -12
View File
@@ -829,18 +829,10 @@ impl<B: BlockT> ChainSync<B> {
self.queue_blocks.clear();
self.best_importing_number = Zero::zero();
self.blocks.clear();
match protocol.client().info() {
Ok(info) => {
self.best_queued_hash = info.best_queued_hash.unwrap_or(info.chain.best_hash);
self.best_queued_number = info.best_queued_number.unwrap_or(info.chain.best_number);
debug!(target:"sync", "Restarted with {} ({})", self.best_queued_number, self.best_queued_hash);
},
Err(e) => {
debug!(target:"sync", "Error reading blockchain: {:?}", e);
self.best_queued_hash = self.genesis_hash;
self.best_queued_number = Zero::zero();
}
}
let info = protocol.client().info();
self.best_queued_hash = info.best_queued_hash.unwrap_or(info.chain.best_hash);
self.best_queued_number = info.best_queued_number.unwrap_or(info.chain.best_number);
debug!(target:"sync", "Restarted with {} ({})", self.best_queued_number, self.best_queued_hash);
let ids: Vec<PeerId> = self.peers.drain().map(|(id, _)| id).collect();
for id in ids {
self.new_peer(protocol, id);
+6 -6
View File
@@ -163,7 +163,7 @@ impl PeersClient {
}
}
pub fn info(&self) -> ClientResult<ClientInfo<Block>> {
pub fn info(&self) -> ClientInfo<Block> {
match *self {
PeersClient::Full(ref client) => client.info(),
PeersClient::Light(ref client) => client.info(),
@@ -465,7 +465,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
/// Called after blockchain has been populated to updated current state.
fn start(&self) {
// Update the sync state to the latest chain state.
let info = self.client.info().expect("In-mem client does not fail");
let info = self.client.info();
let header = self
.client
.header(&BlockId::Hash(info.chain.best_hash))
@@ -538,7 +538,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
/// Send block import notifications.
fn send_import_notifications(&self) {
let info = self.client.info().expect("In-mem client does not fail");
let info = self.client.info();
let mut best_hash = self.best_hash.lock();
match *best_hash {
@@ -554,7 +554,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
/// Send block finalization notifications.
fn send_finality_notifications(&self) {
let info = self.client.info().expect("In-mem client does not fail");
let info = self.client.info();
let mut finalized_hash = self.finalized_hash.lock();
match *finalized_hash {
@@ -625,7 +625,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
pub fn generate_blocks<F>(&self, count: usize, origin: BlockOrigin, edit_block: F) -> H256
where F: FnMut(BlockBuilder<Block, PeersFullClient>) -> Block
{
let best_hash = self.client.info().unwrap().chain.best_hash;
let best_hash = self.client.info().chain.best_hash;
self.generate_blocks_at(BlockId::Hash(best_hash), count, origin, edit_block)
}
@@ -674,7 +674,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
/// Push blocks to the peer (simplified: with or without a TX)
pub fn push_blocks(&self, count: usize, with_tx: bool) -> H256 {
let best_hash = self.client.info().unwrap().chain.best_hash;
let best_hash = self.client.info().chain.best_hash;
self.push_blocks_at(BlockId::Hash(best_hash), count, with_tx)
}
+12 -12
View File
@@ -320,8 +320,8 @@ fn own_blocks_are_announced() {
net.peer(0).on_block_imported(header.hash(), &header);
net.sync();
assert_eq!(net.peer(0).client.as_in_memory_backend().blockchain().info().unwrap().best_number, 1);
assert_eq!(net.peer(1).client.as_in_memory_backend().blockchain().info().unwrap().best_number, 1);
assert_eq!(net.peer(0).client.as_in_memory_backend().blockchain().info().best_number, 1);
assert_eq!(net.peer(1).client.as_in_memory_backend().blockchain().info().best_number, 1);
let peer0_chain = net.peer(0).client.as_in_memory_backend().blockchain().clone();
assert!(net.peer(1).client.as_in_memory_backend().blockchain().canon_equals_to(&peer0_chain));
assert!(net.peer(2).client.as_in_memory_backend().blockchain().canon_equals_to(&peer0_chain));
@@ -356,9 +356,9 @@ fn blocks_are_not_announced_by_light_nodes() {
// peer 0 has the best chain
// peer 1 has the best chain
// peer 2 has genesis-chain only
assert_eq!(net.peer(0).client.info().unwrap().chain.best_number, 1);
assert_eq!(net.peer(1).client.info().unwrap().chain.best_number, 1);
assert_eq!(net.peer(2).client.info().unwrap().chain.best_number, 0);
assert_eq!(net.peer(0).client.info().chain.best_number, 1);
assert_eq!(net.peer(1).client.info().chain.best_number, 1);
assert_eq!(net.peer(2).client.info().chain.best_number, 0);
}
#[test]
@@ -371,13 +371,13 @@ fn can_sync_small_non_best_forks() {
// small fork + reorg on peer 1.
net.peer(0).push_blocks_at(BlockId::Number(30), 2, true);
let small_hash = net.peer(0).client().info().unwrap().chain.best_hash;
let small_hash = net.peer(0).client().info().chain.best_hash;
net.peer(0).push_blocks_at(BlockId::Number(30), 10, false);
assert_eq!(net.peer(0).client().info().unwrap().chain.best_number, 40);
assert_eq!(net.peer(0).client().info().chain.best_number, 40);
// peer 1 only ever had the long fork.
net.peer(1).push_blocks(10, false);
assert_eq!(net.peer(1).client().info().unwrap().chain.best_number, 40);
assert_eq!(net.peer(1).client().info().chain.best_number, 40);
assert!(net.peer(0).client().header(&BlockId::Hash(small_hash)).unwrap().is_some());
assert!(net.peer(1).client().header(&BlockId::Hash(small_hash)).unwrap().is_none());
@@ -386,7 +386,7 @@ fn can_sync_small_non_best_forks() {
// synchronization: 0 synced to longer chain and 1 didn't sync to small chain.
assert_eq!(net.peer(0).client().info().unwrap().chain.best_number, 40);
assert_eq!(net.peer(0).client().info().chain.best_number, 40);
assert!(net.peer(0).client().header(&BlockId::Hash(small_hash)).unwrap().is_some());
assert!(!net.peer(1).client().header(&BlockId::Hash(small_hash)).unwrap().is_some());
@@ -416,8 +416,8 @@ fn can_not_sync_from_light_peer() {
net.sync();
// ensure #0 && #1 have the same best block
let full0_info = net.peer(0).client.info().unwrap().chain;
let light_info = net.peer(1).client.info().unwrap().chain;
let full0_info = net.peer(0).client.info().chain;
let light_info = net.peer(1).client.info().chain;
assert_eq!(full0_info.best_number, 1);
assert_eq!(light_info.best_number, 1);
assert_eq!(light_info.best_hash, full0_info.best_hash);
@@ -430,7 +430,7 @@ fn can_not_sync_from_light_peer() {
net.sync_with(true, Some(vec![0].into_iter().collect()));
// ensure that the #2 has failed to sync block #1
assert_eq!(net.peer(2).client.info().unwrap().chain.best_number, 0);
assert_eq!(net.peer(2).client.info().chain.best_number, 0);
// and that the #1 is still connected to #2
// (because #2 has not tried to fetch block data from the #1 light node)
assert_eq!(net.peer(1).protocol_status().num_peers, 2);