mod dkg;
mod join;
mod node_msgs;
mod op_id;
mod section_sig;
use super::{data::CmdResponse, MsgId};
use crate::messaging::AuthorityProof;
use crate::network_knowledge::{NodeState, RelocationProof, SapCandidate, SectionTreeUpdate};
use crate::SectionAuthorityProvider;
pub use dkg::DkgSessionId;
pub use join::{JoinRejectReason, JoinRequest, JoinResponse};
pub use node_msgs::{NodeDataCmd, NodeDataQuery, NodeEvent, NodeQueryResponse};
pub use op_id::OperationId;
pub use section_sig::{SectionSig, SectionSigShare, SectionSigned};
use bls::PublicKey as BlsPublicKey;
use ed25519::Signature;
use qp2p::UsrMsgBytes;
use serde::{Deserialize, Serialize};
use sn_consensus::{Generation, SignedVote};
use sn_sdkg::DkgSignedVote;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{self, Display, Formatter};
use xor_name::XorName;
pub type SectionPeers = BTreeSet<SectionSigned<NodeState>>;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, custom_debug::Debug)]
pub enum AntiEntropyKind {
Retry {
#[debug(skip)]
bounced_msg: UsrMsgBytes,
},
Redirect {
#[debug(skip)]
bounced_msg: UsrMsgBytes,
},
Update { members: SectionPeers },
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum SectionStateVote {
NodeIsOffline(NodeState),
JoinsAllowed(bool),
}
#[derive(Clone, PartialEq, Serialize, Deserialize, custom_debug::Debug)]
#[allow(clippy::large_enum_variant, clippy::derive_partial_eq_without_eq)]
pub enum NodeMsg {
AntiEntropy {
section_tree_update: SectionTreeUpdate,
kind: AntiEntropyKind,
},
AntiEntropyProbe(BlsPublicKey),
Relocate(SectionSigned<NodeState>),
MembershipVotes(Vec<SignedVote<NodeState>>),
MembershipAE(Generation),
TryJoin(Option<RelocationProof>),
JoinResponse(JoinResponse),
DkgStart(DkgSessionId, SectionSigShare),
DkgEphemeralPubKey {
session_id: DkgSessionId,
section_auth: AuthorityProof<SectionSig>,
pub_key: BlsPublicKey,
sig: Signature,
},
DkgVotes {
session_id: DkgSessionId,
pub_keys: BTreeMap<XorName, (BlsPublicKey, Signature)>,
votes: Vec<DkgSignedVote>,
},
DkgAE(DkgSessionId),
RequestHandover {
sap: SectionAuthorityProvider,
sig_share: SectionSigShare,
},
HandoverVotes(Vec<SignedVote<SapCandidate>>),
HandoverAE(Generation),
SectionHandoverPromotion {
sap: SectionSigned<SectionAuthorityProvider>,
sig_share: SectionSigShare,
},
SectionSplitPromotion {
sap0: SectionSigned<SectionAuthorityProvider>,
sig_share0: SectionSigShare,
sap1: SectionSigned<SectionAuthorityProvider>,
sig_share1: SectionSigShare,
},
ProposeSectionState {
proposal: SectionStateVote,
sig_share: SectionSigShare,
},
NodeEvent(NodeEvent),
NodeDataCmd(NodeDataCmd),
NodeDataQuery(NodeDataQuery),
}
impl NodeMsg {
pub fn is_join(&self) -> bool {
matches!(self, NodeMsg::TryJoin(_))
}
pub fn is_ae(&self) -> bool {
matches!(self, NodeMsg::AntiEntropy { .. })
}
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeDataResponse {
QueryResponse {
response: NodeQueryResponse,
operation_id: OperationId,
},
CmdResponse {
response: CmdResponse,
correlation_id: MsgId,
},
}
impl Display for NodeDataResponse {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::QueryResponse { response, .. } => {
write!(f, "NodeDataResponse::QueryResponse({response:?})")
}
Self::CmdResponse { response, .. } => {
write!(f, "NodeDataResponse::CmdResponse({response:?})")
}
}
}
}
impl NodeMsg {
pub fn statemap_states(&self) -> crate::statemap::State {
use crate::statemap::State;
match self {
Self::AntiEntropy { .. } => State::AntiEntropy,
Self::AntiEntropyProbe { .. } => State::AntiEntropy,
Self::Relocate(_) => State::Relocate,
Self::MembershipAE(_) => State::Membership,
Self::MembershipVotes(_) => State::Membership,
Self::TryJoin(_) => State::Join,
Self::JoinResponse(_) => State::Join,
Self::DkgStart { .. } => State::Dkg,
Self::DkgEphemeralPubKey { .. } => State::Dkg,
Self::DkgVotes { .. } => State::Dkg,
Self::DkgAE { .. } => State::Dkg,
Self::RequestHandover { .. } => State::Dkg,
Self::HandoverVotes(_) => State::Handover,
Self::HandoverAE(_) => State::Handover,
Self::SectionHandoverPromotion { .. } => State::Handover,
Self::SectionSplitPromotion { .. } => State::Handover,
Self::ProposeSectionState { .. } => State::Propose,
Self::NodeEvent(_) => State::Node,
Self::NodeDataCmd(_) => State::Node,
Self::NodeDataQuery(_) => State::Node,
}
}
}
impl Display for NodeMsg {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::AntiEntropy { .. } => write!(f, "NodeMsg::AntiEntropy"),
Self::AntiEntropyProbe { .. } => write!(f, "NodeMsg::AntiEntropyProbe"),
Self::Relocate { .. } => write!(f, "NodeMsg::Relocate"),
Self::MembershipVotes { .. } => write!(f, "NodeMsg::MembershipVotes"),
Self::MembershipAE { .. } => write!(f, "NodeMsg::MembershipAE"),
Self::TryJoin(_) => write!(f, "NodeMsg::TryJoin"),
Self::JoinResponse { .. } => write!(f, "NodeMsg::JoinResponse"),
Self::DkgStart { .. } => write!(f, "NodeMsg::DkgStart"),
Self::DkgEphemeralPubKey { .. } => write!(f, "NodeMsg::DkgEphemeralPubKey"),
Self::DkgVotes { .. } => write!(f, "NodeMsg::DkgVotes"),
Self::DkgAE { .. } => write!(f, "NodeMsg::DkgAE"),
Self::RequestHandover { .. } => write!(f, "NodeMsg::RequestHandover"),
Self::HandoverVotes { .. } => write!(f, "NodeMsg::HandoverVotes"),
Self::HandoverAE { .. } => write!(f, "NodeMsg::HandoverAE"),
Self::SectionHandoverPromotion { .. } => write!(f, "NodeMsg::SectionHandoverPromotion"),
Self::SectionSplitPromotion { .. } => write!(f, "NodeMsg::SectionSplitPromotion"),
Self::ProposeSectionState { .. } => write!(f, "NodeMsg::ProposeSectionState"),
Self::NodeEvent { .. } => write!(f, "NodeMsg::NodeEvent"),
Self::NodeDataCmd { .. } => write!(f, "NodeMsg::NodeCmd"),
Self::NodeDataQuery { .. } => write!(f, "NodeMsg::NodeQuery"),
}
}
}