From d6d6768136700166d9b69975a7c6644ef3986aa7 Mon Sep 17 00:00:00 2001 From: Yeastplume Date: Tue, 17 Oct 2023 14:38:28 +0100 Subject: [PATCH 1/2] Add self spend transaction state --- controller/tests/contract_self_spend.rs | 2 +- libwallet/src/contract/actions/sign.rs | 1 + libwallet/src/contract/utils.rs | 11 ++++++++--- libwallet/src/types.rs | 6 ++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/controller/tests/contract_self_spend.rs b/controller/tests/contract_self_spend.rs index 5b23e375e..bb90ce03a 100644 --- a/controller/tests/contract_self_spend.rs +++ b/controller/tests/contract_self_spend.rs @@ -85,7 +85,7 @@ fn contract_self_spend_tx_impl(test_dir: &'static str) -> Result<(), libwallet:: assert!(refreshed); assert_eq!(txs.len() as u64, bh + 1); // send wallet didn't mine 4 blocks and made 1 tx let tx_log = txs[txs.len() - 5].clone(); // TODO: why -5 and not -4? - assert_eq!(tx_log.tx_type, TxLogEntryType::TxSent); + assert_eq!(tx_log.tx_type, TxLogEntryType::TxSelfSpend); assert_eq!(tx_log.amount_credited, 0); assert_eq!(tx_log.amount_debited, 0); assert_eq!(tx_log.num_inputs, 1); diff --git a/libwallet/src/contract/actions/sign.rs b/libwallet/src/contract/actions/sign.rs index d909c418e..aef428a16 100644 --- a/libwallet/src/contract/actions/sign.rs +++ b/libwallet/src/contract/actions/sign.rs @@ -82,6 +82,7 @@ where contract::slate::verify_payment_proof(&sl, expected_net_change, &p.receiver_address)?; // noop for the receiver } + contract::slate::sign(w, keychain_mask, &mut sl, &mut context)?; contract::slate::transition_state(&mut sl)?; diff --git a/libwallet/src/contract/utils.rs b/libwallet/src/contract/utils.rs index 596179015..6e2dcbc7d 100644 --- a/libwallet/src/contract/utils.rs +++ b/libwallet/src/contract/utils.rs @@ -33,10 +33,14 @@ pub fn create_tx_log_entry( parent_key_id: Identifier, log_id: u32, ) -> Result { - let log_type = if net_change > 0 { - TxLogEntryType::TxReceived + let log_type = if slate.num_participants == 1 { + TxLogEntryType::TxSelfSpend } else { - TxLogEntryType::TxSent + if net_change > 0 { + TxLogEntryType::TxReceived + } else { + TxLogEntryType::TxSent + } }; let mut t = TxLogEntry::new(parent_key_id.clone(), log_type, log_id); // TODO: TxLogEntry has stored_tx field. Check what this needs to be set to and check other fields as well @@ -209,6 +213,7 @@ where .unwrap() } }; + // Update TxLogEntry if we have signed the contract (we have data about the kernel) if is_signed { update_tx_log_entry(w, keychain_mask, &slate, &context, &mut tx_log_entry)?; diff --git a/libwallet/src/types.rs b/libwallet/src/types.rs index 1cf1451d7..8b3962598 100644 --- a/libwallet/src/types.rs +++ b/libwallet/src/types.rs @@ -791,6 +791,10 @@ pub enum TxLogEntryType { TxReceivedCancelled, /// Sent transaction that was rolled back by user TxSentCancelled, + /// Self spend, as per contracts and mwmixnet + TxSelfSpend, + /// Self Spend Cancelled (has to happen before sent to chain, flag rather than delete) + TxSelfSpendCancelled, /// Received transaction that was reverted on-chain TxReverted, } @@ -804,6 +808,8 @@ impl fmt::Display for TxLogEntryType { TxLogEntryType::TxReceivedCancelled => write!(f, "Received Tx\n- Cancelled"), TxLogEntryType::TxSentCancelled => write!(f, "Sent Tx\n- Cancelled"), TxLogEntryType::TxReverted => write!(f, "Received Tx\n- Reverted"), + TxLogEntryType::TxSelfSpend => write!(f, "Self Spend"), + TxLogEntryType::TxSelfSpendCancelled => write!(f, "Self Spend\n- Cancelled"), } } } From 3b35ee77d5c3bb60587938b72a83c4ad797aa43d Mon Sep 17 00:00:00 2001 From: Yeastplume Date: Wed, 18 Oct 2023 13:13:31 +0100 Subject: [PATCH 2/2] subtle errors with output states and tx lookups - fixes --- controller/tests/contract_self_spend_custom.rs | 3 ++- libwallet/src/api_impl/types.rs | 3 +++ libwallet/src/internal/updater.rs | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/controller/tests/contract_self_spend_custom.rs b/controller/tests/contract_self_spend_custom.rs index e54a4d936..2bd51eed4 100644 --- a/controller/tests/contract_self_spend_custom.rs +++ b/controller/tests/contract_self_spend_custom.rs @@ -44,6 +44,7 @@ fn contract_self_spend_custom_tx_impl(test_dir: &'static str) -> Result<(), libw wallet::controller::owner_single_use(Some(send_wallet.clone()), send_mask, None, |api, m| { let (_, commits) = api.retrieve_outputs(m, true, false, None)?; + println!("OOOT: {:?}", commits[0].output); use_inputs = format!( "{},{}", commits[0].output.commit.as_ref().unwrap(), @@ -104,7 +105,7 @@ fn contract_self_spend_custom_tx_impl(test_dir: &'static str) -> Result<(), libw assert!(refreshed); assert_eq!(txs.len() as u64, bh + 1); // send wallet didn't mine 4 blocks and made 1 tx let tx_log = txs[txs.len() - 5].clone(); // TODO: why -5 and not -4? - assert_eq!(tx_log.tx_type, TxLogEntryType::TxSent); + assert_eq!(tx_log.tx_type, TxLogEntryType::TxSelfSpend); assert_eq!(tx_log.amount_credited, 0); assert_eq!(tx_log.amount_debited, 0); assert_eq!(tx_log.num_inputs, 3); diff --git a/libwallet/src/api_impl/types.rs b/libwallet/src/api_impl/types.rs index f50bfc48d..61b7b810d 100644 --- a/libwallet/src/api_impl/types.rs +++ b/libwallet/src/api_impl/types.rs @@ -200,6 +200,8 @@ pub struct RetrieveTxQueryArgs { pub include_received_only: Option, /// whether to only consider coinbase transactions pub include_coinbase_only: Option, + /// whether to only consider self spend transactions + pub include_self_spend_only: Option, /// whether to only consider reverted transactions pub include_reverted_only: Option, /// lower bound on the total amount (amount_credited - amount_debited), inclusive @@ -237,6 +239,7 @@ impl Default for RetrieveTxQueryArgs { include_sent_only: Some(false), include_received_only: Some(false), include_coinbase_only: Some(false), + include_self_spend_only: Some(false), include_reverted_only: Some(false), min_amount: None, max_amount: None, diff --git a/libwallet/src/internal/updater.rs b/libwallet/src/internal/updater.rs index a9958dec0..08f4271d3 100644 --- a/libwallet/src/internal/updater.rs +++ b/libwallet/src/internal/updater.rs @@ -146,6 +146,7 @@ where if v { tx_entry.tx_type == TxLogEntryType::TxSent || tx_entry.tx_type == TxLogEntryType::TxSentCancelled + || tx_entry.tx_type == TxLogEntryType::TxSelfSpend } else { true } @@ -158,6 +159,7 @@ where if v { tx_entry.tx_type == TxLogEntryType::TxReceived || tx_entry.tx_type == TxLogEntryType::TxReceivedCancelled + || tx_entry.tx_type == TxLogEntryType::TxSelfSpend } else { true } @@ -176,6 +178,17 @@ where true } }) + .filter(|tx_entry| { + if let Some(v) = query_args.include_self_spend_only { + if v { + tx_entry.tx_type == TxLogEntryType::TxSelfSpend + } else { + true + } + } else { + true + } + }) .filter(|tx_entry| { if let Some(v) = query_args.include_reverted_only { if v { @@ -365,7 +378,8 @@ where !tx_entry.confirmed && (tx_entry.tx_type == TxLogEntryType::TxReceived || tx_entry.tx_type == TxLogEntryType::TxSent - || tx_entry.tx_type == TxLogEntryType::TxReverted) + || tx_entry.tx_type == TxLogEntryType::TxReverted + || tx_entry.tx_type == TxLogEntryType::TxSelfSpend) } false => true, };