use super::{CmdResponse, Error, QueryResponse};
use crate::dbcs::DbcReason;
use crate::messaging::system::SectionSigned;
use crate::network_knowledge::{SectionAuthorityProvider, SectionsDAG};
use crate::types::{fees::FeeCiphers, SpentbookAddress};
use sn_dbc::{DbcTransaction, PublicKey, SpentProof};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use xor_name::XorName;
#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize, Debug)]
pub enum SpendQuery {
GetFees(PublicKey),
GetSpentProofShares(SpentbookAddress),
}
#[derive(Eq, PartialEq, Clone, Serialize, Deserialize, custom_debug::Debug)]
pub enum SpentbookCmd {
Spend {
public_key: PublicKey,
#[debug(skip)]
tx: DbcTransaction,
reason: DbcReason,
#[debug(skip)]
spent_proofs: BTreeSet<SpentProof>,
#[debug(skip)]
spent_transactions: BTreeSet<DbcTransaction>,
#[debug(skip)]
network_knowledge: Option<(SectionsDAG, SectionSigned<SectionAuthorityProvider>)>,
#[debug(skip)]
#[cfg(not(feature = "data-network"))]
fee_ciphers: BTreeMap<XorName, FeeCiphers>,
},
}
impl SpentbookCmd {
pub fn to_error_response(&self, error: Error) -> CmdResponse {
match self {
Self::Spend { .. } => CmdResponse::SpendKey(Err(error)),
}
}
}
impl SpendQuery {
pub fn to_error_response(&self, error: Error) -> QueryResponse {
match self {
Self::GetFees(_) => QueryResponse::GetFees(Err(error)),
Self::GetSpentProofShares(_) => QueryResponse::GetSpentProofShares(Err(error)),
}
}
pub fn dst_address(&self) -> SpentbookAddress {
match self {
Self::GetFees(dbc_id) => {
SpentbookAddress::new(XorName::from_content(&dbc_id.to_bytes()))
}
Self::GetSpentProofShares(address) => *address,
}
}
pub fn dst_name(&self) -> XorName {
*self.dst_address().name()
}
}
impl SpentbookCmd {
pub fn name(&self) -> XorName {
*self.dst_address().name()
}
pub fn dst_address(&self) -> SpentbookAddress {
match self {
Self::Spend { public_key, .. } => {
SpentbookAddress::new(XorName::from_content(&public_key.to_bytes()))
}
}
}
}