Skip to content

deps!: update pkarr to v3 #3186

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 22 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
455 changes: 242 additions & 213 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions iroh-dns-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ humantime-serde = "1.1.1"
iroh-metrics = { version = "0.32.0", features = ["metrics", "service"] }
lru = "0.12.3"
n0-future = "0.1.2"
pkarr = { version = "2.3.1", features = [ "async", "relay", "dht"], default-features = false }
pkarr = { version = "3.6", features = ["relays", "dht"], default-features = false }
rcgen = "0.13"
redb = "2.0.0"
regex = "1.10.3"
Expand Down Expand Up @@ -61,7 +61,6 @@ z32 = "1.1.1"
criterion = "0.5.1"
hickory-resolver = "=0.25.0-alpha.5"
iroh = { path = "../iroh" }
pkarr = { version = "2.3.1", features = ["rand"] }
rand = "0.8"
rand_chacha = "0.3.1"
testresult = "0.4.1"
Expand Down
29 changes: 12 additions & 17 deletions iroh-dns-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod tests {
discovery::pkarr::PkarrRelayClient, dns::DnsResolver, node_info::NodeInfo, RelayUrl,
SecretKey,
};
use pkarr::{PkarrClient, SignedPacket};
use pkarr::{SignedPacket, Timestamp};
use testresult::TestResult;
use tracing_test::traced_test;

Expand Down Expand Up @@ -101,13 +101,13 @@ mod tests {
30,
dns::rdata::RData::AAAA(Ipv6Addr::LOCALHOST.into()),
));
SignedPacket::from_packet(&keypair, &packet)?
SignedPacket::new(&keypair, &packet.answers, Timestamp::now())?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not important here, but if there are other places where you have similar verbosity, reminder that the new SignedPacket::builder() api is much nicer.

};
let pkarr_client = pkarr::PkarrRelayClient::new(pkarr::RelaySettings {
relays: vec![pkarr_relay_url.to_string()],
..Default::default()
})?;
pkarr_client.as_async().publish(&signed_packet).await?;
let pkarr_client = pkarr::Client::builder()
.no_default_network()
.relays(&[pkarr_relay_url])?
.build()?;
pkarr_client.publish(&signed_packet, None).await?;

