Refactor to use only chain info (#4516)

This commit is contained in:
Nikolay Volf
2020-01-02 14:46:07 +03:00
committed by Bastian Köcher
parent 8ecc450fd9
commit 6d06a19f41
30 changed files with 178 additions and 175 deletions
+5 -5
View File
@@ -17,8 +17,8 @@
//! Blockchain access trait
use sc_client::Client as SubstrateClient;
use sp_blockchain::Error;
use sc_client_api::{ChangesProof, StorageProof, ClientInfo, CallExecutor};
use sp_blockchain::{Error, Info as BlockchainInfo};
use sc_client_api::{ChangesProof, StorageProof, CallExecutor};
use sp_consensus::{BlockImport, BlockStatus, Error as ConsensusError};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sp_runtime::generic::{BlockId};
@@ -29,7 +29,7 @@ use sp_core::storage::{StorageKey, ChildInfo};
/// Local client abstraction for the network.
pub trait Client<Block: BlockT>: Send + Sync {
/// Get blockchain info.
fn info(&self) -> ClientInfo<Block>;
fn info(&self) -> BlockchainInfo<Block>;
/// Get block status.
fn block_status(&self, id: &BlockId<Block>) -> Result<BlockStatus, Error>;
@@ -99,8 +99,8 @@ impl<B, E, Block, RA> Client<Block> for SubstrateClient<B, E, Block, RA> where
Block: BlockT<Hash=H256>,
RA: Send + Sync
{
fn info(&self) -> ClientInfo<Block> {
(self as &SubstrateClient<B, E, Block, RA>).info()
fn info(&self) -> BlockchainInfo<Block> {
(self as &SubstrateClient<B, E, Block, RA>).chain_info()
}
fn block_status(&self, id: &BlockId<Block>) -> Result<BlockStatus, Error> {
+6 -6
View File
@@ -473,7 +473,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
chain,
},
light_dispatch: LightDispatch::new(checker),
genesis_hash: info.chain.genesis_hash,
genesis_hash: info.genesis_hash,
sync,
specialization,
handshaking_peers: HashMap::new(),
@@ -1010,7 +1010,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
.context_data
.chain
.info()
.chain.best_number;
.best_number;
let blocks_difference = self_best_block
.checked_sub(&status.best_number)
.unwrap_or_else(Zero::zero)
@@ -1232,7 +1232,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
return;
}
let is_best = self.context_data.chain.info().chain.best_hash == hash;
let is_best = self.context_data.chain.info().best_hash == hash;
debug!(target: "sync", "Reannouncing block {:?}", hash);
self.send_announcement(&header, data, is_best, true)
}
@@ -1278,10 +1278,10 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
let status = message::generic::Status {
version: CURRENT_VERSION,
min_supported_version: MIN_VERSION,
genesis_hash: info.chain.genesis_hash,
genesis_hash: info.genesis_hash,
roles: self.config.roles.into(),
best_number: info.chain.best_number,
best_hash: info.chain.best_hash,
best_number: info.best_number,
best_hash: info.best_hash,
chain_status: self.specialization.status(),
};
@@ -28,8 +28,7 @@
//!
use blocks::BlockCollection;
use sc_client_api::ClientInfo;
use sp_blockchain::Error as ClientError;
use sp_blockchain::{Error as ClientError, Info as BlockchainInfo};
use sp_consensus::{BlockOrigin, BlockStatus,
block_validation::{BlockAnnounceValidator, Validation},
import_queue::{IncomingBlock, BlockImportResult, BlockImportError}
@@ -291,7 +290,7 @@ impl<B: BlockT> ChainSync<B> {
pub fn new(
role: Roles,
client: Arc<dyn crate::chain::Client<B>>,
info: &ClientInfo<B>,
info: &BlockchainInfo<B>,
request_builder: Option<BoxFinalityProofRequestBuilder<B>>,
block_announce_validator: Box<dyn BlockAnnounceValidator<B> + Send>,
max_parallel_downloads: u32,
@@ -306,9 +305,9 @@ impl<B: BlockT> ChainSync<B> {
client,
peers: HashMap::new(),
blocks: BlockCollection::new(),
best_queued_hash: info.chain.best_hash,
best_queued_number: info.chain.best_number,
best_imported_number: info.chain.best_number,
best_queued_hash: info.best_hash,
best_queued_number: info.best_number,
best_imported_number: info.best_number,
extra_finality_proofs: ExtraRequests::new(),
extra_justifications: ExtraRequests::new(),
role,
@@ -579,7 +578,7 @@ impl<B: BlockT> ChainSync<B> {
let attrs = &self.required_block_attributes;
let fork_targets = &mut self.fork_targets;
let mut have_requests = false;
let last_finalized = self.client.info().chain.finalized_number;
let last_finalized = self.client.info().finalized_number;
let best_queued = self.best_queued_number;
let client = &self.client;
let queue = &self.queue_blocks;
@@ -1142,8 +1141,8 @@ impl<B: BlockT> ChainSync<B> {
self.queue_blocks.clear();
self.blocks.clear();
let info = self.client.info();
self.best_queued_hash = info.chain.best_hash;
self.best_queued_number = std::cmp::max(info.chain.best_number, self.best_imported_number);
self.best_queued_hash = info.best_hash;
self.best_queued_number = std::cmp::max(info.best_number, self.best_imported_number);
self.is_idle = false;
debug!(target:"sync", "Restarted with {} ({})", self.best_queued_number, self.best_queued_hash);
let old_peers = std::mem::replace(&mut self.peers, HashMap::new());
+8 -8
View File
@@ -28,10 +28,10 @@ use libp2p::build_multiaddr;
use log::trace;
use sc_network::FinalityProofProvider;
use sp_blockchain::{
Result as ClientResult, well_known_cache_keys::{self, Id as CacheKeyId},
Result as ClientResult, well_known_cache_keys::{self, Id as CacheKeyId}, Info as BlockchainInfo,
};
use sc_client_api::{
ClientInfo, BlockchainEvents, BlockImportNotification,
BlockchainEvents, BlockImportNotification,
FinalityNotifications, ImportNotifications,
FinalityNotification,
backend::{AuxStore, Backend, Finalizer}
@@ -161,10 +161,10 @@ impl PeersClient {
}
}
pub fn info(&self) -> ClientInfo<Block> {
pub fn info(&self) -> BlockchainInfo<Block> {
match *self {
PeersClient::Full(ref client, ref _backend) => client.info(),
PeersClient::Light(ref client, ref _backend) => client.info(),
PeersClient::Full(ref client, ref _backend) => client.chain_info(),
PeersClient::Light(ref client, ref _backend) => client.chain_info(),
}
}
@@ -270,7 +270,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
pub fn generate_blocks<F>(&mut self, count: usize, origin: BlockOrigin, edit_block: F) -> H256
where F: FnMut(BlockBuilder<Block, PeersFullClient>) -> Block
{
let best_hash = self.client.info().chain.best_hash;
let best_hash = self.client.info().best_hash;
self.generate_blocks_at(BlockId::Hash(best_hash), count, origin, edit_block)
}
@@ -320,7 +320,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
/// Push blocks to the peer (simplified: with or without a TX)
pub fn push_blocks(&mut self, count: usize, with_tx: bool) -> H256 {
let best_hash = self.client.info().chain.best_hash;
let best_hash = self.client.info().best_hash;
self.push_blocks_at(BlockId::Hash(best_hash), count, with_tx)
}
@@ -689,7 +689,7 @@ pub trait TestNetFactory: Sized {
if peer.is_major_syncing() || peer.network.num_queued_blocks() != 0 {
return Async::NotReady
}
match (highest, peer.client.info().chain.best_hash) {
match (highest, peer.client.info().best_hash) {
(None, b) => highest = Some(b),
(Some(ref a), ref b) if a == b => {},
(Some(_), _) => return Async::NotReady,
+18 -18
View File
@@ -374,8 +374,8 @@ fn own_blocks_are_announced() {
net.block_until_sync(&mut runtime);
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(0).client.info().best_number, 1);
assert_eq!(net.peer(1).client.info().best_number, 1);
let peer0 = &net.peers()[0];
assert!(net.peers()[1].blockchain_canon_equals(peer0));
(net.peers()[2].blockchain_canon_equals(peer0));
@@ -396,9 +396,9 @@ fn blocks_are_not_announced_by_light_nodes() {
// Sync between 0 and 1.
net.peer(0).push_blocks(1, false);
assert_eq!(net.peer(0).client.info().chain.best_number, 1);
assert_eq!(net.peer(0).client.info().best_number, 1);
net.block_until_sync(&mut runtime);
assert_eq!(net.peer(1).client.info().chain.best_number, 1);
assert_eq!(net.peer(1).client.info().best_number, 1);
// Add another node and remove node 0.
net.add_full_peer(&ProtocolConfig::default());
@@ -410,7 +410,7 @@ fn blocks_are_not_announced_by_light_nodes() {
net.poll();
delay.poll().map_err(|_| ())
})).unwrap();
assert_eq!(net.peer(1).client.info().chain.best_number, 0);
assert_eq!(net.peer(1).client.info().best_number, 0);
}
#[test]
@@ -423,13 +423,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().chain.best_hash;
let small_hash = net.peer(0).client().info().best_hash;
net.peer(0).push_blocks_at(BlockId::Number(30), 10, false);
assert_eq!(net.peer(0).client().info().chain.best_number, 40);
assert_eq!(net.peer(0).client().info().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().chain.best_number, 40);
assert_eq!(net.peer(1).client().info().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());
@@ -446,7 +446,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().chain.best_number, 40);
assert_eq!(net.peer(0).client().info().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());
@@ -493,8 +493,8 @@ fn can_not_sync_from_light_peer() {
net.block_until_sync(&mut runtime);
// ensure #0 && #1 have the same best block
let full0_info = net.peer(0).client.info().chain;
let light_info = net.peer(1).client.info().chain;
let full0_info = net.peer(0).client.info();
let light_info = net.peer(1).client.info();
assert_eq!(full0_info.best_number, 1);
assert_eq!(light_info.best_number, 1);
assert_eq!(light_info.best_hash, full0_info.best_hash);
@@ -555,14 +555,14 @@ fn can_sync_explicit_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().chain.best_hash;
let small_number = net.peer(0).client().info().chain.best_number;
let small_hash = net.peer(0).client().info().best_hash;
let small_number = net.peer(0).client().info().best_number;
net.peer(0).push_blocks_at(BlockId::Number(30), 10, false);
assert_eq!(net.peer(0).client().info().chain.best_number, 40);
assert_eq!(net.peer(0).client().info().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().chain.best_number, 40);
assert_eq!(net.peer(1).client().info().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());
@@ -579,7 +579,7 @@ fn can_sync_explicit_forks() {
// synchronization: 0 synced to longer chain and 1 didn't sync to small chain.
assert_eq!(net.peer(0).client().info().chain.best_number, 40);
assert_eq!(net.peer(0).client().info().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());
@@ -612,8 +612,8 @@ fn syncs_header_only_forks() {
net.peer(1).push_blocks(2, false);
net.peer(0).push_blocks(2, true);
let small_hash = net.peer(0).client().info().chain.best_hash;
let small_number = net.peer(0).client().info().chain.best_number;
let small_hash = net.peer(0).client().info().best_hash;
let small_number = net.peer(0).client().info().best_number;
net.peer(1).push_blocks(4, false);
net.block_until_sync(&mut runtime);