bitcoin/policy.rs
1// SPDX-License-Identifier: CC0-1.0
2
3//! Bitcoin policy.
4//!
5//! This module exposes some constants and functions used in the reference
6//! implementation and which, as a consequence, define some network rules.
7//!
8//! # *Warning*
9//! While the constants present in this module are very unlikely to change, they do not define
10//! Bitcoin. As such they must not be relied upon as if they were consensus rules.
11//!
12//! These values were taken from bitcoind v0.21.1 (194b9b8792d9b0798fdb570b79fa51f1d1f5ebaf).
13//!
14
15use core::cmp;
16
17use super::blockdata::constants::{MAX_BLOCK_SIGOPS_COST, WITNESS_SCALE_FACTOR};
18
19/// Maximum weight of a transaction for it to be relayed by most nodes on the network
20pub const MAX_STANDARD_TX_WEIGHT: u32 = 400_000;
21
22/// Minimum non-witness size for a standard transaction (1 segwit input + 1 P2WPKH output = 82 bytes)
23pub const MIN_STANDARD_TX_NONWITNESS_SIZE: u32 = 82;
24
25/// Maximum number of sigops in a standard tx.
26pub const MAX_STANDARD_TX_SIGOPS_COST: u32 = MAX_BLOCK_SIGOPS_COST as u32 / 5;
27
28/// The minimum incremental *feerate* (despite the name), in sats per virtual kilobyte for RBF.
29pub const DEFAULT_INCREMENTAL_RELAY_FEE: u32 = 1_000;
30
31/// The number of bytes equivalent per signature operation. Affects transaction relay through the
32/// virtual size computation.
33pub const DEFAULT_BYTES_PER_SIGOP: u32 = 20;
34
35/// The minimum feerate, in sats per kilo-virtualbyte, for defining dust. An output is considered
36/// dust if spending it under this feerate would cost more in fee.
37pub const DUST_RELAY_TX_FEE: u32 = 3_000;
38
39/// Minimum feerate, in sats per virtual kilobyte, for a transaction to be relayed by most nodes on
40/// the network.
41pub const DEFAULT_MIN_RELAY_TX_FEE: u32 = 1_000;
42
43/// Default number of hours for an unconfirmed transaction to expire in most of the network nodes'
44/// mempools.
45pub const DEFAULT_MEMPOOL_EXPIRY: u32 = 336;
46
47/// The virtual transaction size, as computed by default by bitcoind node.
48pub fn get_virtual_tx_size(weight: i64, n_sigops: i64) -> i64 {
49 (cmp::max(weight, n_sigops * DEFAULT_BYTES_PER_SIGOP as i64) + WITNESS_SCALE_FACTOR as i64 - 1)
50 / WITNESS_SCALE_FACTOR as i64
51}