2022-11-22 18:40:24 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/options,
|
|
|
|
stew/results,
|
|
|
|
stew/byteutils,
|
|
|
|
nimcrypto/sha2
|
|
|
|
import
|
2023-12-19 14:10:27 +00:00
|
|
|
../waku_core,
|
|
|
|
../common/paging
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Waku message digest
|
|
|
|
# TODO: Move this into the driver implementations. We should be passing
|
|
|
|
# here a buffer containing a serialized implementation dependent cursor.
|
|
|
|
|
|
|
|
type MessageDigest* = MDigest[256]
|
|
|
|
|
2023-11-08 00:41:23 +00:00
|
|
|
proc computeDigest*(msg: WakuMessage): MessageDigest =
|
2022-11-22 18:40:24 +00:00
|
|
|
var ctx: sha256
|
|
|
|
ctx.init()
|
|
|
|
defer: ctx.clear()
|
|
|
|
|
2023-10-19 09:59:17 +00:00
|
|
|
ctx.update(msg.contentTopic.toBytes())
|
2023-11-08 00:41:23 +00:00
|
|
|
ctx.update(msg.payload)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
# Computes the hash
|
|
|
|
return ctx.finish()
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: Move this into the driver implementations. We should be passing
|
|
|
|
# here a buffer containing a serialized implementation dependent cursor.
|
|
|
|
type DbCursor = object
|
|
|
|
pubsubTopic*: PubsubTopic
|
|
|
|
senderTime*: Timestamp
|
|
|
|
storeTime*: Timestamp
|
|
|
|
digest*: MessageDigest
|
|
|
|
|
|
|
|
|
|
|
|
## Public API types
|
|
|
|
|
|
|
|
type
|
|
|
|
# TODO: We should be passing here a buffer containing a serialized
|
|
|
|
# implementation dependent cursor. Waku archive logic should be independent
|
|
|
|
# of the cursor format.
|
|
|
|
ArchiveCursor* = DbCursor
|
|
|
|
|
|
|
|
ArchiveQuery* = object
|
|
|
|
pubsubTopic*: Option[PubsubTopic]
|
|
|
|
contentTopics*: seq[ContentTopic]
|
|
|
|
cursor*: Option[ArchiveCursor]
|
|
|
|
startTime*: Option[Timestamp]
|
|
|
|
endTime*: Option[Timestamp]
|
|
|
|
pageSize*: uint
|
2023-12-19 14:10:27 +00:00
|
|
|
direction*: PagingDirection
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
ArchiveResponse* = object
|
|
|
|
messages*: seq[WakuMessage]
|
|
|
|
cursor*: Option[ArchiveCursor]
|
|
|
|
|
|
|
|
ArchiveErrorKind* {.pure.} = enum
|
|
|
|
UNKNOWN = uint32(0)
|
|
|
|
DRIVER_ERROR = uint32(1)
|
|
|
|
INVALID_QUERY = uint32(2)
|
|
|
|
|
|
|
|
ArchiveError* = object
|
|
|
|
case kind*: ArchiveErrorKind
|
|
|
|
of DRIVER_ERROR, INVALID_QUERY:
|
|
|
|
# TODO: Add an enum to be able to distinguish between error causes
|
|
|
|
cause*: string
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
ArchiveResult* = Result[ArchiveResponse, ArchiveError]
|
|
|
|
|
|
|
|
|
|
|
|
proc `$`*(err: ArchiveError): string =
|
|
|
|
case err.kind:
|
|
|
|
of ArchiveErrorKind.DRIVER_ERROR:
|
|
|
|
"DIRVER_ERROR: " & err.cause
|
|
|
|
of ArchiveErrorKind.INVALID_QUERY:
|
|
|
|
"INVALID_QUERY: " & err.cause
|
|
|
|
of ArchiveErrorKind.UNKNOWN:
|
|
|
|
"UNKNOWN"
|
2023-01-11 11:19:59 +00:00
|
|
|
|
|
|
|
proc invalidQuery*(T: type ArchiveError, cause: string): T =
|
|
|
|
ArchiveError(kind: ArchiveErrorKind.INVALID_QUERY, cause: cause)
|