Skip to content
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

WIP: Pkarr Republisher #85

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
storage/
.continuerules
91 changes: 73 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [
"pubky-*",

"http-relay",

"pkarr-republisher",
"examples"
]

Expand Down
1 change: 1 addition & 0 deletions pkarr-republisher/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
published_secret*.txt
42 changes: 42 additions & 0 deletions pkarr-republisher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "pkarr-republisher"
version = "0.1.0"
edition = "2021"
authors = ["Severin Alex Bühler <[email protected]>"]
description = "A pkarr packet republisher."
license = "MIT"
homepage = "https://github.com/pubky/pubky-core"
repository = "https://github.com/pubky/pubky-core"
keywords = ["pkarr", "mainline", "pubky"]
categories = ["web-programming"]

[dependencies]
anyhow = "1.0.95"
pkarr = "3.5.3"
tokio = { version = "1.43.0", features = ["full"] }
tracing = "0.1.41"
futures-lite = { version = "2.6.0"}
thiserror = "2.0.12"

# bin dependencies
clap = { version = "4.4", features = ["derive"] }
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
ctrlc = "3.4.5"
hex = "0.4.3"
rand = "0.9.0"

[dev-dependencies]
pubky-testnet = { path = "../pubky-testnet" }


[[bin]]
name = "publish_and_save"
path = "src/bin/publish_and_save.rs"

[[bin]]
name = "read_and_verify"
path = "src/bin/read_and_verify.rs"

[[bin]]
name = "read_and_republish"
path = "src/bin/read_and_republish.rs"
69 changes: 69 additions & 0 deletions pkarr-republisher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Pkarr Republisher

> Early version. Expect breaking API changes. Can still be heavily performance optimized especially by improving the `mainline` lib.

A pkarr packet republisher. Takes [pkarr](https://github.com/pubky/pkarr) and makes it resilient to UDP unreliabilities and CPU exhaustion
by retrying operations with an exponential backoff. Retries help with UDP packet loss and the backoff gives the CPU time to recover.

## Usage

**ResilientClient** Pkarr Client with retry and exponential backoff.

```rust
use pkarr_republisher::ResilientClient;

let client = ResilientClient::new().unwrap();
let public_key = Keypair::random().public_key();

// Republish with retries
match client.republish(public_key.clone(), None).await {
Ok(info) => {
println!("Key {public_key} published to {} nodes after {} attempt(s).", info.published_nodes_count, info.attempts_needed);
},
Err(err) => {
if err.is_missing() {
println!("Key {public_key} not found in DHT.");
}
if err.is_publish_failed() {
println!("Key {public_key} failed to publish. {err}");
}
}
}
```

**MultiRepublisher** Multi-threaded republisher of pkarr keys.

```rust
use pkarr_republisher::MultiRepublisher;
use pkarr::{Keypair, PublicKey};

let public_keys: Vec<PublicKey> = (0..100).map(|_| Keypair::random().public_key()).collect();
let republisher = MultiRepublisher::new().unwrap();
let results = republisher.run(public_keys, 10).await.expect("UDP socket build infalliable");

// Verify result of each republished key.
for (key, result) in results {
match result {
Ok(info) => {
println!("Key {} published to {} nodes after {} attempt(s).", key, info.published_nodes_count, info.attempts_needed);
},
Err(err) => {
if err.is_missing() {
println!("Key {} not found in DHT.", key);
} else if err.is_publish_failed() {
println!("Key {} failed to publish: {}", key, err);
} else {
println!("Key {} encountered an error: {}", key, err);
}
}
}
}
```

## Examples

The [src/bin folder](./src/bin) contains example scripts to test the performance of the republisher.

- [publish_and_save](./src/bin/publish_and_save.rs) Publishes x keys multi-threaded and saves them in `published_secrets.txt`.
- [read_and_verify](./src/bin/read_and_verify.rs) Takes a random sample of the published keys and verifies on how many nodes they've been stored on.
- [read_and_republish](./src/bin/read_and_republish.rs) takes the saved keys and republishes them multi-threaded.
Loading
Loading