-
Notifications
You must be signed in to change notification settings - Fork 235
feat(iroh): publish and resolve user-defined data in discovery #3176
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
Conversation
5e49912
to
9432910
Compare
Documentation for this PR has been generated and is available at: https://n0-computer.github.io/iroh/pr/3176/docs/iroh/ Last updated: 2025-02-21T15:33:17Z |
32a29f4
to
14b1f3b
Compare
14b1f3b
to
0324657
Compare
a2cd248
to
644e762
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good to me! some nits around comments and potential API changes.
iroh-relay/src/dns/node_info.rs
Outdated
} | ||
} | ||
|
||
// User defined data that can be published and resolved through node discovery. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// User defined data that can be published and resolved through node discovery. | |
/// User-defined data that can be published and resolved through node discovery. |
9970bb3
to
66f9691
Compare
761ffe7
to
181a4be
Compare
66f9691
to
1514e11
Compare
1514e11
to
c1627cb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## Description Adds a `NodeData` struct that contains the information about a node that can be published in discovery services. This struct is both used in `iroh_relay::dns::NodeInfo` and in `iroh::discovery::Discovery`. Also, `iroh::discovery::DiscoveryItem` has no public fields anymore and uses `NodeInfo` internally to again reduce duplication. With these changes, the fields published about a node in discovery now are only defined in a single place. This PR also serves these prupsoes: * We want to add some "user defined data" to discovery (see #3176). This would add a third argument to `Discovery::publish`; with this PR instead we can just add it as an optional field to `NodeData`. * It makes it possible to potentially add data to discovery after 1.0. The fields of `NodeData` are private, so we can add fields, and setter/getter methods, without this being a semver-breaking change. A few other places are changed for consistency: `StaticProvider` now works with `NodeInfo` instead of `NodeAddr` (`NodeInfo` can be easily converted into `NodeAddr`). `DnsProvider::lookup_node_by_id` etc. now return `NodeInfo` instead of `NodeAddr`. A few method names are adjusted for consistency. `NodeInfo` is constructed in a builder-style fashion instead of passing all fields to `new`. ## Breaking Changes * `iroh::discovery::Discovery::publish` now takes `data: &NodeData` as its single argument. `iroh::discovery::NodeData` is a re-export of `iroh_relay::dns::node_info::NodeData`, and contains relay URL and direct addresses. See docs for `NodeData` for details. * `iroh::discovery::DiscoveryItem` no longer has any public fields. There are now getters for the contained data, and constructors for createing a `DiscoveryItem` from a `NodeInfo`. * `iroh_relay::dns::node_info::NodeInfo` is changed. * `NodeInfo::new` now has a single `NodeId` argument. Use `NodeInfo::with_direct_addresses` and `NodeInfo::with_relay_url` to set addresses and relay URL. Alternatively, use `NodeInfo::from_parts` and pass a `NodeData` struct. * `NodeInfo` now has two public fields `node_id: NodeId` and `data: NodeData`, and setter and getter methods for the relay URL and direct addresses. * ` iroh::discovery::pkarr::PkarrPublisher::update_addr_info` now takes a `NodeData` argument ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
c1627cb
to
51319ec
Compare
@@ -63,6 +63,8 @@ const N0_LOCAL_SWARM: &str = "iroh.local.swarm"; | |||
/// Used in the [`crate::endpoint::Source::Discovery`] enum variant as the `name`. | |||
pub const NAME: &str = "local.swarm.discovery"; | |||
|
|||
const USER_DATA_ATTRIBUTE: &str = "user-data"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment for what this is used?
// Get the user-defined data from the resolved peer info. We expect an attribute with a value | ||
// that parses as `UserData`. Otherwise, omit. | ||
let user_data = if let Some(Some(user_data)) = peer.txt_attribute(USER_DATA_ATTRIBUTE) { | ||
user_data.parse().ok() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should print at least a debug message of the errror here, so that it isn't fully silent if there is invalid data floating around, and you are trying to debug why it can't get resolved
Description
Based on #3175
Adds support for publishing and resolving a string of user-defined data through Iroh's discovery services.
This gives applications a way to add additional information to the records published and resolved through discovery, in addition to the addressing information (relay URL and direct addresses). Iroh itself does not parse or use this user-defined data string in any way.
All existing discovery services are updated to support the user data. All DNS-based discovery services encode the user-defined data in a TXT record, in the same way as the DNS/pkarr services already encode the relay URL and direct addresses. Therefore, the length of the
user-data=<user-data>
string is limited to 255 bytes, which is the max length for a single character string in DNS messages. Thus, the length of the actual user-defined data string is limited to 245 bytes (255 minuxuser-data=
). This limit is enforced when constructing aUserData
(which is just a wrapper around aString
otherwise).The local swarm discovery uses
swarm-discovery
under the hood, for which I added support for TXT records in rkuhn/swarm-discovery#12. This was released as[email protected]
.Breaking Changes
Notes & open questions
Note that currently the
DiscoveryItem
which contains the user-defined data is only accessible when directly calling the methods on theDiscovery
trait. For discoveries initiated by the endpoint, both active fromEndpoint::connect
and passive from the magicsock's subscription toDiscovery::subscribe
, theDiscoveryItem
s are currently not exposed in any way, thus there's no way for an app to access the user data of nodes discovered by the endpoint. However, with #3181, there will be a way to watch for allDiscoveryItem
s found by the endpoint.Change checklist