cosmic_applet_avestruz/guide/relays.rs
1// SPDX-License-Identifier: MIT
2
3//! Which relays there are, which of them is asked or told what, and whose
4//! word is taken for where a relay is. Documentation only.
5//!
6//! A Nostr client is mostly a set of decisions about relays: which to
7//! connect to, which to ask about whom, where to send what, and what to
8//! make of a relay somebody else names. The code that carries those
9//! decisions out is [`crate::nostr::client`], with the choosing in
10//! [`crate::nostr::outbox`] and the account's lists in
11//! [`crate::nostr::relay_lists`]. This page is the decisions themselves.
12//!
13//! 1. [Three sets of relays](#three-sets-of-relays)
14//! 2. [Whose relays are the account's own](#whose-relays-are-the-accounts-own)
15//! 3. [Reading other people](#reading-other-people)
16//! 4. [The life of one of other people's relays](#the-life-of-one-of-other-peoples-relays)
17//! 5. [Where what is written goes](#where-what-is-written-goes)
18//! 6. [The account's lists](#the-accounts-lists)
19//! 7. [Whose word is taken](#whose-word-is-taken)
20//! 8. [What a relay says of itself](#what-a-relay-says-of-itself)
21//! 9. [The numbers](#the-numbers)
22//!
23//! # Three sets of relays
24//!
25//! ```mermaid
26//! flowchart TB
27//! subgraph service["NostrService"]
28//! home["The home pool"]
29//! others["The pool of other people's relays"]
30//! private["The relay Avestruz runs itself"]
31//! store[("One event store")]
32//! end
33//! config["Relays the config names"] --> home
34//! outbox["Relays the account's relay list says it writes to"] --> home
35//! theirlists["Relays other people's relay lists name"] --> others
36//! hints["Relays the address of a note names"] --> others
37//! indexers["The account's indexer relays"] --> others
38//! search["The account's search relays"] --> others
39//! home <--> store
40//! others --> store
41//! private <--> store
42//! pipe["A pipe inside the process"] --- private
43//! ```
44//!
45//! | | The home pool | The pool of other people's | The relay it runs itself |
46//! |---|---|---|---|
47//! | Which relays | The account's own: the config's, then the ones its relay list says it writes to | The ones other people's relay lists name, the ones a note's address names, and the account's indexer and search relays | One, over the same store |
48//! | Asked about | Everything | The people who list each, and nobody else | The account's drafts |
49//! | Sent | Everything the account writes | What is meant for the people who read there | Drafts, and nothing else |
50//! | Connected | For as long as the applet runs | When something is asked of them, and for a while after | It listens nowhere: a connection is made in memory |
51//! | How many | The config's, and up to [`ACCOUNT_RELAYS_MAX`] of the list's | Up to [`REACH_CAP`] for one question, and as many kept | One |
52//!
53//! Every relay of both pools sends into one store, so a note that came
54//! from anywhere is there for everything.
55//!
56//! # Whose relays are the account's own
57//!
58//! An account says where it writes in its relay list (NIP-65, kind
59//! 10002). Until that list is known, and for an account that has none,
60//! the relays the config names are where it is looked for.
61//!
62//! ```mermaid
63//! stateDiagram-v2
64//! state "The config's relays are in use" as Config
65//! state "The account's outbox relays are in use" as Listed
66//! [*] --> Config: the applet starts
67//! Config --> Config: the account has no relay list, or none of its relays connects
68//! Config --> Listed: one of the relays its list says it writes to is connected
69//! Listed --> Config: signed out, or the list names none any more
70//! ```
71//!
72//! While the account's outbox relays are in use, the config's relays
73//! *stand down*: they are not asked and not written to, and are hung up
74//! on a little later. Except those on this machine or this network, which
75//! stay in use: no relay list would name them, and they are there to be
76//! used.
77//!
78//! The reason for standing down is that the config's relays are a guess.
79//! Once the account has said where it is, asking three large public
80//! relays about everything as well is three more parties told what the
81//! account reads, for notes its own relays have.
82//!
83//! Only the relays the list says the account *writes to* become its own.
84//! The ones it says it reads are where other people send what is meant
85//! for it, and are not read from here yet.
86//!
87//! Where: the `Own` struct in [`crate::nostr::client`], and
88//! [`NostrService::use_relays_of`].
89//!
90//! # Reading other people
91//!
92//! The people an account follows don't all write to the account's relays.
93//! Each says where they write in a relay list of their own, and their
94//! notes are read from there as well (the *outbox model*).
95//!
96//! Everyone names several relays and everyone's differ, so asking all of
97//! them would be a connection to half the network. [`plan`] picks few.
98//!
99//! ```mermaid
100//! flowchart TD
101//! people["The people a question is about"] --> lists["Each one's relay list, from the store"]
102//! lists --> stale{"Asked about within the hour?"}
103//! stale -->|"no"| refresh["The account's relays, and its indexer relays, are asked for the lists"]
104//! stale -->|"yes"| filter
105//! refresh --> filter["Relays that may not be connected to are dropped"]
106//! filter --> short["Each person is to be had from 2 relays, counting the account's own"]
107//! short --> pick["Take the relay that helps the most people who are still short"]
108//! pick --> more{"Anyone still short, and fewer than 20 taken?"}
109//! more -->|"yes"| pick
110//! more -->|"no"| connect["Connect to those taken. One that doesn't connect is left alone for ten minutes"]
111//! connect --> ask["Each is asked about the people who list it, and only them"]
112//! ```
113//!
114//! An example. Five people are followed, the account's own relay is `H`,
115//! and each is to be had from two relays:
116//!
117//! | Person | Writes to | Short of |
118//! |---|---|---|
119//! | Ana | `H`, `A` | 1 |
120//! | Bia | `A`, `B` | 2 |
121//! | Caio | `A`, `C` | 2 |
122//! | Davi | `B`, `D` | 2 |
123//! | Eva | `H`, `E`, `F` | 1 |
124//!
125//! ```mermaid
126//! flowchart LR
127//! subgraph taken["Taken, in this order"]
128//! A["A: Ana, Bia, Caio"] --> B["B: Bia, Davi"] --> C["C: Caio"] --> D["D: Davi"] --> E["E: Eva"]
129//! end
130//! subgraph not["Not taken"]
131//! F["F: Eva has her two"]
132//! end
133//! H["H: the account's own, asked about everyone anyway"]
134//! ```
135//!
136//! `A` helps three people and goes first. `B` helps two. Then `C`, `D`
137//! and `E` each help one who is still short, and `F` helps nobody: Eva
138//! has `H` and `E`. Six relays were named, five are asked, and each is
139//! asked only about the people who list it.
140//!
141//! The same people and lists always give the same relays in the same
142//! order, so the choice doesn't wander from one question to the next.
143//!
144//! This example is also a test
145//! (`the_plan_the_guide_works_through_is_the_plan_that_is_made`, in
146//! [`crate::nostr::outbox`]), so that it stays what the code does.
147//!
148//! The same planning serves, with other lists, for everything asked about
149//! other people:
150//!
151//! | Asked | Of the relays | Besides the account's own |
152//! |---|---|---|
153//! | The feed, a profile's notes | The authors write to | |
154//! | A profile | Its person writes to | And the account's indexer relays |
155//! | A thread | The note's author, and the first few people it names, write to or read | And the relay the note's address named |
156//! | A quoted note, a bookmarked note | The author writes to, if who that is is known | And the relay its address or its bookmark named |
157//! | Someone's relay list | | The account's indexer relays |
158//!
159//! What other people's relays send is always *on top of* what the
160//! account's own had: if none of them is reached, that is no error.
161//!
162//! Where: [`plan`], [`NostrService::fetch_feed`], and the private
163//! `relays_of`, `reach_for` and `ask_their_relays` beside it.
164//!
165//! # The life of one of other people's relays
166//!
167//! ```mermaid
168//! stateDiagram-v2
169//! [*] --> Planned: a relay list or an address names it, and a plan takes it
170//! Planned --> Connected: it connects in time
171//! Planned --> Avoided: it doesn't, and is taken out of the pool again
172//! Connected --> Connected: asked, and it answers
173//! Connected --> Avoided: it says no, or doesn't answer in time
174//! Connected --> Asleep: nothing asked of it for two minutes
175//! Asleep --> Connected: something is asked of it
176//! Asleep --> [*]: unused for a quarter of an hour, or room is needed
177//! Avoided --> Planned: ten minutes on, a plan may take it again
178//! ```
179//!
180//! A relay that is avoided is left alone for ten minutes: plans are made
181//! without it, and the others are asked.
182//!
183//! A relay of other people's gets less time than the account's own, to
184//! connect and to answer: it is asked for what may well be had already,
185//! and a list of notes waits for the slowest of them.
186//!
187//! # Where what is written goes
188//!
189//! ```mermaid
190//! flowchart LR
191//! event["A signed event"] --> kind{"What is it?"}
192//! kind -->|"a note, a comment, a repost, a reaction"| both["The account's relays, and the relays the people it names read"]
193//! kind -->|"a contact list, a bookmark list, a list of relays"| own["The account's relays"]
194//! kind -->|"a relay list"| wide["The account's relays, as they were before and as they are now"]
195//! kind -->|"a draft"| private["The relay Avestruz runs itself, and no other"]
196//! kind -->|"a zap request"| nowhere["No relay: it goes to the Lightning address, inside the request for an invoice"]
197//! ```
198//!
199//! | What | Kind | Goes to |
200//! |---|---|---|
201//! | A note, a reply, a quote | 1 | The account's relays, and two of the relays each person it names reads |
202//! | A comment | 1111 | The same |
203//! | A repost | 6, 16 | The same |
204//! | A reaction | 7 | The same |
205//! | The contact list | 3 | The account's relays |
206//! | The bookmark list | 10003 | The account's relays |
207//! | The relay list | 10002 | The account's relays, the ones it wrote to before as well as the ones it writes to now |
208//! | The other lists of relays | 10006, 10007, 10050, 10086, 10089 | The account's relays |
209//! | A draft | 31234 | The relay Avestruz runs itself |
210//!
211//! Publishing succeeds when one relay took the event. Sending to the
212//! people an event names is on top of that: if none of their relays takes
213//! it, it is still on the account's own, where anyone can find it.
214//!
215//! Where: [`NostrService::publish`], with the private `send_home` and
216//! `deliver` beside it.
217//!
218//! # The account's lists
219//!
220//! The relays page shows seven sets of relays, which are six lists. An
221//! account's relay list holds two of them.
222//!
223//! ```mermaid
224//! flowchart LR
225//! subgraph k10002["Relay list, kind 10002"]
226//! outbox["Outbox: r tags marked write, or unmarked"]
227//! inbox["Inbox: r tags marked read, or unmarked"]
228//! end
229//! subgraph nip51["Lists of relay tags, public or kept private in the content"]
230//! dm["Direct messages, kind 10050"]
231//! search["Search, kind 10007"]
232//! indexer["Indexer, kind 10086"]
233//! trusted["Trusted, kind 10089"]
234//! blocked["Blocked, kind 10006"]
235//! end
236//! outbox --> use1["The account's own relays"]
237//! search --> use2["The relays a search asks"]
238//! indexer --> use3["Asked for profiles and relay lists the account's own don't have"]
239//! blocked --> use4["Never connected to on somebody else's word"]
240//! inbox --> kept["Kept in the list for other clients to go by"]
241//! dm --> kept
242//! trusted --> kept
243//! ```
244//!
245//! | Set | List | Said to anyone? | What Avestruz does with it | What it keeps to itself is read |
246//! |---|---|---|---|---|
247//! | Outbox | 10002 | Yes | These are the account's own relays | It keeps nothing to itself |
248//! | Inbox | 10002 | Yes | Shown and changed. Other people send here | It keeps nothing to itself |
249//! | Direct messages | 10050 | Yes | Shown and changed | It keeps nothing to itself |
250//! | Search | 10007 | Either | The relays a search asks | When a search opens |
251//! | Indexer | 10086 | Either | Where profiles and relay lists are looked up | When the popup opens |
252//! | Trusted | 10089 | Either | Shown and changed | On the relays page |
253//! | Blocked | 10006 | Either | Never connected to because a list or an address names them | When the popup opens |
254//!
255//! "Either" is a list that names relays in its tags, for anyone, or among
256//! the tags it keeps to itself, encrypted in its content. Reading the
257//! second takes the account's key, which may mean asking a person, so
258//! each list's private half is read when it is first needed and not
259//! before.
260//!
261//! There are no built-in search or indexer relays. An account that has
262//! set none searches nowhere, and the page says where to set them.
263//!
264//! Where: [`crate::nostr::relay_lists`], [`crate::handlers::relays`],
265//! [`crate::views::relays`].
266//!
267//! # Whose word is taken
268//!
269//! Most relays this client hears of, it hears of from somebody else: a
270//! relay list anyone could have written, the address of a note in
271//! somebody's text. Connecting wherever such a thing points would let
272//! anyone make this machine knock on any door.
273//!
274//! ```mermaid
275//! flowchart TD
276//! named["A relay is named"] --> who{"By whom?"}
277//! who -->|"the config, or the account's own list"| yes["Connected to"]
278//! who -->|"someone's relay list, a note's address, a bookmark"| wss{"wss, on the open internet?"}
279//! wss -->|"no: unencrypted, on this machine or network, or an onion service"| no["Not connected to"]
280//! wss -->|"yes"| blocked{"On the account's blocked list?"}
281//! blocked -->|"yes"| no
282//! blocked -->|"no"| failed{"Failed in the last ten minutes?"}
283//! failed -->|"yes"| later["Not this time"]
284//! failed -->|"no"| yes
285//! ```
286//!
287//! The test is [`public`]. A relay on this machine or network is refused
288//! when somebody else names it, since a list could name one to have its
289//! readers knock on doors at home; but one the *config* names stays in
290//! use, since whoever wrote the config meant it.
291//!
292//! # What a relay says of itself
293//!
294//! A relay answers over `https`, at its own address, with what it is and
295//! what it does (NIP-11). The relays page asks the relays of the sets
296//! that need something of a relay, and says under each what gets in the
297//! way of what its set is for ([`Section::hindrances`]):
298//!
299//! | Set | What gets in the way | Why |
300//! |---|---|---|
301//! | Search | It names the NIPs it supports, and not NIP-50 | It is asked to search |
302//! | Direct messages | It names the NIPs it supports, and not NIP-42 | It should give messages only to whom they are for, which takes knowing who asks |
303//! | Direct messages, Inbox | It restricts who may write, or has to be paid | Other people write there to reach the account |
304//! | Inbox | Whoever connects has to say who they are first | A client that doesn't can't write there |
305//! | Outbox | Whoever connects has to say who they are first | A client that doesn't can't read the account's notes there |
306//!
307//! This is the relay's own word. One that doesn't answer, or names no
308//! NIPs, is not said anything of.
309//!
310//! Where: [`crate::nostr::relay_info`], [`Section::hindrances`].
311//!
312//! # The numbers
313//!
314//! Each links the constant, which is what counts.
315//!
316//! | What | Value | Constant |
317//! |---|---|---|
318//! | Notes in one page of a feed | 50 | [`FEED_PAGE`] |
319//! | Relays of the account's list that join its own | 8 | [`ACCOUNT_RELAYS_MAX`] |
320//! | Relays each person is read from, the account's own among them | 2 | [`RELAYS_PER_PERSON`] |
321//! | Other people's relays one question reaches for | 20 | [`REACH_CAP`] |
322//! | An own relay's time to answer | 10 s | [`FETCH_TIMEOUT`] |
323//! | One of other people's relays' time to connect, and to answer | 2 s, 6 s | [`REACH_CONNECT_WAIT`], [`REACH_TIMEOUT`] |
324//! | A relay that failed is left alone for | 10 min | [`REACH_AVOID`] |
325//! | One of other people's relays is hung up on after | 2 min idle | [`REACH_SLEEP`] |
326//! | And taken out of the pool after | 15 min unused | [`REACH_KEEP`] |
327//! | Someone's relay list is asked for again after | 1 h | [`RELAY_LISTS_FRESH`] |
328//! | A search relay's time to answer | 8 s | [`SEARCH_TIMEOUT`] |
329//! | A relay's time to take what is published | 10 s | [`PUBLISH_TIMEOUT`] |
330//! | People of a conversation whose relays are asked about it | 5 | [`THREAD_PEOPLE`] |
331//!
332//! [`plan`]: crate::nostr::outbox::plan
333//! [`public`]: crate::nostr::outbox::public
334//! [`Section::hindrances`]: crate::nostr::relay_lists::Section::hindrances
335//! [`NostrService::use_relays_of`]: crate::nostr::client::NostrService::use_relays_of
336//! [`NostrService::fetch_feed`]: crate::nostr::client::NostrService::fetch_feed
337//! [`NostrService::publish`]: crate::nostr::client::NostrService::publish
338//! [`FEED_PAGE`]: crate::nostr::client::FEED_PAGE
339//! [`ACCOUNT_RELAYS_MAX`]: crate::nostr::client::ACCOUNT_RELAYS_MAX
340//! [`RELAYS_PER_PERSON`]: crate::nostr::client::RELAYS_PER_PERSON
341//! [`REACH_CAP`]: crate::nostr::client::REACH_CAP
342//! [`FETCH_TIMEOUT`]: crate::nostr::client::FETCH_TIMEOUT
343//! [`REACH_CONNECT_WAIT`]: crate::nostr::client::REACH_CONNECT_WAIT
344//! [`REACH_TIMEOUT`]: crate::nostr::client::REACH_TIMEOUT
345//! [`REACH_AVOID`]: crate::nostr::client::REACH_AVOID
346//! [`REACH_SLEEP`]: crate::nostr::client::REACH_SLEEP
347//! [`REACH_KEEP`]: crate::nostr::client::REACH_KEEP
348//! [`RELAY_LISTS_FRESH`]: crate::nostr::client::RELAY_LISTS_FRESH
349//! [`SEARCH_TIMEOUT`]: crate::nostr::client::SEARCH_TIMEOUT
350//! [`PUBLISH_TIMEOUT`]: crate::nostr::client::PUBLISH_TIMEOUT
351//! [`THREAD_PEOPLE`]: crate::nostr::client::THREAD_PEOPLE