Move params to network module

The `Params` struct is currently defined in the `consensus` module which
has become a collection of orthogonal consensus-ish things. We would
like to put things in more descriptive places.

The `Params` struct defines constants that are network specific so it
makes sense to put it in the `network` module. As soft proof of this
argument note in this patch how often the `Params` type is imported
along with the `Network` type.

API break:

The type is no longer available at `bitcoin::consensus::Params` but
rather is re-exported at `bitcoin::network::Params`.
This commit is contained in:
Tobin C. Harding 2024-07-17 07:28:40 +10:00
parent 045a661ebe
commit 54c30556a2
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
10 changed files with 17 additions and 17 deletions

View File

@ -39,7 +39,6 @@ use hashes::{hash160, HashEngine};
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};
use crate::address::script_pubkey::ScriptBufExt as _;
use crate::consensus::Params;
use crate::constants::{
PUBKEY_ADDRESS_PREFIX_MAIN, PUBKEY_ADDRESS_PREFIX_TEST, SCRIPT_ADDRESS_PREFIX_MAIN,
SCRIPT_ADDRESS_PREFIX_TEST,
@ -47,7 +46,7 @@ use crate::constants::{
use crate::crypto::key::{
CompressedPublicKey, PubkeyHash, PublicKey, TweakedPublicKey, UntweakedPublicKey,
};
use crate::network::{Network, NetworkKind};
use crate::network::{Network, NetworkKind, Params};
use crate::prelude::{String, ToOwned};
use crate::script::witness_program::WitnessProgram;
use crate::script::witness_version::WitnessVersion;
@ -892,7 +891,7 @@ mod tests {
use hex_lit::hex;
use super::*;
use crate::consensus::params;
use crate::network::params;
use crate::network::Network::{Bitcoin, Testnet};
fn roundtrips(addr: &Address, network: Network) {

View File

@ -13,9 +13,10 @@ use hashes::{sha256d, HashEngine};
use io::{BufRead, Write};
use super::Weight;
use crate::consensus::{encode, Decodable, Encodable, Params};
use crate::consensus::{encode, Decodable, Encodable};
use crate::internal_macros::{impl_consensus_encoding, impl_hashencode};
use crate::merkle_tree::{MerkleNode as _, TxMerkleNode, WitnessMerkleNode};
use crate::network::Params;
use crate::pow::{CompactTarget, Target, Work};
use crate::prelude::Vec;
use crate::transaction::{Transaction, Wtxid};

View File

@ -10,10 +10,9 @@ use hashes::sha256d;
use internals::impl_array_newtype;
use crate::block::{self, Block};
use crate::consensus::Params;
use crate::internal_macros::impl_array_newtype_stringify;
use crate::locktime::absolute;
use crate::network::Network;
use crate::network::{Network, Params};
use crate::opcodes::all::*;
use crate::pow::CompactTarget;
use crate::transaction::{self, OutPoint, Transaction, TxIn, TxOut};
@ -216,7 +215,7 @@ mod test {
use super::*;
use crate::consensus::encode::serialize;
use crate::consensus::params;
use crate::network::params;
use crate::Txid;
#[test]

View File

@ -118,9 +118,8 @@ impl OutPoint {
/// # Examples
///
/// ```rust
/// use bitcoin::consensus::params;
/// use bitcoin::constants::genesis_block;
/// use bitcoin::Network;
/// use bitcoin::{params, Network};
///
/// let block = genesis_block(&params::MAINNET);
/// let tx = &block.txdata[0];

View File

@ -6,7 +6,6 @@
//! conform to Bitcoin consensus.
pub mod encode;
pub mod params;
#[cfg(feature = "serde")]
pub mod serde;
#[cfg(feature = "bitcoinconsensus")]
@ -23,7 +22,6 @@ use crate::consensus;
#[doc(inline)]
pub use self::{
encode::{deserialize, deserialize_partial, serialize, Decodable, Encodable, ReadExt, WriteExt},
params::Params,
};
#[cfg(feature = "bitcoinconsensus")]

View File

@ -123,12 +123,12 @@ pub use crate::{
blockdata::weight::Weight,
blockdata::witness::{self, Witness},
consensus::encode::VarInt,
consensus::params,
crypto::ecdsa,
crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, CompressedPublicKey, WPubkeyHash, XOnlyPublicKey},
crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag},
merkle_tree::{MerkleBlock, TxMerkleNode, WitnessMerkleNode},
network::{Network, NetworkKind},
network::params::{self, Params},
pow::{CompactTarget, Target, Work},
psbt::Psbt,
sighash::{EcdsaSighashType, TapSighashType},

View File

@ -18,6 +18,8 @@
//! assert_eq!(&bytes[..], &[0xF9, 0xBE, 0xB4, 0xD9]);
//! ```
pub mod params;
use core::fmt;
use core::str::FromStr;
@ -25,11 +27,14 @@ use internals::write_err;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::consensus::Params;
use crate::constants::ChainHash;
use crate::p2p::Magic;
use crate::prelude::{String, ToOwned};
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
pub use self::params::Params;
/// What kind of network we are on.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NetworkKind {

View File

@ -11,7 +11,7 @@
//! custom type that can be used is such places you might want to do the following:
//!
//! ```
//! use bitcoin::consensus::Params;
//! use bitcoin::network::Params;
//! use bitcoin::{p2p, Script, ScriptBuf, Network, Target};
//!
//! const POW_TARGET_SPACING: u64 = 120; // Two minutes.

View File

@ -28,9 +28,8 @@ use internals::{debug_from_display, write_err};
use io::{BufRead, Write};
use crate::consensus::encode::{self, Decodable, Encodable};
use crate::consensus::Params;
use crate::network::{Network, Params};
use crate::prelude::{Borrow, BorrowMut, String, ToOwned};
use crate::Network;
#[rustfmt::skip]
#[doc(inline)]

View File

@ -15,7 +15,7 @@ use units::parse::{self, ParseIntError, PrefixedHexError, UnprefixedHexError};
use crate::block::{BlockHash, Header};
use crate::consensus::encode::{self, Decodable, Encodable};
use crate::consensus::Params;
use crate::network::Params;
/// Implement traits and methods shared by `Target` and `Work`.
macro_rules! do_impl {