Prevent wallet attempting to re-pay a cancelled invoice transaction (#707)

This commit is contained in:
Yeastplume 2024-04-24 09:03:58 +01:00 committed by GitHub
parent 75363a9a25
commit 297c828532
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 0 deletions

View file

@ -246,6 +246,48 @@ fn invoice_tx_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
})?; })?;
assert_eq!(slate.state, SlateState::Invoice3); assert_eq!(slate.state, SlateState::Invoice3);
// test that payee can only cancel once
let _ = test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, 3, false);
bh += 3;
wallet::controller::owner_single_use(Some(wallet2.clone()), mask2, None, |api, m| {
// Wallet 2 inititates an invoice transaction, requesting payment
let args = IssueInvoiceTxArgs {
amount: reward * 2,
..Default::default()
};
slate = api.issue_invoice_tx(m, args)?;
Ok(())
})?;
assert_eq!(slate.state, SlateState::Invoice1);
let orig_slate = slate.clone();
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
// Wallet 1 receives the invoice transaction
let args = InitTxArgs {
src_acct_name: None,
amount: slate.amount,
minimum_confirmations: 2,
max_outputs: 500,
num_change_outputs: 1,
selection_strategy_is_use_all: true,
..Default::default()
};
slate = api.process_invoice_tx(m, &slate, args.clone())?;
api.tx_lock_outputs(m, &slate)?;
// Wallet 1 cancels the invoice transaction
api.cancel_tx(m, None, Some(slate.id))?;
// Wallet 1 attempts to repay again
let res = api.process_invoice_tx(m, &orig_slate, args);
assert!(res.is_err());
Ok(())
})?;
assert_eq!(slate.state, SlateState::Invoice2);
// let logging finish // let logging finish
stopper.store(false, Ordering::Relaxed); stopper.store(false, Ordering::Relaxed);
thread::sleep(Duration::from_millis(200)); thread::sleep(Duration::from_millis(200));

View file

@ -680,6 +680,9 @@ where
if t.tx_type == TxLogEntryType::TxSent { if t.tx_type == TxLogEntryType::TxSent {
return Err(Error::TransactionAlreadyReceived(ret_slate.id.to_string())); return Err(Error::TransactionAlreadyReceived(ret_slate.id.to_string()));
} }
if t.tx_type == TxLogEntryType::TxSentCancelled {
return Err(Error::TransactionWasCancelled(ret_slate.id.to_string()));
}
} }
let height = w.w2n_client().get_chain_tip()?.0; let height = w.w2n_client().get_chain_tip()?.0;

View file

@ -153,6 +153,10 @@ pub enum Error {
#[error("Transaction {0} has already been received")] #[error("Transaction {0} has already been received")]
TransactionAlreadyReceived(String), TransactionAlreadyReceived(String),
/// Transaction has been cancelled
#[error("Transaction {0} has been cancelled")]
TransactionWasCancelled(String),
/// Attempt to repost a transaction that's not completed and stored /// Attempt to repost a transaction that's not completed and stored
#[error("Transaction building not completed: {0}")] #[error("Transaction building not completed: {0}")]
TransactionBuildingNotCompleted(u32), TransactionBuildingNotCompleted(u32),