Use from_sat_unchecked for hardcoded ints
We have an `_unchecked` amount constructor that makes no assumptions about the argument. We would like to start enforcing MAX_MONEY but the diff to introduce this is massive. In an effort to make it smaller we can do all the hardcoded ints first. We did this already but a bunch more snuck in or were missed. In any amount constructor that passes in a hardcoded const as a decimal integer (i.e., not hex) use the `_unchecked` version. Done in preparation for enforcing MAX_MONEY.
This commit is contained in:
parent
8fdec67f7d
commit
00b71a670f
|
@ -148,7 +148,7 @@ fn sighash_p2wpkh() {
|
|||
let inp_idx = 0;
|
||||
//output value from the referenced vout:0 from the referenced tx:
|
||||
//bitcoin-cli getrawtransaction 752d675b9cc0bd14e0bd23969effee0005ad6d7e550dcc832f0216c7ffd4e15c 3
|
||||
let ref_out_value = Amount::from_sat(200000000);
|
||||
let ref_out_value = Amount::from_sat_unchecked(200000000);
|
||||
|
||||
println!("\nsighash_p2wpkh:");
|
||||
compute_sighash_p2wpkh(&raw_tx, inp_idx, ref_out_value);
|
||||
|
@ -178,7 +178,7 @@ fn sighash_p2wsh_multisig_2x2() {
|
|||
//For the witness transaction sighash computation, we need its referenced output's value from the original transaction:
|
||||
//bitcoin-cli getrawtransaction 2845399a8cd7a52733f9f9d0e0b8b6c5d1c88aea4cee09f8d8fa762912b49e1b 3
|
||||
//we need vout 0 value in sats:
|
||||
let ref_out_value = Amount::from_sat(968240);
|
||||
let ref_out_value = Amount::from_sat_unchecked(968240);
|
||||
|
||||
println!("\nsighash_p2wsh_multisig_2x2:");
|
||||
compute_sighash_p2wsh(&raw_tx, 0, ref_out_value);
|
||||
|
|
|
@ -142,7 +142,7 @@ fn mul_div() {
|
|||
|
||||
#[test]
|
||||
fn neg() {
|
||||
let amount = -SignedAmount::from_sat(2);
|
||||
let amount = -SignedAmount::from_sat_unchecked(2);
|
||||
assert_eq!(amount.to_sat(), -2);
|
||||
}
|
||||
|
||||
|
@ -231,7 +231,7 @@ fn amount_checked_div_by_weight_floor() {
|
|||
#[cfg(feature = "alloc")]
|
||||
#[test]
|
||||
fn amount_checked_div_by_fee_rate() {
|
||||
let amount = Amount::from_sat(1000);
|
||||
let amount = Amount::from_sat_unchecked(1000);
|
||||
let fee_rate = FeeRate::from_sat_per_kwu(2);
|
||||
|
||||
// Test floor division
|
||||
|
@ -244,7 +244,7 @@ fn amount_checked_div_by_fee_rate() {
|
|||
assert_eq!(weight, Weight::from_wu(500_000)); // Same result for exact division
|
||||
|
||||
// Test truncation behavior
|
||||
let amount = Amount::from_sat(1000);
|
||||
let amount = Amount::from_sat_unchecked(1000);
|
||||
let fee_rate = FeeRate::from_sat_per_kwu(3);
|
||||
let floor_weight = amount.checked_div_by_fee_rate_floor(fee_rate).unwrap();
|
||||
let ceil_weight = amount.checked_div_by_fee_rate_ceil(fee_rate).unwrap();
|
||||
|
|
|
@ -197,7 +197,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn fee_rate_div_by_weight() {
|
||||
let fee_rate = Amount::from_sat(329) / Weight::from_wu(381);
|
||||
let fee_rate = Amount::from_sat_unchecked(329) / Weight::from_wu(381);
|
||||
assert_eq!(fee_rate, FeeRate::from_sat_per_kwu(863));
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ mod tests {
|
|||
|
||||
let fee_rate = FeeRate::from_sat_per_vb(2).unwrap();
|
||||
let weight = Weight::from_vb(3).unwrap();
|
||||
assert_eq!(fee_rate.fee_wu(weight).unwrap(), Amount::from_sat(6));
|
||||
assert_eq!(fee_rate.fee_wu(weight).unwrap(), Amount::from_sat_unchecked(6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -217,7 +217,7 @@ mod tests {
|
|||
assert!(fee_overflow.is_none());
|
||||
|
||||
let fee_rate = FeeRate::from_sat_per_vb(2).unwrap();
|
||||
assert_eq!(fee_rate.fee_vb(3).unwrap(), Amount::from_sat(6));
|
||||
assert_eq!(fee_rate.fee_vb(3).unwrap(), Amount::from_sat_unchecked(6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -227,7 +227,7 @@ mod tests {
|
|||
.unwrap()
|
||||
.checked_mul_by_weight(weight)
|
||||
.expect("expected Amount");
|
||||
assert_eq!(Amount::from_sat(100), fee);
|
||||
assert_eq!(Amount::from_sat_unchecked(100), fee);
|
||||
|
||||
let fee = FeeRate::from_sat_per_kwu(10).checked_mul_by_weight(Weight::MAX);
|
||||
assert!(fee.is_none());
|
||||
|
@ -235,14 +235,14 @@ mod tests {
|
|||
let weight = Weight::from_vb(3).unwrap();
|
||||
let fee_rate = FeeRate::from_sat_per_vb(3).unwrap();
|
||||
let fee = fee_rate.checked_mul_by_weight(weight).unwrap();
|
||||
assert_eq!(Amount::from_sat(9), fee);
|
||||
assert_eq!(Amount::from_sat_unchecked(9), fee);
|
||||
|
||||
let weight = Weight::from_wu(381);
|
||||
let fee_rate = FeeRate::from_sat_per_kwu(864);
|
||||
let fee = fee_rate.checked_mul_by_weight(weight).unwrap();
|
||||
// 381 * 0.864 yields 329.18.
|
||||
// The result is then rounded up to 330.
|
||||
assert_eq!(fee, Amount::from_sat(330));
|
||||
assert_eq!(fee, Amount::from_sat_unchecked(330));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -250,7 +250,7 @@ mod tests {
|
|||
fn multiply() {
|
||||
let two = FeeRate::from_sat_per_vb(2).unwrap();
|
||||
let three = Weight::from_vb(3).unwrap();
|
||||
let six = Amount::from_sat(6);
|
||||
let six = Amount::from_sat_unchecked(6);
|
||||
|
||||
assert_eq!(two * three, six);
|
||||
}
|
||||
|
@ -258,13 +258,13 @@ mod tests {
|
|||
#[test]
|
||||
fn amount_div_by_fee_rate() {
|
||||
// Test exact division
|
||||
let amount = Amount::from_sat(1000);
|
||||
let amount = Amount::from_sat_unchecked(1000);
|
||||
let fee_rate = FeeRate::from_sat_per_kwu(2);
|
||||
let weight = amount / fee_rate;
|
||||
assert_eq!(weight, Weight::from_wu(500_000));
|
||||
|
||||
// Test truncation behavior
|
||||
let amount = Amount::from_sat(1000);
|
||||
let amount = Amount::from_sat_unchecked(1000);
|
||||
let fee_rate = FeeRate::from_sat_per_kwu(3);
|
||||
let weight = amount / fee_rate;
|
||||
// 1000 * 1000 = 1,000,000 msats
|
||||
|
|
Loading…
Reference in New Issue