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:
+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