From fcf3086f1e94a175508292c0e006ecb4d2f16d6e Mon Sep 17 00:00:00 2001 From: nazeh Date: Wed, 12 Feb 2025 21:14:17 +0300 Subject: [PATCH] feat(testnet): add testnet binary --- .gitignore | 1 - Cargo.lock | 114 +++++++++++++++++---------------- examples/authz/README.md | 2 +- pubky-homeserver/src/io/mod.rs | 14 ++++ pubky-testnet/Cargo.toml | 5 ++ pubky-testnet/README.md | 14 ++++ pubky-testnet/src/lib.rs | 72 ++++++++++++++++++++- pubky-testnet/src/main.rs | 18 +++++- pubky/pkg/package.json | 2 +- 9 files changed, 180 insertions(+), 62 deletions(-) diff --git a/.gitignore b/.gitignore index e8e644d..af03ac7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ target/ -config.toml storage/ diff --git a/Cargo.lock b/Cargo.lock index 3d6a741..812931e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -376,9 +376,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.16.0" +version = "3.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" [[package]] name = "byteorder" @@ -394,9 +394,9 @@ checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9" [[package]] name = "cc" -version = "1.2.9" +version = "1.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8293772165d9345bdaaa39b45b2109591e63fe5e6fbc23c6ff930a048aa310b" +checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda" dependencies = [ "shlex", ] @@ -556,9 +556,9 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "cpufeatures" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16b80225097f2e5ae4e7179dd2266824648f3e2f49d9134d584b76389d31c4c3" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" dependencies = [ "libc", ] @@ -1263,9 +1263,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.9.5" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" +checksum = "f2d708df4e7140240a16cd6ab0ab65c972d7433ab77819ea693fde9c43811e2a" [[package]] name = "httpdate" @@ -1488,9 +1488,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.7.0" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" +checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" dependencies = [ "equivalent", "hashbrown 0.15.2", @@ -1507,9 +1507,9 @@ dependencies = [ [[package]] name = "ipnet" -version = "2.10.1" +version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" +checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" [[package]] name = "is_terminal_polyfill" @@ -1670,9 +1670,9 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" [[package]] name = "miniz_oxide" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924" +checksum = "b3b1c9bd4fe1f0f8b387f6eb9eb3b4a1aa26185e5750efb9140301703f62cd1b" dependencies = [ "adler2", ] @@ -1699,9 +1699,9 @@ dependencies = [ [[package]] name = "native-tls" -version = "0.2.12" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" +checksum = "0dab59f8e050d5df8e4dd87d9206fb6f65a483e20ac9fda365ade4fab353196c" dependencies = [ "libc", "log", @@ -1771,9 +1771,9 @@ checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" [[package]] name = "openssl" -version = "0.10.68" +version = "0.10.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5" +checksum = "61cfb4e166a8bb8c9b55c500bc2308550148ece889be90f609377e58140f42c6" dependencies = [ "bitflags", "cfg-if", @@ -1797,15 +1797,15 @@ dependencies = [ [[package]] name = "openssl-probe" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" [[package]] name = "openssl-sys" -version = "0.9.104" +version = "0.9.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741" +checksum = "8b22d5b84be05a8d6947c7cb71f7c849aa0f112acd4bf51c2a7c1c988ac0a9dc" dependencies = [ "cc", "libc", @@ -1919,18 +1919,18 @@ dependencies = [ [[package]] name = "pin-project" -version = "1.1.8" +version = "1.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e2ec53ad785f4d35dac0adea7f7dc6f1bb277ad84a680c7afefeae05d1f5916" +checksum = "dfe2e71e1471fe07709406bf725f710b02927c9c54b2b5b2ec0e8087d97c327d" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.8" +version = "1.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d56a66c0c55993aa927429d0f8a0abfd74f084e4d9c192cffed01e418d83eefb" +checksum = "f6e859e6e5bd50440ab63c47e3ebabc90f26251f7c73c3d3e837b74a1cc3fa67" dependencies = [ "proc-macro2", "quote", @@ -2107,6 +2107,8 @@ dependencies = [ "flume", "futures-lite", "futures-util", + "getrandom 0.2.15", + "getrandom 0.3.1", "js-sys", "log", "pkarr", @@ -2204,8 +2206,10 @@ dependencies = [ "mainline", "pkarr-relay", "pubky", + "pubky-common 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "pubky-homeserver", "tokio", + "tracing-subscriber", "url", ] @@ -2539,9 +2543,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustc-hash" -version = "2.1.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" [[package]] name = "rustc_version" @@ -2554,9 +2558,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.43" +version = "0.38.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a78891ee6bf2340288408954ac787aa063d8e8817e9f53abb37c695c6d834ef6" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" dependencies = [ "bitflags", "errno", @@ -2567,9 +2571,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.21" +version = "0.23.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f287924602bf649d949c63dc8ac8b235fa5387d394020705b80c4eb597ce5b8" +checksum = "47796c98c480fce5406ef69d1c76378375492c3b0a0de587be0c1d9feb12f395" dependencies = [ "once_cell", "ring", @@ -2590,9 +2594,9 @@ dependencies = [ [[package]] name = "rustls-pki-types" -version = "1.10.1" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2bf47e6ff922db3825eb750c4e2ff784c6ff8fb9e13046ef6a1d1c5401b0b37" +checksum = "917ce264624a4b4db1c364dcc35bfca9ded014d0a958cd47ad3e960e988ea51c" dependencies = [ "web-time", ] @@ -2616,9 +2620,9 @@ checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" [[package]] name = "ryu" -version = "1.0.18" +version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd" [[package]] name = "salsa20" @@ -2681,9 +2685,9 @@ checksum = "c2fdfc24bc566f839a2da4c4295b82db7d25a24253867d5c64355abb5799bdbe" [[package]] name = "semver" -version = "1.0.24" +version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cb6eb87a131f756572d7fb904f6e7b68633f09cca868c5df1c4b8d1a694bbba" +checksum = "f79dfe2d285b0488816f30e700a7438c5a73d816b5b7d3ac72fbc48b0d185e03" [[package]] name = "serde" @@ -2726,9 +2730,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.135" +version = "1.0.138" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9" +checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949" dependencies = [ "itoa", "memchr", @@ -2916,9 +2920,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.96" +version = "2.0.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" +checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" dependencies = [ "proc-macro2", "quote", @@ -2977,13 +2981,13 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.15.0" +version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a8a559c81686f576e8cd0290cd2a24a2a9ad80c98b3478856500fcbd7acd704" +checksum = "38c246215d7d24f48ae091a2902398798e05d978b24315d6efbc00ede9a8bb91" dependencies = [ "cfg-if", "fastrand", - "getrandom 0.2.15", + "getrandom 0.3.1", "once_cell", "rustix", "windows-sys 0.59.0", @@ -3180,9 +3184,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.23" +version = "0.22.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02a8b472d1a3d7c18e2d61a489aee3453fd9031c33e4f55bd533f4a7adca1bee" +checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474" dependencies = [ "indexmap", "serde", @@ -3358,9 +3362,9 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "unicode-ident" -version = "1.0.14" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" [[package]] name = "universal-hash" @@ -3409,9 +3413,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "valuable" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" [[package]] name = "vcpkg" @@ -3542,9 +3546,9 @@ dependencies = [ [[package]] name = "webpki-roots" -version = "0.26.7" +version = "0.26.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d642ff16b7e79272ae451b7322067cdc17cadf68c23264be9d94a32319efe7e" +checksum = "2210b291f7ea53617fbafcc4939f10914214ec15aace5ba62293a668f322c5c9" dependencies = [ "rustls-pki-types", ] @@ -3760,9 +3764,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86e376c75f4f43f44db463cf729e0d3acbf954d13e22c51e26e4c264b4ab545f" +checksum = "59690dea168f2198d1a3b0cac23b8063efcd11012f10ae4698f284808c8ef603" dependencies = [ "memchr", ] diff --git a/examples/authz/README.md b/examples/authz/README.md index 86adf64..e73a751 100644 --- a/examples/authz/README.md +++ b/examples/authz/README.md @@ -12,7 +12,7 @@ It consists of 2 parts: First you need to be running a local testnet Homeserver, in the root of this repo run ```bash -cargo run --bin pubky_homeserver -- --testnet +cargo run --bin pubky_testnet ``` Run the frontend of the 3rd party app diff --git a/pubky-homeserver/src/io/mod.rs b/pubky-homeserver/src/io/mod.rs index 46bead3..4f45128 100644 --- a/pubky-homeserver/src/io/mod.rs +++ b/pubky-homeserver/src/io/mod.rs @@ -36,6 +36,20 @@ impl HomeserverBuilder { self } + /// Configure the DHT bootstrapping nodes that this Homeserver is connected to. + pub fn bootstrap(&mut self, bootstrap: &[String]) -> &mut Self { + self.0.io.bootstrap = Some(bootstrap.to_vec()); + + self + } + + /// Configure Pkarr relays used by this Homeserver + pub fn relays(&mut self, _relays: &[url::Url]) -> &mut Self { + // TODO: make it not a noop if we are going to support relays in homeservers. + + self + } + /// Run a Homeserver /// /// # Safety diff --git a/pubky-testnet/Cargo.toml b/pubky-testnet/Cargo.toml index a65565b..d53e486 100644 --- a/pubky-testnet/Cargo.toml +++ b/pubky-testnet/Cargo.toml @@ -16,6 +16,11 @@ http-relay = "0.1.0" mainline = "5.2.0" pkarr-relay = "0.2.0" pubky = { version = "0.3.0", path = "../pubky" } +pubky-common = "0.2.0" pubky-homeserver = { version = "0.1.0", path = "../pubky-homeserver" } tokio = { version = "1.43.0", features = ["full"] } +tracing-subscriber = "0.3.19" url = "2.5.4" + +[dev-dependencies] +tracing-subscriber = "0.3.19" diff --git a/pubky-testnet/README.md b/pubky-testnet/README.md index cb39841..7f17efe 100644 --- a/pubky-testnet/README.md +++ b/pubky-testnet/README.md @@ -9,8 +9,22 @@ All resources are ephemeral, databases are in the operating system's temporaray ### Inline testing ```rust +use pubky_testnet::Testnet; +#[tokio::main] +async fn main () { + // Run a new testnet. + let testnet = Testnet::run().await.unwrap(); + // Optionally create and run a Homeserver. + let server = testnet.run_homeserver().await.unwrap(); + + // Optionally create and run an HTTP Relay. + let http_relay = testnet.run_http_relay().await.unwrap(); + + // Create a Pubky Client from the testnet. + let client = testnet.client_builder().build().unwrap(); +} ``` ### Binary (hardcoded testnet, and browser support). diff --git a/pubky-testnet/src/lib.rs b/pubky-testnet/src/lib.rs index d4fdcb5..df1dd07 100644 --- a/pubky-testnet/src/lib.rs +++ b/pubky-testnet/src/lib.rs @@ -1,11 +1,15 @@ +use std::time::Duration; + use anyhow::Result; use http_relay::HttpRelay; -use pubky::ClientBuilder; +use pubky::{ClientBuilder, Keypair}; +use pubky_common::timestamp::Timestamp; use pubky_homeserver::Homeserver; use url::Url; pub struct Testnet { dht: mainline::Testnet, + bootstrap: Vec, relays: Vec, } @@ -14,6 +18,7 @@ impl Testnet { let dht = mainline::Testnet::new(10)?; let mut testnet = Self { + bootstrap: dht.bootstrap.clone(), dht, relays: vec![], }; @@ -23,11 +28,72 @@ impl Testnet { Ok(testnet) } + /// 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` + /// 4. An HTTP relay running on port [15412](pubky_common::constants::testnet_ports::HTTP_RELAY) + pub async fn run_with_hardcoded_configurations() -> Result { + let dht = mainline::Testnet::new(10)?; + + let node = mainline::Dht::builder() + .server_mode() + .bootstrap(&dht.bootstrap) + .build()?; + + let bootstrap = vec![node.info().local_addr().to_string()]; + + let storage = std::env::temp_dir().join(Timestamp::now().to_string()); + + // TODO: add a builder for pkarr relay for consistency? + let relay = { + let mut config = pkarr_relay::Config { + http_port: 15411, + cache_path: Some(storage.join("pkarr-relay")), + rate_limiter: None, + ..Default::default() + }; + + config + .pkarr + .request_timeout(Duration::from_millis(100)) + .bootstrap(&bootstrap) + .dht(|builder| builder.server_mode()); + + unsafe { pkarr_relay::Relay::run(config).await? } + }; + + { + let mut builder = Homeserver::builder(); + + builder + .keypair(Keypair::from_secret_key(&[0; 32])) + .storage(storage) + .bootstrap(&bootstrap) + .relays(&[]); + + unsafe { builder.run().await? } + }; + + { + HttpRelay::builder().http_port(15412).run().await? + }; + + let testnet = Self { + dht, + bootstrap, + relays: vec![relay], + }; + + Ok(testnet) + } + // === Getters === /// Returns a list of DHT bootstrapping nodes. pub fn bootstrap(&self) -> &[String] { - &self.dht.bootstrap + &self.bootstrap } /// Returns a list of pkarr relays. @@ -44,7 +110,7 @@ impl Testnet { /// Run an HTTP Relay pub async fn run_http_relay(&self) -> Result { - HttpRelay::builder().build().await + HttpRelay::builder().run().await } /// Create a [ClientBuilder] and configure it to use this local test network. diff --git a/pubky-testnet/src/main.rs b/pubky-testnet/src/main.rs index 7f755fb..522e0d3 100644 --- a/pubky-testnet/src/main.rs +++ b/pubky-testnet/src/main.rs @@ -1,2 +1,18 @@ +use anyhow::Result; +use pubky_testnet::Testnet; + #[tokio::main] -async fn main() {} +async fn main() -> Result<()> { + tracing_subscriber::fmt() + .with_env_filter( + "pubky_homeserver=debug,http_relay=debug,pkarr_relay=debug,tower_http=debug" + .to_string(), + ) + .init(); + + Testnet::run_with_hardcoded_configurations().await?; + + tokio::signal::ctrl_c().await?; + + Ok(()) +} diff --git a/pubky/pkg/package.json b/pubky/pkg/package.json index 54f6f82..d29d46a 100644 --- a/pubky/pkg/package.json +++ b/pubky/pkg/package.json @@ -9,7 +9,7 @@ "url": "git+https://github.com/pubky/pubky-core.git" }, "scripts": { - "testnet": "cargo run -p pubky-homeserver -- --testnet", + "testnet": "cargo run -p pubky-testnet", "test": "npm run test-nodejs && npm run test-browser", "test-nodejs": "tape test/*.js -cov", "test-browser": "browserify test/*.js -p esmify | npx tape-run",