feat: Rebrand Polkadot/Substrate references to PezkuwiChain
This commit systematically rebrands various references from Parity Technologies' Polkadot/Substrate ecosystem to PezkuwiChain within the kurdistan-sdk. Key changes include: - Updated external repository URLs (zombienet-sdk, parity-db, parity-scale-codec, wasm-instrument) to point to pezkuwichain forks. - Modified internal documentation and code comments to reflect PezkuwiChain naming and structure. - Replaced direct references to with or specific paths within the for XCM, Pezkuwi, and other modules. - Cleaned up deprecated issue and PR references in various and files, particularly in and modules. - Adjusted image and logo URLs in documentation to point to PezkuwiChain assets. - Removed or rephrased comments related to external Polkadot/Substrate PRs and issues. This is a significant step towards fully customizing the SDK for the PezkuwiChain ecosystem.
This commit is contained in:
@@ -0,0 +1,917 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{collections::BTreeSet, sync::Arc, time::Duration};
|
||||
|
||||
use pezsc_network::{NetworkPeers, ReputationChange};
|
||||
use pezsc_network_gossip::{MessageIntent, ValidationResult, Validator, ValidatorContext};
|
||||
use pezsc_network_types::PeerId;
|
||||
use pezsp_runtime::traits::{Block, Hash, Header, NumberFor};
|
||||
|
||||
use codec::{Decode, DecodeAll, Encode};
|
||||
use log::{debug, trace};
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use wasm_timer::Instant;
|
||||
|
||||
use crate::{
|
||||
communication::{benefit, cost, peers::KnownPeers},
|
||||
justification::{
|
||||
proof_block_num_and_set_id, verify_with_validator_set, BeefyVersionedFinalityProof,
|
||||
},
|
||||
keystore::BeefyKeystore,
|
||||
LOG_TARGET,
|
||||
};
|
||||
use pezsp_application_crypto::RuntimeAppPublic;
|
||||
use pezsp_consensus_beefy::{AuthorityIdBound, ValidatorSet, ValidatorSetId, VoteMessage};
|
||||
|
||||
// Timeout for rebroadcasting messages.
|
||||
#[cfg(not(test))]
|
||||
const REBROADCAST_AFTER: Duration = Duration::from_secs(60);
|
||||
#[cfg(test)]
|
||||
const REBROADCAST_AFTER: Duration = Duration::from_secs(5);
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(super) enum Action<H> {
|
||||
// repropagate under given topic, to the given peers, applying cost/benefit to originator.
|
||||
Keep(H, ReputationChange),
|
||||
// discard, applying cost/benefit to originator.
|
||||
Discard(ReputationChange),
|
||||
// ignore, no cost/benefit applied to originator.
|
||||
DiscardNoReport,
|
||||
}
|
||||
|
||||
/// An outcome of examining a message.
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
enum Consider {
|
||||
/// Accept the message.
|
||||
Accept,
|
||||
/// Message is too early. Reject.
|
||||
RejectPast,
|
||||
/// Message is from the future. Reject.
|
||||
RejectFuture,
|
||||
/// Message cannot be evaluated. Reject.
|
||||
CannotEvaluate,
|
||||
}
|
||||
|
||||
/// BEEFY gossip message type that gets encoded and sent on the network.
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
pub(crate) enum GossipMessage<B: Block, AuthorityId: AuthorityIdBound> {
|
||||
/// BEEFY message with commitment and single signature.
|
||||
Vote(VoteMessage<NumberFor<B>, AuthorityId, <AuthorityId as RuntimeAppPublic>::Signature>),
|
||||
/// BEEFY justification with commitment and signatures.
|
||||
FinalityProof(BeefyVersionedFinalityProof<B, AuthorityId>),
|
||||
}
|
||||
|
||||
impl<B: Block, AuthorityId: AuthorityIdBound> GossipMessage<B, AuthorityId> {
|
||||
/// Return inner vote if this message is a Vote.
|
||||
pub fn unwrap_vote(
|
||||
self,
|
||||
) -> Option<VoteMessage<NumberFor<B>, AuthorityId, <AuthorityId as RuntimeAppPublic>::Signature>>
|
||||
{
|
||||
match self {
|
||||
GossipMessage::Vote(vote) => Some(vote),
|
||||
GossipMessage::FinalityProof(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return inner finality proof if this message is a FinalityProof.
|
||||
pub fn unwrap_finality_proof(self) -> Option<BeefyVersionedFinalityProof<B, AuthorityId>> {
|
||||
match self {
|
||||
GossipMessage::Vote(_) => None,
|
||||
GossipMessage::FinalityProof(proof) => Some(proof),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Gossip engine votes messages topic
|
||||
pub(crate) fn votes_topic<B: Block>() -> B::Hash
|
||||
where
|
||||
B: Block,
|
||||
{
|
||||
<<B::Header as Header>::Hashing as Hash>::hash(b"beefy-votes")
|
||||
}
|
||||
|
||||
/// Gossip engine justifications messages topic
|
||||
pub(crate) fn proofs_topic<B: Block>() -> B::Hash
|
||||
where
|
||||
B: Block,
|
||||
{
|
||||
<<B::Header as Header>::Hashing as Hash>::hash(b"beefy-justifications")
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct GossipFilterCfg<'a, B: Block, AuthorityId: AuthorityIdBound> {
|
||||
pub start: NumberFor<B>,
|
||||
pub end: NumberFor<B>,
|
||||
pub validator_set: &'a ValidatorSet<AuthorityId>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct FilterInner<B: Block, AuthorityId: AuthorityIdBound> {
|
||||
pub start: NumberFor<B>,
|
||||
pub end: NumberFor<B>,
|
||||
pub validator_set: ValidatorSet<AuthorityId>,
|
||||
}
|
||||
|
||||
struct Filter<B: Block, AuthorityId: AuthorityIdBound> {
|
||||
// specifies live rounds
|
||||
inner: Option<FilterInner<B, AuthorityId>>,
|
||||
// cache of seen valid justifications in active rounds
|
||||
rounds_with_valid_proofs: BTreeSet<NumberFor<B>>,
|
||||
}
|
||||
|
||||
impl<B: Block, AuthorityId: AuthorityIdBound> Filter<B, AuthorityId> {
|
||||
pub fn new() -> Self {
|
||||
Self { inner: None, rounds_with_valid_proofs: BTreeSet::new() }
|
||||
}
|
||||
|
||||
/// Update filter to new `start` and `set_id`.
|
||||
fn update(&mut self, cfg: GossipFilterCfg<B, AuthorityId>) {
|
||||
self.rounds_with_valid_proofs
|
||||
.retain(|&round| round >= cfg.start && round <= cfg.end);
|
||||
// only clone+overwrite big validator_set if set_id changed
|
||||
match self.inner.as_mut() {
|
||||
Some(f) if f.validator_set.id() == cfg.validator_set.id() => {
|
||||
f.start = cfg.start;
|
||||
f.end = cfg.end;
|
||||
},
|
||||
_ =>
|
||||
self.inner = Some(FilterInner {
|
||||
start: cfg.start,
|
||||
end: cfg.end,
|
||||
validator_set: cfg.validator_set.clone(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Accept if `max(session_start, best_beefy) <= round <= best_grandpa`,
|
||||
/// and vote `set_id` matches session set id.
|
||||
///
|
||||
/// Latest concluded round is still considered alive to allow proper gossiping for it.
|
||||
fn consider_vote(&self, round: NumberFor<B>, set_id: ValidatorSetId) -> Consider {
|
||||
self.inner
|
||||
.as_ref()
|
||||
.map(|f|
|
||||
// only from current set and only [filter.start, filter.end]
|
||||
if set_id < f.validator_set.id() || round < f.start {
|
||||
Consider::RejectPast
|
||||
} else if set_id > f.validator_set.id() || round > f.end {
|
||||
Consider::RejectFuture
|
||||
} else {
|
||||
Consider::Accept
|
||||
})
|
||||
.unwrap_or(Consider::CannotEvaluate)
|
||||
}
|
||||
|
||||
/// Return true if `round` is >= than `max(session_start, best_beefy)`,
|
||||
/// and proof `set_id` matches session set id.
|
||||
///
|
||||
/// Latest concluded round is still considered alive to allow proper gossiping for it.
|
||||
fn consider_finality_proof(&self, round: NumberFor<B>, set_id: ValidatorSetId) -> Consider {
|
||||
self.inner
|
||||
.as_ref()
|
||||
.map(|f|
|
||||
// only from current set and only >= filter.start
|
||||
if round < f.start || set_id < f.validator_set.id() {
|
||||
Consider::RejectPast
|
||||
} else if set_id > f.validator_set.id() {
|
||||
Consider::RejectFuture
|
||||
} else {
|
||||
Consider::Accept
|
||||
}
|
||||
)
|
||||
.unwrap_or(Consider::CannotEvaluate)
|
||||
}
|
||||
|
||||
/// Add new _known_ `round` to the set of seen valid justifications.
|
||||
fn mark_round_as_proven(&mut self, round: NumberFor<B>) {
|
||||
self.rounds_with_valid_proofs.insert(round);
|
||||
}
|
||||
|
||||
/// Check if `round` is already part of seen valid justifications.
|
||||
fn is_already_proven(&self, round: NumberFor<B>) -> bool {
|
||||
self.rounds_with_valid_proofs.contains(&round)
|
||||
}
|
||||
|
||||
fn validator_set(&self) -> Option<&ValidatorSet<AuthorityId>> {
|
||||
self.inner.as_ref().map(|f| &f.validator_set)
|
||||
}
|
||||
}
|
||||
|
||||
/// BEEFY gossip validator
|
||||
///
|
||||
/// Validate BEEFY gossip messages and limit the number of live BEEFY voting rounds.
|
||||
///
|
||||
/// Allows messages for 'rounds >= last concluded' to flow, everything else gets
|
||||
/// rejected/expired.
|
||||
///
|
||||
///All messaging is handled in a single BEEFY global topic.
|
||||
pub(crate) struct GossipValidator<B, N, AuthorityId: AuthorityIdBound>
|
||||
where
|
||||
B: Block,
|
||||
{
|
||||
votes_topic: B::Hash,
|
||||
justifs_topic: B::Hash,
|
||||
gossip_filter: RwLock<Filter<B, AuthorityId>>,
|
||||
next_rebroadcast: Mutex<Instant>,
|
||||
known_peers: Arc<Mutex<KnownPeers<B>>>,
|
||||
network: Arc<N>,
|
||||
}
|
||||
|
||||
impl<B, N, AuthorityId> GossipValidator<B, N, AuthorityId>
|
||||
where
|
||||
B: Block,
|
||||
AuthorityId: AuthorityIdBound,
|
||||
{
|
||||
pub(crate) fn new(known_peers: Arc<Mutex<KnownPeers<B>>>, network: Arc<N>) -> Self {
|
||||
Self {
|
||||
votes_topic: votes_topic::<B>(),
|
||||
justifs_topic: proofs_topic::<B>(),
|
||||
gossip_filter: RwLock::new(Filter::new()),
|
||||
next_rebroadcast: Mutex::new(Instant::now() + REBROADCAST_AFTER),
|
||||
known_peers,
|
||||
network,
|
||||
}
|
||||
}
|
||||
|
||||
/// Update gossip validator filter.
|
||||
///
|
||||
/// Only votes for `set_id` and rounds `start <= round <= end` will be accepted.
|
||||
pub(crate) fn update_filter(&self, filter: GossipFilterCfg<B, AuthorityId>) {
|
||||
debug!(
|
||||
target: LOG_TARGET,
|
||||
"🥩 New gossip filter: start {:?}, end {:?}, validator set id {:?}",
|
||||
filter.start, filter.end, filter.validator_set.id()
|
||||
);
|
||||
self.gossip_filter.write().update(filter);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, N, AuthorityId> GossipValidator<B, N, AuthorityId>
|
||||
where
|
||||
B: Block,
|
||||
N: NetworkPeers,
|
||||
AuthorityId: AuthorityIdBound,
|
||||
{
|
||||
fn report(&self, who: PeerId, cost_benefit: ReputationChange) {
|
||||
self.network.report_peer(who, cost_benefit);
|
||||
}
|
||||
|
||||
fn validate_vote(
|
||||
&self,
|
||||
vote: VoteMessage<NumberFor<B>, AuthorityId, <AuthorityId as RuntimeAppPublic>::Signature>,
|
||||
sender: &PeerId,
|
||||
) -> Action<B::Hash> {
|
||||
let round = vote.commitment.block_number;
|
||||
let set_id = vote.commitment.validator_set_id;
|
||||
self.known_peers.lock().note_vote_for(*sender, round);
|
||||
|
||||
// Verify general usefulness of the message.
|
||||
// We are going to discard old votes right away (without verification).
|
||||
{
|
||||
let filter = self.gossip_filter.read();
|
||||
|
||||
match filter.consider_vote(round, set_id) {
|
||||
Consider::RejectPast => return Action::Discard(cost::OUTDATED_MESSAGE),
|
||||
Consider::RejectFuture => return Action::Discard(cost::FUTURE_MESSAGE),
|
||||
// When we can't evaluate, it's our fault (e.g. filter not initialized yet), we
|
||||
// discard the vote without punishing or rewarding the sending peer.
|
||||
Consider::CannotEvaluate => return Action::DiscardNoReport,
|
||||
Consider::Accept => {},
|
||||
}
|
||||
|
||||
// ensure authority is part of the set.
|
||||
if !filter
|
||||
.validator_set()
|
||||
.map(|set| set.validators().contains(&vote.id))
|
||||
.unwrap_or(false)
|
||||
{
|
||||
debug!(target: LOG_TARGET, "Message from voter not in validator set: {}", vote.id);
|
||||
return Action::Discard(cost::UNKNOWN_VOTER);
|
||||
}
|
||||
}
|
||||
|
||||
if BeefyKeystore::verify(&vote.id, &vote.signature, &vote.commitment.encode()) {
|
||||
Action::Keep(self.votes_topic, benefit::VOTE_MESSAGE)
|
||||
} else {
|
||||
debug!(
|
||||
target: LOG_TARGET,
|
||||
"🥩 Bad signature on message: {:?}, from: {:?}", vote, sender
|
||||
);
|
||||
Action::Discard(cost::BAD_SIGNATURE)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_finality_proof(
|
||||
&self,
|
||||
proof: BeefyVersionedFinalityProof<B, AuthorityId>,
|
||||
sender: &PeerId,
|
||||
) -> Action<B::Hash> {
|
||||
let (round, set_id) = proof_block_num_and_set_id::<B, AuthorityId>(&proof);
|
||||
self.known_peers.lock().note_vote_for(*sender, round);
|
||||
|
||||
let action = {
|
||||
let guard = self.gossip_filter.read();
|
||||
|
||||
// Verify general usefulness of the justification.
|
||||
match guard.consider_finality_proof(round, set_id) {
|
||||
Consider::RejectPast => return Action::Discard(cost::OUTDATED_MESSAGE),
|
||||
Consider::RejectFuture => return Action::Discard(cost::FUTURE_MESSAGE),
|
||||
// When we can't evaluate, it's our fault (e.g. filter not initialized yet), we
|
||||
// discard the proof without punishing or rewarding the sending peer.
|
||||
Consider::CannotEvaluate => return Action::DiscardNoReport,
|
||||
Consider::Accept => {},
|
||||
}
|
||||
|
||||
if guard.is_already_proven(round) {
|
||||
return Action::Discard(benefit::NOT_INTERESTED);
|
||||
}
|
||||
|
||||
// Verify justification signatures.
|
||||
guard
|
||||
.validator_set()
|
||||
.map(|validator_set| {
|
||||
if let Err((_, signatures_checked)) =
|
||||
verify_with_validator_set::<B, AuthorityId>(round, validator_set, &proof)
|
||||
{
|
||||
debug!(
|
||||
target: LOG_TARGET,
|
||||
"🥩 Bad signatures on message: {:?}, from: {:?}", proof, sender
|
||||
);
|
||||
let mut cost = cost::INVALID_PROOF;
|
||||
cost.value +=
|
||||
cost::PER_SIGNATURE_CHECKED.saturating_mul(signatures_checked as i32);
|
||||
Action::Discard(cost)
|
||||
} else {
|
||||
Action::Keep(self.justifs_topic, benefit::VALIDATED_PROOF)
|
||||
}
|
||||
})
|
||||
// When we can't evaluate, it's our fault (e.g. filter not initialized yet), we
|
||||
// discard the proof without punishing or rewarding the sending peer.
|
||||
.unwrap_or(Action::DiscardNoReport)
|
||||
};
|
||||
if matches!(action, Action::Keep(_, _)) {
|
||||
self.gossip_filter.write().mark_round_as_proven(round);
|
||||
}
|
||||
action
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, N, AuthorityId> Validator<B> for GossipValidator<B, N, AuthorityId>
|
||||
where
|
||||
B: Block,
|
||||
AuthorityId: AuthorityIdBound,
|
||||
N: NetworkPeers + Send + Sync,
|
||||
{
|
||||
fn peer_disconnected(&self, _context: &mut dyn ValidatorContext<B>, who: &PeerId) {
|
||||
self.known_peers.lock().remove(who);
|
||||
}
|
||||
|
||||
fn validate(
|
||||
&self,
|
||||
context: &mut dyn ValidatorContext<B>,
|
||||
sender: &PeerId,
|
||||
mut data: &[u8],
|
||||
) -> ValidationResult<B::Hash> {
|
||||
let raw = data;
|
||||
let action = match GossipMessage::<B, AuthorityId>::decode_all(&mut data) {
|
||||
Ok(GossipMessage::Vote(msg)) => self.validate_vote(msg, sender),
|
||||
Ok(GossipMessage::FinalityProof(proof)) => self.validate_finality_proof(proof, sender),
|
||||
Err(e) => {
|
||||
debug!(target: LOG_TARGET, "Error decoding message: {}", e);
|
||||
let bytes = raw.len().min(i32::MAX as usize) as i32;
|
||||
let cost = ReputationChange::new(
|
||||
bytes.saturating_mul(cost::PER_UNDECODABLE_BYTE),
|
||||
"BEEFY: Bad packet",
|
||||
);
|
||||
Action::Discard(cost)
|
||||
},
|
||||
};
|
||||
match action {
|
||||
Action::Keep(topic, cb) => {
|
||||
self.report(*sender, cb);
|
||||
context.broadcast_message(topic, data.to_vec(), false);
|
||||
ValidationResult::ProcessAndKeep(topic)
|
||||
},
|
||||
Action::Discard(cb) => {
|
||||
self.report(*sender, cb);
|
||||
ValidationResult::Discard
|
||||
},
|
||||
Action::DiscardNoReport => ValidationResult::Discard,
|
||||
}
|
||||
}
|
||||
|
||||
fn message_expired<'a>(&'a self) -> Box<dyn FnMut(B::Hash, &[u8]) -> bool + 'a> {
|
||||
let filter = self.gossip_filter.read();
|
||||
Box::new(move |_topic, mut data| {
|
||||
match GossipMessage::<B, AuthorityId>::decode_all(&mut data) {
|
||||
Ok(GossipMessage::Vote(msg)) => {
|
||||
let round = msg.commitment.block_number;
|
||||
let set_id = msg.commitment.validator_set_id;
|
||||
let expired = filter.consider_vote(round, set_id) != Consider::Accept;
|
||||
trace!(target: LOG_TARGET, "🥩 Vote for round #{} expired: {}", round, expired);
|
||||
expired
|
||||
},
|
||||
Ok(GossipMessage::FinalityProof(proof)) => {
|
||||
let (round, set_id) = proof_block_num_and_set_id::<B, AuthorityId>(&proof);
|
||||
let expired = filter.consider_finality_proof(round, set_id) != Consider::Accept;
|
||||
trace!(
|
||||
target: LOG_TARGET,
|
||||
"🥩 Finality proof for round #{} expired: {}",
|
||||
round,
|
||||
expired
|
||||
);
|
||||
expired
|
||||
},
|
||||
Err(_) => true,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn message_allowed<'a>(
|
||||
&'a self,
|
||||
) -> Box<dyn FnMut(&PeerId, MessageIntent, &B::Hash, &[u8]) -> bool + 'a> {
|
||||
let do_rebroadcast = {
|
||||
let now = Instant::now();
|
||||
let mut next_rebroadcast = self.next_rebroadcast.lock();
|
||||
if now >= *next_rebroadcast {
|
||||
trace!(target: LOG_TARGET, "🥩 Gossip rebroadcast");
|
||||
*next_rebroadcast = now + REBROADCAST_AFTER;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
let filter = self.gossip_filter.read();
|
||||
Box::new(move |_who, intent, _topic, mut data| {
|
||||
if let MessageIntent::PeriodicRebroadcast = intent {
|
||||
return do_rebroadcast;
|
||||
}
|
||||
|
||||
match GossipMessage::<B, AuthorityId>::decode_all(&mut data) {
|
||||
Ok(GossipMessage::Vote(msg)) => {
|
||||
let round = msg.commitment.block_number;
|
||||
let set_id = msg.commitment.validator_set_id;
|
||||
let allowed = filter.consider_vote(round, set_id) == Consider::Accept;
|
||||
trace!(target: LOG_TARGET, "🥩 Vote for round #{} allowed: {}", round, allowed);
|
||||
allowed
|
||||
},
|
||||
Ok(GossipMessage::FinalityProof(proof)) => {
|
||||
let (round, set_id) = proof_block_num_and_set_id::<B, AuthorityId>(&proof);
|
||||
let allowed = filter.consider_finality_proof(round, set_id) == Consider::Accept;
|
||||
trace!(
|
||||
target: LOG_TARGET,
|
||||
"🥩 Finality proof for round #{} allowed: {}",
|
||||
round,
|
||||
allowed
|
||||
);
|
||||
allowed
|
||||
},
|
||||
Err(_) => false,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::{communication::peers::PeerReport, keystore::BeefyKeystore};
|
||||
use pezsc_network_test::Block;
|
||||
use pezsp_application_crypto::key_types::BEEFY as BEEFY_KEY_TYPE;
|
||||
use pezsp_consensus_beefy::{
|
||||
ecdsa_crypto, known_payloads, test_utils::Keyring, Commitment, MmrRootHash, Payload,
|
||||
SignedCommitment, VoteMessage,
|
||||
};
|
||||
use pezsp_keystore::{testing::MemoryKeystore, Keystore};
|
||||
|
||||
pub(crate) struct TestNetwork {
|
||||
report_sender: futures::channel::mpsc::UnboundedSender<PeerReport>,
|
||||
}
|
||||
|
||||
impl TestNetwork {
|
||||
pub fn new() -> (Self, futures::channel::mpsc::UnboundedReceiver<PeerReport>) {
|
||||
let (tx, rx) = futures::channel::mpsc::unbounded();
|
||||
|
||||
(Self { report_sender: tx }, rx)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl NetworkPeers for TestNetwork {
|
||||
fn set_authorized_peers(&self, _: std::collections::HashSet<PeerId>) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn set_authorized_only(&self, _: bool) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn add_known_address(&self, _: PeerId, _: pezsc_network::Multiaddr) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange) {
|
||||
let _ = self.report_sender.unbounded_send(PeerReport { who: peer_id, cost_benefit });
|
||||
}
|
||||
|
||||
fn peer_reputation(&self, _: &PeerId) -> i32 {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn disconnect_peer(&self, _: PeerId, _: pezsc_network::ProtocolName) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn accept_unreserved_peers(&self) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn deny_unreserved_peers(&self) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn add_reserved_peer(
|
||||
&self,
|
||||
_: pezsc_network::config::MultiaddrWithPeerId,
|
||||
) -> Result<(), String> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn remove_reserved_peer(&self, _: PeerId) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn set_reserved_peers(
|
||||
&self,
|
||||
_: pezsc_network::ProtocolName,
|
||||
_: std::collections::HashSet<pezsc_network::Multiaddr>,
|
||||
) -> Result<(), String> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn add_peers_to_reserved_set(
|
||||
&self,
|
||||
_: pezsc_network::ProtocolName,
|
||||
_: std::collections::HashSet<pezsc_network::Multiaddr>,
|
||||
) -> Result<(), String> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn remove_peers_from_reserved_set(
|
||||
&self,
|
||||
_: pezsc_network::ProtocolName,
|
||||
_: Vec<PeerId>,
|
||||
) -> Result<(), String> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn sync_num_connected(&self) -> usize {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn peer_role(&self, _: PeerId, _: Vec<u8>) -> Option<pezsc_network::ObservedRole> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
async fn reserved_peers(&self) -> Result<Vec<PeerId>, ()> {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
struct TestContext;
|
||||
impl<B: pezsp_runtime::traits::Block> ValidatorContext<B> for TestContext {
|
||||
fn broadcast_topic(&mut self, _topic: B::Hash, _force: bool) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn broadcast_message(&mut self, _topic: B::Hash, _message: Vec<u8>, _force: bool) {}
|
||||
|
||||
fn send_message(&mut self, _who: &pezsc_network_types::PeerId, _message: Vec<u8>) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn send_topic(&mut self, _who: &pezsc_network_types::PeerId, _topic: B::Hash, _force: bool) {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sign_commitment<BN: Encode>(
|
||||
who: &Keyring<ecdsa_crypto::AuthorityId>,
|
||||
commitment: &Commitment<BN>,
|
||||
) -> ecdsa_crypto::Signature {
|
||||
let store = MemoryKeystore::new();
|
||||
store.ecdsa_generate_new(BEEFY_KEY_TYPE, Some(&who.to_seed())).unwrap();
|
||||
let beefy_keystore: BeefyKeystore<ecdsa_crypto::AuthorityId> = Some(store.into()).into();
|
||||
beefy_keystore.sign(&who.public(), &commitment.encode()).unwrap()
|
||||
}
|
||||
|
||||
fn dummy_vote(
|
||||
block_number: u64,
|
||||
) -> VoteMessage<u64, ecdsa_crypto::AuthorityId, ecdsa_crypto::Signature> {
|
||||
let payload = Payload::from_single_entry(
|
||||
known_payloads::MMR_ROOT_ID,
|
||||
MmrRootHash::default().encode(),
|
||||
);
|
||||
let commitment = Commitment { payload, block_number, validator_set_id: 0 };
|
||||
let signature = sign_commitment(&Keyring::Alice, &commitment);
|
||||
|
||||
VoteMessage { commitment, id: Keyring::Alice.public(), signature }
|
||||
}
|
||||
|
||||
pub fn dummy_proof(
|
||||
block_number: u64,
|
||||
validator_set: &ValidatorSet<ecdsa_crypto::AuthorityId>,
|
||||
) -> BeefyVersionedFinalityProof<Block, ecdsa_crypto::AuthorityId> {
|
||||
let payload = Payload::from_single_entry(
|
||||
known_payloads::MMR_ROOT_ID,
|
||||
MmrRootHash::default().encode(),
|
||||
);
|
||||
let commitment = Commitment { payload, block_number, validator_set_id: validator_set.id() };
|
||||
let signatures = validator_set
|
||||
.validators()
|
||||
.iter()
|
||||
.map(|validator: &ecdsa_crypto::AuthorityId| {
|
||||
Some(sign_commitment(
|
||||
&Keyring::<ecdsa_crypto::AuthorityId>::from_public(validator).unwrap(),
|
||||
&commitment,
|
||||
))
|
||||
})
|
||||
.collect();
|
||||
|
||||
BeefyVersionedFinalityProof::<Block, ecdsa_crypto::AuthorityId>::V1(SignedCommitment {
|
||||
commitment,
|
||||
signatures,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_validate_messages() {
|
||||
let keys = vec![Keyring::<ecdsa_crypto::AuthorityId>::Alice.public()];
|
||||
let validator_set =
|
||||
ValidatorSet::<ecdsa_crypto::AuthorityId>::new(keys.clone(), 0).unwrap();
|
||||
|
||||
let (network, mut report_stream) = TestNetwork::new();
|
||||
|
||||
let gv = GossipValidator::<Block, _, ecdsa_crypto::AuthorityId>::new(
|
||||
Arc::new(Mutex::new(KnownPeers::new())),
|
||||
Arc::new(network),
|
||||
);
|
||||
let sender = PeerId::random();
|
||||
let mut context = TestContext;
|
||||
|
||||
// reject message, decoding error
|
||||
let bad_encoding = b"0000000000".as_slice();
|
||||
let expected_cost = ReputationChange::new(
|
||||
(bad_encoding.len() as i32).saturating_mul(cost::PER_UNDECODABLE_BYTE),
|
||||
"BEEFY: Bad packet",
|
||||
);
|
||||
let mut expected_report = PeerReport { who: sender, cost_benefit: expected_cost };
|
||||
let res = gv.validate(&mut context, &sender, bad_encoding);
|
||||
assert!(matches!(res, ValidationResult::Discard));
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
|
||||
// verify votes validation
|
||||
|
||||
let vote = dummy_vote(3);
|
||||
let encoded =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::Vote(vote.clone()).encode();
|
||||
|
||||
// filter not initialized
|
||||
let res = gv.validate(&mut context, &sender, &encoded);
|
||||
assert!(matches!(res, ValidationResult::Discard));
|
||||
// nothing reported
|
||||
assert!(report_stream.try_next().is_err());
|
||||
|
||||
gv.update_filter(GossipFilterCfg { start: 0, end: 10, validator_set: &validator_set });
|
||||
// nothing in cache first time
|
||||
let res = gv.validate(&mut context, &sender, &encoded);
|
||||
assert!(matches!(res, ValidationResult::ProcessAndKeep(_)));
|
||||
expected_report.cost_benefit = benefit::VOTE_MESSAGE;
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
|
||||
// reject vote, voter not in validator set
|
||||
let mut bad_vote = vote.clone();
|
||||
bad_vote.id = Keyring::Bob.public();
|
||||
let bad_vote = GossipMessage::<Block, ecdsa_crypto::AuthorityId>::Vote(bad_vote).encode();
|
||||
let res = gv.validate(&mut context, &sender, &bad_vote);
|
||||
assert!(matches!(res, ValidationResult::Discard));
|
||||
expected_report.cost_benefit = cost::UNKNOWN_VOTER;
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
|
||||
// reject if the round is not GRANDPA finalized
|
||||
gv.update_filter(GossipFilterCfg { start: 1, end: 2, validator_set: &validator_set });
|
||||
let number = vote.commitment.block_number;
|
||||
let set_id = vote.commitment.validator_set_id;
|
||||
assert_eq!(gv.gossip_filter.read().consider_vote(number, set_id), Consider::RejectFuture);
|
||||
let res = gv.validate(&mut context, &sender, &encoded);
|
||||
assert!(matches!(res, ValidationResult::Discard));
|
||||
expected_report.cost_benefit = cost::FUTURE_MESSAGE;
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
|
||||
// reject if the round is not live anymore
|
||||
gv.update_filter(GossipFilterCfg { start: 7, end: 10, validator_set: &validator_set });
|
||||
let number = vote.commitment.block_number;
|
||||
let set_id = vote.commitment.validator_set_id;
|
||||
assert_eq!(gv.gossip_filter.read().consider_vote(number, set_id), Consider::RejectPast);
|
||||
let res = gv.validate(&mut context, &sender, &encoded);
|
||||
assert!(matches!(res, ValidationResult::Discard));
|
||||
expected_report.cost_benefit = cost::OUTDATED_MESSAGE;
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
|
||||
// now verify proofs validation
|
||||
|
||||
// reject old proof
|
||||
let proof = dummy_proof(5, &validator_set);
|
||||
let encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
let res = gv.validate(&mut context, &sender, &encoded_proof);
|
||||
assert!(matches!(res, ValidationResult::Discard));
|
||||
expected_report.cost_benefit = cost::OUTDATED_MESSAGE;
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
|
||||
// accept next proof with good set_id
|
||||
let proof = dummy_proof(7, &validator_set);
|
||||
let encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
let res = gv.validate(&mut context, &sender, &encoded_proof);
|
||||
assert!(matches!(res, ValidationResult::ProcessAndKeep(_)));
|
||||
expected_report.cost_benefit = benefit::VALIDATED_PROOF;
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
|
||||
// accept future proof with good set_id
|
||||
let proof = dummy_proof(20, &validator_set);
|
||||
let encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
let res = gv.validate(&mut context, &sender, &encoded_proof);
|
||||
assert!(matches!(res, ValidationResult::ProcessAndKeep(_)));
|
||||
expected_report.cost_benefit = benefit::VALIDATED_PROOF;
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
|
||||
// reject proof, future set_id
|
||||
let bad_validator_set = ValidatorSet::<ecdsa_crypto::AuthorityId>::new(keys, 1).unwrap();
|
||||
let proof = dummy_proof(20, &bad_validator_set);
|
||||
let encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
let res = gv.validate(&mut context, &sender, &encoded_proof);
|
||||
assert!(matches!(res, ValidationResult::Discard));
|
||||
expected_report.cost_benefit = cost::FUTURE_MESSAGE;
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
|
||||
// reject proof, bad signatures (Bob instead of Alice)
|
||||
let bad_validator_set =
|
||||
ValidatorSet::<ecdsa_crypto::AuthorityId>::new(vec![Keyring::Bob.public()], 0).unwrap();
|
||||
let proof = dummy_proof(21, &bad_validator_set);
|
||||
let encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
let res = gv.validate(&mut context, &sender, &encoded_proof);
|
||||
assert!(matches!(res, ValidationResult::Discard));
|
||||
expected_report.cost_benefit = cost::INVALID_PROOF;
|
||||
expected_report.cost_benefit.value += cost::PER_SIGNATURE_CHECKED;
|
||||
assert_eq!(report_stream.try_next().unwrap().unwrap(), expected_report);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn messages_allowed_and_expired() {
|
||||
let keys = vec![Keyring::Alice.public()];
|
||||
let validator_set =
|
||||
ValidatorSet::<ecdsa_crypto::AuthorityId>::new(keys.clone(), 0).unwrap();
|
||||
let gv = GossipValidator::<Block, _, ecdsa_crypto::AuthorityId>::new(
|
||||
Arc::new(Mutex::new(KnownPeers::new())),
|
||||
Arc::new(TestNetwork::new().0),
|
||||
);
|
||||
gv.update_filter(GossipFilterCfg { start: 0, end: 10, validator_set: &validator_set });
|
||||
let sender = pezsc_network_types::PeerId::random();
|
||||
let topic = Default::default();
|
||||
let intent = MessageIntent::Broadcast;
|
||||
|
||||
// conclude 2
|
||||
gv.update_filter(GossipFilterCfg { start: 2, end: 10, validator_set: &validator_set });
|
||||
let mut allowed = gv.message_allowed();
|
||||
let mut expired = gv.message_expired();
|
||||
|
||||
// check bad vote format
|
||||
assert!(!allowed(&sender, intent, &topic, &mut [0u8; 16]));
|
||||
assert!(expired(topic, &mut [0u8; 16]));
|
||||
|
||||
// inactive round 1 -> expired
|
||||
let vote = dummy_vote(1);
|
||||
let mut encoded_vote =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::Vote(vote).encode();
|
||||
assert!(!allowed(&sender, intent, &topic, &mut encoded_vote));
|
||||
assert!(expired(topic, &mut encoded_vote));
|
||||
let proof = dummy_proof(1, &validator_set);
|
||||
let mut encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
assert!(!allowed(&sender, intent, &topic, &mut encoded_proof));
|
||||
assert!(expired(topic, &mut encoded_proof));
|
||||
|
||||
// active round 2 -> !expired - concluded but still gossiped
|
||||
let vote = dummy_vote(2);
|
||||
let mut encoded_vote =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::Vote(vote).encode();
|
||||
assert!(allowed(&sender, intent, &topic, &mut encoded_vote));
|
||||
assert!(!expired(topic, &mut encoded_vote));
|
||||
let proof = dummy_proof(2, &validator_set);
|
||||
let mut encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
assert!(allowed(&sender, intent, &topic, &mut encoded_proof));
|
||||
assert!(!expired(topic, &mut encoded_proof));
|
||||
// using wrong set_id -> !allowed, expired
|
||||
let bad_validator_set =
|
||||
ValidatorSet::<ecdsa_crypto::AuthorityId>::new(keys.clone(), 1).unwrap();
|
||||
let proof = dummy_proof(2, &bad_validator_set);
|
||||
let mut encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
assert!(!allowed(&sender, intent, &topic, &mut encoded_proof));
|
||||
assert!(expired(topic, &mut encoded_proof));
|
||||
|
||||
// in progress round 3 -> !expired
|
||||
let vote = dummy_vote(3);
|
||||
let mut encoded_vote =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::Vote(vote).encode();
|
||||
assert!(allowed(&sender, intent, &topic, &mut encoded_vote));
|
||||
assert!(!expired(topic, &mut encoded_vote));
|
||||
let proof = dummy_proof(3, &validator_set);
|
||||
let mut encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
assert!(allowed(&sender, intent, &topic, &mut encoded_proof));
|
||||
assert!(!expired(topic, &mut encoded_proof));
|
||||
|
||||
// unseen round 4 -> !expired
|
||||
let vote = dummy_vote(4);
|
||||
let mut encoded_vote =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::Vote(vote).encode();
|
||||
assert!(allowed(&sender, intent, &topic, &mut encoded_vote));
|
||||
assert!(!expired(topic, &mut encoded_vote));
|
||||
let proof = dummy_proof(4, &validator_set);
|
||||
let mut encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
assert!(allowed(&sender, intent, &topic, &mut encoded_proof));
|
||||
assert!(!expired(topic, &mut encoded_proof));
|
||||
|
||||
// future round 11 -> expired
|
||||
let vote = dummy_vote(11);
|
||||
let mut encoded_vote =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::Vote(vote).encode();
|
||||
assert!(!allowed(&sender, intent, &topic, &mut encoded_vote));
|
||||
assert!(expired(topic, &mut encoded_vote));
|
||||
// future proofs allowed while same set_id -> allowed
|
||||
let proof = dummy_proof(11, &validator_set);
|
||||
let mut encoded_proof =
|
||||
GossipMessage::<Block, ecdsa_crypto::AuthorityId>::FinalityProof(proof).encode();
|
||||
assert!(allowed(&sender, intent, &topic, &mut encoded_proof));
|
||||
assert!(!expired(topic, &mut encoded_proof));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn messages_rebroadcast() {
|
||||
let keys = vec![Keyring::Alice.public()];
|
||||
let validator_set =
|
||||
ValidatorSet::<ecdsa_crypto::AuthorityId>::new(keys.clone(), 0).unwrap();
|
||||
let gv = GossipValidator::<Block, _, ecdsa_crypto::AuthorityId>::new(
|
||||
Arc::new(Mutex::new(KnownPeers::new())),
|
||||
Arc::new(TestNetwork::new().0),
|
||||
);
|
||||
gv.update_filter(GossipFilterCfg { start: 0, end: 10, validator_set: &validator_set });
|
||||
let sender = pezsc_network_types::PeerId::random();
|
||||
let topic = Default::default();
|
||||
|
||||
let vote = dummy_vote(1);
|
||||
let mut encoded_vote = vote.encode();
|
||||
|
||||
// re-broadcasting only allowed at `REBROADCAST_AFTER` intervals
|
||||
let intent = MessageIntent::PeriodicRebroadcast;
|
||||
let mut allowed = gv.message_allowed();
|
||||
|
||||
// rebroadcast not allowed so soon after GossipValidator creation
|
||||
assert!(!allowed(&sender, intent, &topic, &mut encoded_vote));
|
||||
|
||||
// hack the inner deadline to be `now`
|
||||
*gv.next_rebroadcast.lock() = Instant::now();
|
||||
|
||||
// still not allowed on old `allowed` closure result
|
||||
assert!(!allowed(&sender, intent, &topic, &mut encoded_vote));
|
||||
|
||||
// renew closure result
|
||||
let mut allowed = gv.message_allowed();
|
||||
// rebroadcast should be allowed now
|
||||
assert!(allowed(&sender, intent, &topic, &mut encoded_vote));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Communication streams for the BEEFY networking protocols.
|
||||
|
||||
pub mod notification;
|
||||
pub mod request_response;
|
||||
|
||||
pub(crate) mod gossip;
|
||||
pub(crate) mod peers;
|
||||
|
||||
pub(crate) mod beefy_protocol_name {
|
||||
use array_bytes::bytes2hex;
|
||||
use pezsc_network::ProtocolName;
|
||||
|
||||
/// BEEFY votes gossip protocol name suffix.
|
||||
const GOSSIP_NAME: &str = "/beefy/2";
|
||||
/// BEEFY justifications protocol name suffix.
|
||||
const JUSTIFICATIONS_NAME: &str = "/beefy/justifications/1";
|
||||
|
||||
/// Name of the votes gossip protocol used by BEEFY.
|
||||
///
|
||||
/// Must be registered towards the networking in order for BEEFY voter to properly function.
|
||||
pub fn gossip_protocol_name<Hash: AsRef<[u8]>>(
|
||||
genesis_hash: Hash,
|
||||
fork_id: Option<&str>,
|
||||
) -> ProtocolName {
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}{}", bytes2hex("", genesis_hash), fork_id, GOSSIP_NAME).into()
|
||||
} else {
|
||||
format!("/{}{}", bytes2hex("", genesis_hash), GOSSIP_NAME).into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Name of the BEEFY justifications request-response protocol.
|
||||
pub fn justifications_protocol_name<Hash: AsRef<[u8]>>(
|
||||
genesis_hash: Hash,
|
||||
fork_id: Option<&str>,
|
||||
) -> ProtocolName {
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}{}", bytes2hex("", genesis_hash), fork_id, JUSTIFICATIONS_NAME).into()
|
||||
} else {
|
||||
format!("/{}{}", bytes2hex("", genesis_hash), JUSTIFICATIONS_NAME).into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the configuration value to put in
|
||||
/// [`pezsc_network::config::FullNetworkConfiguration`].
|
||||
/// For standard protocol name see [`beefy_protocol_name::gossip_protocol_name`].
|
||||
pub fn beefy_peers_set_config<
|
||||
B: pezsp_runtime::traits::Block,
|
||||
N: pezsc_network::NetworkBackend<B, <B as pezsp_runtime::traits::Block>::Hash>,
|
||||
>(
|
||||
gossip_protocol_name: pezsc_network::ProtocolName,
|
||||
metrics: pezsc_network::service::NotificationMetrics,
|
||||
peer_store_handle: std::sync::Arc<dyn pezsc_network::peer_store::PeerStoreProvider>,
|
||||
) -> (N::NotificationProtocolConfig, Box<dyn pezsc_network::NotificationService>) {
|
||||
let (cfg, notification_service) = N::notification_config(
|
||||
gossip_protocol_name,
|
||||
Vec::new(),
|
||||
1024 * 1024,
|
||||
None,
|
||||
pezsc_network::config::SetConfig {
|
||||
in_peers: 25,
|
||||
out_peers: 25,
|
||||
reserved_nodes: Vec::new(),
|
||||
non_reserved_mode: pezsc_network::config::NonReservedPeerMode::Accept,
|
||||
},
|
||||
metrics,
|
||||
peer_store_handle,
|
||||
);
|
||||
(cfg, notification_service)
|
||||
}
|
||||
|
||||
// cost scalars for reporting peers.
|
||||
mod cost {
|
||||
use pezsc_network::ReputationChange as Rep;
|
||||
// Message that's for an outdated round.
|
||||
pub(super) const OUTDATED_MESSAGE: Rep = Rep::new(-50, "BEEFY: Past message");
|
||||
// Message that's from the future relative to our current set-id.
|
||||
pub(super) const FUTURE_MESSAGE: Rep = Rep::new(-100, "BEEFY: Future message");
|
||||
// Vote message containing bad signature.
|
||||
pub(super) const BAD_SIGNATURE: Rep = Rep::new(-100, "BEEFY: Bad signature");
|
||||
// Message received with vote from voter not in validator set.
|
||||
pub(super) const UNKNOWN_VOTER: Rep = Rep::new(-150, "BEEFY: Unknown voter");
|
||||
// Message containing invalid proof.
|
||||
pub(super) const INVALID_PROOF: Rep = Rep::new(-5000, "BEEFY: Invalid commit");
|
||||
// Reputation cost per signature checked for invalid proof.
|
||||
pub(super) const PER_SIGNATURE_CHECKED: i32 = -25;
|
||||
// Reputation cost per byte for un-decodable message.
|
||||
pub(super) const PER_UNDECODABLE_BYTE: i32 = -5;
|
||||
// On-demand request was refused by peer.
|
||||
pub(super) const REFUSAL_RESPONSE: Rep = Rep::new(-100, "BEEFY: Proof request refused");
|
||||
// On-demand request for a proof that can't be found in the backend.
|
||||
pub(super) const UNKNOWN_PROOF_REQUEST: Rep = Rep::new(-150, "BEEFY: Unknown proof request");
|
||||
}
|
||||
|
||||
// benefit scalars for reporting peers.
|
||||
mod benefit {
|
||||
use pezsc_network::ReputationChange as Rep;
|
||||
pub(super) const VOTE_MESSAGE: Rep = Rep::new(100, "BEEFY: Round vote message");
|
||||
pub(super) const NOT_INTERESTED: Rep = Rep::new(10, "BEEFY: Not interested in round");
|
||||
pub(super) const VALIDATED_PROOF: Rep = Rep::new(100, "BEEFY: Justification");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use pezsp_core::H256;
|
||||
|
||||
#[test]
|
||||
fn beefy_protocols_names() {
|
||||
use beefy_protocol_name::{gossip_protocol_name, justifications_protocol_name};
|
||||
// Create protocol name using random genesis hash.
|
||||
let genesis_hash = H256::random();
|
||||
let genesis_hex = array_bytes::bytes2hex("", genesis_hash);
|
||||
|
||||
let expected_gossip_name = format!("/{}/beefy/2", genesis_hex);
|
||||
let gossip_proto_name = gossip_protocol_name(&genesis_hash, None);
|
||||
assert_eq!(gossip_proto_name.to_string(), expected_gossip_name);
|
||||
|
||||
let expected_justif_name = format!("/{}/beefy/justifications/1", genesis_hex);
|
||||
let justif_proto_name = justifications_protocol_name(&genesis_hash, None);
|
||||
assert_eq!(justif_proto_name.to_string(), expected_justif_name);
|
||||
|
||||
// Create protocol name using hardcoded genesis hash. Verify exact representation.
|
||||
let genesis_hash = [
|
||||
50, 4, 60, 123, 58, 106, 216, 246, 194, 188, 139, 193, 33, 212, 202, 171, 9, 55, 123,
|
||||
94, 8, 43, 12, 251, 187, 57, 173, 19, 188, 74, 205, 147,
|
||||
];
|
||||
let genesis_hex = "32043c7b3a6ad8f6c2bc8bc121d4caab09377b5e082b0cfbbb39ad13bc4acd93";
|
||||
|
||||
let expected_gossip_name = format!("/{}/beefy/2", genesis_hex);
|
||||
let gossip_proto_name = gossip_protocol_name(&genesis_hash, None);
|
||||
assert_eq!(gossip_proto_name.to_string(), expected_gossip_name);
|
||||
|
||||
let expected_justif_name = format!("/{}/beefy/justifications/1", genesis_hex);
|
||||
let justif_proto_name = justifications_protocol_name(&genesis_hash, None);
|
||||
assert_eq!(justif_proto_name.to_string(), expected_justif_name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use pezsc_utils::notification::{NotificationSender, NotificationStream, TracingKeyStr};
|
||||
use pezsp_runtime::traits::Block as BlockT;
|
||||
|
||||
use crate::justification::BeefyVersionedFinalityProof;
|
||||
|
||||
/// The sending half of the notifications channel(s) used to send
|
||||
/// notifications about best BEEFY block from the gadget side.
|
||||
pub type BeefyBestBlockSender<Block> = NotificationSender<<Block as BlockT>::Hash>;
|
||||
|
||||
/// The receiving half of a notifications channel used to receive
|
||||
/// notifications about best BEEFY blocks determined on the gadget side.
|
||||
pub type BeefyBestBlockStream<Block> =
|
||||
NotificationStream<<Block as BlockT>::Hash, BeefyBestBlockTracingKey>;
|
||||
|
||||
/// The sending half of the notifications channel(s) used to send notifications
|
||||
/// about versioned finality proof generated at the end of a BEEFY round.
|
||||
pub type BeefyVersionedFinalityProofSender<Block, AuthorityId> =
|
||||
NotificationSender<BeefyVersionedFinalityProof<Block, AuthorityId>>;
|
||||
|
||||
/// The receiving half of a notifications channel used to receive notifications
|
||||
/// about versioned finality proof generated at the end of a BEEFY round.
|
||||
pub type BeefyVersionedFinalityProofStream<Block, AuthorityId> = NotificationStream<
|
||||
BeefyVersionedFinalityProof<Block, AuthorityId>,
|
||||
BeefyVersionedFinalityProofTracingKey,
|
||||
>;
|
||||
|
||||
/// Provides tracing key for BEEFY best block stream.
|
||||
#[derive(Clone)]
|
||||
pub struct BeefyBestBlockTracingKey;
|
||||
impl TracingKeyStr for BeefyBestBlockTracingKey {
|
||||
const TRACING_KEY: &'static str = "mpsc_beefy_best_block_notification_stream";
|
||||
}
|
||||
|
||||
/// Provides tracing key for BEEFY versioned finality proof stream.
|
||||
#[derive(Clone)]
|
||||
pub struct BeefyVersionedFinalityProofTracingKey;
|
||||
impl TracingKeyStr for BeefyVersionedFinalityProofTracingKey {
|
||||
const TRACING_KEY: &'static str = "mpsc_beefy_versioned_finality_proof_notification_stream";
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Logic for keeping track of BEEFY peers.
|
||||
|
||||
use pezsc_network::ReputationChange;
|
||||
use pezsc_network_types::PeerId;
|
||||
use pezsp_runtime::traits::{Block, NumberFor, Zero};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
/// Report specifying a reputation change for a given peer.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct PeerReport {
|
||||
pub who: PeerId,
|
||||
pub cost_benefit: ReputationChange,
|
||||
}
|
||||
|
||||
struct PeerData<B: Block> {
|
||||
last_voted_on: NumberFor<B>,
|
||||
}
|
||||
|
||||
impl<B: Block> Default for PeerData<B> {
|
||||
fn default() -> Self {
|
||||
PeerData { last_voted_on: Zero::zero() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep a simple map of connected peers
|
||||
/// and the most recent voting round they participated in.
|
||||
pub struct KnownPeers<B: Block> {
|
||||
live: HashMap<PeerId, PeerData<B>>,
|
||||
}
|
||||
|
||||
impl<B: Block> KnownPeers<B> {
|
||||
pub fn new() -> Self {
|
||||
Self { live: HashMap::new() }
|
||||
}
|
||||
|
||||
/// Note vote round number for `peer`.
|
||||
pub fn note_vote_for(&mut self, peer: PeerId, round: NumberFor<B>) {
|
||||
let data = self.live.entry(peer).or_default();
|
||||
data.last_voted_on = round.max(data.last_voted_on);
|
||||
}
|
||||
|
||||
/// Remove connected `peer`.
|
||||
pub fn remove(&mut self, peer: &PeerId) {
|
||||
self.live.remove(peer);
|
||||
}
|
||||
|
||||
/// Return _filtered and cloned_ list of peers that have voted on higher than `block`.
|
||||
pub fn further_than(&self, block: NumberFor<B>) -> VecDeque<PeerId> {
|
||||
self.live
|
||||
.iter()
|
||||
.filter_map(|(k, v)| (v.last_voted_on > block).then_some(k))
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Answer whether `peer` is part of `KnownPeers` set.
|
||||
pub fn contains(&self, peer: &PeerId) -> bool {
|
||||
self.live.contains_key(peer)
|
||||
}
|
||||
|
||||
/// Number of peers in the set.
|
||||
pub fn len(&self) -> usize {
|
||||
self.live.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_track_known_peers_progress() {
|
||||
let (alice, bob, charlie) = (PeerId::random(), PeerId::random(), PeerId::random());
|
||||
let mut peers = KnownPeers::<pezsc_network_test::Block>::new();
|
||||
assert!(peers.live.is_empty());
|
||||
|
||||
// 'Tracked' Bob seen voting for 5.
|
||||
peers.note_vote_for(bob, 5);
|
||||
// Previously unseen Charlie now seen voting for 10.
|
||||
peers.note_vote_for(charlie, 10);
|
||||
|
||||
assert_eq!(peers.live.len(), 2);
|
||||
assert!(!peers.contains(&alice));
|
||||
assert!(peers.contains(&bob));
|
||||
assert!(peers.contains(&charlie));
|
||||
|
||||
// Get peers at block > 4
|
||||
let further_than_4 = peers.further_than(4);
|
||||
// Should be Bob and Charlie
|
||||
assert_eq!(further_than_4.len(), 2);
|
||||
assert!(further_than_4.contains(&bob));
|
||||
assert!(further_than_4.contains(&charlie));
|
||||
|
||||
// 'Tracked' Alice seen voting for 10.
|
||||
peers.note_vote_for(alice, 10);
|
||||
|
||||
// Get peers at block > 9
|
||||
let further_than_9 = peers.further_than(9);
|
||||
// Should be Charlie and Alice
|
||||
assert_eq!(further_than_9.len(), 2);
|
||||
assert!(further_than_9.contains(&charlie));
|
||||
assert!(further_than_9.contains(&alice));
|
||||
|
||||
// Remove Alice
|
||||
peers.remove(&alice);
|
||||
assert_eq!(peers.live.len(), 2);
|
||||
assert!(!peers.contains(&alice));
|
||||
|
||||
// Get peers at block >= 9
|
||||
let further_than_9 = peers.further_than(9);
|
||||
// Now should be just Charlie
|
||||
assert_eq!(further_than_9.len(), 1);
|
||||
assert!(further_than_9.contains(&charlie));
|
||||
}
|
||||
}
|
||||
+224
@@ -0,0 +1,224 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Bizinikiwi.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// Bizinikiwi is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Bizinikiwi is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Bizinikiwi. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Helper for handling (i.e. answering) BEEFY justifications requests from a remote peer.
|
||||
|
||||
use codec::DecodeAll;
|
||||
use futures::{channel::oneshot, StreamExt};
|
||||
use log::{debug, trace};
|
||||
use pezsc_client_api::BlockBackend;
|
||||
use pezsc_network::{
|
||||
config as netconfig, service::traits::RequestResponseConfig, types::ProtocolName,
|
||||
NetworkBackend, ReputationChange,
|
||||
};
|
||||
use pezsc_network_types::PeerId;
|
||||
use pezsp_consensus_beefy::BEEFY_ENGINE_ID;
|
||||
use pezsp_runtime::traits::Block;
|
||||
use std::{marker::PhantomData, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
communication::{
|
||||
cost,
|
||||
request_response::{
|
||||
on_demand_justifications_protocol_config, Error, JustificationRequest,
|
||||
BEEFY_SYNC_LOG_TARGET,
|
||||
},
|
||||
},
|
||||
metric_inc,
|
||||
metrics::{register_metrics, OnDemandIncomingRequestsMetrics},
|
||||
};
|
||||
|
||||
/// A request coming in, including a sender for sending responses.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct IncomingRequest<B: Block> {
|
||||
/// `PeerId` of sending peer.
|
||||
pub peer: PeerId,
|
||||
/// The sent request.
|
||||
pub payload: JustificationRequest<B>,
|
||||
/// Sender for sending response back.
|
||||
pub pending_response: oneshot::Sender<netconfig::OutgoingResponse>,
|
||||
}
|
||||
|
||||
impl<B: Block> IncomingRequest<B> {
|
||||
/// Create new `IncomingRequest`.
|
||||
pub fn new(
|
||||
peer: PeerId,
|
||||
payload: JustificationRequest<B>,
|
||||
pending_response: oneshot::Sender<netconfig::OutgoingResponse>,
|
||||
) -> Self {
|
||||
Self { peer, payload, pending_response }
|
||||
}
|
||||
|
||||
/// Try building from raw network request.
|
||||
///
|
||||
/// This function will fail if the request cannot be decoded and will apply passed in
|
||||
/// reputation changes in that case.
|
||||
///
|
||||
/// Params:
|
||||
/// - The raw request to decode
|
||||
/// - Reputation changes to apply for the peer in case decoding fails.
|
||||
pub fn try_from_raw<F>(
|
||||
raw: netconfig::IncomingRequest,
|
||||
reputation_changes_on_err: F,
|
||||
) -> Result<Self, Error>
|
||||
where
|
||||
F: FnOnce(usize) -> Vec<ReputationChange>,
|
||||
{
|
||||
let netconfig::IncomingRequest { payload, peer, pending_response } = raw;
|
||||
let payload = match JustificationRequest::decode_all(&mut payload.as_ref()) {
|
||||
Ok(payload) => payload,
|
||||
Err(err) => {
|
||||
let response = netconfig::OutgoingResponse {
|
||||
result: Err(()),
|
||||
reputation_changes: reputation_changes_on_err(payload.len()),
|
||||
sent_feedback: None,
|
||||
};
|
||||
if let Err(_) = pending_response.send(response) {
|
||||
return Err(Error::DecodingErrorNoReputationChange(peer, err));
|
||||
}
|
||||
return Err(Error::DecodingError(peer, err));
|
||||
},
|
||||
};
|
||||
Ok(Self::new(peer, payload, pending_response))
|
||||
}
|
||||
}
|
||||
|
||||
/// Receiver for incoming BEEFY justifications requests.
|
||||
///
|
||||
/// Takes care of decoding and handling of invalid encoded requests.
|
||||
pub(crate) struct IncomingRequestReceiver {
|
||||
raw: async_channel::Receiver<netconfig::IncomingRequest>,
|
||||
}
|
||||
|
||||
impl IncomingRequestReceiver {
|
||||
pub fn new(inner: async_channel::Receiver<netconfig::IncomingRequest>) -> Self {
|
||||
Self { raw: inner }
|
||||
}
|
||||
|
||||
/// Try to receive the next incoming request.
|
||||
///
|
||||
/// Any received request will be decoded, on decoding errors the provided reputation changes
|
||||
/// will be applied and an error will be reported.
|
||||
pub async fn recv<B, F>(&mut self, reputation_changes: F) -> Result<IncomingRequest<B>, Error>
|
||||
where
|
||||
B: Block,
|
||||
F: FnOnce(usize) -> Vec<ReputationChange>,
|
||||
{
|
||||
let req = match self.raw.next().await {
|
||||
None => return Err(Error::RequestChannelExhausted),
|
||||
Some(raw) => IncomingRequest::<B>::try_from_raw(raw, reputation_changes)?,
|
||||
};
|
||||
Ok(req)
|
||||
}
|
||||
}
|
||||
|
||||
/// Handler for incoming BEEFY justifications requests from a remote peer.
|
||||
pub struct BeefyJustifsRequestHandler<B, Client> {
|
||||
pub(crate) request_receiver: IncomingRequestReceiver,
|
||||
pub(crate) justif_protocol_name: ProtocolName,
|
||||
pub(crate) client: Arc<Client>,
|
||||
pub(crate) metrics: Option<OnDemandIncomingRequestsMetrics>,
|
||||
pub(crate) _block: PhantomData<B>,
|
||||
}
|
||||
|
||||
impl<B, Client> BeefyJustifsRequestHandler<B, Client>
|
||||
where
|
||||
B: Block,
|
||||
Client: BlockBackend<B> + Send + Sync,
|
||||
{
|
||||
/// Create a new [`BeefyJustifsRequestHandler`].
|
||||
pub fn new<Hash: AsRef<[u8]>, Network: NetworkBackend<B, <B as Block>::Hash>>(
|
||||
genesis_hash: Hash,
|
||||
fork_id: Option<&str>,
|
||||
client: Arc<Client>,
|
||||
prometheus_registry: Option<prometheus_endpoint::Registry>,
|
||||
) -> (Self, Network::RequestResponseProtocolConfig) {
|
||||
let (request_receiver, config): (_, Network::RequestResponseProtocolConfig) =
|
||||
on_demand_justifications_protocol_config::<_, _, Network>(genesis_hash, fork_id);
|
||||
let justif_protocol_name = config.protocol_name().clone();
|
||||
let metrics = register_metrics(prometheus_registry);
|
||||
(
|
||||
Self { request_receiver, justif_protocol_name, client, metrics, _block: PhantomData },
|
||||
config,
|
||||
)
|
||||
}
|
||||
|
||||
/// Network request-response protocol name used by this handler.
|
||||
pub fn protocol_name(&self) -> ProtocolName {
|
||||
self.justif_protocol_name.clone()
|
||||
}
|
||||
|
||||
// Sends back justification response if justification found in client backend.
|
||||
fn handle_request(&self, request: IncomingRequest<B>) -> Result<(), Error> {
|
||||
let mut reputation_changes = vec![];
|
||||
let maybe_encoded_proof = self
|
||||
.client
|
||||
.block_hash(request.payload.begin)
|
||||
.ok()
|
||||
.flatten()
|
||||
.and_then(|hash| self.client.justifications(hash).ok().flatten())
|
||||
.and_then(|justifs| justifs.get(BEEFY_ENGINE_ID).cloned())
|
||||
.ok_or_else(|| reputation_changes.push(cost::UNKNOWN_PROOF_REQUEST));
|
||||
request
|
||||
.pending_response
|
||||
.send(netconfig::OutgoingResponse {
|
||||
result: maybe_encoded_proof,
|
||||
reputation_changes,
|
||||
sent_feedback: None,
|
||||
})
|
||||
.map_err(|_| Error::SendResponse)
|
||||
}
|
||||
|
||||
/// Run [`BeefyJustifsRequestHandler`].
|
||||
///
|
||||
/// Should never end, returns `Error` otherwise.
|
||||
pub async fn run(&mut self) -> Error {
|
||||
trace!(target: BEEFY_SYNC_LOG_TARGET, "🥩 Running BeefyJustifsRequestHandler");
|
||||
|
||||
while let Ok(request) = self
|
||||
.request_receiver
|
||||
.recv(|bytes| {
|
||||
let bytes = bytes.min(i32::MAX as usize) as i32;
|
||||
vec![ReputationChange::new(
|
||||
bytes.saturating_mul(cost::PER_UNDECODABLE_BYTE),
|
||||
"BEEFY: Bad request payload",
|
||||
)]
|
||||
})
|
||||
.await
|
||||
{
|
||||
let peer = request.peer;
|
||||
match self.handle_request(request) {
|
||||
Ok(()) => {
|
||||
metric_inc!(self.metrics, beefy_successful_justification_responses);
|
||||
debug!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 Handled BEEFY justification request from {:?}.", peer
|
||||
)
|
||||
},
|
||||
Err(e) => {
|
||||
// peer reputation changes already applied in `self.handle_request()`
|
||||
metric_inc!(self.metrics, beefy_failed_justification_responses);
|
||||
debug!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 Failed to handle BEEFY justification request from {:?}: {}", peer, e,
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
Error::RequestsReceiverStreamClosed
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Request/response protocol for syncing BEEFY justifications.
|
||||
|
||||
mod incoming_requests_handler;
|
||||
pub(crate) mod outgoing_requests_engine;
|
||||
|
||||
pub use incoming_requests_handler::BeefyJustifsRequestHandler;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use codec::{Decode, Encode, Error as CodecError};
|
||||
use pezsc_network::NetworkBackend;
|
||||
use pezsc_network_types::PeerId;
|
||||
use pezsp_runtime::traits::{Block, NumberFor};
|
||||
|
||||
use crate::communication::{beefy_protocol_name::justifications_protocol_name, peers::PeerReport};
|
||||
use incoming_requests_handler::IncomingRequestReceiver;
|
||||
|
||||
// 10 seems reasonable, considering justifs are explicitly requested only
|
||||
// for mandatory blocks, by nodes that are syncing/catching-up.
|
||||
const JUSTIF_CHANNEL_SIZE: usize = 10;
|
||||
|
||||
const MAX_RESPONSE_SIZE: u64 = 1024 * 1024;
|
||||
const JUSTIF_REQUEST_TIMEOUT: Duration = Duration::from_secs(3);
|
||||
|
||||
const BEEFY_SYNC_LOG_TARGET: &str = "beefy::sync";
|
||||
|
||||
/// Get the configuration for the BEEFY justifications Request/response protocol.
|
||||
///
|
||||
/// Returns a receiver for messages received on this protocol and the requested
|
||||
/// `ProtocolConfig`.
|
||||
///
|
||||
/// Consider using [`BeefyJustifsRequestHandler`] instead of this low-level function.
|
||||
pub(crate) fn on_demand_justifications_protocol_config<
|
||||
Hash: AsRef<[u8]>,
|
||||
B: Block,
|
||||
Network: NetworkBackend<B, <B as Block>::Hash>,
|
||||
>(
|
||||
genesis_hash: Hash,
|
||||
fork_id: Option<&str>,
|
||||
) -> (IncomingRequestReceiver, Network::RequestResponseProtocolConfig) {
|
||||
let name = justifications_protocol_name(genesis_hash, fork_id);
|
||||
let fallback_names = vec![];
|
||||
let (tx, rx) = async_channel::bounded(JUSTIF_CHANNEL_SIZE);
|
||||
let rx = IncomingRequestReceiver::new(rx);
|
||||
let cfg = Network::request_response_config(
|
||||
name,
|
||||
fallback_names,
|
||||
32,
|
||||
MAX_RESPONSE_SIZE,
|
||||
// We are connected to all validators:
|
||||
JUSTIF_REQUEST_TIMEOUT,
|
||||
Some(tx),
|
||||
);
|
||||
(rx, cfg)
|
||||
}
|
||||
|
||||
/// BEEFY justification request.
|
||||
#[derive(Debug, Clone, Encode, Decode)]
|
||||
pub struct JustificationRequest<B: Block> {
|
||||
/// Start collecting proofs from this block.
|
||||
pub begin: NumberFor<B>,
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
Client(#[from] pezsp_blockchain::Error),
|
||||
|
||||
#[error(transparent)]
|
||||
RuntimeApi(#[from] pezsp_api::ApiError),
|
||||
|
||||
/// Decoding failed, we were able to change the peer's reputation accordingly.
|
||||
#[error("Decoding request failed for peer {0}.")]
|
||||
DecodingError(PeerId, #[source] CodecError),
|
||||
|
||||
/// Decoding failed, but sending reputation change failed.
|
||||
#[error("Decoding request failed for peer {0}, and changing reputation failed.")]
|
||||
DecodingErrorNoReputationChange(PeerId, #[source] CodecError),
|
||||
|
||||
/// Incoming request stream exhausted. Should only happen on shutdown.
|
||||
#[error("Incoming request channel got closed.")]
|
||||
RequestChannelExhausted,
|
||||
|
||||
#[error("Failed to send response.")]
|
||||
SendResponse,
|
||||
|
||||
#[error("Received invalid response.")]
|
||||
InvalidResponse(PeerReport),
|
||||
|
||||
#[error("Internal error while getting response.")]
|
||||
ResponseError,
|
||||
|
||||
#[error("On-demand requests receiver stream terminated.")]
|
||||
RequestsReceiverStreamClosed,
|
||||
}
|
||||
+284
@@ -0,0 +1,284 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Generating request logic for request/response protocol for syncing BEEFY justifications.
|
||||
|
||||
use codec::Encode;
|
||||
use futures::channel::{oneshot, oneshot::Canceled};
|
||||
use log::{debug, warn};
|
||||
use parking_lot::Mutex;
|
||||
use pezsc_network::{
|
||||
request_responses::{IfDisconnected, RequestFailure},
|
||||
NetworkRequest, ProtocolName,
|
||||
};
|
||||
use pezsc_network_types::PeerId;
|
||||
use pezsp_consensus_beefy::{AuthorityIdBound, ValidatorSet};
|
||||
use pezsp_runtime::traits::{Block, NumberFor};
|
||||
use std::{collections::VecDeque, result::Result, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
communication::{
|
||||
benefit, cost,
|
||||
peers::PeerReport,
|
||||
request_response::{Error, JustificationRequest, BEEFY_SYNC_LOG_TARGET},
|
||||
},
|
||||
justification::{decode_and_verify_finality_proof, BeefyVersionedFinalityProof},
|
||||
metric_inc, metric_set,
|
||||
metrics::{register_metrics, OnDemandOutgoingRequestsMetrics},
|
||||
KnownPeers,
|
||||
};
|
||||
|
||||
/// Response type received from network.
|
||||
type Response = Result<(Vec<u8>, ProtocolName), RequestFailure>;
|
||||
/// Used to receive a response from the network.
|
||||
type ResponseReceiver = oneshot::Receiver<Response>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct RequestInfo<B: Block, AuthorityId: AuthorityIdBound> {
|
||||
block: NumberFor<B>,
|
||||
active_set: ValidatorSet<AuthorityId>,
|
||||
}
|
||||
|
||||
enum State<B: Block, AuthorityId: AuthorityIdBound> {
|
||||
Idle,
|
||||
AwaitingResponse(PeerId, RequestInfo<B, AuthorityId>, ResponseReceiver),
|
||||
}
|
||||
|
||||
/// Possible engine responses.
|
||||
pub(crate) enum ResponseInfo<B: Block, AuthorityId: AuthorityIdBound> {
|
||||
/// No peer response available yet.
|
||||
Pending,
|
||||
/// Valid justification provided alongside peer reputation changes.
|
||||
ValidProof(BeefyVersionedFinalityProof<B, AuthorityId>, PeerReport),
|
||||
/// No justification yet, only peer reputation changes.
|
||||
PeerReport(PeerReport),
|
||||
}
|
||||
|
||||
pub struct OnDemandJustificationsEngine<B: Block, AuthorityId: AuthorityIdBound> {
|
||||
network: Arc<dyn NetworkRequest + Send + Sync>,
|
||||
protocol_name: ProtocolName,
|
||||
|
||||
live_peers: Arc<Mutex<KnownPeers<B>>>,
|
||||
peers_cache: VecDeque<PeerId>,
|
||||
|
||||
state: State<B, AuthorityId>,
|
||||
metrics: Option<OnDemandOutgoingRequestsMetrics>,
|
||||
}
|
||||
|
||||
impl<B: Block, AuthorityId: AuthorityIdBound> OnDemandJustificationsEngine<B, AuthorityId> {
|
||||
pub fn new(
|
||||
network: Arc<dyn NetworkRequest + Send + Sync>,
|
||||
protocol_name: ProtocolName,
|
||||
live_peers: Arc<Mutex<KnownPeers<B>>>,
|
||||
prometheus_registry: Option<prometheus_endpoint::Registry>,
|
||||
) -> Self {
|
||||
let metrics = register_metrics(prometheus_registry);
|
||||
Self {
|
||||
network,
|
||||
protocol_name,
|
||||
live_peers,
|
||||
peers_cache: VecDeque::new(),
|
||||
state: State::Idle,
|
||||
metrics,
|
||||
}
|
||||
}
|
||||
|
||||
fn reset_peers_cache_for_block(&mut self, block: NumberFor<B>) {
|
||||
self.peers_cache = self.live_peers.lock().further_than(block);
|
||||
}
|
||||
|
||||
fn try_next_peer(&mut self) -> Option<PeerId> {
|
||||
let live = self.live_peers.lock();
|
||||
while let Some(peer) = self.peers_cache.pop_front() {
|
||||
if live.contains(&peer) {
|
||||
return Some(peer);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn request_from_peer(&mut self, peer: PeerId, req_info: RequestInfo<B, AuthorityId>) {
|
||||
debug!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 requesting justif #{:?} from peer {:?}", req_info.block, peer,
|
||||
);
|
||||
|
||||
let payload = JustificationRequest::<B> { begin: req_info.block }.encode();
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
self.network.start_request(
|
||||
peer,
|
||||
self.protocol_name.clone(),
|
||||
payload,
|
||||
None,
|
||||
tx,
|
||||
IfDisconnected::ImmediateError,
|
||||
);
|
||||
|
||||
self.state = State::AwaitingResponse(peer, req_info, rx);
|
||||
}
|
||||
|
||||
/// Start new justification request for `block`, if no other request is in progress.
|
||||
///
|
||||
/// `active_set` will be used to verify validity of potential responses.
|
||||
pub fn request(&mut self, block: NumberFor<B>, active_set: ValidatorSet<AuthorityId>) {
|
||||
// ignore new requests while there's already one pending
|
||||
if matches!(self.state, State::AwaitingResponse(_, _, _)) {
|
||||
return;
|
||||
}
|
||||
self.reset_peers_cache_for_block(block);
|
||||
|
||||
// Start the requests engine - each unsuccessful received response will automatically
|
||||
// trigger a new request to the next peer in the `peers_cache` until there are none left.
|
||||
if let Some(peer) = self.try_next_peer() {
|
||||
self.request_from_peer(peer, RequestInfo { block, active_set });
|
||||
} else {
|
||||
metric_inc!(self.metrics, beefy_on_demand_justification_no_peer_to_request_from);
|
||||
debug!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 no good peers to request justif #{:?} from", block
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Cancel any pending request for block numbers smaller or equal to `block`.
|
||||
pub fn cancel_requests_older_than(&mut self, block: NumberFor<B>) {
|
||||
match &self.state {
|
||||
State::AwaitingResponse(_, req_info, _) if req_info.block <= block => {
|
||||
debug!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 cancel pending request for justification #{:?}", req_info.block
|
||||
);
|
||||
self.state = State::Idle;
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn process_response(
|
||||
&mut self,
|
||||
peer: &PeerId,
|
||||
req_info: &RequestInfo<B, AuthorityId>,
|
||||
response: Result<Response, Canceled>,
|
||||
) -> Result<BeefyVersionedFinalityProof<B, AuthorityId>, Error> {
|
||||
response
|
||||
.map_err(|e| {
|
||||
debug!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 on-demand sc-network channel sender closed, err: {:?}", e
|
||||
);
|
||||
Error::ResponseError
|
||||
})?
|
||||
.map_err(|e| {
|
||||
debug!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 for on demand justification #{:?}, peer {:?} error: {:?}",
|
||||
req_info.block,
|
||||
peer,
|
||||
e
|
||||
);
|
||||
match e {
|
||||
RequestFailure::Refused => {
|
||||
metric_inc!(self.metrics, beefy_on_demand_justification_peer_refused);
|
||||
let peer_report =
|
||||
PeerReport { who: *peer, cost_benefit: cost::REFUSAL_RESPONSE };
|
||||
Error::InvalidResponse(peer_report)
|
||||
},
|
||||
_ => {
|
||||
metric_inc!(self.metrics, beefy_on_demand_justification_peer_error);
|
||||
Error::ResponseError
|
||||
},
|
||||
}
|
||||
})
|
||||
.and_then(|(encoded, _)| {
|
||||
decode_and_verify_finality_proof::<B, AuthorityId>(
|
||||
&encoded[..],
|
||||
req_info.block,
|
||||
&req_info.active_set,
|
||||
)
|
||||
.map_err(|(err, signatures_checked)| {
|
||||
metric_inc!(self.metrics, beefy_on_demand_justification_invalid_proof);
|
||||
debug!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 for on demand justification #{:?}, peer {:?} responded with invalid proof: {:?}",
|
||||
req_info.block, peer, err
|
||||
);
|
||||
let mut cost = cost::INVALID_PROOF;
|
||||
cost.value +=
|
||||
cost::PER_SIGNATURE_CHECKED.saturating_mul(signatures_checked as i32);
|
||||
Error::InvalidResponse(PeerReport { who: *peer, cost_benefit: cost })
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn next(&mut self) -> ResponseInfo<B, AuthorityId> {
|
||||
let (peer, req_info, resp) = match &mut self.state {
|
||||
State::Idle => {
|
||||
futures::future::pending::<()>().await;
|
||||
return ResponseInfo::Pending;
|
||||
},
|
||||
State::AwaitingResponse(peer, req_info, receiver) => {
|
||||
let resp = receiver.await;
|
||||
(*peer, req_info.clone(), resp)
|
||||
},
|
||||
};
|
||||
// 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;
|
||||
|
||||
metric_set!(self.metrics, beefy_on_demand_live_peers, self.live_peers.lock().len() as u64);
|
||||
|
||||
let block = req_info.block;
|
||||
match self.process_response(&peer, &req_info, resp) {
|
||||
Err(err) => {
|
||||
// No valid justification received, try next peer in our set.
|
||||
if let Some(peer) = self.try_next_peer() {
|
||||
self.request_from_peer(peer, req_info);
|
||||
} else {
|
||||
metric_inc!(
|
||||
self.metrics,
|
||||
beefy_on_demand_justification_no_peer_to_request_from
|
||||
);
|
||||
|
||||
let num_cache = self.peers_cache.len();
|
||||
let num_live = self.live_peers.lock().len();
|
||||
warn!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 ran out of peers to request justif #{block:?} from num_cache={num_cache} num_live={num_live} err={err:?}",
|
||||
);
|
||||
}
|
||||
// Report peer based on error type.
|
||||
if let Error::InvalidResponse(peer_report) = err {
|
||||
ResponseInfo::PeerReport(peer_report)
|
||||
} else {
|
||||
ResponseInfo::Pending
|
||||
}
|
||||
},
|
||||
Ok(proof) => {
|
||||
metric_inc!(self.metrics, beefy_on_demand_justification_good_proof);
|
||||
debug!(
|
||||
target: BEEFY_SYNC_LOG_TARGET,
|
||||
"🥩 received valid on-demand justif #{block:?} from {peer:?}",
|
||||
);
|
||||
let peer_report = PeerReport { who: peer, cost_benefit: benefit::VALIDATED_PROOF };
|
||||
ResponseInfo::ValidProof(proof, peer_report)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user