Skip to content

Commit 8c6f7a3

Browse files
committed
Notes must exist before you can fetch them, now.
1 parent 7e2558f commit 8c6f7a3

4 files changed

Lines changed: 25 additions & 68 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Notes must exist before you can fetch them, now.

src/bucketsrelay/v2/objs.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type
4646
StorageLimitExceeded = 3
4747
TransferLimitExceeeded = 4
4848
InvalidParams = 5
49+
NotFound = 6
4950

5051
RelayMessage* = object
5152
resp_id*: int

src/bucketsrelay/v2/proto2.nim

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,7 @@ proc updateSchema*(db: DbConn) =
240240
store INTEGER DEFAULT 0,
241241
PRIMARY KEY (period, ip, pubkey)
242242
)""")
243-
244-
#----------- in-memory stuff
245-
db.exec(sql"""CREATE TEMPORARY TABLE note_sub (
246-
topic TEXT PRIMARY KEY,
247-
pubkey TEXT NOT NULL
248-
)""")
249-
db.exec(sql"CREATE INDEX note_sub_pubkey ON note_sub(pubkey)")
250-
243+
251244

252245
#-------------------------------------------------------------------
253246
# Relay code
@@ -318,7 +311,6 @@ proc initAuth*[T](relay: Relay[T], client: T): RelayConnection[T] =
318311
proc disconnect*[T](relay: Relay[T], conn: RelayConnection[T]) =
319312
if conn.pubkey.isSome:
320313
let pubkey = conn.pubkey.get()
321-
relay.db.exec(sql"DELETE FROM note_sub WHERE pubkey=?", pubkey)
322314
relay.clients.del(pubkey)
323315
info &"[{conn.pubkey.abbr}] disconnected"
324316

@@ -416,21 +408,6 @@ proc delExpiredNotes(relay: Relay) =
416408
let offstring = &"{offset} seconds"
417409
relay.db.exec(sql"DELETE FROM note WHERE created <= datetime('now', ?)", offstring)
418410

419-
proc addNoteSub(relay: Relay, topic: string, pubkey: PublicKey) =
420-
## Record that a pubkey is subscribed to a topic
421-
try:
422-
relay.db.exec(sql"INSERT INTO note_sub (topic, pubkey) VALUES (?,?)", topic.DbBlob, pubkey)
423-
info &"[{pubkey.abbr}] sub {topic}"
424-
except CatchableError:
425-
raise ValueError.newException("Topic already subscribed")
426-
427-
proc getNoteSub(relay: Relay, topic: string): Option[PublicKey] =
428-
## Return a PublicKey who is listening for a note by topic.
429-
relay.delExpiredNotes()
430-
let orow = relay.db.getRow(sql"SELECT pubkey FROM note_sub WHERE topic = ?", topic.DbBlob)
431-
if orow.isSome:
432-
return some(PublicKey.fromDB(orow.get()[0].b))
433-
434411
proc popNote(relay: Relay, topic: string): Option[string] =
435412
let db = relay.db
436413
relay.delExpiredNotes()
@@ -449,10 +426,6 @@ proc popNote(relay: Relay, topic: string): Option[string] =
449426
warn &"[note] error " & getCurrentExceptionMsg()
450427
db.exec(sql"ROLLBACK")
451428

452-
proc delNoteSub(relay: Relay, topic: string) =
453-
relay.db.exec(sql"DELETE FROM note_sub WHERE topic = ?", topic.DbBlob)
454-
info &"[note] del {topic}"
455-
456429
proc noteCount(relay: Relay, pubkey: PublicKey): int =
457430
## Return the number of notes currently published by this ip
458431
relay.db.getRow(sql"SELECT count(*) FROM note WHERE src = ?", pubkey).get()[0].i.int
@@ -572,34 +545,15 @@ proc handleCommand*[T](relay: Relay[T], conn: var RelayConnection[T], cmd: Relay
572545
pubkey = pubkey,
573546
publish = 1,
574547
)
575-
let opubkey = relay.getNoteSub(cmd.pub_topic)
576-
if opubkey.isSome:
577-
# someone is waiting
578-
var other_conn = relay.clients[opubkey.get()]
579-
conn.sendOkay(cmd)
580-
other_conn.sendMessage(RelayMessage(
581-
kind: Note,
582-
resp_id: 0, # Not triggered by other_conn's command
583-
note_data: cmd.pub_data,
584-
note_topic: cmd.pub_topic,
585-
))
586-
relay.delNoteSub(cmd.pub_topic)
587-
relay.db.record_transfer_stat(
588-
ip = other_conn.ip,
589-
pubkey = other_conn.pubkey.get(),
590-
data_out = cmd.pub_data.len,
548+
try:
549+
relay.db.exec(sql"INSERT INTO note (topic, data, src) VALUES (?, ?, ?)",
550+
cmd.pub_topic.DbBlob,
551+
cmd.pub_data.DbBlob,
552+
pubkey,
591553
)
592-
else:
593-
# no one is waiting
594-
try:
595-
relay.db.exec(sql"INSERT INTO note (topic, data, src) VALUES (?, ?, ?)",
596-
cmd.pub_topic.DbBlob,
597-
cmd.pub_data.DbBlob,
598-
pubkey,
599-
)
600-
conn.sendOkay(cmd)
601-
except:
602-
conn.sendError(cmd, "Duplicate topic", Generic)
554+
conn.sendOkay(cmd)
555+
except:
556+
conn.sendError(cmd, "Duplicate topic", Generic)
603557
of FetchNote:
604558
if cmd.fetch_topic.len > RELAY_MAX_TOPIC_SIZE:
605559
conn.sendError(cmd, "Topic too long", TooLarge)
@@ -620,8 +574,8 @@ proc handleCommand*[T](relay: Relay[T], conn: var RelayConnection[T], cmd: Relay
620574
data_out = data.len,
621575
)
622576
else:
623-
# the note isn't here yet
624-
relay.addNoteSub(cmd.fetch_topic, conn.pubkey.get())
577+
# the note doesn't exist
578+
conn.sendError(cmd, "Topic not found", NotFound)
625579
of SendData:
626580
if cmd.send_val.len > RELAY_MAX_MESSAGE_SIZE:
627581
conn.sendError(cmd, "Data too long", TooLarge)

tests/tproto2.nim

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,9 @@ suite "notes":
247247
kind: FetchNote,
248248
fetch_topic: "heyo",
249249
))
250-
relay.handleCommand(alice, RelayCommand(
251-
kind: PublishNote,
252-
pub_topic: "heyo",
253-
pub_data: "foo",
254-
))
255-
check alice.pop(Okay).ok_cmd == PublishNote
256-
let note = alice.pop(Note)
257-
check note.note_data == "foo"
258-
check note.note_topic == "heyo"
250+
let err = alice.pop(Error)
251+
check err.err_cmd == FetchNote
252+
check err.err_code == NotFound
259253

260254
test "publish max size topic":
261255
let relay = testRelay()
@@ -308,7 +302,9 @@ suite "notes":
308302
kind: FetchNote,
309303
fetch_topic: "topic",
310304
))
311-
check alice.msgCount == 0
305+
let err = alice.pop(Error)
306+
check err.err_cmd == FetchNote
307+
check err.err_code == NotFound
312308

313309
test "fetch note again":
314310
let relay = testRelay()
@@ -333,7 +329,9 @@ suite "notes":
333329
kind: FetchNote,
334330
fetch_topic: "sometopic",
335331
))
336-
check alice.msgCount == 0
332+
let err = alice.pop(Error)
333+
check err.err_cmd == FetchNote
334+
check err.err_code == NotFound
337335

338336
test "sub then disconnect, the pub":
339337
let relay = testRelay()
@@ -344,6 +342,9 @@ suite "notes":
344342
kind: FetchNote,
345343
fetch_topic: "foo",
346344
))
345+
let err = bob.pop(Error)
346+
check err.err_cmd == FetchNote
347+
check err.err_code == NotFound
347348
relay.disconnect(bob)
348349

349350
relay.handleCommand(alice, RelayCommand(

0 commit comments

Comments
 (0)