Move p2p::constants::Network to crate root

The `Network` type is not a p2p construct, it is more general, used
throughout the codebase to define _which_ Bitcoin network we are
operating on.
This commit is contained in:
Tobin C. Harding 2023-06-09 16:18:39 +10:00
parent 0f78943ef0
commit d4e8f49fc3
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
14 changed files with 29 additions and 38 deletions

View File

@ -6,7 +6,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use std::{env, process};
use bitcoin::consensus::{encode, Decodable};
use bitcoin::p2p::{self, address, constants, message, message_network};
use bitcoin::p2p::{self, address, message, message_network};
use bitcoin::secp256k1;
use bitcoin::secp256k1::rand::Rng;
@ -29,7 +29,7 @@ fn main() {
let version_message = build_version_message(address);
let first_message =
message::RawNetworkMessage::new(constants::Network::Bitcoin.magic(), version_message);
message::RawNetworkMessage::new(bitcoin::Network::Bitcoin.magic(), version_message);
if let Ok(mut stream) = TcpStream::connect(address) {
// Send the message
@ -47,7 +47,7 @@ fn main() {
println!("Received version message: {:?}", reply.payload());
let second_message = message::RawNetworkMessage::new(
constants::Network::Bitcoin.magic(),
bitcoin::Network::Bitcoin.magic(),
message::NetworkMessage::Verack,
);

View File

@ -46,7 +46,7 @@ use crate::blockdata::script::witness_version::{self, WitnessVersion};
use crate::blockdata::script::{self, Script, ScriptBuf};
use crate::crypto::key::{PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
use crate::hash_types::{PubkeyHash, ScriptHash};
use crate::p2p::constants::Network;
use crate::network::Network;
use crate::prelude::*;
use crate::taproot::TapNodeHash;
@ -996,7 +996,7 @@ mod tests {
use super::*;
use crate::crypto::key::PublicKey;
use crate::p2p::constants::Network::{Bitcoin, Testnet};
use crate::network::Network::{Bitcoin, Testnet};
fn roundtrips(addr: &Address) {
assert_eq!(

View File

@ -23,7 +23,7 @@ use crate::crypto::key::{self, KeyPair, PrivateKey, PublicKey};
use crate::hash_types::XpubIdentifier;
use crate::internal_macros::impl_bytes_newtype;
use crate::io::Write;
use crate::p2p::constants::Network;
use crate::network::Network;
use crate::prelude::*;
/// A chain code
@ -866,7 +866,7 @@ mod tests {
use super::ChildNumber::{Hardened, Normal};
use super::*;
use crate::internal_macros::hex;
use crate::p2p::constants::Network::{self, Bitcoin};
use crate::network::Network::{self, Bitcoin};
#[test]
fn test_parse_derivation_path() {

View File

@ -20,7 +20,7 @@ use crate::blockdata::script;
use crate::blockdata::transaction::{OutPoint, Sequence, Transaction, TxIn, TxOut};
use crate::blockdata::witness::Witness;
use crate::internal_macros::impl_bytes_newtype;
use crate::p2p::constants::Network;
use crate::network::Network;
use crate::pow::CompactTarget;
use crate::Amount;
@ -198,7 +198,7 @@ mod test {
use crate::blockdata::locktime::absolute;
use crate::consensus::encode::serialize;
use crate::internal_macros::hex;
use crate::p2p::constants::Network;
use crate::network::Network;
#[test]
fn bitcoin_genesis_first_transaction() {

View File

@ -68,7 +68,7 @@ impl OutPoint {
///
/// ```rust
/// use bitcoin::constants::genesis_block;
/// use bitcoin::p2p::constants::Network;
/// use bitcoin::Network;
///
/// let block = genesis_block(Network::Bitcoin);
/// let tx = &block.txdata[0];
@ -1508,7 +1508,7 @@ mod tests {
#[test]
fn test_is_coinbase() {
use crate::blockdata::constants;
use crate::p2p::constants::Network;
use crate::network::Network;
let genesis = constants::genesis_block(Network::Bitcoin);
assert!(genesis.txdata[0].is_coinbase());

View File

@ -6,7 +6,7 @@
//! chains (such as mainnet, testnet).
//!
use crate::p2p::constants::Network;
use crate::network::Network;
use crate::pow::Work;
/// Parameters that influence chain consensus.

View File

@ -18,7 +18,7 @@ pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, X
use crate::crypto::ecdsa;
use crate::hash_types::{PubkeyHash, WPubkeyHash};
use crate::p2p::constants::Network;
use crate::network::Network;
use crate::prelude::*;
use crate::taproot::{TapNodeHash, TapTweakHash};
use crate::{base58, io};
@ -741,7 +741,7 @@ mod tests {
use super::*;
use crate::address::Address;
use crate::io;
use crate::p2p::constants::Network::{Bitcoin, Testnet};
use crate::network::Network::{Bitcoin, Testnet};
#[test]
fn test_key_derivation() {

View File

@ -1120,7 +1120,7 @@ mod tests {
use crate::crypto::key::PublicKey;
use crate::crypto::sighash::{LegacySighash, TapSighash};
use crate::internal_macros::hex;
use crate::p2p::constants::Network;
use crate::network::Network;
use crate::taproot::TapLeafHash;
extern crate serde_json;

View File

@ -106,6 +106,7 @@ pub(crate) mod crypto;
pub mod error;
pub mod hash_types;
pub mod merkle_tree;
pub mod network;
pub mod policy;
pub mod pow;
pub mod psbt;
@ -146,7 +147,7 @@ pub use crate::hash_types::{
BlockHash, PubkeyHash, ScriptHash, Txid, WPubkeyHash, WScriptHash, Wtxid,
};
pub use crate::merkle_tree::MerkleBlock;
pub use crate::p2p::constants::Network;
pub use crate::network::Network;
pub use crate::pow::{CompactTarget, Target, Work};
pub use crate::psbt::Psbt;

View File

@ -1,22 +1,15 @@
// SPDX-License-Identifier: CC0-1.0
//! Bitcoin network constants.
//! Bitcoin network.
//!
//! This module provides various constants relating to the Bitcoin network
//! protocol, such as protocol versioning and magic header bytes.
//!
//! The [`Network`][1] type implements the [`Decodable`][2] and
//! [`Encodable`][3] traits and encodes the magic bytes of the given
//! network.
//!
//! [1]: enum.Network.html
//! [2]: ../../consensus/encode/trait.Decodable.html
//! [3]: ../../consensus/encode/trait.Encodable.html
//! The term "network" is overloaded, here [`Network`] refers to the specific
//! Bitcoin network we are operating on e.g., signet, regtest. The terms
//! "network" and "chain" are often used interchangeably for this concept.
//!
//! # Example: encoding a network's magic bytes
//!
//! ```rust
//! use bitcoin::p2p::constants::Network;
//! use bitcoin::Network;
//! use bitcoin::consensus::encode::serialize;
//!
//! let network = Network::Bitcoin;
@ -130,7 +123,7 @@ impl Network {
/// # Examples
///
/// ```rust
/// use bitcoin::p2p::constants::Network;
/// use bitcoin::Network;
/// use bitcoin::blockdata::constants::ChainHash;
///
/// let network = Network::Bitcoin;
@ -143,7 +136,7 @@ impl Network {
/// # Examples
///
/// ```rust
/// use bitcoin::p2p::constants::Network;
/// use bitcoin::Network;
/// use bitcoin::blockdata::constants::ChainHash;
/// use std::convert::TryFrom;
///
@ -387,7 +380,7 @@ mod tests {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(crate = "actual_serde")]
struct T {
#[serde(with = "crate::p2p::constants::as_core_arg")]
#[serde(with = "crate::network::as_core_arg")]
pub network: Network,
}

View File

@ -549,8 +549,8 @@ mod test {
use crate::blockdata::transaction::Transaction;
use crate::consensus::encode::{deserialize, deserialize_partial, serialize};
use crate::internal_macros::hex;
use crate::network::Network;
use crate::p2p::address::{AddrV2, AddrV2Message, Address};
use crate::p2p::constants::Network;
use crate::p2p::message_blockdata::{GetBlocksMessage, GetHeadersMessage, Inventory};
use crate::p2p::message_bloom::{BloomFlags, FilterAdd, FilterLoad};
use crate::p2p::message_compact_blocks::{GetBlockTxn, SendCmpct};

View File

@ -5,8 +5,6 @@
//! This module defines support for (de)serialization and network transport
//! of Bitcoin data and Bitcoin p2p network messages.
pub mod constants;
#[cfg(feature = "std")]
pub mod address;
#[cfg(feature = "std")]
@ -33,9 +31,8 @@ use internals::{debug_from_display, write_err};
use crate::consensus::encode::{self, Decodable, Encodable};
use crate::error::impl_std_error;
use crate::io;
use crate::p2p::constants::Network;
use crate::prelude::{Borrow, BorrowMut, String, ToOwned};
use crate::{io, Network};
/// Version of the protocol as appearing in network message headers.
///

View File

@ -823,7 +823,7 @@ mod tests {
use crate::blockdata::transaction::{OutPoint, Sequence, Transaction, TxIn, TxOut};
use crate::blockdata::witness::Witness;
use crate::internal_macros::hex;
use crate::p2p::constants::Network::Bitcoin;
use crate::network::Network::Bitcoin;
use crate::psbt::map::{Input, Output};
use crate::psbt::raw;
use crate::psbt::serialize::{Deserialize, Serialize};

View File

@ -1,7 +1,7 @@
use bitcoin::address::Address;
use bitcoin::blockdata::script;
use bitcoin::consensus::encode;
use bitcoin::p2p::constants::Network;
use bitcoin::Network;
use honggfuzz::fuzz;
fn do_test(data: &[u8]) {