ant-protocol 1.0.14

Defines the network protocol for Autonomi
Documentation
// 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::{NetworkAddress, error::Result};

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>>,
    },
    /// *** From now on, the order of variants shall be retained to be backward compatible
    // ===== GetVersion =====
    //
    /// Response to [`GetVersion`]
    ///
    /// [`GetVersion`]: crate::messages::Query::GetVersion
    GetVersion {
        peer: NetworkAddress,
        version: String,
    },
    /// Response to [`PutRecord`]
    ///
    /// [`PutRecord`]: crate::messages::Query::PutRecord
    PutRecord {
        /// Result of record upload.
        result: Result<()>,
        /// Node's Peer Address
        peer_address: NetworkAddress,
        /// Correspondent Record Address
        record_addr: NetworkAddress,
    },
    /// Response to [`GetMerkleCandidateQuote`]
    ///
    /// Node's signed commitment containing:
    /// - pub_key (can derive PeerId)
    /// - quoting_metrics (current node state)
    /// - reward_address (where to send payment)
    /// - merkle_payment_timestamp (binds signature to time)
    /// - signature (proves pub_key owns reward_address)
    ///
    /// [`GetMerkleCandidateQuote`]: crate::messages::Query::GetMerkleCandidateQuote
    GetMerkleCandidateQuote(Result<ant_evm::merkle_payments::MerklePaymentCandidateNode>),
    // ===== DevGetClosestPeersFromNetwork =====
    //
    /// Response to [`DevGetClosestPeersFromNetwork`]
    /// Returns closest peers from a network query (not just local routing table).
    /// Only available when the `developer` feature is enabled.
    ///
    /// [`DevGetClosestPeersFromNetwork`]: crate::messages::Query::DevGetClosestPeersFromNetwork
    #[cfg(feature = "developer")]
    DevGetClosestPeersFromNetwork {
        /// The target address that the original request is about.
        target: NetworkAddress,
        /// The node that performed the query (the queried node's peer address)
        queried_node: NetworkAddress,
        /// Closest peers from the network query, with their multiaddresses.
        peers: Vec<(NetworkAddress, Vec<Multiaddr>)>,
    },
}

// 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:?}"
                )
            }
            QueryResponse::GetVersion { peer, version } => {
                write!(f, "GetVersion peer {peer:?} has version of {version:?}")
            }
            QueryResponse::PutRecord {
                result,
                peer_address,
                record_addr,
            } => {
                write!(
                    f,
                    "PutRecord(Record {record_addr:?} uploaded to {peer_address:?} with result {result:?})",
                )
            }
            QueryResponse::GetMerkleCandidateQuote(result) => match result {
                Ok(candidate) => {
                    write!(
                        f,
                        "GetMerkleCandidateQuote(Ok(reward_address: {:?}, timestamp: {}))",
                        candidate.reward_address, candidate.merkle_payment_timestamp
                    )
                }
                Err(err) => {
                    write!(f, "GetMerkleCandidateQuote(Err({err:?}))")
                }
            },
            #[cfg(feature = "developer")]
            QueryResponse::DevGetClosestPeersFromNetwork {
                target,
                queried_node,
                peers,
            } => {
                let addresses: Vec<_> = peers.iter().map(|(addr, _)| addr.clone()).collect();
                write!(
                    f,
                    "DevGetClosestPeersFromNetwork target {target:?} from {queried_node:?} found {} close peers {addresses:?}",
                    peers.len()
                )
            }
        }
    }
}

/// 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<()>),
}