Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Improve binding for waku_sync #3102

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vendor/negentropy
34 changes: 24 additions & 10 deletions waku/waku_sync/raw_bindings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ proc toBufferSeq(buffLen: uint, buffPtr: ptr Buffer): seq[Buffer] =

type NegentropyStorage* = distinct pointer

proc get_last_error(): cstring {.header: NEGENTROPY_HEADER, importc: "get_last_error".}

# https://github.com/waku-org/negentropy/blob/d4845b95b5a2d9bee28555833e7502db71bf319f/cpp/negentropy_wrapper.h#L27
proc storage_init(
db_path: cstring, name: cstring
Expand Down Expand Up @@ -194,6 +196,10 @@ proc new*(T: type NegentropyStorage): Result[T, string] =
#[ TODO: Uncomment once we move to lmdb
if storage == nil:
return err("storage initialization failed") ]#

if storage == nil:
return err($get_last_error())

return ok(storage)

proc delete*(storage: NegentropyStorage) =
Expand All @@ -211,7 +217,7 @@ proc erase*(
if res:
return ok()
else:
return err("erase error")
return err($get_last_error())

proc insert*(
storage: NegentropyStorage, id: int64, hash: WakuMessageHash
Expand All @@ -225,7 +231,7 @@ proc insert*(
if res:
return ok()
else:
return err("insert error")
return err($get_last_error())

proc len*(storage: NegentropyStorage): int =
int(storage.size)
Expand All @@ -245,6 +251,10 @@ proc new*(
#[ TODO: Uncomment once we move to lmdb
if storage == nil:
return err("storage initialization failed") ]#

if subrange == nil:
return err($get_last_error())

return ok(subrange)

proc delete*(subrange: NegentropySubRangeStorage) =
Expand Down Expand Up @@ -296,16 +306,20 @@ proc new*(
let raw_negentropy =
constructNegentropy(NegentropyStorage(storage), uint64(frameSizeLimit))

let negentropy = NegentropyWithStorage(inner: raw_negentropy)
if cast[pointer](raw_negentropy) == nil:
return err($get_last_error())

let negentropy = NegentropyWithStorage(inner: raw_negentropy)
return ok(negentropy)
elif storage is NegentropySubRangeStorage:
let raw_negentropy = constructNegentropyWithSubRange(
NegentropySubRangeStorage(storage), uint64(frameSizeLimit)
)

let negentropy = NegentropyWithSubRange(inner: raw_negentropy)
if cast[pointer](raw_negentropy) == nil:
return err($get_last_error())

let negentropy = NegentropyWithSubRange(inner: raw_negentropy)
return ok(negentropy)

method delete*(self: NegentropyWithSubRange) =
Expand All @@ -319,7 +333,7 @@ method initiate*(self: NegentropyWithSubRange): Result[NegentropyPayload, string
let ret = self.inner.raw_initiate_subrange(myResultPtr)
if ret < 0 or myResultPtr == nil:
error "negentropy initiate failed with code ", code = ret
return err("negentropy already initiated!")
return err($get_last_error())
let bytes: seq[byte] = bufferToBytes(addr(myResultPtr.output))
free_result(myResultPtr)
trace "received return from initiate", len = myResultPtr.output.len
Expand All @@ -340,7 +354,7 @@ method serverReconcile*(
let ret = self.inner.raw_reconcile_subrange(queryBufPtr, myResultPtr)
if ret < 0:
error "raw_reconcile failed with code ", code = ret
return err($myResultPtr.error)
return err($get_last_error())
trace "received return from raw_reconcile", len = myResultPtr.output.len

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

let output = bufferToBytes(addr myResult.output)

Expand Down Expand Up @@ -414,7 +428,7 @@ method initiate*(self: NegentropyWithStorage): Result[NegentropyPayload, string]
let ret = self.inner.raw_initiate(myResultPtr)
if ret < 0 or myResultPtr == nil:
error "negentropy initiate failed with code ", code = ret
return err("negentropy already initiated!")
return err($get_last_error())
let bytes: seq[byte] = bufferToBytes(addr(myResultPtr.output))
free_result(myResultPtr)
trace "received return from initiate", len = myResultPtr.output.len
Expand All @@ -435,7 +449,7 @@ method serverReconcile*(
let ret = self.inner.raw_reconcile(queryBufPtr, myResultPtr)
if ret < 0:
error "raw_reconcile failed with code ", code = ret
return err($myResultPtr.error)
return err($get_last_error())
trace "received return from raw_reconcile", len = myResultPtr.output.len

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

let output = bufferToBytes(addr myResult.output)

Expand Down
Loading