Skip to content

Commit

Permalink
client/beefy: small code improvements (paritytech#12414)
Browse files Browse the repository at this point in the history
* client/beefy: remove bounds on type definitions

* client/beefy: remove gossip protocol legacy name

* client/beefy: simplify justification request response engine

Signed-off-by: Adrian Catangiu <[email protected]>
  • Loading branch information
acatangiu authored and ark0f committed Feb 27, 2023
1 parent 0ae9038 commit 0794998
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 38 deletions.
5 changes: 0 additions & 5 deletions client/beefy/src/communication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ pub(crate) mod beefy_protocol_name {
/// BEEFY justifications protocol name suffix.
const JUSTIFICATIONS_NAME: &str = "/beefy/justifications/1";

/// Old names for the gossip protocol, used for backward compatibility.
pub(super) const LEGACY_NAMES: [&str; 1] = ["/paritytech/beefy/1"];

/// Name of the votes gossip protocol used by BEEFY.
///
/// Must be registered towards the networking in order for BEEFY voter to properly function.
Expand Down Expand Up @@ -73,9 +70,7 @@ pub fn beefy_peers_set_config(
) -> sc_network_common::config::NonDefaultSetConfig {
let mut cfg =
sc_network_common::config::NonDefaultSetConfig::new(gossip_protocol_name, 1024 * 1024);

cfg.allow_non_reserved(25, 25);
cfg.add_fallback_names(beefy_protocol_name::LEGACY_NAMES.iter().map(|&n| n.into()).collect());
cfg
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

use beefy_primitives::{crypto::AuthorityId, BeefyApi, ValidatorSet};
use codec::Encode;
use futures::{
channel::{oneshot, oneshot::Canceled},
stream::{self, StreamExt},
};
use futures::channel::{oneshot, oneshot::Canceled};
use log::{debug, error, warn};
use parking_lot::Mutex;
use sc_network::{PeerId, ProtocolName};
Expand All @@ -50,8 +47,8 @@ type Response = Result<Vec<u8>, RequestFailure>;
type ResponseReceiver = oneshot::Receiver<Response>;

enum State<B: Block> {
Idle(stream::Pending<Result<Response, Canceled>>),
AwaitingResponse(PeerId, NumberFor<B>, stream::Once<ResponseReceiver>),
Idle,
AwaitingResponse(PeerId, NumberFor<B>, ResponseReceiver),
}

pub struct OnDemandJustificationsEngine<B: Block, R> {
Expand Down Expand Up @@ -83,7 +80,7 @@ where
protocol_name,
live_peers,
peers_cache: VecDeque::new(),
state: State::Idle(stream::pending()),
state: State::Idle,
}
}

Expand Down Expand Up @@ -118,15 +115,14 @@ where
IfDisconnected::ImmediateError,
);

self.state = State::AwaitingResponse(peer, block, stream::once(rx));
self.state = State::AwaitingResponse(peer, block, rx);
}

/// If no other request is in progress, start new justification request for `block`.
pub fn request(&mut self, block: NumberFor<B>) {
// ignore new requests while there's already one pending
match &self.state {
State::AwaitingResponse(_, _, _) => return,
State::Idle(_) => (),
if matches!(self.state, State::AwaitingResponse(_, _, _)) {
return
}
self.reset_peers_cache_for_block(block);

Expand All @@ -148,7 +144,7 @@ where
"🥩 cancel pending request for justification #{:?}",
number
);
self.state = State::Idle(stream::pending());
self.state = State::Idle;
},
_ => (),
}
Expand Down Expand Up @@ -194,19 +190,19 @@ where

pub async fn next(&mut self) -> Option<BeefyVersionedFinalityProof<B>> {
let (peer, block, resp) = match &mut self.state {
State::Idle(pending) => {
let _ = pending.next().await;
// This never happens since 'stream::pending' never generates any items.
State::Idle => {
futures::pending!();
// Doesn't happen as 'futures::pending!()' is an 'await' barrier that never passes.
return None
},
State::AwaitingResponse(peer, block, receiver) => {
let resp = receiver.next().await?;
let resp = receiver.await;
(*peer, *block, resp)
},
};
// We received the awaited response. Our 'stream::once()' receiver will never generate any
// other response, meaning we're done with current state. Move the engine to `State::Idle`.
self.state = State::Idle(stream::pending());
// We received the awaited response. Our 'receiver' will never generate any other response,
// meaning we're done with current state. Move the engine to `State::Idle`.
self.state = State::Idle;

let block_id = BlockId::number(block);
let validator_set = self
Expand Down
16 changes: 2 additions & 14 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ where
}

/// BEEFY gadget network parameters.
pub struct BeefyNetworkParams<B, N>
where
B: Block,
N: GossipNetwork<B> + NetworkRequest + SyncOracle + Send + Sync + 'static,
{
pub struct BeefyNetworkParams<B: Block, N> {
/// Network implementing gossip, requests and sync-oracle.
pub network: Arc<N>,
/// Chain specific BEEFY gossip protocol name. See
Expand All @@ -171,15 +167,7 @@ where
}

/// BEEFY gadget initialization parameters.
pub struct BeefyParams<B, BE, C, N, R>
where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
R: ProvideRuntimeApi<B>,
R::Api: BeefyApi<B> + MmrApi<B, MmrRootHash>,
N: GossipNetwork<B> + NetworkRequest + SyncOracle + Send + Sync + 'static,
{
pub struct BeefyParams<B: Block, BE, C, N, R> {
/// BEEFY client
pub client: Arc<C>,
/// Client Backend
Expand Down

0 comments on commit 0794998

Please sign in to comment.