Split SyncContext from protocol Context (#2550)

This commit is contained in:
Pierre Krieger
2019-05-13 21:16:52 +02:00
committed by Gavin Wood
parent 20e809668b
commit e5b0a98f1a
3 changed files with 55 additions and 36 deletions
+1 -2
View File
@@ -24,8 +24,7 @@ use network_libp2p::PeerId;
use runtime_primitives::Justification;
use runtime_primitives::traits::{Block as BlockT, NumberFor};
use crate::message;
use crate::protocol::Context;
use crate::sync::{PeerSync, PeerSyncState};
use crate::sync::{Context, PeerSync, PeerSyncState};
// Time to wait before trying to get the same extra data from the same peer.
const EXTRA_RETRY_WAIT: Duration = Duration::from_secs(10);
+31 -33
View File
@@ -29,7 +29,7 @@ use crate::message::generic::{Message as GenericMessage, ConsensusMessage};
use crate::consensus_gossip::{ConsensusGossip, MessageRecipient as GossipMessageRecipient};
use crate::on_demand::OnDemandService;
use crate::specialization::NetworkSpecialization;
use crate::sync::{ChainSync, Status as SyncStatus, SyncState};
use crate::sync::{ChainSync, Context as SyncContext, Status as SyncStatus, SyncState};
use crate::service::{NetworkChan, NetworkMsg, TransactionPool, ExHashT};
use crate::config::{ProtocolConfig, Roles};
use parking_lot::RwLock;
@@ -151,9 +151,6 @@ pub struct PeerInfo<B: BlockT> {
/// Context for a network-specific handler.
pub trait Context<B: BlockT> {
/// Get a reference to the client.
fn client(&self) -> &crate::chain::Client<B>;
/// Adjusts the reputation of the peer. Use this to point out that a peer has been malign or
/// irresponsible or appeared lazy.
fn report_peer(&mut self, who: PeerId, reputation: i32);
@@ -161,15 +158,6 @@ pub trait Context<B: BlockT> {
/// Force disconnecting from a peer. Use this when a peer misbehaved.
fn disconnect_peer(&mut self, who: PeerId);
/// Get peer info.
fn peer_info(&self, peer: &PeerId) -> Option<PeerInfo<B>>;
/// Request a block from a peer.
fn send_block_request(&mut self, who: PeerId, request: BlockRequestMessage<B>);
/// Request a finality proof from a peer.
fn send_finality_proof_request(&mut self, who: PeerId, request: FinalityProofRequestMessage<B::Hash>);
/// Send a consensus message to a peer.
fn send_consensus(&mut self, who: PeerId, consensus: ConsensusMessage);
@@ -198,26 +186,6 @@ impl<'a, B: BlockT + 'a, H: ExHashT + 'a> Context<B> for ProtocolContext<'a, B,
self.network_chan.send(NetworkMsg::DisconnectPeer(who))
}
fn peer_info(&self, who: &PeerId) -> Option<PeerInfo<B>> {
self.context_data.peers.get(who).map(|p| p.info.clone())
}
fn client(&self) -> &Client<B> {
&*self.context_data.chain
}
fn send_block_request(&mut self, who: PeerId, request: BlockRequestMessage<B>) {
send_message(&mut self.context_data.peers, &self.network_chan, who,
GenericMessage::BlockRequest(request)
)
}
fn send_finality_proof_request(&mut self, who: PeerId, request: FinalityProofRequestMessage<B::Hash>) {
send_message(&mut self.context_data.peers, &self.network_chan, who,
GenericMessage::FinalityProofRequest(request)
)
}
fn send_consensus(&mut self, who: PeerId, consensus: ConsensusMessage) {
send_message(&mut self.context_data.peers, &self.network_chan, who,
GenericMessage::Consensus(consensus)
@@ -231,6 +199,36 @@ impl<'a, B: BlockT + 'a, H: ExHashT + 'a> Context<B> for ProtocolContext<'a, B,
}
}
impl<'a, B: BlockT + 'a, H: ExHashT + 'a> SyncContext<B> for ProtocolContext<'a, B, H> {
fn report_peer(&mut self, who: PeerId, reputation: i32) {
self.network_chan.send(NetworkMsg::ReportPeer(who, reputation))
}
fn disconnect_peer(&mut self, who: PeerId) {
self.network_chan.send(NetworkMsg::DisconnectPeer(who))
}
fn peer_info(&self, who: &PeerId) -> Option<PeerInfo<B>> {
self.context_data.peers.get(who).map(|p| p.info.clone())
}
fn client(&self) -> &Client<B> {
&*self.context_data.chain
}
fn send_finality_proof_request(&mut self, who: PeerId, request: FinalityProofRequestMessage<B::Hash>) {
send_message(&mut self.context_data.peers, &self.network_chan, who,
GenericMessage::FinalityProofRequest(request)
)
}
fn send_block_request(&mut self, who: PeerId, request: BlockRequestMessage<B>) {
send_message(&mut self.context_data.peers, &self.network_chan, who,
GenericMessage::BlockRequest(request)
)
}
}
/// Data necessary to create a context.
struct ContextData<B: BlockT, H: ExHashT> {
// All connected peers
+23 -1
View File
@@ -33,7 +33,7 @@
use std::cmp::max;
use std::collections::{HashMap, VecDeque};
use log::{debug, trace, warn, info};
use crate::protocol::Context;
use crate::protocol::PeerInfo as ProtocolPeerInfo;
use network_libp2p::PeerId;
use client::{BlockStatus, ClientInfo};
use consensus::{BlockOrigin, import_queue::{IncomingBlock, SharedFinalityProofRequestBuilder}};
@@ -63,6 +63,28 @@ const ANCESTRY_BLOCK_ERROR_REPUTATION_CHANGE: i32 = -(1 << 9);
/// Reputation change when a peer sent us a status message with a different genesis than us.
const GENESIS_MISMATCH_REPUTATION_CHANGE: i32 = i32::min_value() + 1;
/// Context for a network-specific handler.
pub trait Context<B: BlockT> {
/// Get a reference to the client.
fn client(&self) -> &crate::chain::Client<B>;
/// Adjusts the reputation of the peer. Use this to point out that a peer has been malign or
/// irresponsible or appeared lazy.
fn report_peer(&mut self, who: PeerId, reputation: i32);
/// Force disconnecting from a peer. Use this when a peer misbehaved.
fn disconnect_peer(&mut self, who: PeerId);
/// Get peer info.
fn peer_info(&self, peer: &PeerId) -> Option<ProtocolPeerInfo<B>>;
/// Request a finality proof from a peer.
fn send_finality_proof_request(&mut self, who: PeerId, request: message::FinalityProofRequest<B::Hash>);
/// Request a block from a peer.
fn send_block_request(&mut self, who: PeerId, request: message::BlockRequest<B>);
}
#[derive(Debug)]
pub(crate) struct PeerSync<B: BlockT> {
pub common_number: NumberFor<B>,