ant_protocol/messages/response.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
// Copyright 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.
use crate::{error::Result, NetworkAddress};
use super::ChunkProof;
use ant_evm::PaymentQuote;
use bytes::Bytes;
use core::fmt;
use libp2p::Multiaddr;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
/// The response to a query, containing the query result.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum QueryResponse {
// ===== GetStoreQuote =====
//
/// Response to [`GetStoreQuote`]
///
/// [`GetStoreQuote`]: crate::messages::Query::GetStoreQuote
GetStoreQuote {
/// The store cost quote for storing the next record.
quote: Result<PaymentQuote>,
/// Node's Peer Address
peer_address: NetworkAddress,
/// Storage proofs based on requested target address and difficulty
storage_proofs: Vec<(NetworkAddress, Result<ChunkProof>)>,
},
CheckNodeInProblem {
/// Address of the peer that queried
reporter_address: NetworkAddress,
/// Address of the target to be queried
target_address: NetworkAddress,
/// Status flag indicating whether the target is in trouble
is_in_trouble: bool,
},
// ===== ReplicatedRecord =====
//
/// Response to [`GetReplicatedRecord`]
///
/// [`GetReplicatedRecord`]: crate::messages::Query::GetReplicatedRecord
GetReplicatedRecord(Result<(NetworkAddress, Bytes)>),
// ===== ChunkExistenceProof =====
//
/// Response to [`GetChunkExistenceProof`]
///
/// [`GetChunkExistenceProof`]: crate::messages::Query::GetChunkExistenceProof
GetChunkExistenceProof(Vec<(NetworkAddress, Result<ChunkProof>)>),
// ===== GetClosestPeers =====
//
/// Response to [`GetClosestPeers`]
///
/// [`GetClosestPeers`]: crate::messages::Query::GetClosestPeers
GetClosestPeers {
// The target address that the original request is about.
target: NetworkAddress,
// `Multiaddr` is required to allow the requester to dial the peer
// Note: the list doesn't contain the node that being queried.
peers: Vec<(NetworkAddress, Vec<Multiaddr>)>,
// Signature of signing the above (if requested), for future economic model usage.
signature: Option<Vec<u8>>,
},
}
// Debug implementation for QueryResponse, to avoid printing Vec<u8>
impl Debug for QueryResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
QueryResponse::GetStoreQuote {
quote,
peer_address,
storage_proofs,
} => {
let payment_address = quote.as_ref().map(|q| q.rewards_address).ok();
write!(
f,
"GetStoreQuote(quote: {quote:?}, from {peer_address:?} w/ payment_address: {payment_address:?}, and {} storage proofs)",
storage_proofs.len()
)
}
QueryResponse::CheckNodeInProblem {
reporter_address,
target_address,
is_in_trouble,
} => {
write!(
f,
"CheckNodeInProblem({reporter_address:?} report target {target_address:?} as {is_in_trouble:?} in problem"
)
}
QueryResponse::GetReplicatedRecord(result) => match result {
Ok((holder, data)) => {
write!(
f,
"GetReplicatedRecord(Ok((holder: {:?}, datalen: {:?})))",
holder,
data.len()
)
}
Err(err) => {
write!(f, "GetReplicatedRecord(Err({err:?}))")
}
},
QueryResponse::GetChunkExistenceProof(proofs) => {
let addresses: Vec<_> = proofs.iter().map(|(addr, _)| addr.clone()).collect();
write!(f, "GetChunkExistenceProof(checked chunks: {addresses:?})")
}
QueryResponse::GetClosestPeers { target, peers, .. } => {
let addresses: Vec<_> = peers.iter().map(|(addr, _)| addr.clone()).collect();
write!(
f,
"GetClosestPeers target {target:?} close peers {addresses:?}"
)
}
}
}
}
/// The response to a Cmd, containing the query result.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CmdResponse {
//
// ===== Replication =====
//
/// Response to replication cmd
Replicate(Result<()>),
/// Response to fresh replication cmd
FreshReplicate(Result<()>),
//
// ===== PeerConsideredAsBad =====
//
/// Response to the considered as bad notification
PeerConsideredAsBad(Result<()>),
}