Skip to content

Commit c3756e3

Browse files
chore: Improve binding for waku_sync (#3102)
1 parent 2e6c299 commit c3756e3

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

vendor/negentropy

waku/waku_sync/raw_bindings.nim

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ proc toBufferSeq(buffLen: uint, buffPtr: ptr Buffer): seq[Buffer] =
6767

6868
type NegentropyStorage* = distinct pointer
6969

70+
proc get_last_error(): cstring {.header: NEGENTROPY_HEADER, importc: "get_last_error".}
71+
7072
# https://github.com/waku-org/negentropy/blob/d4845b95b5a2d9bee28555833e7502db71bf319f/cpp/negentropy_wrapper.h#L27
7173
proc storage_init(
7274
db_path: cstring, name: cstring
@@ -194,6 +196,10 @@ proc new*(T: type NegentropyStorage): Result[T, string] =
194196
#[ TODO: Uncomment once we move to lmdb
195197
if storage == nil:
196198
return err("storage initialization failed") ]#
199+
200+
if storage == nil:
201+
return err($get_last_error())
202+
197203
return ok(storage)
198204

199205
proc delete*(storage: NegentropyStorage) =
@@ -211,7 +217,7 @@ proc erase*(
211217
if res:
212218
return ok()
213219
else:
214-
return err("erase error")
220+
return err($get_last_error())
215221

216222
proc insert*(
217223
storage: NegentropyStorage, id: int64, hash: WakuMessageHash
@@ -225,7 +231,7 @@ proc insert*(
225231
if res:
226232
return ok()
227233
else:
228-
return err("insert error")
234+
return err($get_last_error())
229235

230236
proc len*(storage: NegentropyStorage): int =
231237
int(storage.size)
@@ -245,6 +251,10 @@ proc new*(
245251
#[ TODO: Uncomment once we move to lmdb
246252
if storage == nil:
247253
return err("storage initialization failed") ]#
254+
255+
if subrange == nil:
256+
return err($get_last_error())
257+
248258
return ok(subrange)
249259

250260
proc delete*(subrange: NegentropySubRangeStorage) =
@@ -296,16 +306,20 @@ proc new*(
296306
let raw_negentropy =
297307
constructNegentropy(NegentropyStorage(storage), uint64(frameSizeLimit))
298308

299-
let negentropy = NegentropyWithStorage(inner: raw_negentropy)
309+
if cast[pointer](raw_negentropy) == nil:
310+
return err($get_last_error())
300311

312+
let negentropy = NegentropyWithStorage(inner: raw_negentropy)
301313
return ok(negentropy)
302314
elif storage is NegentropySubRangeStorage:
303315
let raw_negentropy = constructNegentropyWithSubRange(
304316
NegentropySubRangeStorage(storage), uint64(frameSizeLimit)
305317
)
306318

307-
let negentropy = NegentropyWithSubRange(inner: raw_negentropy)
319+
if cast[pointer](raw_negentropy) == nil:
320+
return err($get_last_error())
308321

322+
let negentropy = NegentropyWithSubRange(inner: raw_negentropy)
309323
return ok(negentropy)
310324

311325
method delete*(self: NegentropyWithSubRange) =
@@ -319,7 +333,7 @@ method initiate*(self: NegentropyWithSubRange): Result[NegentropyPayload, string
319333
let ret = self.inner.raw_initiate_subrange(myResultPtr)
320334
if ret < 0 or myResultPtr == nil:
321335
error "negentropy initiate failed with code ", code = ret
322-
return err("negentropy already initiated!")
336+
return err($get_last_error())
323337
let bytes: seq[byte] = bufferToBytes(addr(myResultPtr.output))
324338
free_result(myResultPtr)
325339
trace "received return from initiate", len = myResultPtr.output.len
@@ -340,7 +354,7 @@ method serverReconcile*(
340354
let ret = self.inner.raw_reconcile_subrange(queryBufPtr, myResultPtr)
341355
if ret < 0:
342356
error "raw_reconcile failed with code ", code = ret
343-
return err($myResultPtr.error)
357+
return err($get_last_error())
344358
trace "received return from raw_reconcile", len = myResultPtr.output.len
345359

346360
let outputBytes: seq[byte] = bufferToBytes(addr(myResultPtr.output))
@@ -368,7 +382,7 @@ method clientReconcile*(
368382
let ret = self.inner.raw_reconcile_with_ids_subrange(cQuery.unsafeAddr, myResultPtr)
369383
if ret < 0:
370384
error "raw_reconcile failed with code ", code = ret
371-
return err($myResultPtr.error)
385+
return err($get_last_error())
372386

373387
let output = bufferToBytes(addr myResult.output)
374388

@@ -414,7 +428,7 @@ method initiate*(self: NegentropyWithStorage): Result[NegentropyPayload, string]
414428
let ret = self.inner.raw_initiate(myResultPtr)
415429
if ret < 0 or myResultPtr == nil:
416430
error "negentropy initiate failed with code ", code = ret
417-
return err("negentropy already initiated!")
431+
return err($get_last_error())
418432
let bytes: seq[byte] = bufferToBytes(addr(myResultPtr.output))
419433
free_result(myResultPtr)
420434
trace "received return from initiate", len = myResultPtr.output.len
@@ -435,7 +449,7 @@ method serverReconcile*(
435449
let ret = self.inner.raw_reconcile(queryBufPtr, myResultPtr)
436450
if ret < 0:
437451
error "raw_reconcile failed with code ", code = ret
438-
return err($myResultPtr.error)
452+
return err($get_last_error())
439453
trace "received return from raw_reconcile", len = myResultPtr.output.len
440454

441455
let outputBytes: seq[byte] = bufferToBytes(addr(myResultPtr.output))
@@ -463,7 +477,7 @@ method clientReconcile*(
463477
let ret = self.inner.raw_reconcile_with_ids(cQuery.unsafeAddr, myResultPtr)
464478
if ret < 0:
465479
error "raw_reconcile failed with code ", code = ret
466-
return err($myResultPtr.error)
480+
return err($get_last_error())
467481

468482
let output = bufferToBytes(addr myResult.output)
469483

0 commit comments

Comments
 (0)