@@ -418,37 +418,66 @@ impl Transaction {
418
418
ty == EcdsaSigHashType :: Single && input_index >= self . output . len ( )
419
419
}
420
420
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.
425
422
#[ inline]
423
+ #[ deprecated( since = "0.28.0" , note = "Please use `transaction::weight` instead." ) ]
426
424
pub fn get_weight ( & self ) -> usize {
427
- self . get_scaled_size ( WITNESS_SCALE_FACTOR )
425
+ self . weight ( )
428
426
}
429
427
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.
431
439
#[ inline]
440
+ #[ deprecated( since = "0.28.0" , note = "Please use `transaction::size` instead." ) ]
432
441
pub fn get_size ( & self ) -> usize {
433
- self . get_scaled_size ( 1 )
442
+ self . size ( )
434
443
}
435
444
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.
441
464
///
442
- /// [`bip141 `]: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
465
+ /// [`BIP141 `]: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
443
466
/// [`policy`]: ../policy/mod.rs.html
444
467
#[ 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 ( ) ;
447
470
( weight + WITNESS_SCALE_FACTOR - 1 ) / WITNESS_SCALE_FACTOR
448
471
}
449
472
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." ) ]
451
475
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 {
452
481
let mut input_size = 0 ;
453
482
for input in & self . input {
454
483
input_size += 32 + 4 + 4 + // outpoint (32+4) + nSequence
@@ -473,8 +502,8 @@ impl Transaction {
473
502
non_input_size + input_size
474
503
}
475
504
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 {
478
507
let mut input_weight = 0 ;
479
508
let mut inputs_with_witnesses = 0 ;
480
509
for input in & self . input {
@@ -924,10 +953,10 @@ mod tests {
924
953
"a6eab3c14ab5272a58a5ba91505ba1a4b6d7a3a9fcbd187b6cd99a7b6d548cb7" . to_string( ) ) ;
925
954
assert_eq ! ( format!( "{:x}" , realtx. wtxid( ) ) ,
926
955
"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( ) ) ;
931
960
}
932
961
933
962
#[ test]
@@ -959,23 +988,23 @@ mod tests {
959
988
assert_eq ! ( format!( "{:x}" , realtx. wtxid( ) ) ,
960
989
"80b7d8a82d5d5bf92905b06f2014dd699e03837ca172e3a59d51426ebbe3e7f5" . to_string( ) ) ;
961
990
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 ) ;
965
994
// Since
966
995
// size = stripped_size + witness_size
967
996
// weight = WITNESS_SCALE_FACTOR * stripped_size + witness_size
968
997
// then,
969
998
// stripped_size = (weight - size) / (WITNESS_SCALE_FACTOR - 1)
970
999
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) ;
972
1001
// Construct a transaction without the witness data.
973
1002
let mut tx_without_witness = realtx. clone ( ) ;
974
1003
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) ;
979
1008
}
980
1009
981
1010
#[ test]
@@ -1059,7 +1088,7 @@ mod tests {
1059
1088
1060
1089
assert_eq ! ( format!( "{:x}" , tx. wtxid( ) ) , "d6ac4a5e61657c4c604dcde855a1db74ec6b3e54f32695d72c5e11c7761ea1b4" ) ;
1061
1090
assert_eq ! ( format!( "{:x}" , tx. txid( ) ) , "9652aa62b0e748caeec40c4cb7bc17c6792435cc3dfe447dd1ca24f912a1c6ec" ) ;
1062
- assert_eq ! ( tx. get_weight ( ) , 2718 ) ;
1091
+ assert_eq ! ( tx. weight ( ) , 2718 ) ;
1063
1092
1064
1093
// non-segwit tx from my mempool
1065
1094
let tx_bytes = Vec :: from_hex (
@@ -1091,7 +1120,7 @@ mod tests {
1091
1120
fn test_segwit_tx_decode ( ) {
1092
1121
let tx_bytes = Vec :: from_hex ( "010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3603da1b0e00045503bd5704c7dd8a0d0ced13bb5785010800000000000a636b706f6f6c122f4e696e6a61506f6f6c2f5345475749542fffffffff02b4e5a212000000001976a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac0000000000000000266a24aa21a9edf91c46b49eb8a29089980f02ee6b57e7d63d33b18b4fddac2bcd7db2a39837040120000000000000000000000000000000000000000000000000000000000000000000000000" ) . unwrap ( ) ;
1093
1122
let tx: Transaction = deserialize ( & tx_bytes) . unwrap ( ) ;
1094
- assert_eq ! ( tx. get_weight ( ) , 780 ) ;
1123
+ assert_eq ! ( tx. weight ( ) , 780 ) ;
1095
1124
serde_round_trip ! ( tx) ;
1096
1125
1097
1126
let consensus_encoded = serialize ( & tx) ;
@@ -1549,13 +1578,13 @@ mod benches {
1549
1578
const SOME_TX : & ' static str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000" ;
1550
1579
1551
1580
#[ bench]
1552
- pub fn bench_transaction_get_size ( bh : & mut Bencher ) {
1581
+ pub fn bench_transaction_size ( bh : & mut Bencher ) {
1553
1582
let raw_tx = Vec :: from_hex ( SOME_TX ) . unwrap ( ) ;
1554
1583
1555
1584
let mut tx: Transaction = deserialize ( & raw_tx) . unwrap ( ) ;
1556
1585
1557
1586
bh. iter ( || {
1558
- black_box ( black_box ( & mut tx) . get_size ( ) ) ;
1587
+ black_box ( black_box ( & mut tx) . size ( ) ) ;
1559
1588
} ) ;
1560
1589
}
1561
1590
0 commit comments