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