Skip to content

Commit

Permalink
Merge pull request #2706 from maqi/quoting_fixes
Browse files Browse the repository at this point in the history
fix(client): quoting handle paid entry; pick among closest
  • Loading branch information
maqi authored Feb 5, 2025
2 parents ceedec1 + bb7fcbe commit 0bfaf47
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
14 changes: 13 additions & 1 deletion autonomi/src/client/high_level/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,24 @@ impl Client {
let payment_option_clone = payment_option.clone();

async move {
let target_addr = ScratchpadAddress::new(sp_secret_key.public_key().into());
info!(
"Updating Scratchpad at {target_addr:?} with content of {} bytes",
content.len()
);
match client
.scratchpad_update(&sp_secret_key.clone().into(), content_type, &content)
.await
{
Ok(()) => Ok(None),
Ok(()) => {
info!(
"Updated Scratchpad at {target_addr:?} with content of {} bytes",
content.len()
);
Ok(None)
}
Err(ScratchpadError::CannotUpdateNewScratchpad) => {
info!("Creating Scratchpad at {target_addr:?}");
let (price, addr) = client
.scratchpad_create(
&sp_secret_key.into(),
Expand Down
18 changes: 15 additions & 3 deletions autonomi/src/client/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,31 @@ impl Client {
let mut quotes_to_pay_per_addr = HashMap::new();

for result in raw_quotes_per_addr {
let (content_addr, raw_quotes) = result?;
let (content_addr, mut raw_quotes) = result?;
debug!(
"fetched market price for content_addr: {content_addr}, with {} quotes.",
raw_quotes.len()
);

// FIXME: find better way to deal with paid content addrs and feedback to the user
// assume that content addr is already paid for and uploaded
if raw_quotes.len() <= CLOSE_GROUP_SIZE / 2 {
if raw_quotes.is_empty() {
debug!("content_addr: {content_addr} is already paid for. No need to fetch market price.");
continue;
}

let target_addr = NetworkAddress::from_chunk_address(ChunkAddress::new(content_addr));
// With the expand of quoting candidates,
// we shall get CLOSE_GROUP_SIZE closest into further procedure.
raw_quotes.sort_by_key(|(peer_id, _)| {
NetworkAddress::from_peer(*peer_id).distance(&target_addr)
});

// ask smart contract for the market price
let quoting_metrics: Vec<QuotingMetrics> = raw_quotes
.clone()
.iter()
.take(CLOSE_GROUP_SIZE)
.map(|(_, q)| q.quoting_metrics.clone())
.collect();

Expand All @@ -131,7 +139,7 @@ impl Client {
.collect();

// sort by price
prices.sort_by(|(_, _, price_a), (_, _, price_b)| price_a.cmp(price_b));
prices.sort_by_key(|(_, _, price)| *price);

// we need at least 5 valid quotes to pay for the data
const MINIMUM_QUOTES_TO_PAY: usize = 5;
Expand Down Expand Up @@ -199,6 +207,10 @@ async fn fetch_store_quote_with_retries(
loop {
match fetch_store_quote(network, content_addr, data_type, data_size).await {
Ok(quote) => {
if quote.is_empty() {
// Empty quotes indicates the record already exists.
break Ok((content_addr, quote));
}
if quote.len() < CLOSE_GROUP_SIZE {
retries += 1;
error!("Error while fetching store quote: not enough quotes ({}/{CLOSE_GROUP_SIZE}), retry #{retries}, quotes {quote:?}",
Expand Down
3 changes: 3 additions & 0 deletions autonomi/tests/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ async fn vault_expand() -> Result<()> {
.await?;
assert_eq!(cost, AttoTokens::from_u64(6));

// Short break is required to avoid client choked by the last query round
tokio::time::sleep(std::time::Duration::from_secs(10)).await;

let (fetched_content, fetched_content_type) = client.fetch_and_decrypt_vault(&main_key).await?;
assert_eq!(fetched_content_type, content_type);
assert_eq!(fetched_content, update_content_10_mb);
Expand Down

0 comments on commit 0bfaf47

Please sign in to comment.