Skip to content

Commit

Permalink
feat: allowing client carryout storage check when GetStoreCost
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Nov 25, 2024
1 parent 57eb2c3 commit 8889ef3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
15 changes: 14 additions & 1 deletion sn_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,12 @@ impl Network {
return Err(NetworkError::NoStoreCostResponses);
}

let request = Request::Query(Query::GetStoreCost(record_address.clone()));
// Client shall decide whether to carry out storage verification or not.
let request = Request::Query(Query::GetStoreCost {
key: record_address.clone(),
nonce: None,
difficulty: 0,
});
let responses = self
.send_and_get_responses(&close_nodes, &request, true)
.await;
Expand All @@ -398,7 +403,11 @@ impl Network {
quote: Ok(quote),
payment_address,
peer_address,
storage_proofs,
}) => {
if !storage_proofs.is_empty() {
debug!("Storage proofing during GetStoreCost to be implemented.");
}
// Check the quote itself is valid.
if quote.cost
!= AttoTokens::from_u64(calculate_cost_for_records(
Expand All @@ -416,7 +425,11 @@ impl Network {
quote: Err(ProtocolError::RecordExists(_)),
payment_address,
peer_address,
storage_proofs,
}) => {
if !storage_proofs.is_empty() {
debug!("Storage proofing during GetStoreCost to be implemented.");
}
all_costs.push((peer_address, payment_address, PaymentQuote::zero()));
}
_ => {
Expand Down
28 changes: 24 additions & 4 deletions sn_node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,30 @@ impl Node {
payment_address: RewardsAddress,
) -> Response {
let resp: QueryResponse = match query {
Query::GetStoreCost(address) => {
debug!("Got GetStoreCost request for {address:?}");
let record_key = address.to_record_key();
Query::GetStoreCost {
key,
nonce,
difficulty,
} => {
debug!("Got GetStoreCost request for {key:?} with difficulty {difficulty}");
let record_key = key.to_record_key();
let self_id = network.peer_id();

let store_cost = network.get_local_storecost(record_key.clone()).await;

let storage_proofs = if let Some(nonce) = nonce {
Self::respond_x_closest_record_proof(
network,
key.clone(),
nonce,
difficulty,
false,
)
.await
} else {
vec![]
};

match store_cost {
Ok((cost, quoting_metrics, bad_nodes)) => {
if cost == AttoTokens::zero() {
Expand All @@ -530,26 +547,29 @@ impl Node {
)),
payment_address,
peer_address: NetworkAddress::from_peer(self_id),
storage_proofs,
}
} else {
QueryResponse::GetStoreCost {
quote: Self::create_quote_for_storecost(
network,
cost,
&address,
&key,
&quoting_metrics,
bad_nodes,
&payment_address,
),
payment_address,
peer_address: NetworkAddress::from_peer(self_id),
storage_proofs,
}
}
}
Err(_) => QueryResponse::GetStoreCost {
quote: Err(ProtocolError::GetStoreCostFailed),
payment_address,
peer_address: NetworkAddress::from_peer(self_id),
storage_proofs,
},
}
}
Expand Down
26 changes: 21 additions & 5 deletions sn_protocol/src/messages/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ use serde::{Deserialize, Serialize};
#[derive(Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize, Debug)]
pub enum Query {
/// Retrieve the cost of storing a record at the given address.
GetStoreCost(NetworkAddress),
/// The storage verification is optional to be undertaken
GetStoreCost {
/// The Address of the record to be stored.
key: NetworkAddress,
/// The random nonce that nodes use to produce the Proof (i.e., hash(record+nonce))
/// Set to None if no need to carry out storage check.
nonce: Option<Nonce>,
/// Defines the expected number of answers to the challenge.
/// Node shall try their best to fulfill the number, based on their capacity.
/// Set to 0 to indicate not carry out any verification.
difficulty: usize,
},
/// Retrieve a specific record from a specific peer.
///
/// This should eventually lead to a [`GetReplicatedRecord`] response.
Expand Down Expand Up @@ -60,10 +71,11 @@ impl Query {
/// Used to send a query to the close group of the address.
pub fn dst(&self) -> NetworkAddress {
match self {
Query::GetStoreCost(address) | Query::CheckNodeInProblem(address) => address.clone(),
Query::CheckNodeInProblem(address) => address.clone(),
// Shall not be called for this, as this is a `one-to-one` message,
// and the destination shall be decided by the requester already.
Query::GetReplicatedRecord { key, .. }
Query::GetStoreCost { key, .. }
| Query::GetReplicatedRecord { key, .. }
| Query::GetRegisterRecord { key, .. }
| Query::GetChunkExistenceProof { key, .. } => key.clone(),
}
Expand All @@ -73,8 +85,12 @@ impl Query {
impl std::fmt::Display for Query {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Query::GetStoreCost(address) => {
write!(f, "Query::GetStoreCost({address:?})")
Query::GetStoreCost {
key,
nonce,
difficulty,
} => {
write!(f, "Query::GetStoreCost({key:?} {nonce:?} {difficulty})")
}
Query::GetReplicatedRecord { key, requester } => {
write!(f, "Query::GetReplicatedRecord({requester:?} {key:?})")
Expand Down
6 changes: 5 additions & 1 deletion sn_protocol/src/messages/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum QueryResponse {
payment_address: RewardsAddress,
/// 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
Expand Down Expand Up @@ -67,10 +69,12 @@ impl Debug for QueryResponse {
quote,
payment_address,
peer_address,
storage_proofs,
} => {
write!(
f,
"GetStoreCost(quote: {quote:?}, from {peer_address:?} w/ payment_address: {payment_address:?})"
"GetStoreCost(quote: {quote:?}, from {peer_address:?} w/ payment_address: {payment_address:?}, and {} storage proofs)",
storage_proofs.len()
)
}
QueryResponse::CheckNodeInProblem {
Expand Down

0 comments on commit 8889ef3

Please sign in to comment.