Skip to content

Commit

Permalink
feat(node): check for double payment from client
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Jul 1, 2024
1 parent c813442 commit 8b0d0a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sn_node/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub enum Error {
paid: NanoTokens,
expected: NanoTokens,
},
#[error("A payment we received contains cash notes already confirmed to be spent")]
ReusedPayment,

// ---------- Initialize Errors
#[error("Failed to generate a reward key")]
Expand Down
21 changes: 20 additions & 1 deletion sn_node/src/put_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,29 @@ impl Node {

// unpack transfer
trace!("Unpacking incoming Transfers for record {pretty_key}");
let (received_fee, cash_notes, royalties_cash_notes_r) = self
let (received_fee, mut cash_notes, royalties_cash_notes_r) = self
.cash_notes_from_transfers(payment.transfers, &wallet, pretty_key.clone())
.await?;

// check for cash notes that we have already spent
// this can happen in cases where the client retries a failed PUT after we have already used the cash note
cash_notes.retain(|cash_note| {
let spend_addr = SpendAddress::from_unique_pubkey(&cash_note.unique_pubkey());
let already_spent = matches!(wallet.get_confirmed_spend(spend_addr), Ok(Some(_spend)));
if already_spent {
warn!(
"Double spend {} detected for record payment {pretty_key}",
cash_note.unique_pubkey()
);
}
// retain the `CashNote` if it's not already spent
!already_spent
});
if cash_notes.is_empty() {
info!("All incoming cash notes were already spent, no need to further process");
return Err(Error::ReusedPayment);
}

trace!("Received payment of {received_fee:?} for {pretty_key}");

// Notify `record_store` that the node received a payment.
Expand Down

0 comments on commit 8b0d0a4

Please sign in to comment.