-
Notifications
You must be signed in to change notification settings - Fork 11
refactor(dashcore): extract Network into standalone dash-network crate #679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9bbe8e3
refactor(dashcore): extract Network into standalone dash-network crate
QuantumExplorer 01dc5bc
refactor(dash-network): move `FFINetwork` + extern fn into the new crate
QuantumExplorer 7bd6b6f
drop dashcore ffi feature and update dash-network header generation
ZocoLini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ groups: | |
| - dashcore | ||
| - dashcore_hashes | ||
| - dashcore-private | ||
| - dash-network | ||
|
|
||
| spv: | ||
| - dash-spv | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| [package] | ||
| name = "dash-network" | ||
| version = { workspace = true } | ||
| edition = "2024" | ||
| authors = ["Dash Core Team"] | ||
| description = "The `Network` enum and its pure helpers (magic bytes, activation heights, default P2P ports) extracted from `dashcore` into a minimal-dependency crate so that lightweight consumers can depend on it without pulling in the full protocol library." | ||
| license = "MIT" | ||
| repository = "https://github.com/dashpay/rust-dashcore" | ||
|
|
||
| [lib] | ||
| name = "dash_network" | ||
| path = "src/lib.rs" | ||
|
|
||
| [features] | ||
| default = [] | ||
| # serde::{Serialize, Deserialize} impls for Network. | ||
| serde = ["dep:serde"] | ||
| # bincode::{Encode, Decode} impls for Network. | ||
| bincode = ["dep:bincode", "dep:bincode_derive"] | ||
| # C-ABI `FFINetwork` mirror + `dashcore_network_get_name` extern fn. | ||
| ffi = [] | ||
|
|
||
| [dependencies] | ||
| serde = { version = "1.0.219", default-features = false, features = ["derive"], optional = true } | ||
| bincode = { version = "2.0.1", optional = true } | ||
| bincode_derive = { version = "2.0.1", optional = true } | ||
|
|
||
| [build-dependencies] | ||
| cbindgen = "0.29" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| use std::{env, fs, path::Path}; | ||
|
|
||
| fn main() { | ||
| if std::env::var("CARGO_FEATURE_FFI").is_ok() { | ||
| generate_bindings(); | ||
| } | ||
| } | ||
|
|
||
| fn generate_bindings() { | ||
| let crate_name = env::var("CARGO_PKG_NAME").unwrap(); | ||
| let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
| let out_dir = env::var("OUT_DIR").unwrap(); | ||
|
|
||
| println!("cargo:rerun-if-changed=cbindgen.toml"); | ||
| println!("cargo:rerun-if-changed=src/"); | ||
|
|
||
| let target_dir = Path::new(&out_dir) | ||
| .ancestors() | ||
| .nth(3) // This line moves up to the target/<PROFILE> directory | ||
| .expect("Failed to find target dir"); | ||
|
|
||
| let include_dir = target_dir.join("include").join(&crate_name); | ||
|
|
||
| fs::create_dir_all(&include_dir).unwrap(); | ||
|
|
||
| let output_path = include_dir.join(format!("{}.h", &crate_name)); | ||
|
|
||
| let config_path = Path::new(&crate_dir).join("cbindgen.toml"); | ||
| let config = cbindgen::Config::from_file(&config_path).expect("Failed to read cbindgen.toml"); | ||
|
|
||
| cbindgen::Builder::new() | ||
| .with_crate(&crate_dir) | ||
| .with_config(config) | ||
| .generate() | ||
| .expect("Unable to generate bindings") | ||
| .write_to_file(&output_path); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| //! The Dash [`Network`] enum and its pure helpers, extracted from the main | ||
| //! `dashcore` crate so that lightweight consumers can depend on it without | ||
| //! pulling in the full protocol library. | ||
| //! | ||
| //! This crate carries **no** dependencies beyond its optional `serde` / | ||
| //! `bincode` impls. If you need Network::known_genesis_block_hash — which | ||
| //! returns a `BlockHash` — use the extension trait provided by `dashcore`. | ||
| //! | ||
| //! # Example | ||
| //! | ||
| //! ```rust | ||
| //! use dash_network::Network; | ||
| //! | ||
| //! assert_eq!(Network::Mainnet.magic(), 0xBD6B0CBF); | ||
| //! assert_eq!(Network::from_magic(0xBD6B0CBF), Some(Network::Mainnet)); | ||
| //! assert_eq!("testnet".parse::<Network>().unwrap(), Network::Testnet); | ||
| //! assert_eq!(Network::Mainnet.default_p2p_port(), 9999); | ||
| //! ``` | ||
|
|
||
| use core::fmt; | ||
|
|
||
| #[cfg(feature = "ffi")] | ||
| pub mod ffi; | ||
|
|
||
| #[cfg(feature = "bincode")] | ||
| use bincode_derive::{Decode, Encode}; | ||
|
|
||
| /// The Dash network to act on. | ||
| #[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Debug)] | ||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
| #[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] | ||
| #[cfg_attr(feature = "bincode", derive(Encode, Decode))] | ||
| #[repr(u8)] | ||
| pub enum Network { | ||
| /// Dash mainnet, the production network for real transactions. | ||
| Mainnet, | ||
| /// Dash public test network for protocol-level testing without real funds. | ||
| Testnet, | ||
| /// Dash development network, an isolated environment for feature development and testing. | ||
| Devnet, | ||
| /// Local regression testing network for deterministic, offline testing with instant block generation. | ||
| Regtest, | ||
| } | ||
|
ZocoLini marked this conversation as resolved.
|
||
|
|
||
| impl Network { | ||
| /// Creates a `Network` from the network magic bytes. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// use dash_network::Network; | ||
| /// | ||
| /// assert_eq!(Some(Network::Mainnet), Network::from_magic(0xBD6B0CBF)); | ||
| /// assert_eq!(None, Network::from_magic(0xFFFFFFFF)); | ||
| /// ``` | ||
| pub const fn from_magic(magic: u32) -> Option<Network> { | ||
| // Note: any new entries here must be added to `magic` below. | ||
| match magic { | ||
| 0xBD6B0CBF => Some(Network::Mainnet), | ||
| 0xFFCAE2CE => Some(Network::Testnet), | ||
| 0xCEFFCAE2 => Some(Network::Devnet), | ||
| 0xDCB7C1FC => Some(Network::Regtest), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Return the network magic bytes, which should be encoded little-endian | ||
| /// at the start of every message | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// use dash_network::Network; | ||
| /// | ||
| /// let network = Network::Mainnet; | ||
| /// assert_eq!(network.magic(), 0xBD6B0CBF); | ||
| /// ``` | ||
|
ZocoLini marked this conversation as resolved.
|
||
| pub const fn magic(self) -> u32 { | ||
| // Note: any new entries here must be added to `from_magic` above. | ||
| match self { | ||
| Network::Mainnet => 0xBD6B0CBF, | ||
| Network::Testnet => 0xFFCAE2CE, | ||
| Network::Devnet => 0xCEFFCAE2, | ||
| Network::Regtest => 0xDCB7C1FC, | ||
| } | ||
| } | ||
|
|
||
| /// The block height at which Dash consensus version 20 activates. | ||
| /// | ||
| /// Devnet and regtest activate V20 immediately (height 0). | ||
| pub const fn v20_activation_height(self) -> u32 { | ||
| match self { | ||
| Network::Mainnet => 1_987_776, | ||
| Network::Testnet => 905_100, | ||
| // Devnet and regtest activate V20 immediately. | ||
| Network::Devnet | Network::Regtest => 0, | ||
| } | ||
| } | ||
|
|
||
| /// The default P2P port for this network. | ||
| /// | ||
| /// Regtest's default is the typical Dash Core regtest value; devnets can | ||
| /// vary and should usually come from configuration. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// use dash_network::Network; | ||
| /// | ||
| /// assert_eq!(Network::Mainnet.default_p2p_port(), 9999); | ||
| /// assert_eq!(Network::Testnet.default_p2p_port(), 19999); | ||
| /// ``` | ||
| pub const fn default_p2p_port(self) -> u16 { | ||
| match self { | ||
| Network::Mainnet => 9999, | ||
| Network::Testnet => 19999, | ||
| Network::Devnet => 19799, | ||
| Network::Regtest => 19899, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for Network { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.write_str(match self { | ||
| Network::Mainnet => "mainnet", | ||
| Network::Testnet => "testnet", | ||
| Network::Devnet => "devnet", | ||
| Network::Regtest => "regtest", | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl core::str::FromStr for Network { | ||
| type Err = ParseNetworkError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| match s.to_lowercase().as_str() { | ||
| "mainnet" | "main" => Ok(Network::Mainnet), | ||
| "testnet" | "test" => Ok(Network::Testnet), | ||
| "devnet" | "dev" => Ok(Network::Devnet), | ||
| "regtest" => Ok(Network::Regtest), | ||
| _ => Err(ParseNetworkError(s.to_string())), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Error returned from Network::from_str when the input doesn't name a | ||
| /// known network. | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct ParseNetworkError(pub String); | ||
|
|
||
| impl fmt::Display for ParseNetworkError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "unknown network type: {}", self.0) | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for ParseNetworkError {} | ||
|
|
||
| // ---------- Tests ---------- | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn magic_round_trip_covers_all_variants() { | ||
| for n in [Network::Mainnet, Network::Testnet, Network::Devnet, Network::Regtest] { | ||
| let magic = n.magic(); | ||
| assert_eq!(Network::from_magic(magic), Some(n), "round-trip failed for {:?}", n); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_magic_unknown_is_none() { | ||
| assert_eq!(Network::from_magic(0), None); | ||
| assert_eq!(Network::from_magic(0xFFFFFFFF), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_str_accepts_aliases_and_is_case_insensitive() { | ||
| assert_eq!("MAINNET".parse::<Network>().unwrap(), Network::Mainnet); | ||
| assert_eq!("main".parse::<Network>().unwrap(), Network::Mainnet); | ||
| assert_eq!("Testnet".parse::<Network>().unwrap(), Network::Testnet); | ||
| assert_eq!("test".parse::<Network>().unwrap(), Network::Testnet); | ||
| assert_eq!("devnet".parse::<Network>().unwrap(), Network::Devnet); | ||
| assert_eq!("dev".parse::<Network>().unwrap(), Network::Devnet); | ||
| assert_eq!("regtest".parse::<Network>().unwrap(), Network::Regtest); | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_str_rejects_nonsense() { | ||
| let err = "bogus".parse::<Network>().unwrap_err(); | ||
| assert_eq!(err.0, "bogus"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn display_matches_canonical_lowercase() { | ||
| assert_eq!(Network::Mainnet.to_string(), "mainnet"); | ||
| assert_eq!(Network::Testnet.to_string(), "testnet"); | ||
| assert_eq!(Network::Devnet.to_string(), "devnet"); | ||
| assert_eq!(Network::Regtest.to_string(), "regtest"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn activation_heights_are_stable() { | ||
| assert_eq!(Network::Mainnet.v20_activation_height(), 1_987_776); | ||
| assert_eq!(Network::Testnet.v20_activation_height(), 905_100); | ||
| assert_eq!(Network::Devnet.v20_activation_height(), 0); | ||
| assert_eq!(Network::Regtest.v20_activation_height(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn default_p2p_ports_match_conventions() { | ||
| assert_eq!(Network::Mainnet.default_p2p_port(), 9999); | ||
| assert_eq!(Network::Testnet.default_p2p_port(), 19999); | ||
| } | ||
|
ZocoLini marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.