use crate::{Address, BlobWrite, Error, MsgSender, Result};
use serde::{Deserialize, Serialize};
use sn_data_types::{
Blob, BlobAddress, DebitId, PublicKey, ReplicaEvent, Signature, SignedTransfer,
TransferAgreementProof, TransferValidated,
};
use std::collections::BTreeSet;
use xor_name::XorName;
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeCmd {
System(NodeSystemCmd),
Data(NodeDataCmd),
Transfers(NodeTransferCmd),
}
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeSystemCmd {
RegisterWallet {
wallet: PublicKey,
section: XorName,
},
StorageFull {
node_id: PublicKey,
section: XorName,
},
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeTransferCmd {
PropagateTransfer(TransferAgreementProof),
ValidateSectionPayout(SignedTransfer),
RegisterSectionPayout(TransferAgreementProof),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeDataCmd {
ReplicateChunk {
new_holder: XorName,
address: BlobAddress,
current_holders: BTreeSet<XorName>,
},
Blob(BlobWrite),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeEvent {
ReplicationCompleted {
chunk: BlobAddress,
proof: Signature,
},
SectionPayoutValidated(TransferValidated),
}
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeQuery {
Data(NodeDataQuery),
Rewards(NodeRewardQuery),
Transfers(NodeTransferQuery),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeRewardQuery {
GetWalletId {
old_node_id: XorName,
new_node_id: XorName,
},
}
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeTransferQuery {
GetSectionActorHistory(PublicKey),
GetReplicaEvents(PublicKey),
}
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeDataQuery {
GetChunk {
new_holder: XorName,
address: BlobAddress,
section_authority: MsgSender,
current_holders: BTreeSet<XorName>,
},
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeQueryResponse {
Data(NodeDataQueryResponse),
Rewards(NodeRewardQueryResponse),
Transfers(NodeTransferQueryResponse),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeRewardQueryResponse {
GetWalletId(Result<(PublicKey, XorName)>),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeTransferQueryResponse {
GetSectionActorHistory(Result<Vec<ReplicaEvent>>),
GetReplicaEvents(Result<Vec<ReplicaEvent>>),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeDataQueryResponse {
GetChunk(Result<Blob>),
GetChunks(Result<Vec<Blob>>),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeCmdError {
Data(NodeDataError),
Rewards(NodeRewardError),
Transfers(NodeTransferError),
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeDataError {
ChunkReplication {
address: BlobAddress,
error: Error,
},
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeTransferError {
TransferPropagation(Error),
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeRewardError {
RewardClaiming {
wallet: PublicKey,
error: Error,
},
RewardPayoutInitiation {
id: DebitId,
wallet: PublicKey,
error: Error,
},
RewardPayoutFinalisation {
id: DebitId,
wallet: PublicKey,
error: Error,
},
}
impl NodeCmd {
pub fn dst_address(&self) -> Address {
use Address::*;
use NodeCmd::*;
use NodeDataCmd::*;
use NodeTransferCmd::*;
match self {
System(NodeSystemCmd::RegisterWallet { section, .. }) => Section(*section),
System(NodeSystemCmd::StorageFull { section, .. }) => Section(*section),
Data(cmd) => match cmd {
ReplicateChunk { new_holder, .. } => Node(*new_holder),
Blob(_write) => Node(XorName::default()),
},
Transfers(cmd) => match cmd {
ValidateSectionPayout(signed_debit) => Section(signed_debit.sender().into()),
RegisterSectionPayout(transfer_agreement) => {
Section(transfer_agreement.sender().into())
}
PropagateTransfer(transfer_agreement) => {
Section(transfer_agreement.recipient().into())
}
},
}
}
}
impl NodeEvent {
pub fn dst_address(&self) -> Address {
use Address::*;
use NodeEvent::*;
match self {
ReplicationCompleted { chunk, .. } => Section(*chunk.name()),
SectionPayoutValidated(event) => Section(event.sender().into()),
}
}
}
impl NodeQuery {
pub fn dst_address(&self) -> Address {
use Address::*;
use NodeDataQuery::*;
use NodeQuery::*;
use NodeRewardQuery::*;
use NodeTransferQuery::*;
match self {
Data(data_query) => match data_query {
GetChunk {
current_holders, ..
} => Node(*current_holders.iter().next().unwrap_or(&XorName::random())),
},
Transfers(transfer_query) => match transfer_query {
GetReplicaEvents(section_key) => Section((*section_key).into()),
GetSectionActorHistory(section_key) => Section((*section_key).into()),
},
Rewards(GetWalletId { old_node_id, .. }) => Section(*old_node_id),
}
}
}