Skip to content

Commit beaa2db

Browse files
committed
amount: add from_sat_i32 and from_sat_u32 methods for small constants
We have a ton of calls to `from_sat_unchecked` for small constants which were clearly in range, e.g. in fee.rs. Add a new constfn for these cases. Don't bother making a generic Into<u32>/Into<u16> variant because there isn't an obvious name for it. There are 7 instances where we're using this method with values that are out of range, which we leave as from_sat_unchecked for now.
1 parent 82d9d1a commit beaa2db

12 files changed

Lines changed: 66 additions & 50 deletions

File tree

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: 13 additions & 13 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
)
@@ -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,7 +1725,7 @@ 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 {
@@ -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,7 +2058,7 @@ 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 {
@@ -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 {
@@ -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);

units/src/amount/signed.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ mod encapsulate {
5757
/// See [`Self::MIN`] and [`Self::MAX`].
5858
pub const fn from_sat_unchecked(satoshi: i64) -> SignedAmount { SignedAmount(satoshi) }
5959

60+
/// Constructs a new [`SignedAmount`] with satoshi precision and the given number of satoshis.
61+
///
62+
/// Accepts an `i32` which is guaranteed to be in range for the type, but which can only
63+
/// represent roughly -21.47 to 21.47 BTC.
64+
pub const fn from_sat_i32(satoshi: i32) -> SignedAmount {
65+
SignedAmount(satoshi as i64) // cannot use i64::from in a constfn
66+
}
67+
6068
/// Gets the number of satoshis in this [`SignedAmount`].
6169
///
6270
/// # Examples
@@ -73,9 +81,9 @@ pub use encapsulate::SignedAmount;
7381

7482
impl SignedAmount {
7583
/// The zero amount.
76-
pub const ZERO: Self = SignedAmount::from_sat_unchecked(0);
84+
pub const ZERO: Self = SignedAmount::from_sat_i32(0);
7785
/// Exactly one satoshi.
78-
pub const ONE_SAT: Self = SignedAmount::from_sat_unchecked(1);
86+
pub const ONE_SAT: Self = SignedAmount::from_sat_i32(1);
7987
/// Exactly one bitcoin.
8088
pub const ONE_BTC: Self = SignedAmount::from_btc_i16(1);
8189
/// Exactly fifty bitcoin.

0 commit comments

Comments
 (0)