The pubkey graph system provides efficient social graph queries by creating bidirectional, direction-aware edges between events and pubkeys in the ORLY relay.
Purpose: Compress 32-byte pubkeys to 5-byte serials for space efficiency.
Tables:
pks|pubkey_hash(8)|serial(5) - Hash-to-serial lookup (16 bytes)spk|serial(5) → 32-byte pubkey (value) - Serial-to-pubkey reverse lookupSpace Savings: Each graph edge saves 27 bytes per pubkey reference (32 → 5 bytes).
Bidirectional edges with metadata:
epg|event_serial(5)|pubkey_serial(5)|kind(2)|direction(1) = 16 bytes
peg|pubkey_serial(5)|kind(2)|direction(1)|event_serial(5) = 16 bytes
The direction byte distinguishes relationship types:
| Value | Direction | From Event Perspective | From Pubkey Perspective |
|---|---|---|---|
0 | Author | This pubkey is the event author | I am the author of this event |
1 | P-Tag Out | Event references this pubkey | (not used in reverse) |
2 | P-Tag In | (not used in forward) | I am referenced by this event |
Location in keys:
When an event is saved:
- Event author: ev.Pubkey
- P-tags: All ["p", "<hex-pubkey>", ...] tags
For author (pubkey = event author):
`
epg|eventserial|authorserial|kind|0 (author edge)
peg|authorserial|kind|0|eventserial (is-author edge)
`
For each p-tag (referenced pubkey):
`
epg|eventserial|ptagserial|kind|1 (outbound reference)
peg|ptagserial|kind|2|eventserial (inbound reference)
`
Prefix scan: peg|pubkey_serial|*|0|*
Filter: direction == 0 (author)
Prefix scan: peg|pubkey_serial|*|2|*
Filter: direction == 2 (p-tag inbound)
Prefix scan: peg|pubkey_serial|0x0001|2|*
Exact match: kind == 1, direction == 2
Prefix scan: epg|event_serial|*|*|1
Filter: direction == 1 (p-tag outbound)
Prefix scan: epg|event_serial|*|*|0
Filter: direction == 0 (author)
The GetOrCreatePubkeySerial function uses:
The save-event function deduplicates pubkeys before creating serials:
Per event with N unique pubkeys:
Example: Event with author + 10 p-tags:
Comprehensive tests verify:
The graph structure supports efficient queries for:
- Who does Alice follow? (p-tags authored by Alice) - Who follows Bob? (p-tags referencing Bob) - Common connections between Alice and Bob
- All replies to Alice's events (kind-1 events with p-tag to Alice) - All events Alice has replied to (kind-1 events by Alice with p-tags) - Quote reposts, mentions, reactions by event kind
- Most-mentioned pubkeys (count p-tag-in edges) - Most active authors (count author edges) - Interaction patterns by kind
This is a new index that:
To backfill existing events, run a migration that: