Skip to content

Commit

Permalink
policy: add a function to get the virtual transaction size
Browse files Browse the repository at this point in the history
It's very useful to Bitcoin applications, and especially "L2" ones, to
effectively compute feerates. Currently (and this is very unlikely to
change) bitcoind nodes compute the virtual size as a rounded-up division
of the size in witness units by 4, with a penalty for transactions that
are essentially >5% full of sigops.

Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
  • Loading branch information
darosior committed May 18, 2021
1 parent 2e9d62a commit 7345aa6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/policy/mod.rs
Expand Up @@ -23,7 +23,8 @@
//!
//! These values were taken from bitcoind v0.21.1 (194b9b8792d9b0798fdb570b79fa51f1d1f5ebaf).

use super::blockdata::constants::MAX_BLOCK_SIGOPS_COST;
use super::blockdata::constants::{MAX_BLOCK_SIGOPS_COST, WITNESS_SCALE_FACTOR};
use std::cmp;

/// Maximum weight of a transaction for it to be relayed by most nodes on the network
pub const MAX_STANDARD_TX_WEIGHT: u32 = 400_000;
Expand Down Expand Up @@ -52,3 +53,9 @@ pub const DEFAULT_MIN_RELAY_TX_FEE: u32 = 1_000;
/// Default number of hours for an unconfirmed transaction to expire in most of the network nodes'
/// mempools.
pub const DEFAULT_MEMPOOL_EXPIRY: u32 = 336;

/// The virtual transaction size, as computed by default by bitcoind node.
pub fn get_virtual_tx_size(weight: i64, n_sigops: i64) -> i64 {
(cmp::max(weight, n_sigops * DEFAULT_BYTES_PER_SIGOP as i64) + WITNESS_SCALE_FACTOR as i64 - 1)
/ WITNESS_SCALE_FACTOR as i64
}

0 comments on commit 7345aa6

Please sign in to comment.