Skip to content

Commit e0be90d

Browse files
committed
Merge rust-bitcoin#4256: Followups to rust-bitcoin#4157: remove Amount::from_sat_unchecked
2958521 amount: remove from_sat_unchecked (Andrew Poelstra) d0d7a15 amount: move MIN/MAX constants and constructors inside the privacy boundary (Andrew Poelstra) 004d073 amount: return i64 from parse_signed_to_satoshi (Andrew Poelstra) 3370c14 amount: stop using from_sat_unchecked in tests (Andrew Poelstra) 05c8b04 tests: replace Amount::from_sat_unchecked with from_sat.unwrap (Andrew Poelstra) beaa2db amount: add from_sat_i32 and from_sat_u32 methods for small constants (Andrew Poelstra) 82d9d1a amount: rename `from_int_btc_const` unctions to hungarian ones (Andrew Poelstra) 2ec1c2a units: Make from_int_btc_const take a 16 bit integer (Tobin C. Harding) Pull request description: This does some followups to rust-bitcoin#4157. It pulls in rust-bitcoin#4254, which changes the signature of `Amount::from_int_btc_const` and also replaces a `checked_mul` which can't ever fail with a `*`. It then renames `from_int_btc_const` to `from_btc_u16`/`from_btc_i16` as appropriate, since these names reflect what the function does. It also adds an analogous method for making `Amount`s from sats rather than BTC. Then it systematically removes every instance of `from_sat_unchecked`. There wind up being 7 instances in tests that I just added unwraps() to, plus one instance where we had `let sat = Amount::from_sat_unchecked` which I also added an unwrap() to. In the real code, there was one instance (`Amount::to_signed`) where I needed to add an `expect`. Every other instance I was able to remove, by refactoring logic to use the error case as appropriate. ACKs for top commit: tcharding: ACK 2958521 Kixunil: ACK 2958521 Tree-SHA512: e2c2eb19acdd60da5524f6700e66c140b8e113bec7f2418a2505aeefdbf742eff216fb580891a3ea53255156786f5a6b365b8fb6553e60e42b18d4115d705dd2
2 parents 8aa5556 + 2958521 commit e0be90d

File tree

16 files changed

+182
-184
lines changed

16 files changed

+182
-184
lines changed

bitcoin/examples/ecdsa-psbt-simple.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ const BIP84_DERIVATION_PATH: &str = "m/84'/0'/0'";
4747
const MASTER_FINGERPRINT: &str = "9680603f";
4848

4949
// The dummy UTXO amounts we are spending.
50-
const DUMMY_UTXO_AMOUNT_INPUT_1: Amount = Amount::from_sat_unchecked(20_000_000);
51-
const DUMMY_UTXO_AMOUNT_INPUT_2: Amount = Amount::from_sat_unchecked(10_000_000);
50+
const DUMMY_UTXO_AMOUNT_INPUT_1: Amount = Amount::from_sat_u32(20_000_000);
51+
const DUMMY_UTXO_AMOUNT_INPUT_2: Amount = Amount::from_sat_u32(10_000_000);
5252

5353
// The amounts we are sending to someone, and receiving back as change.
54-
const SPEND_AMOUNT: Amount = Amount::from_sat_unchecked(25_000_000);
55-
const CHANGE_AMOUNT: Amount = Amount::from_sat_unchecked(4_990_000); // 10_000 sat fee.
54+
const SPEND_AMOUNT: Amount = Amount::from_sat_u32(25_000_000);
55+
const CHANGE_AMOUNT: Amount = Amount::from_sat_u32(4_990_000); // 10_000 sat fee.
5656

