From 7345aa60d9486dd4dffd8aa9e4d7fbbd52057a55 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Wed, 12 May 2021 21:33:19 +0200 Subject: [PATCH] policy: add a function to get the virtual transaction size 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 --- src/policy/mod.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/policy/mod.rs b/src/policy/mod.rs index e6cdc26e9f..02efa58b1d 100644 --- a/src/policy/mod.rs +++ b/src/policy/mod.rs @@ -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; @@ -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 +}