Skip to content

Commit

Permalink
feat(client): verify register sync, and repay if not stored on all nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Oct 17, 2023
1 parent a43bdb8 commit 7b77c7b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 6 additions & 2 deletions sn_client/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl Client {

let maybe_record = self
.network
.get_record_from_network(key, None, GetQuorum::Majority, false, Default::default())
.get_record_from_network(key, None, GetQuorum::All, false, Default::default())
.await;
let record = match maybe_record {
Ok(r) => r,
Expand Down Expand Up @@ -297,14 +297,18 @@ impl Client {
info!("Instantiating a new Register replica with address {address:?}");
let (reg, mut total_cost) =
ClientRegister::create_online(self.clone(), address, wallet_client, false).await?;

debug!("{address:?} Created in theorryyyyy");
let reg_address = reg.address();
if verify_store {
debug!("WE SHOULD VERRRRIFYING");
let mut stored = self.verify_register_stored(*reg_address).await.is_ok();

while !stored {
info!("Register not completely stored on the network yet. Retrying...");
// this verify store call here ensures we get the record from Quorum::all
let (reg, top_up_cost) =
ClientRegister::create_online(self.clone(), address, wallet_client, false)
ClientRegister::create_online(self.clone(), address, wallet_client, true)
.await?;
let reg_address = reg.address();

Expand Down
26 changes: 23 additions & 3 deletions sn_client/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,22 @@ impl ClientRegister {
let addr = *self.address();
debug!("Syncing Register at {addr:?}!");
let mut cost = NanoTokens::zero();
let remote_replica = match Self::get_register_from_network(&self.client, addr).await {
let reg_result = if verify_store {
debug!("VERIFYING REGISTER STORED {:?}", self.address());
let res = self.client.verify_register_stored(*self.address()).await;
// we need to keep the error here if verifying so we can retry and pay for storage
// once more below
match res {
Ok(r) => Ok(r.register()?),
Err(error) => Err(error),
}
} else {
Self::get_register_from_network(&self.client, addr).await
};
let remote_replica = match reg_result {
Ok(r) => r,
// any error here will result in a repayment of the register
// TODO: be smart about this and only pay for storage if we need to
Err(err) => {
debug!("Failed to fetch register: {err:?}");
debug!("Creating Register as it doesn't exist at {addr:?}!");
Expand All @@ -190,23 +204,30 @@ impl ClientRegister {
let net_addr = sn_protocol::NetworkAddress::RegisterAddress(addr);
// Let's make the storage payment
cost = wallet_client
.pay_for_storage(std::iter::once(net_addr.clone()), verify_store)
.pay_for_storage(std::iter::once(net_addr.clone()), true)
.await?;

println!("Successfully made payment of {cost} for a Register (At a cost per record of {cost:?}.)");
info!("Successfully made payment of {cost} for a Register (At a cost per record of {cost:?}.)");

if let Err(err) = wallet_client.store_local_wallet() {
warn!("Failed to store wallet with cached payment proofs: {err:?}");
println!("Failed to store wallet with cached payment proofs: {err:?}");
} else {
println!(
"Successfully stored wallet with cached payment proofs, and new balance {}.",
wallet_client.balance()
);
info!(
"Successfully stored wallet with cached payment proofs, and new balance {}.",
wallet_client.balance()
);
}

// Get payment proofs needed to publish the Register
let payment = wallet_client.get_payment_transfers(&net_addr)?;

debug!("payments found: {payment:?}");
self.publish_register(cmd, payment, verify_store).await?;
self.register.clone()
}
Expand Down Expand Up @@ -312,7 +333,6 @@ impl ClientRegister {
if existing_reg.owner() != register.owner() {
return Err(ProtocolError::RegisterAlreadyClaimed(existing_reg.owner()))?;
}
return Ok(()); // no op, since already created
}
SignedRegister::new(register, signature)
}
Expand Down

0 comments on commit 7b77c7b

Please sign in to comment.