pub use blvm_consensus::config::{
AdvancedConfig, BlockValidationConfig, DebugConfig, FeatureFlagsConfig, MempoolConfig,
NetworkMessageLimits, PerformanceConfig, UtxoCommitmentConfig,
};
use crate::ProtocolVersion;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProtocolValidationConfig {
#[serde(default = "default_max_block_size")]
pub max_block_size: u32,
#[serde(default = "default_max_tx_size")]
pub max_tx_size: u32,
#[serde(default = "default_max_script_size")]
pub max_script_size: u32,
#[serde(default = "default_max_txs_per_block")]
pub max_txs_per_block: usize,
#[serde(default = "default_max_locator_hashes")]
pub max_locator_hashes: usize,
}
fn default_max_block_size() -> u32 {
4_000_000
}
fn default_max_tx_size() -> u32 {
1_000_000
}
fn default_max_script_size() -> u32 {
10_000
}
fn default_max_txs_per_block() -> usize {
10_000
}
fn default_max_locator_hashes() -> usize {
100
}
impl Default for ProtocolValidationConfig {
fn default() -> Self {
Self {
max_block_size: 4_000_000,
max_tx_size: 1_000_000,
max_script_size: 10_000,
max_txs_per_block: 10_000,
max_locator_hashes: 100,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ServiceFlagsConfig {
#[serde(default = "default_true")]
pub node_network: bool,
#[serde(default = "default_true")]
pub node_witness: bool,
#[serde(default = "default_false")]
pub node_compact_filters: bool,
#[serde(default = "default_false")]
pub node_network_limited: bool,
#[serde(default = "default_false")]
pub node_fibre: bool,
#[serde(default = "default_false")]
pub node_dandelion: bool,
#[serde(default = "default_false")]
pub node_package_relay: bool,
#[serde(default = "default_false")]
pub node_utxo_commitments: bool,
#[serde(default = "default_false")]
pub node_ban_list_sharing: bool,
#[cfg(feature = "bip324")]
#[serde(default = "default_false")]
pub node_v2_transport: bool,
#[serde(default = "default_false")]
pub node_governance: bool,
}
fn default_true() -> bool {
true
}
fn default_false() -> bool {
false
}
impl Default for ServiceFlagsConfig {
fn default() -> Self {
Self {
node_network: true,
node_witness: true,
node_compact_filters: false,
node_network_limited: false,
node_fibre: false,
node_dandelion: false,
node_package_relay: false,
node_utxo_commitments: false,
node_ban_list_sharing: false,
node_governance: false,
#[cfg(feature = "bip324")]
node_v2_transport: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProtocolFeaturesConfig {
#[serde(default = "default_true")]
pub segwit: bool,
#[serde(default = "default_true")]
pub taproot: bool,
#[serde(default = "default_true")]
pub rbf: bool,
#[serde(default = "default_false")]
pub ctv: bool,
#[serde(default = "default_true")]
pub compact_blocks: bool,
#[serde(default = "default_false")]
pub compact_filters: bool,
}
impl Default for ProtocolFeaturesConfig {
fn default() -> Self {
Self {
segwit: true,
taproot: true,
rbf: true,
ctv: false,
compact_blocks: true,
compact_filters: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FeeRateConfig {
#[serde(default = "default_min_fee_rate")]
pub min_fee_rate: u64,
#[serde(default = "default_max_fee_rate")]
pub max_fee_rate: u64,
}
fn default_min_fee_rate() -> u64 {
1
}
fn default_max_fee_rate() -> u64 {
1_000_000
}
impl Default for FeeRateConfig {
fn default() -> Self {
Self {
min_fee_rate: 1,
max_fee_rate: 1_000_000,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CompactBlockConfig {
#[serde(default = "default_true")]
pub enabled: bool,
#[serde(default = "default_compact_block_version")]
pub preferred_version: u64,
#[serde(default = "default_max_blocktxn_indices")]
pub max_blocktxn_indices: usize,
}
fn default_compact_block_version() -> u64 {
2
}
fn default_max_blocktxn_indices() -> usize {
10_000
}
impl Default for CompactBlockConfig {
fn default() -> Self {
Self {
enabled: true,
preferred_version: 2,
max_blocktxn_indices: 10_000,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct CommonsExtensionsConfig {
#[serde(default = "default_false")]
pub utxo_commitments: bool,
#[serde(default = "default_false")]
pub filtered_blocks: bool,
#[serde(default = "default_false")]
pub ban_list_sharing: bool,
#[serde(default)]
pub default_filter_preferences: FilterPreferencesConfig,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct FilterPreferencesConfig {
#[serde(default = "default_false")]
pub filter_ordinals: bool,
#[serde(default = "default_false")]
pub filter_dust: bool,
#[serde(default = "default_false")]
pub filter_brc20: bool,
#[serde(default)]
pub min_output_value: u64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProtocolConfig {
#[serde(default = "default_protocol_version")]
pub protocol_version: ProtocolVersion,
#[serde(default)]
pub network_limits: NetworkMessageLimits,
#[serde(default)]
pub validation: ProtocolValidationConfig,
#[serde(default)]
pub service_flags: ServiceFlagsConfig,
#[serde(default)]
pub features: ProtocolFeaturesConfig,
#[serde(default)]
pub fee_rates: FeeRateConfig,
#[serde(default)]
pub compact_blocks: CompactBlockConfig,
#[serde(default)]
pub commons: CommonsExtensionsConfig,
}
fn default_protocol_version() -> ProtocolVersion {
ProtocolVersion::BitcoinV1
}
impl Default for ProtocolConfig {
fn default() -> Self {
Self {
protocol_version: ProtocolVersion::BitcoinV1,
network_limits: NetworkMessageLimits::default(),
validation: ProtocolValidationConfig::default(),
service_flags: ServiceFlagsConfig::default(),
features: ProtocolFeaturesConfig::default(),
fee_rates: FeeRateConfig::default(),
compact_blocks: CompactBlockConfig::default(),
commons: CommonsExtensionsConfig::default(),
}
}
}
impl ProtocolConfig {
pub fn from_env() -> Self {
let mut config = Self::default();
if let Ok(val) = std::env::var("BLVM_PROTOCOL_VERSION") {
config.protocol_version = match val.as_str() {
"Testnet3" | "testnet" => ProtocolVersion::Testnet3,
"Regtest" | "regtest" => ProtocolVersion::Regtest,
_ => ProtocolVersion::BitcoinV1,
};
}
if let Ok(val) = std::env::var("BLVM_MAX_ADDR_ADDRESSES") {
if let Ok(count) = val.parse::<usize>() {
config.network_limits.max_addr_addresses = count;
}
}
if let Ok(val) = std::env::var("BLVM_MAX_INV_ITEMS") {
if let Ok(count) = val.parse::<usize>() {
config.network_limits.max_inv_items = count;
}
}
if let Ok(val) = std::env::var("BLVM_MAX_HEADERS") {
if let Ok(count) = val.parse::<usize>() {
config.network_limits.max_headers = count;
}
}
if let Ok(val) = std::env::var("BLVM_MAX_USER_AGENT_LENGTH") {
if let Ok(length) = val.parse::<usize>() {
config.network_limits.max_user_agent_length = length;
}
}
if let Ok(val) = std::env::var("BLVM_MAX_BLOCK_SIZE") {
if let Ok(size) = val.parse::<u32>() {
config.validation.max_block_size = size;
}
}
if let Ok(val) = std::env::var("BLVM_MAX_TX_SIZE") {
if let Ok(size) = val.parse::<u32>() {
config.validation.max_tx_size = size;
}
}
if let Ok(val) = std::env::var("BLVM_MAX_TXS_PER_BLOCK") {
if let Ok(count) = val.parse::<usize>() {
config.validation.max_txs_per_block = count;
}
}
if let Ok(val) = std::env::var("BLVM_MAX_LOCATOR_HASHES") {
if let Ok(count) = val.parse::<usize>() {
config.validation.max_locator_hashes = count;
}
}
if let Ok(val) = std::env::var("BLVM_SERVICE_FIBRE") {
if let Ok(enabled) = val.parse::<bool>() {
config.service_flags.node_fibre = enabled;
}
}
if let Ok(val) = std::env::var("BLVM_SERVICE_UTXO_COMMITMENTS") {
if let Ok(enabled) = val.parse::<bool>() {
config.service_flags.node_utxo_commitments = enabled;
}
}
if let Ok(val) = std::env::var("BLVM_SERVICE_BAN_LIST_SHARING") {
if let Ok(enabled) = val.parse::<bool>() {
config.service_flags.node_ban_list_sharing = enabled;
}
}
if let Ok(val) = std::env::var("BLVM_FEATURE_COMPACT_BLOCKS") {
if let Ok(enabled) = val.parse::<bool>() {
config.features.compact_blocks = enabled;
}
}
if let Ok(val) = std::env::var("BLVM_UTXO_COMMITMENTS") {
if let Ok(enabled) = val.parse::<bool>() {
config.commons.utxo_commitments = enabled;
}
}
config
}
pub fn get_service_flags(&self) -> u64 {
use crate::service_flags::{commons, set_flag, standard};
let mut flags = 0u64;
if self.service_flags.node_network {
set_flag(&mut flags, standard::NODE_NETWORK);
}
if self.service_flags.node_witness {
set_flag(&mut flags, standard::NODE_WITNESS);
}
if self.service_flags.node_compact_filters {
set_flag(&mut flags, standard::NODE_COMPACT_FILTERS);
}
if self.service_flags.node_network_limited {
set_flag(&mut flags, standard::NODE_NETWORK_LIMITED);
}
if self.service_flags.node_fibre {
set_flag(&mut flags, commons::NODE_FIBRE);
}
if self.service_flags.node_dandelion {
set_flag(&mut flags, commons::NODE_DANDELION);
}
if self.service_flags.node_package_relay {
set_flag(&mut flags, commons::NODE_PACKAGE_RELAY);
}
#[cfg(feature = "utxo-commitments")]
if self.service_flags.node_utxo_commitments {
set_flag(&mut flags, commons::NODE_UTXO_COMMITMENTS);
}
if self.service_flags.node_ban_list_sharing {
set_flag(&mut flags, commons::NODE_BAN_LIST_SHARING);
}
if self.service_flags.node_governance {
set_flag(&mut flags, commons::NODE_GOVERNANCE);
}
flags
}
}