bitcoinsv/bitcoin/rules.rs
1#![allow(non_snake_case)]
2#![allow(dead_code)] // lots of important definitions in here that may not be used
3
4/// Bitcoin SV has a number of rules, consensus rules, and policy values. These are defined in this module.
5///
6/// This module makes the consensus rule and policy values available. Applications will generally need
7/// the policy rule values except when dealing with transactions from confirmed blocks in which case
8/// they will need the consensus rule values. The consensus rule values are more generally more permissive
9/// than the policy rule values.
10///
11/// These values are sometimes needed deep within the code so we have chosen to make these available
12/// through atomic global variables rather than having to pass a reference to configuration through
13/// myriad levels of functions. It is not expected that these values will change dynamically, however
14/// that capability is possible so that we can support online reconfiguration.
15///
16/// Rules, Consensus Rules, and Policy values are described in the [Genesis Upgrade specification](https://github.com/bitcoin-sv-specs/protocol/blob/master/updates/genesis-spec.md).
17///
18/// The values in this version of the module are valid for the Bitcoin SV
19/// blockchains after the Genesis Upgrade.
20use std::sync::atomic::{AtomicU64, Ordering};
21
22/// Configurable Consensus Rule for Miners - maximum block size - default value 4GB
23static CRULE_MAX_BLOCK_SIZE: AtomicU64 = AtomicU64::new(4_000_000_000);
24
25/// Configurable Consensus Rule for Clients - maximum block size - default value 10GB
26static POLICY_MAX_BLOCK_SIZE: AtomicU64 = AtomicU64::new(10_000_000_000);
27
28/// Get the policy or consensus rule value of the maximum size of a block.
29///
30/// If `policy` is true then get the policy value, otherwise get the consensus rule value.
31pub fn MAX_BLOCK_SIZE(policy: bool) -> u64 {
32 if policy {
33 POLICY_MAX_BLOCK_SIZE.load(Ordering::Relaxed)
34 } else {
35 CRULE_MAX_BLOCK_SIZE.load(Ordering::Relaxed)
36 }
37}
38
39/// Consensus Rule - maximum size of transactions - 1GB - [link](https://github.com/bitcoin-sv-specs/protocol/blob/master/updates/genesis-spec.md#maximum-transaction-size)
40static CRULE_MAX_TX_SIZE: AtomicU64 = AtomicU64::new(1_000_000_000);
41
42/// Policy - maximum transaction size - default 10MB - [link](https://github.com/bitcoin-sv-specs/protocol/blob/master/updates/genesis-spec.md#maximum-acceptable-transaction-size-policy).
43/// In SV Node source code, it is [here](https://github.com/bitcoin-sv/bitcoin-sv/blob/86eb5e8bdf5573c3cd844a1d81bd4fb151b909e0/src/policy/policy.h#L69).
44static POLICY_MAX_TX_SIZE: AtomicU64 = AtomicU64::new(10_000_000);
45
46/// Get the policy or consensus rule value of the maximum size of a transaction.
47///
48/// If `policy` is true then get the policy value, otherwise get the consensus rule value.
49pub fn MAX_TX_SIZE(policy: bool) -> u64 {
50 if policy {
51 POLICY_MAX_TX_SIZE.load(Ordering::Relaxed)
52 } else {
53 CRULE_MAX_TX_SIZE.load(Ordering::Relaxed)
54 }
55}
56
57/// Consensus Rule - max size of byte sequence - UINT32_MAX
58static CRULE_MAX_BYTE_SEQ_LEN: AtomicU64 = AtomicU64::new(u32::MAX as u64);
59
60/// Policy - max size of byte sequence - UINT32_MAX - local to this software
61static POLICY_MAX_BYTE_SEQ_LEN: AtomicU64 = AtomicU64::new(u32::MAX as u64);
62
63/// Get the policy or consensus rule value of the maximum length of a byte sequence.
64///
65/// If `policy` is true then get the policy value, otherwise get the consensus rule value.
66pub fn MAX_BYTE_SEQ_LEN(policy: bool) -> u64 {
67 if policy {
68 POLICY_MAX_BYTE_SEQ_LEN.load(Ordering::Relaxed)
69 } else {
70 CRULE_MAX_BYTE_SEQ_LEN.load(Ordering::Relaxed)
71 }
72}
73
74/// Consensus Rule - max number of public keys per multisig - INT32_MAX - [link](https://github.com/bitcoin-sv-specs/protocol/blob/master/updates/genesis-spec.md#number-of-public-keys-per-multisig-consensus-rule)
75static CRULE_MAX_MULTISIG_KEYS: AtomicU64 = AtomicU64::new(i32::MAX as u64);
76
77/// Policy - max number of public keys per multisig - 32 - local to this software
78static POLICY_MAX_MULTISIG_KEYS: AtomicU64 = AtomicU64::new(32);
79
80/// Get the policy or consensus rule value of the maximum number of public keys per multisig.
81///
82/// If `policy` is true then get the policy value, otherwise get the consensus rule value.
83pub fn MAX_MULTISIG_KEYS(policy: bool) -> u64 {
84 if policy {
85 POLICY_MAX_MULTISIG_KEYS.load(Ordering::Relaxed)
86 } else {
87 CRULE_MAX_MULTISIG_KEYS.load(Ordering::Relaxed)
88 }
89}
90
91/// Configurable Consensus Rule - max memory used by stacks - default = 200MB - [link](https://github.com/bitcoin-sv-specs/protocol/blob/master/updates/genesis-spec.md#stack-memory-usage-consensus-rule)
92const CRULE_MAX_STACK_MEM: AtomicU64 = AtomicU64::new(200_000_000);
93
94/// Policy - max memory used by stacks - default = 100MB - [link](https://github.com/bitcoin-sv-specs/protocol/blob/master/updates/genesis-spec.md#stack-memory-usage-policy)
95const POLICY_MAX_STACK_MEM: AtomicU64 = AtomicU64::new(100_000_000);
96
97/// Get the policy or consensus rule value of the maximum memory used by the stacks.
98///
99/// If `policy` is true then get the policy value, otherwise get the consensus rule value.
100pub fn MAX_STACK_MEM(policy: bool) -> u64 {
101 if policy {
102 POLICY_MAX_STACK_MEM.load(Ordering::Relaxed)
103 } else {
104 CRULE_MAX_STACK_MEM.load(Ordering::Relaxed)
105 }
106}
107
108/// Policy - transaction evaluation timeout - default 1s = 1000 ms - [link](https://github.com/bitcoin-sv-specs/protocol/blob/master/updates/genesis-spec.md#transaction-evaluation-timeout)
109const POLICY_TX_EVAL_TIMEOUT_MS: AtomicU64 = AtomicU64::new(1_000);
110
111/// Get the policy value of the timeout for evaluating a transaction.
112pub fn TX_EVALE_TIMEOUT_MS() -> u64 {
113 POLICY_TX_EVAL_TIMEOUT_MS.load(Ordering::Relaxed)
114}
115
116/// Consensus Rule - max size of numeric value - 750_000 bytes - [link](https://github.com/bitcoin-sv-specs/protocol/blob/master/updates/genesis-spec.md#numeric-value-size-consensus-rule)
117/// See also [POLICY_MAX_NUMERIC_LEN].
118static CRULE_MAX_NUMERIC_LEN: AtomicU64 = AtomicU64::new(750_000);
119
120/// Policy - max numeric value length - default 250_000 - [link](https://github.com/bitcoin-sv-specs/protocol/blob/master/updates/genesis-spec.md#numeric-value-length)
121/// See also [CRULE_MAX_NUMERIC_LEN_VAL].
122static POLICY_MAX_NUMERIC_LEN: AtomicU64 = AtomicU64::new(250_000);
123
124/// Get the policy or consensus rule value of the maximum length of a numeric value.
125///
126/// If `policy` is true then get the policy value, otherwise get the consensus rule value.
127pub fn MAX_NUMERIC_LEN(policy: bool) -> u64 {
128 if policy {
129 POLICY_MAX_NUMERIC_LEN.load(Ordering::Relaxed)
130 } else {
131 CRULE_MAX_NUMERIC_LEN.load(Ordering::Relaxed)
132 }
133}