use hickory_server::proto::rr::Name;
let pubkey = signed_packet.public_key().to_z32();
Expand Down Expand Up @@ -219,7 +219,7 @@ mod tests {
#[traced_test]
async fn integration_mainline() -> Result<()> {
// run a mainline testnet
let testnet = pkarr::mainline::dht::Testnet::new(5);
let testnet = pkarr::mainline::Testnet::new(5)?;
let bootstrap = testnet.bootstrap.clone();

// spawn our server with mainline support
Expand All @@ -237,13 +237,11 @@ mod tests {
let signed_packet = node_info.to_pkarr_signed_packet(&secret_key, 30)?;

// publish the signed packet to our DHT
let pkarr = PkarrClient::builder()
.dht_settings(pkarr::mainline::dht::DhtSettings {
bootstrap: Some(testnet.bootstrap),
..Default::default()
})
let pkarr = pkarr::Client::builder()
.no_default_network()
.dht(|builder| builder.bootstrap(&testnet.bootstrap))
.build()?;
pkarr.publish(&signed_packet)?;
pkarr.publish(&signed_packet, None).await?;

// resolve via DNS from our server, which will lookup from our DHT
let resolver = test_resolver(nameserver);
Expand All @@ -253,9 +251,6 @@ mod tests {
assert_eq!(res.relay_url(), Some(&relay_url));

server.shutdown().await?;
for mut node in testnet.nodes {
node.shutdown()?;
}
Ok(())
}

Expand Down
15 changes: 6 additions & 9 deletions iroh-dns-server/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anyhow::Result;
use hickory_server::proto::rr::{Name, RecordSet, RecordType, RrKey};
use iroh_metrics::inc;
use lru::LruCache;
use pkarr::{mainline::dht::DhtSettings, PkarrClient, SignedPacket};
use pkarr::{Client as PkarrClient, SignedPacket};
use tokio::sync::Mutex;
use tracing::{debug, trace};
use ttl_cache::TtlCache;
Expand Down Expand Up @@ -66,10 +66,7 @@ impl ZoneStore {
let pkarr_client = match bootstrap {
BootstrapOption::Default => PkarrClient::builder().build().unwrap(),
BootstrapOption::Custom(bootstrap) => PkarrClient::builder()
.dht_settings(DhtSettings {
bootstrap: Some(bootstrap),
..Default::default()
})
.dht(|builder| builder.bootstrap(&bootstrap))
.build()
.unwrap(),
};
Expand Down Expand Up @@ -116,9 +113,9 @@ impl ZoneStore {
//
// it will be cached for some time.
debug!("DHT resolve {}", key.to_z32());
let packet_opt = pkarr.as_ref().clone().as_async().resolve(&key).await?;
let packet_opt = pkarr.resolve(&key).await;
if let Some(packet) = packet_opt {
debug!("DHT resolve successful {:?}", packet.packet());
debug!("DHT resolve successful {:?}", packet);
return self
.cache
.lock()
Expand Down Expand Up @@ -249,12 +246,12 @@ impl CachedZone {
signed_packet_to_hickory_records_without_origin(signed_packet, |_| true)?;
Ok(Self {
records,
timestamp: signed_packet.timestamp(),
timestamp: signed_packet.timestamp().into(),
})
}

fn is_newer_than(&self, signed_packet: &SignedPacket) -> bool {
self.timestamp > signed_packet.timestamp()
self.timestamp > signed_packet.timestamp().into()
}

fn resolve(&self, name: &Name, record_type: RecordType) -> Option<Arc<RecordSet>> {
Expand Down
25 changes: 12 additions & 13 deletions iroh-dns-server/src/store/signed_packets.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{future::Future, path::Path, result, time::Duration};

use anyhow::{Context, Result};
use bytes::Bytes;
use iroh_metrics::inc;
use pkarr::{system_time, SignedPacket};
use pkarr::{SignedPacket, Timestamp};
use redb::{
backends::InMemoryBackend, Database, MultimapTableDefinition, ReadableTable, TableDefinition,
};
Expand Down Expand Up @@ -120,7 +119,7 @@ impl Actor {
let transaction = self.db.begin_write()?;
let mut tables = Tables::new(&transaction)?;
let timeout = tokio::time::sleep(self.options.max_batch_time);
let expired = system_time() - expiry_us;
let expired = Timestamp::now() - expiry_us;
tokio::pin!(timeout);
for _ in 0..self.options.max_batch_size {
tokio::select! {
Expand All @@ -134,7 +133,7 @@ impl Actor {
match msg {
Message::Get { key, res } => {
trace!("get {}", key);
let packet = get_packet(&tables.signed_packets, &key)?;
let packet = get_packet(&tables.signed_packets, &key).context("get packet failed")?;
res.send(packet).ok();
}
Message::Upsert { packet, res } => {
Expand All @@ -146,15 +145,15 @@ impl Actor {
continue;
} else {
// remove the packet from the update time index
tables.update_time.remove(&packet.timestamp().to_be_bytes(), key.as_bytes())?;
tables.update_time.remove(&packet.timestamp().to_bytes(), key.as_bytes())?;
true
}
} else {
false
};
let value = packet.as_bytes();
let value = packet.serialize();
tables.signed_packets.insert(key.as_bytes(), &value[..])?;
tables.update_time.insert(&packet.timestamp().to_be_bytes(), key.as_bytes())?;
tables.update_time.insert(&packet.timestamp().to_bytes(), key.as_bytes())?;
if replaced {
inc!(Metrics, store_packets_updated);
} else {
Expand All @@ -165,8 +164,8 @@ impl Actor {
Message::Remove { key, res } => {
trace!("remove {}", key);
let updated = if let Some(row) = tables.signed_packets.remove(key.as_bytes())? {
let packet = SignedPacket::from_bytes(&Bytes::copy_from_slice(row.value()))?;
tables.update_time.remove(&packet.timestamp().to_be_bytes(), key.as_bytes())?;
let packet = SignedPacket::deserialize(row.value())?;
tables.update_time.remove(&packet.timestamp().to_bytes(), key.as_bytes())?;
inc!(Metrics, store_packets_removed);
true
} else {
Expand Down Expand Up @@ -313,10 +312,10 @@ fn get_packet(
table: &impl ReadableTable<&'static SignedPacketsKey, &'static [u8]>,
key: &PublicKeyBytes,
) -> Result<Option<SignedPacket>> {
let Some(row) = table.get(key.as_ref())? else {
let Some(row) = table.get(key.as_ref()).context("database fetch failed")? else {
return Ok(None);
};
let packet = SignedPacket::from_bytes(&row.value().to_vec().into())?;
let packet = SignedPacket::deserialize(row.value()).context("parsing signed packet failed")?;
Ok(Some(packet))
}

Expand Down Expand Up @@ -344,11 +343,11 @@ async fn evict_task_inner(send: mpsc::Sender<Message>, options: Options) -> anyh
let Ok(snapshot) = rx.await else {
anyhow::bail!("failed to get snapshot");
};
let expired = system_time() - expiry_us;
let expired = Timestamp::now() - expiry_us;
trace!("evicting packets older than {}", expired);
// if getting the range fails we exit the loop and shut down
// if individual reads fail we log the error and limp on
for item in snapshot.update_time.range(..expired.to_be_bytes())? {
for item in snapshot.update_time.range(..expired.to_bytes())? {
let (time, keys) = match item {
Ok(v) => v,
Err(e) => {
Expand Down
2 changes: 1 addition & 1 deletion iroh-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ iroh-metrics = { version = "0.32", default-features = false }
n0-future = "0.1.2"
num_enum = "0.7"
pin-project = "1"
pkarr = { version = "2", default-features = false }
pkarr = { version = "3.6", default-features = false, features = ["signed_packet"] }
postcard = { version = "1", default-features = false, features = [
"alloc",
"use-std",
Expand Down
41 changes: 15 additions & 26 deletions iroh-relay/src/node_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,14 +528,15 @@ impl<T: FromStr + Display + Hash + Ord> TxtAttrs<T> {
let pubkey_z32 = pubkey.to_z32();
let node_id = NodeId::from(*pubkey.verifying_key());
let zone = dns::Name::new(&pubkey_z32)?;
let inner = packet.packet();
let txt_data = inner.answers.iter().filter_map(|rr| match &rr.rdata {
RData::TXT(txt) => match rr.name.without(&zone) {
Some(name) if name.to_string() == IROH_TXT_NAME => Some(txt),
Some(_) | None => None,
},
_ => None,
});
let txt_data = packet
.all_resource_records()
.filter_map(|rr| match &rr.rdata {
RData::TXT(txt) => match rr.name.without(&zone) {
Some(name) if name.to_string() == IROH_TXT_NAME => Some(txt),
Some(_) | None => None,
},
_ => None,
});

let txt_strs = txt_data.filter_map(|s| String::try_from(s.clone()).ok());
Self::from_strings(node_id, txt_strs)
Expand Down Expand Up @@ -597,30 +598,18 @@ impl<T: FromStr + Display + Hash + Ord> TxtAttrs<T> {
secret_key: &SecretKey,
ttl: u32,
) -> Result<pkarr::SignedPacket> {
let packet = self.to_pkarr_dns_packet(ttl)?;
let keypair = pkarr::Keypair::from_secret_key(&secret_key.to_bytes());
let signed_packet = pkarr::SignedPacket::from_packet(&keypair, &packet)
.map_err(|e| anyhow::anyhow!(e.to_string()))?;
Ok(signed_packet)
}

fn to_pkarr_dns_packet(&self, ttl: u32) -> Result<pkarr::dns::Packet<'static>> {
use pkarr::dns::{self, rdata};
let name = dns::Name::new(IROH_TXT_NAME)?.into_owned();
let keypair = pkarr::Keypair::from_secret_key(&secret_key.to_bytes());
let name = dns::Name::new(IROH_TXT_NAME)?;

let mut packet = dns::Packet::new_reply(0);
let mut builder = pkarr::SignedPacket::builder();
for s in self.to_txt_strings() {
let mut txt = rdata::TXT::new();
txt.add_string(&s)?;
let rdata = rdata::RData::TXT(txt.into_owned());
packet.answers.push(dns::ResourceRecord::new(
name.clone(),
dns::CLASS::IN,
ttl,
rdata,
));
builder = builder.txt(name.clone(), txt.into_owned(), ttl);
}
Ok(packet)
let signed_packet = builder.build(&keypair)?;
Ok(signed_packet)
}
}

Expand Down
5 changes: 2 additions & 3 deletions iroh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ iroh-relay = { version = "0.34", path = "../iroh-relay", default-features = fals
n0-future = "0.1.2"
netwatch = { version = "0.4" }
pin-project = "1"
pkarr = { version = "2", default-features = false, features = [
"async",
"relay",
pkarr = { version = "3.6", default-features = false, features = [
"relays",
] }
quinn = { package = "iroh-quinn", version = "0.13.0", default-features = false, features = ["rustls-ring"] }
quinn-proto = { package = "iroh-quinn-proto", version = "0.13.0" }
Expand Down
Loading
Loading