From f79b9c2dff0578c8dc464d807ca3c594aec4d851 Mon Sep 17 00:00:00 2001 From: Anton Yemelyanov Date: Mon, 9 Sep 2024 02:06:59 +0300 Subject: [PATCH] generator: fix incorrect change output value info triggering a sanity-check assert! --- consensus/client/src/utxo.rs | 2 +- wallet/core/src/tx/generator/generator.rs | 1 - wallet/core/src/tx/generator/test.rs | 55 +++++++++++++++++++++-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/consensus/client/src/utxo.rs b/consensus/client/src/utxo.rs index 3f519d067..0a8b3fdb9 100644 --- a/consensus/client/src/utxo.rs +++ b/consensus/client/src/utxo.rs @@ -445,7 +445,7 @@ impl UtxoEntryReference { let outpoint = TransactionOutpoint::simulated(); let script_public_key = kaspa_txscript::pay_to_address_script(address); let block_daa_score = 0; - let is_coinbase = true; + let is_coinbase = false; let utxo_entry = UtxoEntry { address: Some(address.clone()), outpoint, amount, script_public_key, block_daa_score, is_coinbase }; diff --git a/wallet/core/src/tx/generator/generator.rs b/wallet/core/src/tx/generator/generator.rs index 52dafa0f7..398ba1b4d 100644 --- a/wallet/core/src/tx/generator/generator.rs +++ b/wallet/core/src/tx/generator/generator.rs @@ -705,7 +705,6 @@ impl Generator { Ok((DataKind::NoOp, data)) } else if stage.number_of_transactions > 0 { data.aggregate_mass += self.inner.standard_change_output_compute_mass; - data.change_output_value = Some(data.aggregate_input_value - data.transaction_fees); Ok((DataKind::Edge, data)) } else if data.aggregate_input_value < data.transaction_fees { Err(Error::InsufficientFunds { additional_needed: data.transaction_fees - data.aggregate_input_value, origin: "relay" }) diff --git a/wallet/core/src/tx/generator/test.rs b/wallet/core/src/tx/generator/test.rs index 7219ad5a7..990698b72 100644 --- a/wallet/core/src/tx/generator/test.rs +++ b/wallet/core/src/tx/generator/test.rs @@ -16,7 +16,7 @@ use workflow_log::style; use super::*; -const DISPLAY_LOGS: bool = true; +const DISPLAY_LOGS: bool = false; const DISPLAY_EXPECTED: bool = true; #[derive(Clone, Copy, Debug)] @@ -323,6 +323,21 @@ impl Harness { self.clone() } + pub fn accumulate(self: &Rc, count: usize) -> Rc { + for _n in 0..count { + if DISPLAY_LOGS { + println!( + "{}", + style(format!("accumulate gathering transaction: {} ({})", _n, self.accumulator.borrow().list.len())).magenta() + ); + } + let ptx = self.generator.generate_transaction().unwrap().unwrap(); + ptx.accumulate(&mut self.accumulator.borrow_mut()); + } + // println!("accumulated `{}` transactions", self.accumulator.borrow().list.len()); + self.clone() + } + pub fn validate(self: &Rc) -> Rc { while let Some(pt) = self.generator.generate_transaction().unwrap() { pt.accumulate(&mut self.accumulator.borrow_mut()).validate(); @@ -332,7 +347,16 @@ impl Harness { pub fn finalize(self: Rc) { let pt = self.generator.generate_transaction().unwrap(); - assert!(pt.is_none(), "expected no more transactions"); + if pt.is_some() { + let mut pending = self.generator.generate_transaction().unwrap(); + let mut count = 1; + while pending.is_some() { + count += 1; + pending = self.generator.generate_transaction().unwrap(); + } + + panic!("received extra `{}` unexpected transactions", count); + } let summary = self.generator.summary(); if DISPLAY_LOGS { println!("{:#?}", summary); @@ -644,7 +668,7 @@ fn test_generator_inputs_100_outputs_1_fees_exclude_insufficient_funds() -> Resu } #[test] -fn test_generator_inputs_903_outputs_2_fees_exclude() -> Result<()> { +fn test_generator_inputs_1k_outputs_2_fees_exclude() -> Result<()> { generator(test_network_id(), &[10.0; 1_000], &[], Fees::sender(Kaspa(5.0)), [(output_address, Kaspa(9_000.0))].as_slice()) .unwrap() .harness() @@ -676,3 +700,28 @@ fn test_generator_inputs_903_outputs_2_fees_exclude() -> Result<()> { Ok(()) } + +#[test] +fn test_generator_inputs_32k_outputs_2_fees_exclude() -> Result<()> { + let f = 130.0; + generator( + test_network_id(), + &[f; 32_747], + &[], + Fees::sender(Kaspa(10_000.0)), + [(output_address, Kaspa(f * 32_747.0 - 10_001.0))].as_slice(), + ) + .unwrap() + .harness() + .accumulate(379) + .finalize(); + Ok(()) +} + +#[test] +fn test_generator_inputs_250k_outputs_2_sweep() -> Result<()> { + let f = 130.0; + let generator = make_generator(test_network_id(), &[f; 250_000], &[], Fees::None, change_address, PaymentDestination::Change); + generator.unwrap().harness().accumulate(2875).finalize(); + Ok(()) +}