Skip to content
Open
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
54 changes: 53 additions & 1 deletion lib/types/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,51 @@ mod tests {
}
Ok(())
}

// a withdrawal output must be funded for both its payout and its mainchain
// fee, since both leave the treasury
#[test]
fn withdrawal_value_includes_main_fee() {
use super::{
Content, FilledTransaction, GetValue, Output, Transaction,
};
use crate::types::Address;

let value = bitcoin::Amount::from_sat(1000);
let main_fee = bitcoin::Amount::from_sat(300);
let main_address = "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"
.parse::<bitcoin::Address<bitcoin::address::NetworkUnchecked>>()
.unwrap();
let withdrawal = Output {
address: Address::ALL_ZEROS,
content: Content::Withdrawal {
value,
main_fee,
main_address,
},
};
assert_eq!(withdrawal.get_value(), value + main_fee);

let value_output = |amount| Output {
address: Address::ALL_ZEROS,
content: Content::Value(amount),
};
let withdrawal_tx = |funding| FilledTransaction {
transaction: Transaction {
outputs: vec![withdrawal.clone()],
..Default::default()
},
spent_utxos: vec![value_output(funding)],
};

// inputs covering only the payout are insufficient
assert!(withdrawal_tx(value).get_fee().is_err());
// inputs covering payout plus mainchain fee fully fund it
assert_eq!(
withdrawal_tx(value + main_fee).get_fee().unwrap(),
bitcoin::Amount::ZERO
);
}
}

/// Reference to a tx input.
Expand Down Expand Up @@ -359,7 +404,14 @@ mod content {
fn get_value(&self) -> bitcoin::Amount {
match self {
Self::Value(value) => *value,
Self::Withdrawal { value, .. } => *value,
// a withdrawal removes both the payout and the mainchain fee
// from the sidechain, since the enforcer pays both out of the
// treasury
Self::Withdrawal {
value, main_fee, ..
} => {
value.checked_add(*main_fee).unwrap_or(bitcoin::Amount::MAX)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl Wallet {
.checked_add(main_fee)
.ok_or(AmountOverflowError)?,
)?;
let change = total - value - fee;
let change = total - value - fee - main_fee;

let inputs: Vec<_> = coins
.into_iter()
Expand Down