Skip to content

Commit

Permalink
Minimize usage of Network in public API
Browse files Browse the repository at this point in the history
A release or so ago we added `non_exhaustive` to the `Network` enum,
this turned out to make usage of the enum un-ergonomic for downstream
users. After much debate we decided that a way forward was to just
minimize the usage of the enum in the public API by instead use
`AsRef<Params>` so that downstream could define their own network enum
based on the networks they support.

Minimize usage of `Network` by using `AsRef<Params>` as a parameter type
instead. "minimize" because the `Network` still appears in some places.
  • Loading branch information
tcharding committed Apr 3, 2024
1 parent 3ec5eff commit f6467ac
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
7 changes: 5 additions & 2 deletions bitcoin/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use crate::blockdata::constants::{
use crate::blockdata::script::witness_program::WitnessProgram;
use crate::blockdata::script::witness_version::WitnessVersion;
use crate::blockdata::script::{self, Script, ScriptBuf, ScriptHash};
use crate::consensus::Params;
use crate::crypto::key::{
CompressedPublicKey, PubkeyHash, PublicKey, TweakedPublicKey, UntweakedPublicKey,
};
Expand Down Expand Up @@ -505,7 +506,8 @@ impl Address {
pub fn is_spend_standard(&self) -> bool { self.address_type().is_some() }

/// Constructs an [`Address`] from an output script (`scriptPubkey`).
pub fn from_script(script: &Script, network: Network) -> Result<Address, FromScriptError> {
pub fn from_script(script: &Script, params: impl AsRef<Params>) -> Result<Address, FromScriptError> {
let network = params.as_ref().network;
if script.is_p2pkh() {
let bytes = script.as_bytes()[3..23].try_into().expect("statically 20B long");
let hash = PubkeyHash::from_byte_array(bytes);
Expand Down Expand Up @@ -820,6 +822,7 @@ mod tests {
use hex_lit::hex;

use super::*;
use crate::consensus::params;
use crate::network::Network::{Bitcoin, Testnet};

fn roundtrips(addr: &Address, network: Network) {
Expand Down Expand Up @@ -1281,7 +1284,7 @@ mod tests {
assert_eq!(Address::from_script(&bad_p2wpkh, Network::Bitcoin), expected);
assert_eq!(Address::from_script(&bad_p2wsh, Network::Bitcoin), expected);
assert_eq!(
Address::from_script(&invalid_segwitv0_script, Network::Bitcoin),
Address::from_script(&invalid_segwitv0_script, &params::MAINNET),
Err(FromScriptError::WitnessProgram(witness_program::Error::InvalidSegwitV0Length(17)))
);
}
Expand Down
40 changes: 32 additions & 8 deletions bitcoin/src/blockdata/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::blockdata::opcodes::all::*;
use crate::blockdata::script;
use crate::blockdata::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut};
use crate::blockdata::witness::Witness;
use crate::consensus::Params;
use crate::internal_macros::impl_bytes_newtype;
use crate::network::Network;
use crate::pow::CompactTarget;
Expand Down Expand Up @@ -84,11 +85,11 @@ fn bitcoin_genesis_tx() -> Transaction {
}

/// Constructs and returns the genesis block.
pub fn genesis_block(network: Network) -> Block {
pub fn genesis_block(params: impl AsRef<Params>) -> Block {
let txdata = vec![bitcoin_genesis_tx()];
let hash: sha256d::Hash = txdata[0].compute_txid().into();
let merkle_root = hash.into();
match network {
match params.as_ref().network {
Network::Bitcoin => Block {
header: block::Header {
version: block::Version::ONE,
Expand Down Expand Up @@ -169,7 +170,17 @@ impl ChainHash {
///
/// See [BOLT 0](https://github.com/lightning/bolts/blob/ffeece3dab1c52efdb9b53ae476539320fa44938/00-introduction.md#chain_hash)
/// for specification.
pub const fn using_genesis_block(network: Network) -> Self {
pub fn using_genesis_block(params: impl AsRef<Params>) -> Self {
let network = params.as_ref().network;
let hashes = [Self::BITCOIN, Self::TESTNET, Self::SIGNET, Self::REGTEST];
hashes[network as usize]
}

/// Returns the hash of the `network` genesis block for use as a chain hash.
///
/// See [BOLT 0](https://github.com/lightning/bolts/blob/ffeece3dab1c52efdb9b53ae476539320fa44938/00-introduction.md#chain_hash)
/// for specification.
pub const fn using_genesis_block_const(network: Network) -> Self {
let hashes = [Self::BITCOIN, Self::TESTNET, Self::SIGNET, Self::REGTEST];
hashes[network as usize]
}
Expand All @@ -187,6 +198,7 @@ mod test {
use hex::test_hex_unwrap as hex;

use super::*;
use crate::consensus::params;
use crate::consensus::encode::serialize;

#[test]
Expand All @@ -213,9 +225,21 @@ mod test {
);
}

#[test]
fn bitcoin_genesis_block_calling_convention() {
// This is the best.
let _ = genesis_block(&params::MAINNET);
// this works and is ok too.
let _ = genesis_block(&Network::Bitcoin);
let _ = genesis_block(Network::Bitcoin);
// This works too, but is suboptimal because it inlines the const.
let _ = genesis_block(Params::MAINNET);
let _ = genesis_block(&Params::MAINNET);
}

#[test]
fn bitcoin_genesis_full_block() {
let gen = genesis_block(Network::Bitcoin);
let gen = genesis_block(&params::MAINNET);

assert_eq!(gen.header.version, block::Version::ONE);
assert_eq!(gen.header.prev_blockhash, Hash::all_zeros());
Expand All @@ -235,7 +259,7 @@ mod test {

#[test]
fn testnet_genesis_full_block() {
let gen = genesis_block(Network::Testnet);
let gen = genesis_block(&params::TESTNET);
assert_eq!(gen.header.version, block::Version::ONE);
assert_eq!(gen.header.prev_blockhash, Hash::all_zeros());
assert_eq!(
Expand All @@ -253,7 +277,7 @@ mod test {

#[test]
fn signet_genesis_full_block() {
let gen = genesis_block(Network::Signet);
let gen = genesis_block(&params::SIGNET);
assert_eq!(gen.header.version, block::Version::ONE);
assert_eq!(gen.header.prev_blockhash, Hash::all_zeros());
assert_eq!(
Expand All @@ -280,7 +304,7 @@ mod test {
let hash = sha256::Hash::from_slice(genesis_hash.as_byte_array()).unwrap();
let want = format!("{:02x}", hash);

let chain_hash = ChainHash::using_genesis_block(network);
let chain_hash = ChainHash::using_genesis_block_const(network);
let got = format!("{:02x}", chain_hash);

// Compare strings because the spec specifically states how the chain hash must encode to hex.
Expand Down Expand Up @@ -317,7 +341,7 @@ mod test {
// Test vector taken from: https://github.com/lightning/bolts/blob/master/00-introduction.md
#[test]
fn mainnet_chain_hash_test_vector() {
let got = ChainHash::using_genesis_block(Network::Bitcoin).to_string();
let got = ChainHash::using_genesis_block_const(Network::Bitcoin).to_string();
let want = "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000";
assert_eq!(got, want);
}
Expand Down
3 changes: 2 additions & 1 deletion bitcoin/src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ impl OutPoint {
/// # Examples
///
/// ```rust
/// use bitcoin::consensus::params;
/// use bitcoin::constants::genesis_block;
/// use bitcoin::Network;
///
/// let block = genesis_block(Network::Bitcoin);
/// let block = genesis_block(&params::MAINNET);
/// let tx = &block.txdata[0];
///
/// // Coinbase transactions don't have any previous output.
Expand Down
26 changes: 26 additions & 0 deletions bitcoin/src/consensus/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ pub struct Params {
pub no_pow_retargeting: bool,
}

/// The mainnet parameters.
///
/// Use this for a static reference e.g., `&params::MAINNET`.
///
/// For more on static vs const see The Rust Reference [using-statics-or-consts] section.
///
/// [using-statics-or-consts]: <https://doc.rust-lang.org/reference/items/static-items.html#using-statics-or-consts>
pub static MAINNET: Params = Params::MAINNET;
/// The testnet parameters.
pub static TESTNET: Params = Params::TESTNET;
/// The signet parameters.
pub static SIGNET: Params = Params::SIGNET;
/// The regtest parameters.
pub static REGTEST: Params = Params::REGTEST;

impl Params {
/// The mainnet parameters (alias for `Params::MAINNET`).
pub const BITCOIN: Params = Params::MAINNET;
Expand Down Expand Up @@ -163,3 +178,14 @@ impl From<&Network> for &'static Params {
impl AsRef<Params> for Params {
fn as_ref(&self) -> &Params { self }
}

impl AsRef<Params> for Network {
fn as_ref(&self) -> &Params {
match *self {
Network::Bitcoin => &MAINNET,
Network::Testnet => &TESTNET,
Network::Signet => &SIGNET,
Network::Regtest => &REGTEST,
}
}
}
1 change: 1 addition & 0 deletions bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ 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},
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Network {
/// let network = Network::Bitcoin;
/// assert_eq!(network.chain_hash(), ChainHash::BITCOIN);
/// ```
pub fn chain_hash(self) -> ChainHash { ChainHash::using_genesis_block(self) }
pub fn chain_hash(self) -> ChainHash { ChainHash::using_genesis_block_const(self) }

/// Creates a `Network` from the chain hash (genesis block hash).
///
Expand Down

0 comments on commit f6467ac

Please sign in to comment.