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::{NetworkAddress, error::Result};
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 /// Response to [`GetMerkleCandidateQuote`]
91 ///
92 /// Node's signed commitment containing:
93 /// - pub_key (can derive PeerId)
94 /// - quoting_metrics (current node state)
95 /// - reward_address (where to send payment)
96 /// - merkle_payment_timestamp (binds signature to time)
97 /// - signature (proves pub_key owns reward_address)
98 ///
99 /// [`GetMerkleCandidateQuote`]: crate::messages::Query::GetMerkleCandidateQuote
100 GetMerkleCandidateQuote(Result<ant_evm::merkle_payments::MerklePaymentCandidateNode>),
101}
102
103// Debug implementation for QueryResponse, to avoid printing Vec<u8>
104impl Debug for QueryResponse {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 match self {
107 QueryResponse::GetStoreQuote {
108 quote,
109 peer_address,
110 storage_proofs,
111 } => {
112 let payment_address = quote.as_ref().map(|q| q.rewards_address).ok();
113 write!(
114 f,
115 "GetStoreQuote(quote: {quote:?}, from {peer_address:?} w/ payment_address: {payment_address:?}, and {} storage proofs)",
116 storage_proofs.len()
117 )
118 }
119 QueryResponse::CheckNodeInProblem {
120 reporter_address,
121 target_address,
122 is_in_trouble,
123 } => {
124 write!(
125 f,
126 "CheckNodeInProblem({reporter_address:?} report target {target_address:?} as {is_in_trouble:?} in problem"
127 )
128 }
129 QueryResponse::GetReplicatedRecord(result) => match result {
130 Ok((holder, data)) => {
131 write!(
132 f,
133 "GetReplicatedRecord(Ok((holder: {:?}, datalen: {:?})))",
134 holder,
135 data.len()
136 )
137 }
138 Err(err) => {
139 write!(f, "GetReplicatedRecord(Err({err:?}))")
140 }
141 },
142 QueryResponse::GetChunkExistenceProof(proofs) => {
143 let addresses: Vec<_> = proofs.iter().map(|(addr, _)| addr.clone()).collect();
144 write!(f, "GetChunkExistenceProof(checked chunks: {addresses:?})")
145 }
146 QueryResponse::GetClosestPeers { target, peers, .. } => {
147 let addresses: Vec<_> = peers.iter().map(|(addr, _)| addr.clone()).collect();
148 write!(
149 f,
150 "GetClosestPeers target {target:?} close peers {addresses:?}"
151 )
152 }
153 QueryResponse::GetVersion { peer, version } => {
154 write!(f, "GetVersion peer {peer:?} has version of {version:?}")
155 }
156 QueryResponse::PutRecord {
157 result,
158 peer_address,
159 record_addr,
160 } => {
161 write!(
162 f,
163 "PutRecord(Record {record_addr:?} uploaded to {peer_address:?} with result {result:?})",
164 )
165 }
166 QueryResponse::GetMerkleCandidateQuote(result) => match result {
167 Ok(candidate) => {
168 write!(
169 f,
170 "GetMerkleCandidateQuote(Ok(reward_address: {:?}, timestamp: {}))",
171 candidate.reward_address, candidate.merkle_payment_timestamp
172 )
173 }
174 Err(err) => {
175 write!(f, "GetMerkleCandidateQuote(Err({err:?}))")
176 }
177 },
178 }
179 }
180}
181
182/// The response to a Cmd, containing the query result.
183#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
184pub enum CmdResponse {
185 //
186 // ===== Replication =====
187 //
188 /// Response to replication cmd
189 Replicate(Result<()>),
190 /// Response to fresh replication cmd
191 FreshReplicate(Result<()>),
192 //
193 // ===== PeerConsideredAsBad =====
194 //
195 /// Response to the considered as bad notification
196 PeerConsideredAsBad(Result<()>),
197}