Skip to content

Commit

Permalink
generator: fix incorrect change output value info triggering a sanity…
Browse files Browse the repository at this point in the history
…-check assert!
  • Loading branch information
aspect committed Sep 8, 2024
1 parent 6477def commit f79b9c2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion consensus/client/src/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
1 change: 0 additions & 1 deletion wallet/core/src/tx/generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
Expand Down
55 changes: 52 additions & 3 deletions wallet/core/src/tx/generator/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -323,6 +323,21 @@ impl Harness {
self.clone()
}

pub fn accumulate(self: &Rc<Self>, count: usize) -> Rc<Self> {
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<Self>) -> Rc<Self> {
while let Some(pt) = self.generator.generate_transaction().unwrap() {
pt.accumulate(&mut self.accumulator.borrow_mut()).validate();
Expand All @@ -332,7 +347,16 @@ impl Harness {

pub fn finalize(self: Rc<Self>) {
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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(())
}

0 comments on commit f79b9c2

Please sign in to comment.