use std::fmt::{self, Display, Formatter};
use casper_types::{Digest, EraId};
use datasize::DataSize;
use serde::Serialize;
use crate::{
components::{block_accumulator, fetcher::Tag},
types::InvalidProposalError,
};
#[derive(DataSize, Debug, Serialize)]
pub(crate) enum BlocklistJustification {
SentBadItem { tag: Tag },
SentInvalidItem { tag: Tag, error_msg: String },
SentBadFinalitySignature {
#[serde(skip_serializing)]
#[data_size(skip)]
error: block_accumulator::Error,
},
SentBadBlock {
#[serde(skip_serializing)]
#[data_size(skip)]
error: block_accumulator::Error,
},
SentInvalidProposal {
era: EraId,
#[serde(skip_serializing)]
error: Box<InvalidProposalError>,
},
#[allow(dead_code)] PongLimitExceeded,
BadConsensusBehavior,
WrongNetwork {
peer_network_name: String,
},
WrongChainspecHash {
peer_chainspec_hash: Digest,
},
MissingChainspecHash,
DishonestPeer,
SentTooManyFinalitySignatures { max_allowed: u32 },
}
impl Display for BlocklistJustification {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
BlocklistJustification::SentBadItem { tag } => {
write!(f, "sent a {} we couldn't parse", tag)
}
BlocklistJustification::SentInvalidItem { tag, error_msg } => {
write!(f, "sent a {} which failed validation ({})", tag, error_msg)
}
BlocklistJustification::SentBadFinalitySignature { error } => write!(
f,
"sent a finality signature that is invalid or unexpected ({})",
error
),
BlocklistJustification::SentInvalidProposal { era, error } => {
write!(f, "sent an invalid proposal in {} ({:?})", era, error)
}
BlocklistJustification::PongLimitExceeded => {
f.write_str("wrote too many expired or invalid pongs")
}
BlocklistJustification::BadConsensusBehavior => {
f.write_str("sent invalid data in consensus")
}
BlocklistJustification::WrongNetwork { peer_network_name } => write!(
f,
"reported to be on the wrong network ({:?})",
peer_network_name
),
BlocklistJustification::WrongChainspecHash {
peer_chainspec_hash,
} => write!(
f,
"reported a mismatched chainspec hash ({})",
peer_chainspec_hash
),
BlocklistJustification::MissingChainspecHash => {
f.write_str("sent handshake without chainspec hash")
}
BlocklistJustification::SentBadBlock { error } => {
write!(f, "sent a block that is invalid or unexpected ({})", error)
}
BlocklistJustification::DishonestPeer => f.write_str("dishonest peer"),
BlocklistJustification::SentTooManyFinalitySignatures { max_allowed } => write!(
f,
"sent too many finality signatures: maximum {max_allowed} signatures are allowed"
),
}
}
}