use crate::{
Address, Blob, BlobAddress, DebitAgreementProof, Error, PublicKey, ReplicaEvent, Result,
Signature, SignedTransfer, TransferId, TransferValidated, XorName,
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeCmd {
System(NodeSystemCmd),
Data(NodeDataCmd),
Transfers(NodeTransferCmd),
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeSystemCmd {
RegisterWallet {
wallet: PublicKey,
section: XorName,
},
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeTransferCmd {
PropagateTransfer(DebitAgreementProof),
ValidateSectionPayout(SignedTransfer),
RegisterSectionPayout(DebitAgreementProof),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeDataCmd {
DuplicateChunk {
new_holder: XorName,
address: BlobAddress,
fetch_from_holders: BTreeSet<XorName>,
},
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeEvent {
DuplicationComplete {
chunk: BlobAddress,
proof: Signature,
},
SectionPayoutValidated(TransferValidated),
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeQuery {
Data(NodeDataQuery),
Rewards(NodeRewardQuery),
Transfers(NodeTransferQuery),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeRewardQuery {
GetWalletId {
old_node_id: XorName,
new_node_id: XorName,
},
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeTransferQuery {
GetReplicaEvents(PublicKey),
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeDataQuery {
GetChunk {
holder: XorName,
address: BlobAddress,
},
GetChunks {
holder: XorName,
addresses: BTreeSet<BlobAddress>,
},
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeQueryResponse {
Data(NodeDataQueryResponse),
Rewards(NodeRewardQueryResponse),
Transfers(NodeTransferQueryResponse),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeRewardQueryResponse {
GetWalletId(Result<(PublicKey, XorName)>),
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum NodeTransferQueryResponse {
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 {
ChunkDuplication {
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: TransferId,
wallet: PublicKey,
error: Error,
},
RewardPayoutFinalisation {
id: TransferId,
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),
Data(DuplicateChunk { new_holder, .. }) => Node(*new_holder),
Transfers(cmd) => match cmd {
ValidateSectionPayout(signed_transfer) => Section(signed_transfer.from().into()),
RegisterSectionPayout(debit_agreement) => Section(debit_agreement.from().into()),
PropagateTransfer(debit_agreement) => Section(debit_agreement.to().into()),
},
}
}
}
impl NodeEvent {
pub fn dst_address(&self) -> Address {
use Address::*;
use NodeEvent::*;
match self {
DuplicationComplete { chunk, .. } => Section(*chunk.name()),
SectionPayoutValidated(event) => Section(event.from().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 { holder, .. } | GetChunks { holder, .. } => Node(*holder),
},
Transfers(transfer_query) => match transfer_query {
GetReplicaEvents(section_key) => Section((*section_key).into()),
},
Rewards(GetWalletId { old_node_id, .. }) => Section(*old_node_id),
}
}
}