Skip to content

Commit 3d92f6b

Browse files
feat(iroh): allow to override provenance for StaticProvider (#3527)
## Description The `StaticProvider` discovery service has a hard-coded `provenance` of `static-provider`. When using the StaticProvider as a building block of protocols or services, it is useful to override it so that the items can be disambiguated as to from which service they come from. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist <!-- Remove any that are not relevant. --> - [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. - [ ] List all breaking changes in the above "Breaking Changes" section. - [ ] Open an issue or PR on any number0 repos that are affected by this breaking change. Give guidance on how the updates should be handled or do the actual updates themselves. The major ones are: - [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc) - [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip) - [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs) - [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe) - [ ] [`sendme`](https://github.com/n0-computer/sendme) --------- Co-authored-by: Friedel Ziegelmayer <[email protected]>
1 parent b39b325 commit 3d92f6b

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

iroh/src/discovery/static_provider.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,19 @@ use super::{Discovery, DiscoveryError, DiscoveryItem, NodeData, NodeInfo};
6464
/// ```
6565
///
6666
/// [`NodeTicket`]: https://docs.rs/iroh-base/latest/iroh_base/ticket/struct.NodeTicket
67-
#[derive(Debug, Default, Clone)]
68-
#[repr(transparent)]
67+
#[derive(Debug, Clone)]
6968
pub struct StaticProvider {
7069
nodes: Arc<RwLock<BTreeMap<NodeId, StoredNodeInfo>>>,
70+
provenance: &'static str,
71+
}
72+
73+
impl Default for StaticProvider {
74+
fn default() -> Self {
75+
Self {
76+
nodes: Default::default(),
77+
provenance: Self::PROVENANCE,
78+
}
79+
}
7180
}
7281

7382
#[derive(Debug)]
@@ -90,6 +99,20 @@ impl StaticProvider {
9099
Self::default()
91100
}
92101

102+
/// Creates a new static discovery instance with the provided `provenance`.
103+
///
104+
/// The provenance is part of [`DiscoveryItem`]s returned from [`Self::resolve`].
105+
/// It is mostly used for debugging information and allows understanding the origin of
106+
/// addressing information used by an iroh [`Endpoint`].
107+
///
108+
/// [`Endpoint`]: crate::Endpoint
109+
pub fn with_provenance(provenance: &'static str) -> Self {
110+
Self {
111+
nodes: Default::default(),
112+
provenance,
113+
}
114+
}
115+
93116
/// Creates a static discovery instance from node addresses.
94117
///
95118
/// # Examples
@@ -196,7 +219,7 @@ impl Discovery for StaticProvider {
196219
.as_micros() as u64;
197220
let item = DiscoveryItem::new(
198221
NodeInfo::from_parts(node_id, node_info.data.clone()),
199-
Self::PROVENANCE,
222+
self.provenance,
200223
Some(last_updated),
201224
);
202225
Some(stream::iter(Some(Ok(item))).boxed())
@@ -248,4 +271,22 @@ mod tests {
248271

249272
Ok(())
250273
}
274+
275+
#[tokio::test]
276+
async fn test_provenance() -> Result {
277+
let discovery = StaticProvider::with_provenance("foo");
278+
let key = SecretKey::from_bytes(&[0u8; 32]);
279+
let addr = NodeAddr {
280+
node_id: key.public(),
281+
relay_url: Some("https://example.com".parse()?),
282+
direct_addresses: Default::default(),
283+
};
284+
discovery.add_node_info(addr);
285+
let mut stream = discovery.resolve(key.public()).unwrap();
286+
let item = stream.next().await.unwrap()?;
287+
assert_eq!(item.provenance(), "foo");
288+
assert_eq!(item.relay_url(), Some(&("https://example.com".parse()?)));
289+
290+
Ok(())
291+
}
251292
}

0 commit comments

Comments
 (0)