Skip to content

Commit cce9c22

Browse files
committed
Add Transaction::get_size()
1 parent 9718e2c commit cce9c22

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/transaction.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,19 @@ impl Transaction {
527527
/// Get the "weight" of this transaction; roughly equivalent to BIP141, in that witness data is
528528
/// counted as 1 while non-witness data is counted as 4.
529529
pub fn get_weight(&self) -> usize {
530+
self.get_scaled_size(4)
531+
}
532+
533+
/// Gets the regular byte-wise consensus-serialized size of this transaction.
534+
pub fn get_size(&self) -> usize {
535+
self.get_scaled_size(1)
536+
}
537+
538+
fn get_scaled_size(&self, scale_factor: usize) -> usize {
530539
let witness_flag = self.has_witness();
531540

532541
let input_weight = self.input.iter().map(|input| {
533-
4 * (
542+
scale_factor * (
534543
32 + 4 + 4 + // output + nSequence
535544
VarInt(input.script_sig.len() as u64).len() as usize +
536545
input.script_sig.len() + if input.has_issuance() {
@@ -561,7 +570,7 @@ impl Transaction {
561570
}).sum::<usize>();
562571

563572
let output_weight = self.output.iter().map(|output| {
564-
4 * (
573+
scale_factor * (
565574
output.asset.encoded_length() +
566575
output.value.encoded_length() +
567576
output.nonce.encoded_length() +
@@ -577,7 +586,7 @@ impl Transaction {
577586
}
578587
}).sum::<usize>();
579588

580-
4 * (
589+
scale_factor * (
581590
4 + // version
582591
4 + // locktime
583592
VarInt(self.input.len() as u64).len() as usize +
@@ -687,6 +696,7 @@ mod tests {
687696
use bitcoin::hashes::hex::FromHex;
688697
use bitcoin::hashes::sha256d;
689698

699+
use encode::serialize;
690700
use confidential;
691701
use super::*;
692702

@@ -727,6 +737,8 @@ mod tests {
727737
);
728738
assert_eq!(tx.input.len(), 1);
729739
assert_eq!(tx.output.len(), 2);
740+
assert_eq!(tx.get_size(), serialize(&tx).len());
741+
assert_eq!(tx.get_weight(), tx.get_size() * 4);
730742
assert_eq!(tx.output[0].is_fee(), false);
731743
assert_eq!(tx.output[1].is_fee(), true);
732744
assert_eq!(tx.output[0].value, confidential::Value::Explicit(9999996700));
@@ -932,6 +944,8 @@ mod tests {
932944
tx.txid().to_string(),
933945
"d606b563122409191e3b114a41d5611332dc58237ad5d2dccded302664fd56c4"
934946
);
947+
assert_eq!(tx.get_size(), serialize(&tx).len());
948+
assert_eq!(tx.get_weight(), 7296);
935949
assert_eq!(tx.input.len(), 1);
936950
assert_eq!(tx.input[0].is_coinbase(), false);
937951
assert_eq!(tx.is_coinbase(), false);
@@ -969,6 +983,8 @@ mod tests {
969983
"cc1f895908af2509e55719e662acf4a50ca4dcf0454edd718459241745e2b0aa"
970984
);
971985
assert_eq!(tx.input.len(), 1);
986+
assert_eq!(tx.get_size(), serialize(&tx).len());
987+
assert_eq!(tx.get_weight(), 769);
972988
assert_eq!(tx.input[0].is_coinbase(), true);
973989
assert_eq!(!tx.input[0].is_pegin(), true);
974990
assert_eq!(tx.input[0].pegin_data(), None);

0 commit comments

Comments
 (0)