Skip to content

Commit

Permalink
fix(uploader): fix for mismatched ordering of instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin authored and joshuef committed Jun 8, 2024
1 parent 12b0e69 commit 6042273
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions sn_client/src/uploader/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,22 +824,42 @@ impl InnerUploader {
let mut cost_map = BTreeMap::new();
let mut current_batch = vec![];

let mut got_a_previous_force_payment = false;
while let Some(payment) = make_payment_receiver.recv().await {
let make_payments = if let Some((item, quote)) = payment {
let xorname = item.xorname();
trace!("Inserted {xorname:?} into cost_map");

current_batch.push((xorname, quote.clone()));
let _ = cost_map.insert(xorname, (quote.1, quote.2, quote.0.to_bytes()));
cost_map.len() >= batch_size
cost_map.len() >= batch_size || got_a_previous_force_payment
} else {
// using None to indicate as all paid.
let make_payments = !cost_map.is_empty();
trace!("Got a forced forced round of make payment. make_payments: {make_payments:?}");
trace!("Got a forced forced round of make payment.");
// Note: There can be a mismatch of ordering between the main loop and the make payment loop because
// the instructions are sent via a task(channel.send().await). And there is no guarantee for the
// order to come in the same order as they were sent.
//
// We cannot just disobey the instruction inside the child loop, as the mainloop would be expecting
// a result back for a particular instruction.
if !make_payments {
got_a_previous_force_payment = true;
warn!(
"We were told to force make payment, but cost_map is empty, so we can't do that just yet. Waiting for a task to insert a quote into cost_map"
)
}

make_payments
};

if make_payments {
// reset force_make_payment
if got_a_previous_force_payment {
info!("A task inserted a quote into cost_map, so we can now make a forced round of payment!");
got_a_previous_force_payment = false;
}

let result = match wallet_client.pay_for_records(&cost_map, verify_store).await
{
Ok((storage_cost, royalty_fees)) => {
Expand Down

0 comments on commit 6042273

Please sign in to comment.