share proof amount and drop serde defaults

This commit is contained in:
wiesche
2026-09-15 21:59:15 +02:00
parent a1d683294b
commit 0683a3a307
7 changed files with 109 additions and 121 deletions
+3 -30
View File
@@ -594,11 +594,7 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {
}
None => "None".to_owned(),
};
let fee = match tx.fee {
Some(f) => f.fee(), // apply fee mask past HF4
None => 0,
};
let amount = core::amount_to_hr_string(proof_amount(tx, pp.proof_type.is_some(), fee), true);
let amount = core::amount_to_hr_string(tx.payment_proof_amount(), true);
let sender_signature = match pp.sender_signature {
Some(s) => {
@@ -640,15 +636,6 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {
Ok(())
}
/// Contract tx logs store the net amount, so early proofs don't subtract the fee
fn proof_amount(tx: &TxLogEntry, early_proof: bool, fee: u64) -> u64 {
if early_proof || tx.amount_credited >= tx.amount_debited {
tx.amount_credited.abs_diff(tx.amount_debited)
} else {
tx.amount_debited - tx.amount_credited - fee
}
}
/// Display a summary of a contract slate
pub fn contract_view(slate: &Slate, view: &ContractView, was_encrypted: bool) {
println!("\n____ Contract ____\n");
@@ -768,12 +755,11 @@ fn format_net_change(change: i64) -> String {
#[cfg(test)]
mod tests {
use super::{contract_view_table, format_net_change, proof_amount};
use super::{contract_view_table, format_net_change};
use crate::core::core::{FeeFields, Input, Inputs, Output, OutputFeatures, Transaction};
use crate::core::global;
use crate::keychain::Identifier;
use crate::libwallet::contract::types::ContractView;
use crate::libwallet::{Slate, TxLogEntry, TxLogEntryType};
use crate::libwallet::Slate;
use crate::util::secp::pedersen::{Commitment, RangeProof};
fn table_value(table: &prettytable::Table, label: &str) -> String {
@@ -797,19 +783,6 @@ mod tests {
assert_eq!(format_net_change(0), "+0.000000000");
}
#[test]
fn proof_amount_legacy_and_early() {
let mut tx = TxLogEntry::new(Identifier::zero(), TxLogEntryType::TxSent, 0);
// legacy send
tx.amount_debited = 1_500_000_000;
tx.amount_credited = 477_000_000;
assert_eq!(proof_amount(&tx, false, 23_000_000), 1_000_000_000);
// contract send
tx.amount_debited = 200_000_000;
tx.amount_credited = 0;
assert_eq!(proof_amount(&tx, true, 11_750_000), 200_000_000);
}
#[test]
fn contract_view_rows() {
global::set_local_chain_type(global::ChainTypes::AutomatedTesting);
+53
View File
@@ -712,6 +712,59 @@ fn contract_srs_tx_impl(test_dir: &'static str) -> Result<(), libwallet::Error>
assert_eq!(signed.state, SlateState::Standard3);
}
// proof export of a proofless contract whose amount is below the sender's fee
let small_args = |net_change| ContractSetupArgsAPI {
selection_args: common::contract_selection_args(),
net_change,
..Default::default()
};
let receiver_fee = my_fee_contribution(0, 1, 1, 2)?.fee();
let net = i64::try_from((receiver_fee + participant_fee) / 2).unwrap();
let mut small = Slate::blank(0, false);
wallet::controller::owner_single_use(
send_wallet.clone(),
send_mask,
PathBuf::from(test_dir),
|api, m| {
let args = ContractNewArgsAPI {
setup_args: small_args(Some(-net)),
..Default::default()
};
small = api.contract_new(m, &args)?;
Ok(())
},
)?;
wallet::controller::owner_single_use(
recv_wallet.clone(),
recv_mask,
PathBuf::from(test_dir),
|api, m| {
let mut args = small_args(Some(net));
args.selection_args.use_inputs = None;
small = api.contract_sign(m, &small, &args)?;
Ok(())
},
)?;
wallet::controller::owner_single_use(
send_wallet.clone(),
send_mask,
PathBuf::from(test_dir),
|api, m| {
small = api.contract_sign(m, &small, &small_args(None))?;
let tx_log = common::tx_log_for_slate(api, m, &small)?;
assert!(tx_log.fee.map(|f| f.fee()).unwrap_or(0) > tx_log.amount_debited);
assert!(matches!(
api.retrieve_payment_proof(m, false, None, Some(small.id)),
Err(libwallet::Error::PaymentProofRetrieval(_))
));
assert!(matches!(
api.retrieve_payment_proof_early(m, false, None, Some(small.id)),
Err(libwallet::Error::PaymentProofRetrieval(_))
));
Ok(())
},
)?;
// let logging finish
stopper.store(false, Ordering::Relaxed);
thread::sleep(Duration::from_millis(200));