Skip to content

Commit 8091060

Browse files
committed
Add block/tx virtual_size as (ceiling_divide(weight(),4)).
1 parent 2a45264 commit 8091060

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

include/bitcoin/system/chain/block.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class BC_API block
100100
size_t segregated() const NOEXCEPT;
101101
size_t serialized_size(bool witness) const NOEXCEPT;
102102
size_t signature_operations(bool bip16, bool bip141) const NOEXCEPT;
103+
size_t virtual_size() const NOEXCEPT;
103104

104105
/// Computed malleation properties.
105106
bool is_malleable() const NOEXCEPT;

include/bitcoin/system/chain/transaction.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class BC_API transaction
111111
bool is_segregated() const NOEXCEPT;
112112
hash_digest hash(bool witness) const NOEXCEPT;
113113
size_t serialized_size(bool witness) const NOEXCEPT;
114+
size_t virtual_size() const NOEXCEPT;
114115

115116
/// Cache setters/getters, not thread safe.
116117
/// -----------------------------------------------------------------------

src/chain/block.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,12 @@ size_t block::weight() const NOEXCEPT
481481
ceilinged_multiply(total_size_contribution, serialized_size(true)));
482482
}
483483

484+
size_t block::virtual_size() const NOEXCEPT
485+
{
486+
constexpr auto scale = base_size_contribution + total_size_contribution;
487+
return ceilinged_divide(weight(), scale);
488+
}
489+
484490
bool block::is_overweight() const NOEXCEPT
485491
{
486492
return weight() > max_block_weight;

src/chain/transaction.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,12 +563,18 @@ bool transaction::is_segregated() const NOEXCEPT
563563

564564
size_t transaction::weight() const NOEXCEPT
565565
{
566-
// Block weight is 3 * base size * + 1 * total size [bip141].
566+
// Transaction weight is 3 * base size * + 1 * total size [bip141].
567567
return ceilinged_add(
568568
ceilinged_multiply(base_size_contribution, serialized_size(false)),
569569
ceilinged_multiply(total_size_contribution, serialized_size(true)));
570570
}
571571

572+
size_t transaction::virtual_size() const NOEXCEPT
573+
{
574+
constexpr auto scale = base_size_contribution + total_size_contribution;
575+
return ceilinged_divide(weight(), scale);
576+
}
577+
572578
bool transaction::is_overweight() const NOEXCEPT
573579
{
574580
return weight() > max_block_weight;

0 commit comments

Comments
 (0)