floresta_chain/pruned_utreexo/chainparams.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
//! This module provides configuration and parameters for different Bitcoin networks (mainnet,
//! testnet, signet, and regtest).
//!
//! It includes:
//! - Network-specific parameters like block reward halving intervals and maturity periods
//! - DNS seeds for peer discovery
//! - Assumable validation states for Utreexo
//! - Block verification flag exceptions
//!
//! The main struct [`ChainParams`] encapsulates all chain-specific parameters while
//! [`DnsSeed`] handles peer discovery through DNS.
extern crate alloc;
use alloc::vec::Vec;
use core::ffi::c_uint;
use bitcoin::blockdata::constants::genesis_block;
use bitcoin::p2p::ServiceFlags;
use bitcoin::params::Params;
use bitcoin::Block;
use bitcoin::BlockHash;
use bitcoin::Network;
use floresta_common::acchashes;
use floresta_common::bhash;
use rustreexo::accumulator::node_hash::BitcoinNodeHash;
use crate::prelude::*;
use crate::AssumeValidArg;
use crate::BlockchainError;
#[derive(Clone, Debug)]
/// This struct encapsulates all chain-specific parameters.
pub struct ChainParams {
/// Field to store parameters related to the chain consensus.
pub params: Params,
/// The network's first block, also called genesis block.
pub genesis: Block,
/// Interval of blocks until the block reward halves
pub subsidy_halving_interval: u64,
/// When we retarget we expect this many seconds to be elapsed since last time. If
/// it's more, we decrease difficulty, if it's less we increase difficulty
pub pow_target_timespan: u64,
/// We wait this many blocks before a coinbase output can be spent
pub coinbase_maturity: u32,
/// The height at which segwit is activated
pub segwit_activation_height: u32,
/// The height at which csv(CHECK_SEQUENCE_VERIFY) is activated
pub csv_activation_height: u32,
/// A list of exceptions to the rules, where the key is the block hash and the value is the
/// verification flags
pub exceptions: HashMap<BlockHash, c_uint>,
/// The network this chain params is for
pub network: Network,
/// Whether we should enforce BIP-094 "Testnet 4" rules
pub enforce_bip94: bool,
}
/// A dns seed is a authoritative DNS server that returns the IP addresses of nodes that are
/// likely to be accepting incoming connections. This is our preferred way of finding new peers
/// on the first startup, as peers returned by seeds are likely to be online and accepting
/// connections. We may use this as a fallback if we don't have any peers to connect in
/// subsequent startups.
///
/// Some seeds allow filtering by service flags, so we may use this to find peers that are
/// likely to be running Utreexo, for example.
pub struct DnsSeed {
/// The network this peer supports (e.g, mainnet, testnet, etc)
pub network: Network,
/// The domain name of the seed
pub seed: &'static str,
/// Useful filters we can use to find relevant peers
pub filters: ServiceFlags,
}
/// This functionality is used to create a new DNS seed with possible filters.
impl DnsSeed {
/// Create a new DNS seed
pub fn new(network: Network, seed: &'static str, filters: ServiceFlags) -> Self {
DnsSeed {
network,
seed,
filters,
}
}
}
/// If enabled, the node will assume that the provided Utreexo state is valid, and will
/// start running from there. You may use this to make your node start faster, but you
/// should be sure that the provided state is valid. You may or not verify the state,
/// by downloading all blocks on background, and then verifying the final Utreexo state.
#[derive(Debug, Clone)]
pub struct AssumeUtreexoValue {
/// The latest block assumed to be valid. This acc is the roots at this block
pub block_hash: BlockHash,
/// Same as block_hash, but in height
pub height: u32,
/// The roots of the Utreexo accumulator at this block
pub roots: Vec<BitcoinNodeHash>,
/// The number of leaves in the Utreexo accumulator at this block
pub leaves: u64,
}
impl ChainParams {
/// This method is called when Assume Utreexo is set to true. It means that the user will accept the hardcoded utreexo state for the specified block, if it is found in the best chain. We can then sync rapidly from this state.
pub fn get_assume_utreexo(network: Network) -> Result<AssumeUtreexoValue, BlockchainError> {
let genesis = genesis_block(Params::new(network));
match network {
Network::Bitcoin => Ok(AssumeUtreexoValue {
block_hash: bhash!(
"0000000000000000000239f2b7f982df299193bdd693f499e6b893d8276ab7ce"
),
height: 902967,
roots: acchashes![
"bd53eef66849c9d3ca13b62ce694030ac4d4b484c6f490f473b9868a7c5df2e8",
"993ffb1782db628c18c75a5edb58d4d506167d85ca52273e108f35b73bb5b640",
"36d8c4ba5176c816bdae7c4119d9f2ea26a1f743f5e6e626102f66a835eaac6d",
"4c93092c1ecd843d2b439365609e7f616fe681de921a46642951cb90873ba6ce",
"9b4435987e18e1fe4efcb6874bba5cdc66c3e3c68229f54624cb6343787488a4",
"ab1e87c4066bf195fa7b8357874b82de4fa09ddba921499d6fc73aa133200505",
"8f8215e284dbce604988755ba3c764dbfa024ae0d9659cd67b24742f46360849",
"09b5057a8d6e1f61e93baf474220f581bd1a38d8a378dacb5f7fdec532f21e00",
"a331072d7015c8d33a5c17391264a72a7ca1c07e1f5510797064fced7fbe591d",
"c1c647289156980996d9ea46377e8c1b7e5c05940730ef8c25c0d081341221b5",
"330115a495ed14140cd785d44418d84b872480d293972abd66e3325fdc78ac93",
"b1d7a488e1197908efb2091a3b750508cb2fc495d2011bf2c34c5ae2d40bd2a5",
"3b3b2e51ad96e1ae8ce468c7947b8aa2b41ecb400a32edec3dbcfe5ddb9aca50",
"9d852775775f4c1e4a150404776a6b22569a0fe31f2e669fd3b31a0f70072800",
"8e5f6a92169ad67b3f2682f230e2a62fc849b0a47bc36af8ce6cae24a5343126",
"6dbd2925f8aa0745ac34fc9240ce2a7ef86953fc305c6570ef580a0763072bbe",
"8121c38dcb37684c6d50175f5fd2695af3b12ce0263d20eb7cc503b96f7dba0d",
"f5d8b30dd2038e1b3a5ced7a30c961e230270020c336fb649d0a9e169f11b876",
"0466bd4eb9e7be5b8870e97d2a66377525391c16f15dbcc3833853c8d3bae51e",
"976184c55f74cbb780938a20e2a5df2791cf51e712f68a400a6b024c77ad78e4",
]
.to_vec(),
leaves: 2860457445,
}),
Network::Testnet => Ok(AssumeUtreexoValue {
block_hash: genesis.block_hash(),
height: 0,
leaves: 0,
roots: Vec::new(),
}),
Network::Testnet4 => Ok(AssumeUtreexoValue {
block_hash: genesis.block_hash(),
height: 0,
leaves: 0,
roots: Vec::new(),
}),
Network::Signet => Ok(AssumeUtreexoValue {
block_hash: genesis.block_hash(),
height: 0,
leaves: 0,
roots: Vec::new(),
}),
Network::Regtest => Ok(AssumeUtreexoValue {
block_hash: genesis.block_hash(),
height: 0,
leaves: 0,
roots: Vec::new(),
}),
network => Err(BlockchainError::UnsupportedNetwork(network)),
}
}
/// This method is used to assume all the scripts up to a specific block in the chain as valid. It can be None (we will verify all the scripts), user input or hardcoded.
pub fn get_assume_valid(
network: Network,
arg: AssumeValidArg,
) -> Result<Option<BlockHash>, BlockchainError> {
match arg {
AssumeValidArg::Disabled => Ok(None),
AssumeValidArg::UserInput(hash) => Ok(Some(hash)),
AssumeValidArg::Hardcoded => match network {
Network::Bitcoin => Ok(Some(bhash!(
"00000000000000000001ff36aef3a0454cf48887edefa3aab1f91c6e67fee294"
))),
Network::Testnet => Ok(Some(bhash!(
"000000007df22db38949c61ceb3d893b26db65e8341611150e7d0a9cd46be927"
))),
Network::Testnet4 => Ok(Some(bhash!(
"0000000000335c2895f02ebc75773d2ca86095325becb51773ce5151e9bcf4e0"
))),
Network::Signet => Ok(Some(bhash!(
"000000084ece77f20a0b6a7dda9163f4527fd96d59f7941fb8452b3cec855c2e"
))),
Network::Regtest => Ok(Some(bhash!(
"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
))),
network => Err(BlockchainError::UnsupportedNetwork(network)),
},
}
}
}
#[cfg(feature = "bitcoinconsensus")]
/// We use an inverse logic to pick validation flags.
/// When we call verify_script we need to tell what to validate (taproot, segwit, CSV, P2SH...).
/// Although those features were added later in the protocol, their exact template would rarely appear in a transaction.
/// There's almost no transactions in the chain that "looks like segwit but are not segwit".
/// We pretend segwit was enabled since genesis, and only skip this for blocks that have such transactions using hardcoded values.
fn get_exceptions() -> HashMap<BlockHash, c_uint> {
use bitcoinconsensus::VERIFY_NONE;
use bitcoinconsensus::VERIFY_P2SH;
use bitcoinconsensus::VERIFY_WITNESS;
let mut exceptions = HashMap::new();
exceptions.insert(
bhash!("00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22"),
VERIFY_NONE,
); // BIP16 exception on main net
exceptions.insert(
bhash!("0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad"),
VERIFY_P2SH | VERIFY_WITNESS,
); // Taproot exception on main net
exceptions.insert(
bhash!("00000000dd30457c001f4095d208cc1296b0eed002427aa599874af7a432b105"),
VERIFY_NONE,
); // BIP16 exception on test net
exceptions
}
#[cfg(not(feature = "bitcoinconsensus"))]
fn get_exceptions() -> HashMap<BlockHash, c_uint> {
HashMap::new()
}
impl AsRef<Params> for ChainParams {
fn as_ref(&self) -> &Params {
&self.params
}
}
impl TryFrom<Network> for ChainParams {
type Error = BlockchainError;
fn try_from(network: Network) -> Result<Self, Self::Error> {
let genesis = genesis_block(Params::new(network));
let exceptions = get_exceptions();
match network {
Network::Bitcoin => Ok(ChainParams {
params: Params::new(network),
network,
genesis,
pow_target_timespan: 14 * 24 * 60 * 60, // two weeks
subsidy_halving_interval: 210_000,
coinbase_maturity: 100,
segwit_activation_height: 481_824,
csv_activation_height: 419_328,
exceptions,
enforce_bip94: false,
}),
Network::Testnet => Ok(ChainParams {
params: Params::new(network),
network,
genesis,
pow_target_timespan: 14 * 24 * 60 * 60, // two weeks
subsidy_halving_interval: 210_000,
coinbase_maturity: 100,
segwit_activation_height: 834_624,
csv_activation_height: 770_112,
exceptions,
enforce_bip94: false,
}),
Network::Testnet4 => Ok(ChainParams {
params: Params::new(network),
network,
genesis,
pow_target_timespan: 14 * 24 * 60 * 60,
subsidy_halving_interval: 210_000,
coinbase_maturity: 100,
segwit_activation_height: 1,
csv_activation_height: 1,
exceptions,
enforce_bip94: true,
}),
Network::Signet => Ok(ChainParams {
params: Params::new(network),
network,
genesis,
pow_target_timespan: 14 * 24 * 60 * 60, // two weeks
subsidy_halving_interval: 210_000,
coinbase_maturity: 100,
csv_activation_height: 1,
segwit_activation_height: 1,
exceptions,
enforce_bip94: false,
}),
Network::Regtest => Ok(ChainParams {
params: Params::new(network),
network,
genesis,
pow_target_timespan: 14 * 24 * 60 * 60, // two weeks
subsidy_halving_interval: 150,
coinbase_maturity: 100,
csv_activation_height: 0,
segwit_activation_height: 0,
exceptions,
enforce_bip94: false,
}),
network => Err(BlockchainError::UnsupportedNetwork(network)),
}
}
}
/// This function returns the DNS seeds for the given network.
///
/// DNS seeds taken from Bitcoin Core at commit 382b692a503355df7347efd9c128aff465b5583e
///
/// Some dns seeds lets us filter the returned peers by advertised services. We are interested
/// in peers with: UTREEXO, COMPACT_FILTERS, WITNESS and NETWORK. Not all seeds supports all
/// bits, so from this list, we pick the ones they support, and ask for this.
pub fn get_chain_dns_seeds(network: Network) -> Result<Vec<DnsSeed>, BlockchainError> {
let mut seeds = Vec::new();
// x9 or 0x09 means NETWORK + WITNESS
let x9: ServiceFlags = ServiceFlags::from(0x9);
// this mean NETWORK + WITNESS + COMPACT_FILTERS
let x49 = ServiceFlags::from(0x49);
// this mean NETWORK + WITNESS + COMPACT_FILTERS + UTREEXO
let x1000049 = ServiceFlags::from(0x1000049);
// this means NETWORK + WITNESS + UTREEXO
let x1000009 = ServiceFlags::from(0x1000009);
// filters aren't supported (usually returns a static list of peers)
let none = ServiceFlags::NONE;
match network {
Network::Bitcoin => {
seeds.push(DnsSeed::new(
Network::Bitcoin,
"seed.calvinkim.info",
x1000009,
));
seeds.push(DnsSeed::new(
Network::Bitcoin,
"seed.bitcoin.luisschwab.com",
x1000049,
));
seeds.push(DnsSeed::new(
Network::Bitcoin,
"seed.bitcoin.sipa.be",
x9, // no COMPACT_FILTERS
));
seeds.push(DnsSeed::new(Network::Bitcoin, "dnsseed.bluematt.me", x49));
seeds.push(DnsSeed::new(Network::Bitcoin, "seed.bitcoinstats.com", x49));
seeds.push(DnsSeed::new(
Network::Bitcoin,
"seed.btc.petertodd.org",
x49,
));
seeds.push(DnsSeed::new(
Network::Bitcoin,
"seed.bitcoin.sprovoost.nl",
x49,
));
seeds.push(DnsSeed::new(Network::Bitcoin, "dnsseed.emzy.de", x49));
seeds.push(DnsSeed::new(Network::Bitcoin, "seed.bitcoin.wiz.biz", x49));
}
Network::Testnet => {
seeds.push(DnsSeed::new(
Network::Testnet,
"testnet-seed.bitcoin.jonasschnelli.ch",
x49,
));
seeds.push(DnsSeed::new(
Network::Testnet,
"seed.tbtc.petertodd.org",
x49,
));
seeds.push(DnsSeed::new(
Network::Testnet,
"seed.testnet.bitcoin.sprovoost.nl",
x49,
));
seeds.push(DnsSeed::new(
Network::Testnet,
"testnet-seed.bluematt.me",
none,
));
}
Network::Testnet4 => {
seeds.push(DnsSeed::new(
Network::Testnet4,
"seed.testnet4.bitcoin.sprovoost.nl",
none,
));
seeds.push(DnsSeed::new(
Network::Testnet4,
"seed.testnet4.wiz.biz",
none,
));
}
Network::Signet => {
seeds.push(DnsSeed::new(
Network::Signet,
"seed.signet.bitcoin.sprovoost.nl",
x49,
));
seeds.push(DnsSeed::new(Network::Signet, "seed.dlsouza.lol", x1000049));
}
Network::Regtest => {
// No seeds for regtest
}
network => return Err(BlockchainError::UnsupportedNetwork(network)),
};
Ok(seeds)
}