-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.go
More file actions
165 lines (135 loc) · 3.58 KB
/
store.go
File metadata and controls
165 lines (135 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package clip
import (
"fmt"
"sync"
"github.com/nbd-wtf/go-nostr"
)
type announcementState struct {
createdAt nostr.Timestamp
pub string
}
type nodeState struct {
mu sync.RWMutex
lastAnnouncement announcementState
// map with 'd' tag as key
events map[string]*Event
}
func newNodeState() *nodeState {
return &nodeState{
events: make(map[string]*Event),
}
}
type MapStore struct {
mu sync.RWMutex
// map with node pubkey as key
records map[string]*nodeState
}
func NewMapStore() *MapStore {
return &MapStore{
records: make(map[string]*nodeState),
}
}
func (s *MapStore) StoreEvent(ev *Event) error {
id, err := ev.GetIdentifier()
if err != nil {
return err
}
ns := s.getNodeState(id.PubKey)
ns.mu.Lock()
defer ns.mu.Unlock()
if ev.kind == KindNodeAnnouncement {
return s.storeAnnouncement(ns, ev, id)
}
return s.storeRegularEvent(ns, ev, id)
}
func (s *MapStore) storeAnnouncement(ns *nodeState, ev *Event, id *Identifier) error {
// Skip if existing announcement is newer or same
if ns.lastAnnouncement.createdAt >= ev.NostrEvent.CreatedAt {
return fmt.Errorf("existing announcement is newer or same: %d >= %d",
ns.lastAnnouncement.createdAt, ev.NostrEvent.CreatedAt)
}
// Purge old events if pubkey changed (potential nsec compromise)
if ns.lastAnnouncement.pub != ev.NostrEvent.PubKey {
ns.events = make(map[string]*Event)
}
// Store the new announcement
ns.events[id.TagD] = ev
ns.lastAnnouncement = announcementState{
createdAt: ev.NostrEvent.CreatedAt,
pub: ev.NostrEvent.PubKey,
}
return nil
}
func (s *MapStore) storeRegularEvent(ns *nodeState, ev *Event, id *Identifier) error {
// Only accept events matching the last announcement pubkey
if ns.lastAnnouncement.pub != ev.NostrEvent.PubKey {
return fmt.Errorf("event pubkey %s does not match last announcement pubkey %s",
ev.NostrEvent.PubKey, ns.lastAnnouncement.pub)
}
// Skip if existing record is newer or same
if lastRecord, exists := ns.events[id.TagD]; exists {
if lastRecord.NostrEvent.CreatedAt >= ev.NostrEvent.CreatedAt {
return fmt.Errorf("existing record is newer or same: %d >= %d",
lastRecord.NostrEvent.CreatedAt, ev.NostrEvent.CreatedAt)
}
}
ns.events[id.TagD] = ev
return nil
}
func (s *MapStore) getNodeState(pubkey string) *nodeState {
// Fast path: read lock to check if exists
s.mu.RLock()
ns, exists := s.records[pubkey]
s.mu.RUnlock()
if exists {
return ns
}
// Slow path: write lock to create if still missing
s.mu.Lock()
defer s.mu.Unlock()
// Double-check: might have been created by another goroutine
ns, exists = s.records[pubkey]
if exists {
return ns
}
// Create new node state
ns = newNodeState()
s.records[pubkey] = ns
return ns
}
func (s *MapStore) GetEvents(kind Kind, pubKeys map[string]struct{}) []*Event {
events := []*Event{}
pubFilter := newInFilter[string](pubKeys)
// Snapshot node pointers
s.mu.RLock()
nodes := make([]*nodeState, 0, len(s.records))
for pubKey, ns := range s.records {
if !pubFilter(pubKey) {
continue
}
nodes = append(nodes, ns)
}
s.mu.RUnlock()
for _, ns := range nodes {
ns.mu.RLock()
for _, ev := range ns.events {
if ev.kind != kind {
continue
}
events = append(events, ev)
}
ns.mu.RUnlock()
}
return events
}
// newInFilter returns a filter function that checks if an item is in the provided set.
// If the set is empty, all items are considered to be in the set.
func newInFilter[T comparable](set map[T]struct{}) func(T) bool {
return func(item T) bool {
if len(set) == 0 {
return true
}
_, exists := set[item]
return exists
}
}