From 1b932cb27b727088ea6ff4bccbed1d74ac379f49 Mon Sep 17 00:00:00 2001 From: nazeh Date: Wed, 12 Feb 2025 14:41:24 +0300 Subject: [PATCH] feat(testnet): start pubky-testnet crate --- Cargo.lock | 15 +++++++++++-- pubky-testnet/Cargo.toml | 19 ++++++++++++++++ pubky-testnet/README.md | 23 +++++++++++++++++++ pubky-testnet/src/lib.rs | 47 +++++++++++++++++++++++++++++++++++++++ pubky-testnet/src/main.rs | 2 ++ 5 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 pubky-testnet/Cargo.toml create mode 100644 pubky-testnet/README.md create mode 100644 pubky-testnet/src/lib.rs create mode 100644 pubky-testnet/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 4706d45..a7982d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2109,12 +2109,11 @@ dependencies = [ "flume", "futures-lite", "futures-util", - "http-relay 0.1.0", "js-sys", "log", "mainline", "pkarr", - "pubky-common 0.2.0", + "pubky-common 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "pubky-homeserver", "reqwest", "thiserror 2.0.11", @@ -2201,6 +2200,18 @@ dependencies = [ "url", ] +[[package]] +name = "pubky-testnet" +version = "0.1.0" +dependencies = [ + "anyhow", + "mainline", + "pkarr-relay", + "pubky", + "tokio", + "url", +] + [[package]] name = "pubky-timestamp" version = "0.4.0" diff --git a/pubky-testnet/Cargo.toml b/pubky-testnet/Cargo.toml new file mode 100644 index 0000000..aedccf4 --- /dev/null +++ b/pubky-testnet/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "pubky-testnet" +version = "0.1.0" +edition = "2021" +authors = ["Nuh "] +description = "A local test network for Pubky Core development." +license = "MIT" +homepage = "https://github.com/pubky/pubky-core" +repository = "https://github.com/pubky/pubky-core" +keywords = ["pkarr", "pubky", "testnet", "testing"] +categories = ["web-programming", "authentication", "cryptography"] + +[dependencies] +anyhow = "1.0.95" +mainline = "5.2.0" +pkarr-relay = "0.2.0" +pubky = { version = "0.3.0", path = "../pubky" } +tokio = { version = "1.43.0", features = ["full"] } +url = "2.5.4" diff --git a/pubky-testnet/README.md b/pubky-testnet/README.md new file mode 100644 index 0000000..08e1a66 --- /dev/null +++ b/pubky-testnet/README.md @@ -0,0 +1,23 @@ +# Pubky Testnet + +A local test network for developing Pubky Core or applications depending on it. + +All resources are ephemeral, databases are in the operating system's temporaray directories, and all servers are closed as the testnet dropped. + +## Usage + +### Inline testing + +```rust + + +``` + +### Binary (hardcoded testnet, and browser support). + +If you need to run the testnet in a separate process, for example to test Pubky Core in browsers, you need to run this binary, which will create these components with hardcoded configurations: + +1. A local DHT with bootstrapping nodes: `&["localhost:6881"]` +3. A Pkarr Relay running on port [15411](pubky_common::constants::testnet_ports::PKARR_RELAY) +2. A Homeserver with address is hardcoded to `8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo` +6. An HTTP relay running on port [15412](pubky_common::constants::testnet_ports::HTTP_RELAY) diff --git a/pubky-testnet/src/lib.rs b/pubky-testnet/src/lib.rs new file mode 100644 index 0000000..d5f1e12 --- /dev/null +++ b/pubky-testnet/src/lib.rs @@ -0,0 +1,47 @@ +use anyhow::Result; +use url::Url; + +pub struct Testnet { + dht: mainline::Testnet, + relays: Vec, +} + +impl Testnet { + pub async fn run() -> Result { + let dht = mainline::Testnet::new(10)?; + + let mut testnet = Self { + dht, + relays: vec![], + }; + + testnet.run_pkarr_relay().await?; + + Ok(testnet) + } + + // === Getters === + + /// Returns a list of DHT bootstrapping nodes. + pub fn bootstrap(&self) -> &[String] { + &self.dht.bootstrap + } + + /// Returns a list of pkarr relays. + pub fn relays(&self) -> Box<[Url]> { + self.relays.iter().map(|r| r.local_url()).collect() + } + + // === Public Methods === + + // pub fn pubky_client() -> std::result::Result {} + + /// Run a new Pkarr relay. + /// + /// You can access the list of relays at [Self::relays]. + pub async fn run_pkarr_relay(&mut self) -> Result { + let relay = pkarr_relay::Relay::run_test(&self.dht).await?; + + Ok(relay.local_url()) + } +} diff --git a/pubky-testnet/src/main.rs b/pubky-testnet/src/main.rs new file mode 100644 index 0000000..7f755fb --- /dev/null +++ b/pubky-testnet/src/main.rs @@ -0,0 +1,2 @@ +#[tokio::main] +async fn main() {}