5757
// Derive the external address xpriv.
5858
fn get_external_address_xpriv<C: Signing>(

bitcoin/examples/sighash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn sighash_p2wpkh() {
148148
let inp_idx = 0;
149149
//output value from the referenced vout:0 from the referenced tx:
150150
//bitcoin-cli getrawtransaction 752d675b9cc0bd14e0bd23969effee0005ad6d7e550dcc832f0216c7ffd4e15c 3
151-
let ref_out_value = Amount::from_sat_unchecked(200000000);
151+
let ref_out_value = Amount::from_sat_u32(200000000);
152152

153153
println!("\nsighash_p2wpkh:");
154154
compute_sighash_p2wpkh(&raw_tx, inp_idx, ref_out_value);
@@ -178,7 +178,7 @@ fn sighash_p2wsh_multisig_2x2() {
178178
//For the witness transaction sighash computation, we need its referenced output's value from the original transaction:
179179
//bitcoin-cli getrawtransaction 2845399a8cd7a52733f9f9d0e0b8b6c5d1c88aea4cee09f8d8fa762912b49e1b 3
180180
//we need vout 0 value in sats:
181-
let ref_out_value = Amount::from_sat_unchecked(968240);
181+
let ref_out_value = Amount::from_sat_u32(968240);
182182

183183
println!("\nsighash_p2wsh_multisig_2x2:");
184184
compute_sighash_p2wsh(&raw_tx, 0, ref_out_value);

bitcoin/examples/sign-tx-segwit-v0.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use bitcoin::{
1313
Txid, Witness,
1414
};
1515

16-
const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat_unchecked(20_000_000);
17-
const SPEND_AMOUNT: Amount = Amount::from_sat_unchecked(5_000_000);
18-
const CHANGE_AMOUNT: Amount = Amount::from_sat_unchecked(14_999_000); // 1000 sat fee.
16+
const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat_u32(20_000_000);
17+
const SPEND_AMOUNT: Amount = Amount::from_sat_u32(5_000_000);
18+
const CHANGE_AMOUNT: Amount = Amount::from_sat_u32(14_999_000); // 1000 sat fee.
1919

2020
fn main() {
2121
let secp = Secp256k1::new();

bitcoin/examples/sign-tx-taproot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use bitcoin::{
1313
Txid, Witness,
1414
};
1515

16-
const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat_unchecked(20_000_000);
17-
const SPEND_AMOUNT: Amount = Amount::from_sat_unchecked(5_000_000);
18-
const CHANGE_AMOUNT: Amount = Amount::from_sat_unchecked(14_999_000); // 1000 sat fee.
16+
const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat_u32(20_000_000);
17+
const SPEND_AMOUNT: Amount = Amount::from_sat_u32(5_000_000);
18+
const CHANGE_AMOUNT: Amount = Amount::from_sat_u32(14_999_000); // 1000 sat fee.
1919

2020
fn main() {
2121
let secp = Secp256k1::new();

bitcoin/examples/taproot-psbt-simple.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ const BIP86_DERIVATION_PATH: &str = "m/86'/0'/0'";
4545
const MASTER_FINGERPRINT: &str = "9680603f";
4646

4747
// The dummy UTXO amounts we are spending.
48-
const DUMMY_UTXO_AMOUNT_INPUT_1: Amount = Amount::from_sat_unchecked(20_000_000);
49-
const DUMMY_UTXO_AMOUNT_INPUT_2: Amount = Amount::from_sat_unchecked(10_000_000);
48+
const DUMMY_UTXO_AMOUNT_INPUT_1: Amount = Amount::from_sat_u32(20_000_000);
49+
const DUMMY_UTXO_AMOUNT_INPUT_2: Amount = Amount::from_sat_u32(10_000_000);
5050

5151
// The amounts we are sending to someone, and receiving back as change.
52-
const SPEND_AMOUNT: Amount = Amount::from_sat_unchecked(25_000_000);
53-
const CHANGE_AMOUNT: Amount = Amount::from_sat_unchecked(4_990_000); // 10_000 sat fee.
52+
const SPEND_AMOUNT: Amount = Amount::from_sat_u32(25_000_000);
53+
const CHANGE_AMOUNT: Amount = Amount::from_sat_u32(4_990_000); // 10_000 sat fee.
5454

5555
// Derive the external address xpriv.
5656
fn get_external_address_xpriv<C: Signing>(

bitcoin/examples/taproot-psbt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const UTXO_SCRIPT_PUBKEY: &str =
4040
"5120be27fa8b1f5278faf82cab8da23e8761f8f9bd5d5ebebbb37e0e12a70d92dd16";
4141
const UTXO_PUBKEY: &str = "a6ac32163539c16b6b5dbbca01b725b8e8acaa5f821ba42c80e7940062140d19";
4242
const UTXO_MASTER_FINGERPRINT: &str = "e61b318f";
43-
const ABSOLUTE_FEES: Amount = Amount::from_sat_unchecked(1_000);
43+
const ABSOLUTE_FEES: Amount = Amount::from_sat_u32(1_000);
4444

4545
// UTXO_1 will be used for spending example 1
4646
const UTXO_1: P2trUtxo = P2trUtxo {

bitcoin/src/blockdata/script/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ fn bitcoinconsensus() {
675675
let spent_bytes = hex!("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d");
676676
let spent = Script::from_bytes(&spent_bytes);
677677
let spending = hex!("010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000");
678-
spent.verify(0, Amount::from_sat_unchecked(18393430), &spending).unwrap();
678+
spent.verify(0, Amount::from_sat_u32(18393430), &spending).unwrap();
679679
}
680680

681681
#[test]
@@ -684,10 +684,10 @@ fn default_dust_value() {
684684
// well-known scriptPubKey types.
685685
let script_p2wpkh = Builder::new().push_int_unchecked(0).push_slice([42; 20]).into_script();
686686
assert!(script_p2wpkh.is_p2wpkh());
687-
assert_eq!(script_p2wpkh.minimal_non_dust(), Some(Amount::from_sat_unchecked(294)));
687+
assert_eq!(script_p2wpkh.minimal_non_dust(), Some(Amount::from_sat_u32(294)));
688688
assert_eq!(
689689
script_p2wpkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
690-
Some(Amount::from_sat_unchecked(588))
690+
Some(Amount::from_sat_u32(588))
691691
);
692692

693693
let script_p2pkh = Builder::new()
@@ -698,10 +698,10 @@ fn default_dust_value() {
698698
.push_opcode(OP_CHECKSIG)
699699
.into_script();
700700
assert!(script_p2pkh.is_p2pkh());
701-
assert_eq!(script_p2pkh.minimal_non_dust(), Some(Amount::from_sat_unchecked(546)));
701+
assert_eq!(script_p2pkh.minimal_non_dust(), Some(Amount::from_sat_u32(546)));
702702
assert_eq!(
703703
script_p2pkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
704-
Some(Amount::from_sat_unchecked(1092))
704+
Some(Amount::from_sat_u32(1092))
705705
);
706706
}
707707

bitcoin/src/crypto/sighash.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,7 @@ mod tests {
20892089
).unwrap();
20902090

20912091
let spk = ScriptBuf::from_hex("00141d0f172a0ecb48aee1be1f2687d2963ae33f71a1").unwrap();
2092-
let value = Amount::from_sat_unchecked(600_000_000);
2092+
let value = Amount::from_sat_u32(600_000_000);
20932093

20942094
let mut cache = SighashCache::new(&tx);
20952095
assert_eq!(
@@ -2130,7 +2130,7 @@ mod tests {
21302130

21312131
let redeem_script =
21322132
ScriptBuf::from_hex("001479091972186c449eb1ded22b78e40d009bdf0089").unwrap();
2133-
let value = Amount::from_sat_unchecked(1_000_000_000);
2133+
let value = Amount::from_sat_u32(1_000_000_000);
21342134

21352135
let mut cache = SighashCache::new(&tx);
21362136
assert_eq!(
@@ -2180,7 +2180,7 @@ mod tests {
21802180
)
21812181
.unwrap();
21822182

2183-
let value = Amount::from_sat_unchecked(987_654_321);
2183+
let value = Amount::from_sat_u32(987_654_321);
21842184
(tx, witness_script, value)
21852185
}
21862186

bitcoin/src/psbt/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,14 +1429,14 @@ mod tests {
14291429
}],
14301430
output: vec![
14311431
TxOut {
1432-
value: Amount::from_sat_unchecked(99_999_699),
1432+
value: Amount::from_sat_u32(99_999_699),
14331433
script_pubkey: ScriptBuf::from_hex(
14341434
"76a914d0c59903c5bac2868760e90fd521a4665aa7652088ac",
14351435
)
14361436
.unwrap(),
14371437
},
14381438
TxOut {
1439-
value: Amount::from_sat_unchecked(100_000_000),
1439+
value: Amount::from_sat_u32(100_000_000),
14401440
script_pubkey: ScriptBuf::from_hex(
14411441
"a9143545e6e33b832c47050f24d3eeb93c9c03948bc787",
14421442
)
@@ -1502,7 +1502,7 @@ mod tests {
15021502
)]),
15031503
}],
15041504
output: vec![TxOut {
1505-
value: Amount::from_sat_unchecked(190_303_501_938),
1505+
value: Amount::from_sat(190_303_501_938).unwrap(),
15061506
script_pubkey: ScriptBuf::from_hex(
15071507
"a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587",
15081508
)
@@ -1553,7 +1553,7 @@ mod tests {
15531553
Input {
15541554
non_witness_utxo: Some(tx),
15551555
witness_utxo: Some(TxOut {
1556-
value: Amount::from_sat_unchecked(190_303_501_938),
1556+
value: Amount::from_sat(190_303_501_938).unwrap(),
15571557
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587").unwrap(),
15581558
}),
15591559
sighash_type: Some("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY".parse::<PsbtSighashType>().unwrap()),
@@ -1678,11 +1678,11 @@ mod tests {
16781678
],
16791679
output: vec![
16801680
TxOut {
1681-
value: Amount::from_sat_unchecked(99_999_699),
1681+
value: Amount::from_sat_u32(99_999_699),
16821682
script_pubkey: ScriptBuf::from_hex("76a914d0c59903c5bac2868760e90fd521a4665aa7652088ac").unwrap(),
16831683
},
16841684
TxOut {
1685-
value: Amount::from_sat_unchecked(100_000_000),
1685+
value: Amount::from_sat_u32(100_000_000),
16861686
script_pubkey: ScriptBuf::from_hex("a9143545e6e33b832c47050f24d3eeb93c9c03948bc787").unwrap(),
16871687
},
16881688
],
@@ -1725,11 +1725,11 @@ mod tests {
17251725
],
17261726
output: vec![
17271727
TxOut {
1728-
value: Amount::from_sat_unchecked(200_000_000),
1728+
value: Amount::from_sat_u32(200_000_000),
17291729
script_pubkey: ScriptBuf::from_hex("76a91485cff1097fd9e008bb34af709c62197b38978a4888ac").unwrap(),
17301730
},
17311731
TxOut {
1732-
value: Amount::from_sat_unchecked(190_303_501_938),
1732+
value: Amount::from_sat(190_303_501_938).unwrap(),
17331733
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587").unwrap(),
17341734
},
17351735
],
@@ -2011,11 +2011,11 @@ mod tests {
20112011
],
20122012
output: vec![
20132013
TxOut {
2014-
value: Amount::from_sat_unchecked(99_999_699),
2014+
value: Amount::from_sat_u32(99_999_699),
20152015
script_pubkey: ScriptBuf::from_hex("76a914d0c59903c5bac2868760e90fd521a4665aa7652088ac").unwrap(),
20162016
},
20172017
TxOut {
2018-
value: Amount::from_sat_unchecked(100_000_000),
2018+
value: Amount::from_sat_u32(100_000_000),
20192019
script_pubkey: ScriptBuf::from_hex("a9143545e6e33b832c47050f24d3eeb93c9c03948bc787").unwrap(),
20202020
},
20212021
],
@@ -2058,11 +2058,11 @@ mod tests {
20582058
],
20592059
output: vec![
20602060
TxOut {
2061-
value: Amount::from_sat_unchecked(200_000_000),
2061+
value: Amount::from_sat_u32(200_000_000),
20622062
script_pubkey: ScriptBuf::from_hex("76a91485cff1097fd9e008bb34af709c62197b38978a4888ac").unwrap(),
20632063
},
20642064
TxOut {
2065-
value: Amount::from_sat_unchecked(190_303_501_938),
2065+
value: Amount::from_sat(190_303_501_938).unwrap(),
20662066
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587").unwrap(),
20672067
},
20682068
],
@@ -2171,9 +2171,9 @@ mod tests {
21712171

21722172
#[test]
21732173
fn fee() {
2174-
let output_0_val = Amount::from_sat_unchecked(99_999_699);
2175-
let output_1_val = Amount::from_sat_unchecked(100_000_000);
2176-
let prev_output_val = Amount::from_sat_unchecked(200_000_000);
2174+
let output_0_val = Amount::from_sat_u32(99_999_699);
2175+
let output_1_val = Amount::from_sat_u32(100_000_000);
2176+
let prev_output_val = Amount::from_sat_u32(200_000_000);
21772177

21782178
let t = Psbt {
21792179
unsigned_tx: Transaction {
@@ -2234,7 +2234,7 @@ mod tests {
22342234
script_pubkey: ScriptBuf::new()
22352235
},
22362236
TxOut {
2237-
value: Amount::from_sat_unchecked(190_303_501_938),
2237+
value: Amount::from_sat(190_303_501_938).unwrap(),
22382238
script_pubkey: ScriptBuf::new()
22392239
},
22402240
],
@@ -2296,7 +2296,7 @@ mod tests {
22962296

22972297
// First input we can spend. See comment above on key_map for why we use defaults here.
22982298
let txout_wpkh = TxOut {
2299-
value: Amount::from_sat_unchecked(10),
2299+
value: Amount::from_sat_u32(10),
23002300
script_pubkey: ScriptBuf::new_p2wpkh(pk.wpubkey_hash().unwrap()),
23012301
};
23022302
psbt.inputs[0].witness_utxo = Some(txout_wpkh);
@@ -2308,7 +2308,7 @@ mod tests {
23082308
// Second input is unspendable by us e.g., from another wallet that supports future upgrades.
23092309
let unknown_prog = WitnessProgram::new(WitnessVersion::V4, &[0xaa; 34]).unwrap();
23102310
let txout_unknown_future = TxOut {
2311-
value: Amount::from_sat_unchecked(10),
2311+
value: Amount::from_sat_u32(10),
23122312
script_pubkey: ScriptBuf::new_witness_program(&unknown_prog),
23132313
};
23142314
psbt.inputs[1].witness_utxo = Some(txout_unknown_future);

bitcoin/tests/serde.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ fn serde_regression_psbt() {
235235
.unwrap()]),
236236
}],
237237
output: vec![TxOut {
238-
value: Amount::from_sat_unchecked(190_303_501_938),
238+
value: Amount::from_sat(190_303_501_938).unwrap(),
239239
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587")
240240
.unwrap(),
241241
}],
@@ -282,7 +282,7 @@ fn serde_regression_psbt() {
282282
inputs: vec![Input {
283283
non_witness_utxo: Some(tx),
284284
witness_utxo: Some(TxOut {
285-
value: Amount::from_sat_unchecked(190_303_501_938),
285+
value: Amount::from_sat(190_303_501_938).unwrap(),
286286
script_pubkey: ScriptBuf::from_hex("a914339725ba21efd62ac753a9bcd067d6c7a6a39d0587").unwrap(),
287287
}),
288288
sighash_type: Some(PsbtSighashType::from("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY".parse::<EcdsaSighashType>().unwrap())),

0 commit comments

Comments
 (0)