mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-17 11:21:07 +00:00
Explicit sync API for downloading important, possibly orphaned, forks (#3633)
* Explicit sync API * Keep sync requests * Don't request the finalized block we already have * Dropping requests & docs * Renamed a function
This commit is contained in:
committed by
Robert Habermeier
parent
5d82f453e8
commit
3242d7f2b6
@@ -229,6 +229,11 @@ pub struct Peer<D, S: NetworkSpecialization<Block>> {
|
||||
}
|
||||
|
||||
impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
|
||||
/// Get this peer ID.
|
||||
pub fn id(&self) -> PeerId {
|
||||
self.network.service().local_peer_id()
|
||||
}
|
||||
|
||||
/// Returns true if we're major syncing.
|
||||
pub fn is_major_syncing(&self) -> bool {
|
||||
self.network.service().is_major_syncing()
|
||||
@@ -259,6 +264,11 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
|
||||
self.network.service().announce_block(hash, data);
|
||||
}
|
||||
|
||||
/// Request explicit fork sync.
|
||||
pub fn set_sync_fork_request(&self, peers: Vec<PeerId>, hash: <Block as BlockT>::Hash, number: NumberFor<Block>) {
|
||||
self.network.service().set_sync_fork_request(peers, hash, number);
|
||||
}
|
||||
|
||||
/// Add blocks to the peer -- edit the block before adding
|
||||
pub fn generate_blocks<F>(&mut self, count: usize, origin: BlockOrigin, edit_block: F) -> H256
|
||||
where F: FnMut(BlockBuilder<Block, PeersFullClient>) -> Block
|
||||
|
||||
@@ -526,3 +526,59 @@ fn light_peer_imports_header_from_announce() {
|
||||
let known_stale_hash = net.peer(0).push_blocks_at(BlockId::Number(0), 1, true);
|
||||
import_with_announce(&mut net, &mut runtime, known_stale_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_sync_explicit_forks() {
|
||||
let _ = ::env_logger::try_init();
|
||||
let mut runtime = current_thread::Runtime::new().unwrap();
|
||||
let mut net = TestNet::new(2);
|
||||
net.peer(0).push_blocks(30, false);
|
||||
net.peer(1).push_blocks(30, false);
|
||||
|
||||
// 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;
|
||||
net.peer(0).push_blocks_at(BlockId::Number(30), 10, false);
|
||||
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().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());
|
||||
|
||||
// poll until the two nodes connect, otherwise announcing the block will not work
|
||||
runtime.block_on(futures::future::poll_fn::<(), (), _>(|| -> Result<_, ()> {
|
||||
net.poll();
|
||||
if net.peer(0).num_peers() == 0 || net.peer(1).num_peers() == 0 {
|
||||
Ok(Async::NotReady)
|
||||
} else {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
})).unwrap();
|
||||
|
||||
// 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!(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());
|
||||
|
||||
// request explicit sync
|
||||
let first_peer_id = net.peer(0).id();
|
||||
net.peer(1).set_sync_fork_request(vec![first_peer_id], small_hash, small_number);
|
||||
|
||||
// peer 1 downloads the block.
|
||||
runtime.block_on(futures::future::poll_fn::<(), (), _>(|| -> Result<_, ()> {
|
||||
net.poll();
|
||||
|
||||
assert!(net.peer(0).client().header(&BlockId::Hash(small_hash)).unwrap().is_some());
|
||||
if net.peer(1).client().header(&BlockId::Hash(small_hash)).unwrap().is_none() {
|
||||
return Ok(Async::NotReady)
|
||||
}
|
||||
Ok(Async::Ready(()))
|
||||
})).unwrap();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user