use super::{agreement::SectionSigned, section::NodeState};
use crate::SectionAuthorityProvider;
use ed25519_dalek::Signature;
use secured_linked_list::SecuredLinkedList;
use serde::{Deserialize, Serialize};
use std::{
collections::VecDeque,
fmt::{self, Debug, Formatter},
net::SocketAddr,
};
use threshold_crypto::PublicKey as BlsPublicKey;
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct JoinRequest {
pub section_key: BlsPublicKey,
pub resource_proof_response: Option<ResourceProofResponse>,
}
impl Debug for JoinRequest {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
formatter
.debug_struct("JoinRequest")
.field("section_key", &self.section_key)
.field(
"resource_proof_response",
&self
.resource_proof_response
.as_ref()
.map(|proof| proof.solution),
)
.finish()
}
}
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ResourceProofResponse {
pub solution: u64,
pub data: VecDeque<u8>,
pub nonce: [u8; 32],
pub nonce_signature: Signature,
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum JoinResponse {
ResourceChallenge {
data_size: usize,
difficulty: u8,
nonce: [u8; 32],
nonce_signature: Signature,
},
Retry(SectionAuthorityProvider),
Redirect(SectionAuthorityProvider),
Approval {
genesis_key: BlsPublicKey,
section_auth: SectionSigned<SectionAuthorityProvider>,
node_state: SectionSigned<NodeState>,
section_chain: SecuredLinkedList,
},
Rejected(JoinRejectionReason),
}
impl Debug for JoinResponse {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::ResourceChallenge {
data_size,
difficulty,
..
} => f
.debug_struct("ResourceChallenge")
.field("data_size", data_size)
.field("difficulty", difficulty)
.finish(),
Self::Retry(section_auth) => write!(f, "Retry({:?})", section_auth),
Self::Redirect(section_auth) => write!(f, "Redirect({:?})", section_auth),
Self::Approval {
genesis_key,
section_auth,
node_state,
section_chain,
} => f
.debug_struct("Approval")
.field("genesis_key", genesis_key)
.field("section_auth", section_auth)
.field("node_state", node_state)
.field("section_chain", section_chain)
.finish(),
Self::Rejected(reason) => write!(f, "Rejected({:?})", reason),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum JoinRejectionReason {
JoinsDisallowed,
NodeNotReachable(SocketAddr),
}