Add `params` method to `Network`

Writing `network.params()` is less annoying than `Params::network()`, so
this adds it. Making it return a static could also improve performance.
This commit is contained in:
Martin Habovstiak 2023-11-05 11:16:53 +01:00
parent 4fb91277f0
commit 9a8694fae5
2 changed files with 13 additions and 1 deletions

View File

@ -50,7 +50,7 @@ pub struct Params {
impl Params { impl Params {
/// Creates parameters set for the given network. /// Creates parameters set for the given network.
pub fn new(network: Network) -> Self { pub const fn new(network: Network) -> Self {
match network { match network {
Network::Bitcoin => Params { Network::Bitcoin => Params {
network: Network::Bitcoin, network: Network::Bitcoin,

View File

@ -27,6 +27,7 @@ use internals::write_err;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::consensus::Params;
use crate::constants::ChainHash; use crate::constants::ChainHash;
use crate::p2p::Magic; use crate::p2p::Magic;
use crate::prelude::{String, ToOwned}; use crate::prelude::{String, ToOwned};
@ -144,6 +145,17 @@ impl Network {
pub fn from_chain_hash(chain_hash: ChainHash) -> Option<Network> { pub fn from_chain_hash(chain_hash: ChainHash) -> Option<Network> {
Network::try_from(chain_hash).ok() Network::try_from(chain_hash).ok()
} }
/// Returns the associated network parameters.
pub const fn params(self) -> &'static Params {
const PARAMS: [Params; 4] = [
Params::new(Network::Bitcoin),
Params::new(Network::Testnet),
Params::new(Network::Signet),
Params::new(Network::Regtest),
];
&PARAMS[self as usize]
}
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]