Use _unchecked to construct amounts

We have a `_unchecked` constructor now for both `Amount` and
`SignedAmount`. In preparation for enforcing the `MAX_MONEY` invariant
use the `_unchecked` constructor throughout the codebase to construct
amounts from hard coded integer values.
This commit is contained in:
Tobin C. Harding 2024-12-31 13:38:52 +11:00
parent 09df951760
commit a7c44cebf9
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
11 changed files with 53 additions and 53 deletions

View File

@ -46,12 +46,12 @@ const BIP84_DERIVATION_PATH: &str = "m/84'/0'/0'";
const MASTER_FINGERPRINT: &str = "9680603f";
// The dummy UTXO amounts we are spending.
const DUMMY_UTXO_AMOUNT_INPUT_1: Amount = Amount::from_sat(20_000_000);
const DUMMY_UTXO_AMOUNT_INPUT_2: Amount = Amount::from_sat(10_000_000);
const DUMMY_UTXO_AMOUNT_INPUT_1: Amount = Amount::from_sat_unchecked(20_000_000);
const DUMMY_UTXO_AMOUNT_INPUT_2: Amount = Amount::from_sat_unchecked(10_000_000);
// The amounts we are sending to someone, and receiving back as change.
const SPEND_AMOUNT: Amount = Amount::from_sat(25_000_000);
const CHANGE_AMOUNT: Amount = Amount::from_sat(4_990_000); // 10_000 sat fee.
const SPEND_AMOUNT: Amount = Amount::from_sat_unchecked(25_000_000);
const CHANGE_AMOUNT: Amount = Amount::from_sat_unchecked(4_990_000); // 10_000 sat fee.
// Derive the external address xpriv.
fn get_external_address_xpriv<C: Signing>(

View File

@ -12,9 +12,9 @@ use bitcoin::{
Txid, WPubkeyHash, Witness,
};
const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat(20_000_000);
const SPEND_AMOUNT: Amount = Amount::from_sat(5_000_000);
const CHANGE_AMOUNT: Amount = Amount::from_sat(14_999_000); // 1000 sat fee.
const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat_unchecked(20_000_000);
const SPEND_AMOUNT: Amount = Amount::from_sat_unchecked(5_000_000);
const CHANGE_AMOUNT: Amount = Amount::from_sat_unchecked(14_999_000); // 1000 sat fee.
fn main() {
let secp = Secp256k1::new();

View File

@ -13,9 +13,9 @@ use bitcoin::{
Txid, Witness,
};
const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat(20_000_000);
const SPEND_AMOUNT: Amount = Amount::from_sat(5_000_000);
const CHANGE_AMOUNT: Amount = Amount::from_sat(14_999_000); // 1000 sat fee.
const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat_unchecked(20_000_000);
const SPEND_AMOUNT: Amount = Amount::from_sat_unchecked(5_000_000);
const CHANGE_AMOUNT: Amount = Amount::from_sat_unchecked(14_999_000); // 1000 sat fee.
fn main() {
let secp = Secp256k1::new();

View File

@ -45,12 +45,12 @@ const BIP86_DERIVATION_PATH: &str = "m/86'/0'/0'";
const MASTER_FINGERPRINT: &str = "9680603f";
// The dummy UTXO amounts we are spending.
const DUMMY_UTXO_AMOUNT_INPUT_1: Amount = Amount::from_sat(20_000_000);
const DUMMY_UTXO_AMOUNT_INPUT_2: Amount = Amount::from_sat(10_000_000);
const DUMMY_UTXO_AMOUNT_INPUT_1: Amount = Amount::from_sat_unchecked(20_000_000);
const DUMMY_UTXO_AMOUNT_INPUT_2: Amount = Amount::from_sat_unchecked(10_000_000);
// The amounts we are sending to someone, and receiving back as change.
const SPEND_AMOUNT: Amount = Amount::from_sat(25_000_000);
const CHANGE_AMOUNT: Amount = Amount::from_sat(4_990_000); // 10_000 sat fee.
const SPEND_AMOUNT: Amount = Amount::from_sat_unchecked(25_000_000);
const CHANGE_AMOUNT: Amount = Amount::from_sat_unchecked(4_990_000); // 10_000 sat fee.
// Derive the external address xpriv.
fn get_external_address_xpriv<C: Signing>(

View File

@ -40,7 +40,7 @@ const UTXO_SCRIPT_PUBKEY: &str =
"5120be27fa8b1f5278faf82cab8da23e8761f8f9bd5d5ebebbb37e0e12a70d92dd16";
const UTXO_PUBKEY: &str = "a6ac32163539c16b6b5dbbca01b725b8e8acaa5f821ba42c80e7940062140d19";
const UTXO_MASTER_FINGERPRINT: &str = "e61b318f";
const ABSOLUTE_FEES_IN_SATS: Amount = Amount::from_sat(1_000);
const ABSOLUTE_FEES_IN_SATS: Amount = Amount::from_sat_unchecked(1_000);
// UTXO_1 will be used for spending example 1
const UTXO_1: P2trUtxo = P2trUtxo {

View File

@ -112,7 +112,7 @@ fn bitcoin_genesis_tx(params: &Params) -> Transaction {
witness: Witness::default(),
});
ret.output.push(TxOut { value: Amount::from_sat(50 * 100_000_000), script_pubkey: out_script });
ret.output.push(TxOut { value: Amount::from_sat_unchecked(50 * 100_000_000), script_pubkey: out_script });
// end
ret

View File

@ -676,7 +676,7 @@ fn bitcoinconsensus() {
let spent_bytes = hex!("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d");
let spent = Script::from_bytes(&spent_bytes);
let spending = hex!("010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000");
spent.verify(0, crate::Amount::from_sat(18393430), &spending).unwrap();
spent.verify(0, crate::Amount::from_sat_unchecked(18393430), &spending).unwrap();
}
#[test]
@ -685,10 +685,10 @@ fn default_dust_value() {
// well-known scriptPubKey types.
let script_p2wpkh = Builder::new().push_int_unchecked(0).push_slice([42; 20]).into_script();
assert!(script_p2wpkh.is_p2wpkh());
assert_eq!(script_p2wpkh.minimal_non_dust(), crate::Amount::from_sat(294));
assert_eq!(script_p2wpkh.minimal_non_dust(), crate::Amount::from_sat_unchecked(294));
assert_eq!(
script_p2wpkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
crate::Amount::from_sat(588)
crate::Amount::from_sat_unchecked(588)
);
let script_p2pkh = Builder::new()
@ -699,10 +699,10 @@ fn default_dust_value() {
.push_opcode(OP_CHECKSIG)
.into_script();
assert!(script_p2pkh.is_p2pkh());
assert_eq!(script_p2pkh.minimal_non_dust(), crate::Amount::from_sat(546));
assert_eq!(script_p2pkh.minimal_non_dust(), crate::Amount::from_sat_unchecked(546));
assert_eq!(
script_p2pkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
crate::Amount::from_sat(1092)
crate::Amount::from_sat_unchecked(1092)
);
}

View File

@ -2064,7 +2064,7 @@ mod tests {
).unwrap();
let spk = ScriptBuf::from_hex("00141d0f172a0ecb48aee1be1f2687d2963ae33f71a1").unwrap();
let value = Amount::from_sat(600_000_000);
let value = Amount::from_sat_unchecked(600_000_000);
let mut cache = SighashCache::new(&tx);
assert_eq!(
@ -2105,7 +2105,7 @@ mod tests {
let redeem_script =
ScriptBuf::from_hex("001479091972186c449eb1ded22b78e40d009bdf0089").unwrap();
let value = Amount::from_sat(1_000_000_000);
let value = Amount::from_sat_unchecked(1_000_000_000);
let mut cache = SighashCache::new(&tx);
assert_eq!(
@ -2155,7 +2155,7 @@ mod tests {
)
.unwrap();
let value = Amount::from_sat(987_654_321);
let value = Amount::from_sat_unchecked(987_654_321);
(tx, witness_script, value)
}

View File

@ -1413,14 +1413,14 @@ mod tests {
}],
output: vec![
TxOut {
value: Amount::from_sat(99_999_699),
value: Amount::from_sat_unchecked(99_999_699),
script_pubkey: ScriptBuf::from_hex(
"76a914d0c59903c5bac2868760e90fd521a4665aa7652088ac",
)
.unwrap(),
},
TxOut {
value: Amount::from_sat(100_000_000),
value: Amount::from_sat_unchecked(100_000_000),
script_pubkey: ScriptBuf::from_hex(
"a9143545e6e33b832c47050f24d3eeb93c9c03948bc787",
)
@ -1486,7 +1486,7 @@ mod tests {
)]),
}],
output: vec![TxOut {
value: Amount::from_sat(190_303_501_938),
value: Amount::from_sat_unchecked(190_303_501_938),
script_pubkey: ScriptBuf::from_hex(
"a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587",
)
@ -1537,7 +1537,7 @@ mod tests {
Input {
non_witness_utxo: Some(tx),
witness_utxo: Some(TxOut {
value: Amount::from_sat(190_303_501_938),
value: Amount::from_sat_unchecked(190_303_501_938),
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587").unwrap(),
}),
sighash_type: Some("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY".parse::<PsbtSighashType>().unwrap()),
@ -1662,11 +1662,11 @@ mod tests {
],
output: vec![
TxOut {
value: Amount::from_sat(99_999_699),
value: Amount::from_sat_unchecked(99_999_699),
script_pubkey: ScriptBuf::from_hex("76a914d0c59903c5bac2868760e90fd521a4665aa7652088ac").unwrap(),
},
TxOut {
value: Amount::from_sat(100_000_000),
value: Amount::from_sat_unchecked(100_000_000),
script_pubkey: ScriptBuf::from_hex("a9143545e6e33b832c47050f24d3eeb93c9c03948bc787").unwrap(),
},
],
@ -1709,11 +1709,11 @@ mod tests {
],
output: vec![
TxOut {
value: Amount::from_sat(200_000_000),
value: Amount::from_sat_unchecked(200_000_000),
script_pubkey: ScriptBuf::from_hex("76a91485cff1097fd9e008bb34af709c62197b38978a4888ac").unwrap(),
},
TxOut {
value: Amount::from_sat(190_303_501_938),
value: Amount::from_sat_unchecked(190_303_501_938),
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587").unwrap(),
},
],
@ -1995,11 +1995,11 @@ mod tests {
],
output: vec![
TxOut {
value: Amount::from_sat(99_999_699),
value: Amount::from_sat_unchecked(99_999_699),
script_pubkey: ScriptBuf::from_hex("76a914d0c59903c5bac2868760e90fd521a4665aa7652088ac").unwrap(),
},
TxOut {
value: Amount::from_sat(100_000_000),
value: Amount::from_sat_unchecked(100_000_000),
script_pubkey: ScriptBuf::from_hex("a9143545e6e33b832c47050f24d3eeb93c9c03948bc787").unwrap(),
},
],
@ -2042,11 +2042,11 @@ mod tests {
],
output: vec![
TxOut {
value: Amount::from_sat(200_000_000),
value: Amount::from_sat_unchecked(200_000_000),
script_pubkey: ScriptBuf::from_hex("76a91485cff1097fd9e008bb34af709c62197b38978a4888ac").unwrap(),
},
TxOut {
value: Amount::from_sat(190_303_501_938),
value: Amount::from_sat_unchecked(190_303_501_938),
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587").unwrap(),
},
],
@ -2155,9 +2155,9 @@ mod tests {
#[test]
fn fee() {
let output_0_val = Amount::from_sat(99_999_699);
let output_1_val = Amount::from_sat(100_000_000);
let prev_output_val = Amount::from_sat(200_000_000);
let output_0_val = Amount::from_sat_unchecked(99_999_699);
let output_1_val = Amount::from_sat_unchecked(100_000_000);
let prev_output_val = Amount::from_sat_unchecked(200_000_000);
let t = Psbt {
unsigned_tx: Transaction {
@ -2218,7 +2218,7 @@ mod tests {
script_pubkey: ScriptBuf::new()
},
TxOut {
value: Amount::from_sat(190_303_501_938),
value: Amount::from_sat_unchecked(190_303_501_938),
script_pubkey: ScriptBuf::new()
},
],
@ -2280,7 +2280,7 @@ mod tests {
// First input we can spend. See comment above on key_map for why we use defaults here.
let txout_wpkh = TxOut {
value: Amount::from_sat(10),
value: Amount::from_sat_unchecked(10),
script_pubkey: ScriptBuf::new_p2wpkh(pk.wpubkey_hash().unwrap()),
};
psbt.inputs[0].witness_utxo = Some(txout_wpkh);
@ -2292,7 +2292,7 @@ mod tests {
// Second input is unspendable by us e.g., from another wallet that supports future upgrades.
let unknown_prog = WitnessProgram::new(WitnessVersion::V4, &[0xaa; 34]).unwrap();
let txout_unknown_future = TxOut {
value: Amount::from_sat(10),
value: Amount::from_sat_unchecked(10),
script_pubkey: ScriptBuf::new_witness_program(&unknown_prog),
};
psbt.inputs[1].witness_utxo = Some(txout_unknown_future);

View File

@ -238,7 +238,7 @@ fn serde_regression_psbt() {
.unwrap()]),
}],
output: vec![TxOut {
value: Amount::from_sat(190_303_501_938),
value: Amount::from_sat_unchecked(190_303_501_938),
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587")
.unwrap(),
}],
@ -285,7 +285,7 @@ fn serde_regression_psbt() {
inputs: vec![Input {
non_witness_utxo: Some(tx),
witness_utxo: Some(TxOut {
value: Amount::from_sat(190_303_501_938),
value: Amount::from_sat_unchecked(190_303_501_938),
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587").unwrap(),
}),
sighash_type: Some(PsbtSighashType::from("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY".parse::<EcdsaSighashType>().unwrap())),

View File

@ -759,8 +759,8 @@ fn serde_as_btc() {
}
let orig = T {
amt: Amount::from_sat(20_000_000__000_000_01),
samt: SignedAmount::from_sat(-20_000_000__000_000_01),
amt: Amount::from_sat_unchecked(20_000_000__000_000_01),
samt: SignedAmount::from_sat_unchecked(-20_000_000__000_000_01),
};
let json = "{\"amt\": 20000000.00000001, \
@ -795,7 +795,7 @@ fn serde_as_str() {
}
serde_test::assert_tokens(
&T { amt: Amount::from_sat(123_456_789), samt: SignedAmount::from_sat(-123_456_789) },
&T { amt: Amount::from_sat_unchecked(123_456_789), samt: SignedAmount::from_sat_unchecked(-123_456_789) },
&[
serde_test::Token::Struct { name: "T", len: 2 },
serde_test::Token::String("amt"),
@ -823,8 +823,8 @@ fn serde_as_btc_opt() {
}
let with = T {
amt: Some(Amount::from_sat(2_500_000_00)),
samt: Some(SignedAmount::from_sat(-2_500_000_00)),
amt: Some(Amount::from_sat_unchecked(2_500_000_00)),
samt: Some(SignedAmount::from_sat_unchecked(-2_500_000_00)),
};
let without = T { amt: None, samt: None };
@ -865,8 +865,8 @@ fn serde_as_sat_opt() {
}
let with = T {
amt: Some(Amount::from_sat(2_500_000_00)),
samt: Some(SignedAmount::from_sat(-2_500_000_00)),
amt: Some(Amount::from_sat_unchecked(2_500_000_00)),
samt: Some(SignedAmount::from_sat_unchecked(-2_500_000_00)),
};
let without = T { amt: None, samt: None };
@ -907,8 +907,8 @@ fn serde_as_str_opt() {
}
let with = T {
amt: Some(Amount::from_sat(123_456_789)),
samt: Some(SignedAmount::from_sat(-123_456_789)),
amt: Some(Amount::from_sat_unchecked(123_456_789)),
samt: Some(SignedAmount::from_sat_unchecked(-123_456_789)),
};
let without = T { amt: None, samt: None };