chore: Improve binding for waku_sync (#3102)

This commit is contained in:
Darshan K 2024-10-10 14:17:33 +05:30 committed by GitHub
parent 2e6c299d36
commit c3756e3a8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 11 deletions

2
vendor/negentropy vendored

@ -1 +1 @@
Subproject commit 13243f668edb85ef4b660e40833d81435501325f
Subproject commit 449b304d67e9401bd96451cb349eb6b17b713f67

View File

@ -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
@ -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) =
@ -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
@ -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)
@ -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) =
@ -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) =
@ -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
@ -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))
@ -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)
@ -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
@ -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))
@ -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)