upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/45.md
diff options
context:
space:
mode:
authorfiatjaf_ <fiatjaf@gmail.com>2026-02-06 17:26:02 -0300
committerGitHub <noreply@github.com>2026-02-06 17:26:02 -0300
commit3d71a4a78c376a5a71bf44708cd6b02c1773ae0b (patch)
tree947c33a3344fc4dd79742ceee58437595a3bf444 /45.md
parent01838f302ddf639b2cfc1bfbe6232401e82eac58 (diff)
nip45: add hyperloglog relay response (#1561)
Diffstat (limited to '45.md')
-rw-r--r--45.md102
1 files changed, 96 insertions, 6 deletions
diff --git a/45.md b/45.md
index 14d656e..794dbe8 100644
--- a/45.md
+++ b/45.md
@@ -29,29 +29,119 @@ In case a relay uses probabilistic counts, it MAY indicate it in the response wi
29 29
30Whenever the relay decides to refuse to fulfill the `COUNT` request, it MUST return a `CLOSED` message. 30Whenever the relay decides to refuse to fulfill the `COUNT` request, it MUST return a `CLOSED` message.
31 31
32## Examples 32## HyperLogLog
33 33
34### Followers count 34Relays may return an HyperLogLog value together with the count, hex-encoded.
35 35
36``` 36```
37["COUNT", <query_id>, {"kinds": [3], "#p": [<pubkey>]}] 37["COUNT", <subscription_id>, {"count": <integer>, "hll": "<hex>"}]
38["COUNT", <query_id>, {"count": 238}]
39``` 38```
40 39
41### Count posts and reactions 40This is so it enables merging results from multiple relays and yielding a reasonable estimate of reaction counts, comment counts and follower counts, while saving many millions of bytes of bandwidth for everybody.
41
42### Algorithm
43
44This section describes the steps a relay should take in order to return HLL values to clients.
45
461. Upon receiving a filter, if it is eligible (see below) for HyperLogLog, compute the deterministic `offset` for that filter (see below);
472. Initialize 256 registers to `0` for the HLL value;
483. For all the events that are to be counted according to the filter, do this:
49 1. Read the byte at position `offset` of the event `pubkey`, its value will be the register index `ri`;
50 2. Count the number of leading zero bits starting at position `offset+1` of the event `pubkey` and add `1`;
51 3. Compare that with the value stored at register `ri`, if the new number is bigger, store it.
52
53That is all that has to be done on the relay side, and therefore the only part needed for interoperability.
54
55On the client side, these HLL values received from different relays can be merged (by simply going through all the registers in HLL values from each relay and picking the highest value for each register, regardless of the relay).
56
57And finally the absolute count can be estimated by running some methods I don't dare to describe here in English, it's better to check some implementation source code (also, there can be different ways of performing the estimation, with different quirks applied on top of the raw registers).
58
59### `offset` computation
60
61The `offset` (used in the HLL computation above) is derived deterministically from the filter sent by the client to the relay. The formula for obtaining the `offset` value is as follows:
62
63 1. Take the first tag attribute in the filter (with the `#` prefix);
64 2. From that, take its first item (it will be a string);
65 3. Obtain a 32-byte hex string from it:
66 - if the string is an event id or pubkey hex, use it as it is;
67 - if the string is an address (`<kind>:<pubkey>:<d-tag>`), use the `<pubkey>` part;
68 - if the string is anything else, hash it with a `sha256()` and take the result as a hex string;
69 4. From the 64-character hex string obtained before, take the character at position `32`;
70 5. Read that character as a base-16 number;
71 6. Add 8 to it: the result is the `offset`.
72
73For cases not covered above (filters without a tag attribute, for example), behavior isn't yet defined. This NIP may be modified later to specify those, but for now there isn't a use case that justifies using HLL in those circumstances.
74
75### Rationale
76
77The value of `offset` must be deterministic because that's the only way to allow relays to cache the HLL values so they don't have to count thousands of events from the database on every query. It also allows relays to precompute HLL values for any given target `<id>` or `<pubkey>` without having to store the events themselves directly, which can be handy in case of reactions, for example.
78
79### Common filters
80
81Some relays may decide to cache or precompute HLL values for some common canonical queries, and also to refrain from counting events that do not match these specs. These are such queries (this NIP can be modified later if more common useful queries are discovered and start being used):
82
83- **reaction count**: `{"#e": ["<id>"], "kinds": [7]}`
84- **repost count**: `{"#e": ["<id>"], "kinds": [6]}`
85- **quote count**: `{"#q": ["<id>"], "kinds": [1, 1111]}`
86- **reply count**: `{"#e": ["<id>"], "kinds": [1]}`
87- **comment count**: `{"#E": ["<id>"], "kinds": [1111]}`
88- **follower count**: `{"#p": ["<pubkey>"], "kinds": [3]}`
89
90Notice that these queries only include 1 tag attribute with always a single item in it, which means that implementors don't have to check the order in which these attributes show up in the filter.
91
92### Attack vectors
93
94One could mine a pubkey with a certain number of zero bits in the exact place where the HLL algorithm described above would look for them in order to artificially make its reaction or follow "count more" than others. For this to work a different pubkey would have to be created for each different target (event id, followed profile etc). This approach is not very different than creating tons of new pubkeys and using them all to send likes or follow someone in order to inflate their number of followers. The solution is the same in both cases: clients should not fetch these reaction counts from open relays that accept everything, they should base their counts on relays that perform some form of filtering that makes it more likely that only real humans are able to publish there and not bots or artificially-generated pubkeys.
95
96### `hll` encoding
97
98The value `hll` value must be the concatenation of the 256 registers, each being a uint8 value (i.e. a byte). Therefore `hll` will be a 512-character hex string.
99
100### Client-side usage
101
102This algorithm also allows clients to combine HLL responses received from relays with HLL counts computed locally from raw events. It's recommended that clients keep track of HLL values locally and add to these on each message received from relays. For example:
103
104 - a client wants to keep track of the number of reactions an event Z has received over time;
105 - the client has decided it will read reactions from relays A, B and C (the NIP-65 "read" relays of Z's author);
106 - of these, only B and C support HLL responses, so the client fetches both and merges them locally;
107 - then the client fetches all reaction events from A then manually applies each event to the HLL from the previous step, using the same algorithm described above;
108 - finally, the client reads the estimate count from the HLL and displays that to the user;
109 - optionally the client may store that HLL value (together with some "last-read-date" for relay A) and repeat the process again later:
110 - this time it only needs to fetch the new reactions from A and add those to the HLL
111 - and redownload the HLL values from B and C and just reapply them to the local value.
112
113This procedure allows the client to download much less data.
114
115## Examples
116
117### Count notes and reactions
42 118
43``` 119```
44["COUNT", <query_id>, {"kinds": [1, 7], "authors": [<pubkey>]}] 120["COUNT", <query_id>, {"kinds": [1, 7], "authors": [<pubkey>]}]
45["COUNT", <query_id>, {"count": 5}] 121["COUNT", <query_id>, {"count": 5}]
46``` 122```
47 123
48### Count posts approximately 124### Count notes approximately
49 125
50``` 126```
51["COUNT", <query_id>, {"kinds": [1]}] 127["COUNT", <query_id>, {"kinds": [1]}]
52["COUNT", <query_id>, {"count": 93412452, "approximate": true}] 128["COUNT", <query_id>, {"count": 93412452, "approximate": true}]
53``` 129```
54 130
131### Followers count with HyperLogLog
132
133```
134["COUNT", <subscription_id>, {"kinds": [3], "#p": [<pubkey>]}]
135["COUNT", <subscription_id>, {"count": 16578, "hll": "0607070505060806050508060707070706090d080b0605090607070b07090606060b0705070709050807080805080407060906080707080507070805060509040a0b06060704060405070706080607050907070b08060808080b080607090a06060805060604070908050607060805050d05060906090809080807050e0705070507060907060606070708080b0807070708080706060609080705060604060409070a0808050a0506050b0810060a0908070709080b0a07050806060508060607080606080707050806080c0a0707070a080808050608080f070506070706070a0908090c080708080806090508060606090906060d07050708080405070708"}]
136```
137
138### Reaction counts with HyperLogLog
139
140```
141["COUNT", <subscription_id>, {"kinds": [7], "#e": [<id>]}]
142["COUNT", <subscription_id>, {"count": 2044, "hll": "01ef070505060806050508060707070706090d080b0605090607070b07090606060b0705070709050807080805080407060906080707080507070805060509040a0b06060704060405070706080607050907070b08060808080b080607090a06060805060604070908050607060805050d05060906090809080807050e0705070507060907060606070708080b0807070708080706060609080705060604060409070a0808050a0506050b0810060a0908070709080b0a07050806060508060607080606080707050806080c0a0707070a080808050608080f070506070706070a0908090c080708080806090508060606090906060d07050708080405070708"}]
143```
144
55### Relay refuses to count 145### Relay refuses to count
56 146
57``` 147```