Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Contracts] Add self spend transaction state #697

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controller/tests/contract_self_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion controller/tests/contract_self_spend_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions libwallet/src/api_impl/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ pub struct RetrieveTxQueryArgs {
pub include_received_only: Option<bool>,
/// whether to only consider coinbase transactions
pub include_coinbase_only: Option<bool>,
/// whether to only consider self spend transactions
pub include_self_spend_only: Option<bool>,
/// whether to only consider reverted transactions
pub include_reverted_only: Option<bool>,
/// lower bound on the total amount (amount_credited - amount_debited), inclusive
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions libwallet/src/contract/actions/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
11 changes: 8 additions & 3 deletions libwallet/src/contract/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ pub fn create_tx_log_entry(
parent_key_id: Identifier,
log_id: u32,
) -> Result<TxLogEntry, Error> {
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
Expand Down Expand Up @@ -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)?;
Expand Down
16 changes: 15 additions & 1 deletion libwallet/src/internal/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
};
Expand Down
6 changes: 6 additions & 0 deletions libwallet/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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"),
}
}
}
Expand Down