Add NetworkKind

Add a new type `NetworkKind` the describes the kind of network we are
on, ether mainnet or one of the test nets (testnet, regtest, signet).

Do not use the type yet.
This commit is contained in:
Tobin C. Harding 2023-11-17 13:21:06 +11:00
parent 3d6151b9e1
commit 6d5ef23e61
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 28 additions and 1 deletions

View File

@ -129,7 +129,7 @@ pub use crate::{
crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, CompressedPublicKey, WPubkeyHash, XOnlyPublicKey}, crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, CompressedPublicKey, WPubkeyHash, XOnlyPublicKey},
crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag}, crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag},
merkle_tree::MerkleBlock, merkle_tree::MerkleBlock,
network::Network, network::{Network, NetworkKind},
pow::{CompactTarget, Target, Work}, pow::{CompactTarget, Target, Work},
psbt::Psbt, psbt::Psbt,
sighash::{EcdsaSighashType, TapSighashType}, sighash::{EcdsaSighashType, TapSighashType},

View File

@ -31,6 +31,33 @@ use crate::constants::ChainHash;
use crate::p2p::Magic; use crate::p2p::Magic;
use crate::prelude::{String, ToOwned}; use crate::prelude::{String, ToOwned};
/// What kind of network we are on.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NetworkKind {
/// The Bitcoin mainnet network.
Main,
/// Some kind of testnet network.
Test,
}
// We explicitly do not provide `is_testnet`, using `!network.is_mainnet()` is less
// ambiguous due to confusion caused by signet/testnet/regtest.
impl NetworkKind {
/// Returns true if this is real mainnet bitcoin.
pub fn is_mainnet(&self) -> bool { *self == NetworkKind::Main }
}
impl From<Network> for NetworkKind {
fn from(n: Network) -> Self {
use Network::*;
match n {
Bitcoin => NetworkKind::Main,
Testnet | Signet | Regtest => NetworkKind::Test,
}
}
}
/// The cryptocurrency network to act on. /// The cryptocurrency network to act on.
#[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Debug)] #[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]