feat(testnet): start pubky-testnet crate

This commit is contained in:
nazeh
2025-02-12 14:41:24 +03:00
parent f75d4ca158
commit 1b932cb27b
5 changed files with 104 additions and 2 deletions

15
Cargo.lock generated
View File

@@ -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"

19
pubky-testnet/Cargo.toml Normal file
View File

@@ -0,0 +1,19 @@
[package]
name = "pubky-testnet"
version = "0.1.0"
edition = "2021"
authors = ["Nuh <nuh@nuh.dev>"]
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"

23
pubky-testnet/README.md Normal file
View File

@@ -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)

47
pubky-testnet/src/lib.rs Normal file
View File

@@ -0,0 +1,47 @@
use anyhow::Result;
use url::Url;
pub struct Testnet {
dht: mainline::Testnet,
relays: Vec<pkarr_relay::Relay>,
}
impl Testnet {
pub async fn run() -> Result<Self> {
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<pubky::Client, pubky::errors::BuildError> {}
/// Run a new Pkarr relay.
///
/// You can access the list of relays at [Self::relays].
pub async fn run_pkarr_relay(&mut self) -> Result<Url> {
let relay = pkarr_relay::Relay::run_test(&self.dht).await?;
Ok(relay.local_url())
}
}

View File

@@ -0,0 +1,2 @@
#[tokio::main]
async fn main() {}