Skip to content

Commit deb9734

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#861: Remove get_ prefix
3bde1a2 Remove get_ prefix (Tobin Harding) Pull request description: This one might be a viewed as code churn or unnecessarily modifying the API, feel free to NACK :) We have a bunch of methods that use the prefix `get_`, they are not exactly getters because they do more than just access a struct fields so Rust convention relating to getters does not apply, however, the `get_` prefix does not add to the descriptiveness of name hence the shorter form can be used with no loss of clarity. Improve docs and deprecate any methods changed that are pubic. ACKs for top commit: dr-orlovsky: ACK 3bde1a2 apoelstra: ACK 3bde1a2 sanket1729: ACK 3bde1a2 Tree-SHA512: d9e618ba7fec81ad157c2c806d1db273f899d63707c78254c133b619293f9f0c9a4f3a3e091e9aad399479ff80d5d052c424501164374c21bb90fb9783a4824e
2 parents 721f7e4 + a20f0c0 commit deb9734

File tree

2 files changed

+100
-52
lines changed

2 files changed

+100
-52
lines changed

src/blockdata/block.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -249,28 +249,47 @@ impl Block {
249249
bitcoin_merkle_root(hashes).map(|h| h.into())
250250
}
251251

252-
/// The size of the header + the size of the varint with the tx count + the txs themselves
253-
#[inline]
254-
fn get_base_size(&self) -> usize {
252+
/// base_size == size of header + size of encoded transaction count.
253+
fn base_size(&self) -> usize {
255254
80 + VarInt(self.txdata.len() as u64).len()
256255
}
257256

258-
/// Get the size of the block
257+
/// Returns the size of the block.
258+
#[deprecated(since = "0.28.0", note = "Please use `block::size` instead.")]
259259
pub fn get_size(&self) -> usize {
260-
let txs_size: usize = self.txdata.iter().map(Transaction::get_size).sum();
261-
self.get_base_size() + txs_size
260+
self.size()
262261
}
263262

264-
/// Get the strippedsize of the block
263+
/// Returns the size of the block.
264+
///
265+
/// size == size of header + size of encoded transaction count + total size of transactions.
266+
pub fn size(&self) -> usize {
267+
let txs_size: usize = self.txdata.iter().map(Transaction::size).sum();
268+
self.base_size() + txs_size
269+
}
270+
271+
/// Returns the strippedsize of the block.
272+
#[deprecated(since = "0.28.0", note = "Please use `transaction::strippedsize` instead.")]
265273
pub fn get_strippedsize(&self) -> usize {
266-
let txs_size: usize = self.txdata.iter().map(Transaction::get_strippedsize).sum();
267-
self.get_base_size() + txs_size
274+
self.strippedsize()
268275
}
269276

270-
/// Get the weight of the block
277+
/// Returns the strippedsize of the block.
278+
pub fn strippedsize(&self) -> usize {
279+
let txs_size: usize = self.txdata.iter().map(Transaction::strippedsize).sum();
280+
self.base_size() + txs_size
281+
}
282+
283+
/// Returns the weight of the block.
284+
#[deprecated(since = "0.28.0", note = "Please use `transaction::weight` instead.")]
271285
pub fn get_weight(&self) -> usize {
272-
let base_weight = WITNESS_SCALE_FACTOR * self.get_base_size();
273-
let txs_weight: usize = self.txdata.iter().map(Transaction::get_weight).sum();
286+
self.weight()
287+
}
288+
289+
/// Returns the weight of the block.
290+
pub fn weight(&self) -> usize {
291+
let base_weight = WITNESS_SCALE_FACTOR * self.base_size();
292+
let txs_weight: usize = self.txdata.iter().map(Transaction::weight).sum();
274293
base_weight + txs_weight
275294
}
276295

@@ -396,9 +415,9 @@ mod tests {
396415
assert_eq!(real_decode.header.difficulty(Network::Bitcoin), 1);
397416
// [test] TODO: check the transaction data
398417

399-
assert_eq!(real_decode.get_size(), some_block.len());
400-
assert_eq!(real_decode.get_strippedsize(), some_block.len());
401-
assert_eq!(real_decode.get_weight(), some_block.len() * 4);
418+
assert_eq!(real_decode.size(), some_block.len());
419+
assert_eq!(real_decode.strippedsize(), some_block.len());
420+
assert_eq!(real_decode.weight(), some_block.len() * 4);
402421

403422
// should be also ok for a non-witness block as commitment is optional in that case
404423
assert!(real_decode.check_witness_commitment());
@@ -431,9 +450,9 @@ mod tests {
431450
assert_eq!(real_decode.header.difficulty(Network::Testnet), 2456598);
432451
// [test] TODO: check the transaction data
433452

434-
assert_eq!(real_decode.get_size(), segwit_block.len());
435-
assert_eq!(real_decode.get_strippedsize(), 4283);
436-
assert_eq!(real_decode.get_weight(), 17168);
453+
assert_eq!(real_decode.size(), segwit_block.len());
454+
assert_eq!(real_decode.strippedsize(), 4283);
455+
assert_eq!(real_decode.weight(), 17168);
437456

438457
assert!(real_decode.check_witness_commitment());
439458

src/blockdata/transaction.rs

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -418,37 +418,66 @@ impl Transaction {
418418
ty == EcdsaSigHashType::Single && input_index >= self.output.len()
419419
}
420420

421-
/// Gets the "weight" of this transaction, as defined by BIP141. For transactions with an empty
422-
/// witness, this is simply the consensus-serialized size times 4. For transactions with a
423-
/// witness, this is the non-witness consensus-serialized size multiplied by 3 plus the
424-
/// with-witness consensus-serialized size.
421+
/// Returns the "weight" of this transaction, as defined by BIP141.
425422
#[inline]
423+
#[deprecated(since = "0.28.0", note = "Please use `transaction::weight` instead.")]
426424
pub fn get_weight(&self) -> usize {
427-
self.get_scaled_size(WITNESS_SCALE_FACTOR)
425+
self.weight()
428426
}
429427

430-
/// Gets the regular byte-wise consensus-serialized size of this transaction.
428+
/// Returns the "weight" of this transaction, as defined by BIP141.
429+
///
430+
/// For transactions with an empty witness, this is simply the consensus-serialized size times
431+
/// four. For transactions with a witness, this is the non-witness consensus-serialized size
432+
/// multiplied by three plus the with-witness consensus-serialized size.
433+
#[inline]
434+
pub fn weight(&self) -> usize {
435+
self.scaled_size(WITNESS_SCALE_FACTOR)
436+
}
437+
438+
/// Returns the regular byte-wise consensus-serialized size of this transaction.
431439
#[inline]
440+
#[deprecated(since = "0.28.0", note = "Please use `transaction::size` instead.")]
432441
pub fn get_size(&self) -> usize {
433-
self.get_scaled_size(1)
442+
self.size()
434443
}
435444

436-
/// Gets the "vsize" of this transaction. Will be `ceil(weight / 4.0)`.
437-
/// Note this implements the virtual size as per [`bip141`], which is different
438-
/// to what is implemented in Bitcoin Core. The computation should be the same
439-
/// for any remotely sane transaction, and a standardness-rule-correct version
440-
/// is available in the [`policy`] module.
445+
/// Returns the regular byte-wise consensus-serialized size of this transaction.
446+
#[inline]
447+
pub fn size(&self) -> usize {
448+
self.scaled_size(1)
449+
}
450+
451+
/// Returns the "virtual size" (vsize) of this transaction.
452+
#[inline]
453+
#[deprecated(since = "0.28.0", note = "Please use `transaction::vsize` instead.")]
454+
pub fn get_vsize(&self) -> usize {
455+
self.vsize()
456+
}
457+
458+
/// Returns the "virtual size" (vsize) of this transaction.
459+
///
460+
/// Will be `ceil(weight / 4.0)`. Note this implements the virtual size as per [`BIP141`], which
461+
/// is different to what is implemented in Bitcoin Core. The computation should be the same for
462+
/// any remotely sane transaction, and a standardness-rule-correct version is available in the
463+
/// [`policy`] module.
441464
///
442-
/// [`bip141`]: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
465+
/// [`BIP141`]: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
443466
/// [`policy`]: ../policy/mod.rs.html
444467
#[inline]
445-
pub fn get_vsize(&self) -> usize {
446-
let weight = self.get_weight();
468+
pub fn vsize(&self) -> usize {
469+
let weight = self.weight();
447470
(weight + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR
448471
}
449472

450-
/// Gets the size of this transaction excluding the witness data.
473+
/// Returns the size of this transaction excluding the witness data.
474+
#[deprecated(since = "0.28.0", note = "Please use `transaction::strippedsize` instead.")]
451475
pub fn get_strippedsize(&self) -> usize {
476+
self.strippedsize()
477+
}
478+
479+
/// Returns the size of this transaction excluding the witness data.
480+
pub fn strippedsize(&self) -> usize {
452481
let mut input_size = 0;
453482
for input in &self.input {
454483
input_size += 32 + 4 + 4 + // outpoint (32+4) + nSequence
@@ -473,8 +502,8 @@ impl Transaction {
473502
non_input_size + input_size
474503
}
475504

476-
/// Internal utility function for get_{size,weight}
477-
fn get_scaled_size(&self, scale_factor: usize) -> usize {
505+
/// Internal utility function for size/weight functions.
506+
fn scaled_size(&self, scale_factor: usize) -> usize {
478507
let mut input_weight = 0;
479508
let mut inputs_with_witnesses = 0;
480509
for input in &self.input {
@@ -924,10 +953,10 @@ mod tests {
924953
"a6eab3c14ab5272a58a5ba91505ba1a4b6d7a3a9fcbd187b6cd99a7b6d548cb7".to_string());
925954
assert_eq!(format!("{:x}", realtx.wtxid()),
926955
"a6eab3c14ab5272a58a5ba91505ba1a4b6d7a3a9fcbd187b6cd99a7b6d548cb7".to_string());
927-
assert_eq!(realtx.get_weight(), tx_bytes.len()*WITNESS_SCALE_FACTOR);
928-
assert_eq!(realtx.get_size(), tx_bytes.len());
929-
assert_eq!(realtx.get_vsize(), tx_bytes.len());
930-
assert_eq!(realtx.get_strippedsize(), tx_bytes.len());
956+
assert_eq!(realtx.weight(), tx_bytes.len()*WITNESS_SCALE_FACTOR);
957+
assert_eq!(realtx.size(), tx_bytes.len());
958+
assert_eq!(realtx.vsize(), tx_bytes.len());
959+
assert_eq!(realtx.strippedsize(), tx_bytes.len());
931960
}
932961

933962
#[test]
@@ -959,23 +988,23 @@ mod tests {
959988
assert_eq!(format!("{:x}", realtx.wtxid()),
960989
"80b7d8a82d5d5bf92905b06f2014dd699e03837ca172e3a59d51426ebbe3e7f5".to_string());
961990
const EXPECTED_WEIGHT: usize = 442;
962-
assert_eq!(realtx.get_weight(), EXPECTED_WEIGHT);
963-
assert_eq!(realtx.get_size(), tx_bytes.len());
964-
assert_eq!(realtx.get_vsize(), 111);
991+
assert_eq!(realtx.weight(), EXPECTED_WEIGHT);
992+
assert_eq!(realtx.size(), tx_bytes.len());
993+
assert_eq!(realtx.vsize(), 111);
965994
// Since
966995
// size = stripped_size + witness_size
967996
// weight = WITNESS_SCALE_FACTOR * stripped_size + witness_size
968997
// then,
969998
// stripped_size = (weight - size) / (WITNESS_SCALE_FACTOR - 1)
970999
let expected_strippedsize = (EXPECTED_WEIGHT - tx_bytes.len()) / (WITNESS_SCALE_FACTOR - 1);
971-
assert_eq!(realtx.get_strippedsize(), expected_strippedsize);
1000+
assert_eq!(realtx.strippedsize(), expected_strippedsize);
9721001
// Construct a transaction without the witness data.
9731002
let mut tx_without_witness = realtx.clone();
9741003
tx_without_witness.input.iter_mut().for_each(|input| input.witness.clear());
975-
assert_eq!(tx_without_witness.get_weight(), expected_strippedsize*WITNESS_SCALE_FACTOR);
976-
assert_eq!(tx_without_witness.get_size(), expected_strippedsize);
977-
assert_eq!(tx_without_witness.get_vsize(), expected_strippedsize);
978-
assert_eq!(tx_without_witness.get_strippedsize(), expected_strippedsize);
1004+
assert_eq!(tx_without_witness.weight(), expected_strippedsize*WITNESS_SCALE_FACTOR);
1005+
assert_eq!(tx_without_witness.size(), expected_strippedsize);
1006+
assert_eq!(tx_without_witness.vsize(), expected_strippedsize);
1007+
assert_eq!(tx_without_witness.strippedsize(), expected_strippedsize);
9791008
}
9801009

9811010
#[test]
@@ -1059,7 +1088,7 @@ mod tests {
10591088

10601089
assert_eq!(format!("{:x}", tx.wtxid()), "d6ac4a5e61657c4c604dcde855a1db74ec6b3e54f32695d72c5e11c7761ea1b4");
10611090
assert_eq!(format!("{:x}", tx.txid()), "9652aa62b0e748caeec40c4cb7bc17c6792435cc3dfe447dd1ca24f912a1c6ec");
1062-
assert_eq!(tx.get_weight(), 2718);
1091+
assert_eq!(tx.weight(), 2718);
10631092

10641093
// non-segwit tx from my mempool
10651094
let tx_bytes = Vec::from_hex(
@@ -1091,7 +1120,7 @@ mod tests {
10911120
fn test_segwit_tx_decode() {
10921121
let tx_bytes = Vec::from_hex("010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3603da1b0e00045503bd5704c7dd8a0d0ced13bb5785010800000000000a636b706f6f6c122f4e696e6a61506f6f6c2f5345475749542fffffffff02b4e5a212000000001976a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac0000000000000000266a24aa21a9edf91c46b49eb8a29089980f02ee6b57e7d63d33b18b4fddac2bcd7db2a39837040120000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
10931122
let tx: Transaction = deserialize(&tx_bytes).unwrap();
1094-
assert_eq!(tx.get_weight(), 780);
1123+
assert_eq!(tx.weight(), 780);
10951124
serde_round_trip!(tx);
10961125

10971126
let consensus_encoded = serialize(&tx);
@@ -1549,13 +1578,13 @@ mod benches {
15491578
const SOME_TX: &'static str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
15501579

15511580
#[bench]
1552-
pub fn bench_transaction_get_size(bh: &mut Bencher) {
1581+
pub fn bench_transaction_size(bh: &mut Bencher) {
15531582
let raw_tx = Vec::from_hex(SOME_TX).unwrap();
15541583

15551584
let mut tx: Transaction = deserialize(&raw_tx).unwrap();
15561585

15571586
bh.iter(|| {
1558-
black_box(black_box(&mut tx).get_size());
1587+
black_box(black_box(&mut tx).size());
15591588
});
15601589
}
15611590

0 commit comments

Comments
 (0)