Skip to main content

blvm_primitives/
config.rs

1//! Configuration types for consensus and protocol layers
2//!
3//! Provides foundational config structs used by blvm-consensus and blvm-protocol.
4//! Full ConsensusConfig (with mempool, spam_filter, utxo_commitments) remains in
5//! blvm-consensus until Phase 2/4 migration.
6
7use serde::{Deserialize, Serialize};
8
9/// Network message size limits configuration
10///
11/// These limits protect against DoS attacks by bounding the size of network messages.
12/// All limits match Bitcoin protocol defaults.
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub struct NetworkMessageLimits {
15    /// Maximum addresses in an addr message (protocol default: 1000)
16    #[serde(default = "default_max_addr_addresses")]
17    pub max_addr_addresses: usize,
18
19    /// Maximum inventory items in inv/getdata messages (protocol default: 50000)
20    #[serde(default = "default_max_inv_items")]
21    pub max_inv_items: usize,
22
23    /// Maximum headers in a headers message (protocol default: 2000)
24    #[serde(default = "default_max_headers")]
25    pub max_headers: usize,
26
27    /// Maximum user agent length in version message (protocol default: 256 bytes)
28    #[serde(default = "default_max_user_agent_length")]
29    pub max_user_agent_length: usize,
30}
31
32fn default_max_addr_addresses() -> usize {
33    1000
34}
35
36fn default_max_inv_items() -> usize {
37    50000
38}
39
40fn default_max_headers() -> usize {
41    2000
42}
43
44fn default_max_user_agent_length() -> usize {
45    256
46}
47
48impl Default for NetworkMessageLimits {
49    fn default() -> Self {
50        Self {
51            max_addr_addresses: 1000,
52            max_inv_items: 50000,
53            max_headers: 2000,
54            max_user_agent_length: 256,
55        }
56    }
57}
58
59/// Block validation configuration
60#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
61pub struct BlockValidationConfig {
62    /// Assume-valid height: blocks before this height skip signature verification
63    #[serde(default)]
64    pub assume_valid_height: u64,
65
66    /// Assume-valid block hash: when set, verify block at assume_valid_height matches
67    #[serde(default)]
68    pub assume_valid_hash: Option<[u8; 32]>,
69
70    /// Minimum chain work: skip only when best_header_chainwork >= this
71    #[serde(default)]
72    pub n_minimum_chain_work: u128,
73
74    /// Number of recent headers for median time-past calculation (BIP113)
75    #[serde(default = "default_median_time_past_headers")]
76    pub median_time_past_headers: usize,
77
78    /// Enable parallel transaction validation
79    #[serde(default = "default_true")]
80    pub enable_parallel_validation: bool,
81
82    /// Coinbase maturity override (for testing only)
83    #[serde(default)]
84    pub coinbase_maturity_override: u64,
85
86    /// Maximum block sigop cost override (for testing only)
87    #[serde(default)]
88    pub max_block_sigops_cost_override: u64,
89}
90
91fn default_median_time_past_headers() -> usize {
92    11
93}
94
95fn default_true() -> bool {
96    true
97}
98
99impl Default for BlockValidationConfig {
100    fn default() -> Self {
101        Self {
102            assume_valid_height: 0,
103            assume_valid_hash: None,
104            n_minimum_chain_work: 0,
105            median_time_past_headers: 11,
106            enable_parallel_validation: true,
107            coinbase_maturity_override: 0,
108            max_block_sigops_cost_override: 0,
109        }
110    }
111}