diff --git a/packages/core/src/lib/store/history_rpc.ts b/packages/core/src/lib/store/history_rpc.ts index 8f0a971f0e..56dfbf8ede 100644 --- a/packages/core/src/lib/store/history_rpc.ts +++ b/packages/core/src/lib/store/history_rpc.ts @@ -20,7 +20,7 @@ export interface Params { } export class HistoryRPC { - private constructor(public readonly proto: proto.HistoryRPC) {} + private constructor(public readonly proto: proto.HistoryRpc) {} get query(): proto.HistoryQuery | undefined { return this.proto.query; @@ -59,7 +59,7 @@ export class HistoryRPC { return new HistoryRPC({ requestId: uuid(), query: { - pubSubTopic: params.pubSubTopic, + pubsubTopic: params.pubSubTopic, contentFilters, pagingInfo, startTime, @@ -70,12 +70,12 @@ export class HistoryRPC { } decode(bytes: Uint8ArrayList): HistoryRPC { - const res = proto.HistoryRPC.decode(bytes); + const res = proto.HistoryRpc.decode(bytes); return new HistoryRPC(res); } encode(): Uint8Array { - return proto.HistoryRPC.encode(this.proto); + return proto.HistoryRpc.encode(this.proto); } } @@ -84,10 +84,10 @@ function directionToProto( ): proto.PagingInfo.Direction { switch (pageDirection) { case PageDirection.BACKWARD: - return proto.PagingInfo.Direction.DIRECTION_BACKWARD_UNSPECIFIED; + return proto.PagingInfo.Direction.BACKWARD; case PageDirection.FORWARD: - return proto.PagingInfo.Direction.DIRECTION_FORWARD; + return proto.PagingInfo.Direction.FORWARD; default: - return proto.PagingInfo.Direction.DIRECTION_BACKWARD_UNSPECIFIED; + return proto.PagingInfo.Direction.BACKWARD; } } diff --git a/packages/core/src/lib/store/index.ts b/packages/core/src/lib/store/index.ts index 9e4e3129fd..13364b9cf1 100644 --- a/packages/core/src/lib/store/index.ts +++ b/packages/core/src/lib/store/index.ts @@ -6,7 +6,6 @@ import { Cursor, IDecodedMessage, IDecoder, - Index, IStore, ProtocolCreateOptions, } from "@waku/interfaces"; @@ -294,10 +293,7 @@ async function* paginate( const response = reply.response as proto.HistoryResponse; - if ( - response.error && - response.error !== HistoryError.ERROR_NONE_UNSPECIFIED - ) { + if (response.error && response.error !== HistoryError.NONE) { throw "History response contains an Error: " + response.error; } @@ -353,7 +349,7 @@ export function isDefined(msg: T | undefined): msg is T { export async function createCursor( message: IDecodedMessage, pubsubTopic: string = DefaultPubSubTopic -): Promise { +): Promise { if ( !message || !message.timestamp || @@ -373,7 +369,7 @@ export async function createCursor( digest, pubsubTopic, senderTime: messageTime, - receivedTime: messageTime, + receiverTime: messageTime, }; } diff --git a/packages/interfaces/src/store.ts b/packages/interfaces/src/store.ts index 77dd9467df..e1d499e141 100644 --- a/packages/interfaces/src/store.ts +++ b/packages/interfaces/src/store.ts @@ -11,19 +11,13 @@ export interface TimeFilter { endTime: Date; } -export interface Index { - digest?: Uint8Array; - receivedTime?: bigint; - senderTime?: bigint; - pubsubTopic?: string; +export interface Cursor { + digest: Uint8Array; + receiverTime: bigint; + senderTime: bigint; + pubsubTopic: string; } -export type Cursor = { - digest?: Uint8Array; - senderTime?: bigint; - pubsubTopic?: string; -}; - export type StoreQueryOptions = { /** * The direction in which pages are retrieved: @@ -45,7 +39,8 @@ export type StoreQueryOptions = { */ timeFilter?: TimeFilter; /** - * Cursor as an index to start a query from. + * Cursor as an index to start a query from. Must be generated from a Waku + * Message. */ cursor?: Cursor; } & ProtocolOptions; diff --git a/packages/proto/src/lib/store.proto b/packages/proto/src/lib/store.proto index db6cefb98b..41b24f734c 100644 --- a/packages/proto/src/lib/store.proto +++ b/packages/proto/src/lib/store.proto @@ -1,30 +1,34 @@ +// 13/WAKU2-STORE rfc: https://rfc.vac.dev/spec/13/ +// Protocol identifier: /vac/waku/store/2.0.0-beta4 + syntax = "proto3"; import "message.proto"; message Index { - optional bytes digest = 1; - optional sint64 received_time = 2; - optional sint64 sender_time = 3; - optional string pubsub_topic = 4; + bytes digest = 1; + sint64 receiver_time = 2; + sint64 sender_time = 3; + string pubsub_topic = 4; } message PagingInfo { optional uint64 page_size = 1; optional Index cursor = 2; enum Direction { - DIRECTION_BACKWARD_UNSPECIFIED = 0; - DIRECTION_FORWARD = 1; + BACKWARD = 0; + FORWARD = 1; } optional Direction direction = 3; } message ContentFilter { - optional string content_topic = 1; + string content_topic = 1; } message HistoryQuery { - optional string pub_sub_topic = 2; + // The first field is reserved for future use + optional string pubsub_topic = 2; repeated ContentFilter content_filters = 3; optional PagingInfo paging_info = 4; optional sint64 start_time = 5; @@ -32,17 +36,18 @@ message HistoryQuery { } message HistoryResponse { + // The first field is reserved for future use repeated WakuMessage messages = 2; optional PagingInfo paging_info = 3; enum HistoryError { - ERROR_NONE_UNSPECIFIED = 0; - ERROR_INVALID_CURSOR = 1; + NONE = 0; + INVALID_CURSOR = 1; } - optional HistoryError error = 4; + HistoryError error = 4; } -message HistoryRPC { - optional string request_id = 1; +message HistoryRpc { + string request_id = 1; optional HistoryQuery query = 2; optional HistoryResponse response = 3; } diff --git a/packages/proto/src/lib/store.ts b/packages/proto/src/lib/store.ts index 43b36657e8..6d9a663501 100644 --- a/packages/proto/src/lib/store.ts +++ b/packages/proto/src/lib/store.ts @@ -14,10 +14,10 @@ import type { Codec } from "protons-runtime"; import type { Uint8ArrayList } from "uint8arraylist"; export interface Index { - digest?: Uint8Array; - receivedTime?: bigint; - senderTime?: bigint; - pubsubTopic?: string; + digest: Uint8Array; + receiverTime: bigint; + senderTime: bigint; + pubsubTopic: string; } export namespace Index { @@ -31,22 +31,22 @@ export namespace Index { w.fork(); } - if (obj.digest != null) { + if (obj.digest != null && obj.digest.byteLength > 0) { w.uint32(10); w.bytes(obj.digest); } - if (obj.receivedTime != null) { + if (obj.receiverTime != null && obj.receiverTime !== 0n) { w.uint32(16); - w.sint64(obj.receivedTime); + w.sint64(obj.receiverTime); } - if (obj.senderTime != null) { + if (obj.senderTime != null && obj.senderTime !== 0n) { w.uint32(24); w.sint64(obj.senderTime); } - if (obj.pubsubTopic != null) { + if (obj.pubsubTopic != null && obj.pubsubTopic !== "") { w.uint32(34); w.string(obj.pubsubTopic); } @@ -56,7 +56,12 @@ export namespace Index { } }, (reader, length) => { - const obj: any = {}; + const obj: any = { + digest: new Uint8Array(0), + receiverTime: 0n, + senderTime: 0n, + pubsubTopic: "", + }; const end = length == null ? reader.len : reader.pos + length; @@ -68,7 +73,7 @@ export namespace Index { obj.digest = reader.bytes(); break; case 2: - obj.receivedTime = reader.sint64(); + obj.receiverTime = reader.sint64(); break; case 3: obj.senderTime = reader.sint64(); @@ -107,13 +112,13 @@ export interface PagingInfo { export namespace PagingInfo { export enum Direction { - DIRECTION_BACKWARD_UNSPECIFIED = "DIRECTION_BACKWARD_UNSPECIFIED", - DIRECTION_FORWARD = "DIRECTION_FORWARD", + BACKWARD = "BACKWARD", + FORWARD = "FORWARD", } enum __DirectionValues { - DIRECTION_BACKWARD_UNSPECIFIED = 0, - DIRECTION_FORWARD = 1, + BACKWARD = 0, + FORWARD = 1, } export namespace Direction { @@ -193,7 +198,7 @@ export namespace PagingInfo { } export interface ContentFilter { - contentTopic?: string; + contentTopic: string; } export namespace ContentFilter { @@ -207,7 +212,7 @@ export namespace ContentFilter { w.fork(); } - if (obj.contentTopic != null) { + if (obj.contentTopic != null && obj.contentTopic !== "") { w.uint32(10); w.string(obj.contentTopic); } @@ -217,7 +222,9 @@ export namespace ContentFilter { } }, (reader, length) => { - const obj: any = {}; + const obj: any = { + contentTopic: "", + }; const end = length == null ? reader.len : reader.pos + length; @@ -252,7 +259,7 @@ export namespace ContentFilter { } export interface HistoryQuery { - pubSubTopic?: string; + pubsubTopic?: string; contentFilters: ContentFilter[]; pagingInfo?: PagingInfo; startTime?: bigint; @@ -270,9 +277,9 @@ export namespace HistoryQuery { w.fork(); } - if (obj.pubSubTopic != null) { + if (obj.pubsubTopic != null) { w.uint32(18); - w.string(obj.pubSubTopic); + w.string(obj.pubsubTopic); } if (obj.contentFilters != null) { @@ -313,7 +320,7 @@ export namespace HistoryQuery { switch (tag >>> 3) { case 2: - obj.pubSubTopic = reader.string(); + obj.pubsubTopic = reader.string(); break; case 3: obj.contentFilters.push( @@ -358,18 +365,18 @@ export namespace HistoryQuery { export interface HistoryResponse { messages: WakuMessage[]; pagingInfo?: PagingInfo; - error?: HistoryResponse.HistoryError; + error: HistoryResponse.HistoryError; } export namespace HistoryResponse { export enum HistoryError { - ERROR_NONE_UNSPECIFIED = "ERROR_NONE_UNSPECIFIED", - ERROR_INVALID_CURSOR = "ERROR_INVALID_CURSOR", + NONE = "NONE", + INVALID_CURSOR = "INVALID_CURSOR", } enum __HistoryErrorValues { - ERROR_NONE_UNSPECIFIED = 0, - ERROR_INVALID_CURSOR = 1, + NONE = 0, + INVALID_CURSOR = 1, } export namespace HistoryError { @@ -400,7 +407,7 @@ export namespace HistoryResponse { PagingInfo.codec().encode(obj.pagingInfo, w); } - if (obj.error != null) { + if (obj.error != null && __HistoryErrorValues[obj.error] !== 0) { w.uint32(32); HistoryResponse.HistoryError.codec().encode(obj.error, w); } @@ -412,6 +419,7 @@ export namespace HistoryResponse { (reader, length) => { const obj: any = { messages: [], + error: HistoryError.NONE, }; const end = length == null ? reader.len : reader.pos + length; @@ -457,24 +465,24 @@ export namespace HistoryResponse { }; } -export interface HistoryRPC { - requestId?: string; +export interface HistoryRpc { + requestId: string; query?: HistoryQuery; response?: HistoryResponse; } -export namespace HistoryRPC { - let _codec: Codec; +export namespace HistoryRpc { + let _codec: Codec; - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message( + _codec = message( (obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork(); } - if (obj.requestId != null) { + if (obj.requestId != null && obj.requestId !== "") { w.uint32(10); w.string(obj.requestId); } @@ -494,7 +502,9 @@ export namespace HistoryRPC { } }, (reader, length) => { - const obj: any = {}; + const obj: any = { + requestId: "", + }; const end = length == null ? reader.len : reader.pos + length; @@ -531,12 +541,12 @@ export namespace HistoryRPC { return _codec; }; - export const encode = (obj: Partial): Uint8Array => { - return encodeMessage(obj, HistoryRPC.codec()); + export const encode = (obj: Partial): Uint8Array => { + return encodeMessage(obj, HistoryRpc.codec()); }; - export const decode = (buf: Uint8Array | Uint8ArrayList): HistoryRPC => { - return decodeMessage(buf, HistoryRPC.codec()); + export const decode = (buf: Uint8Array | Uint8ArrayList): HistoryRpc => { + return decodeMessage(buf, HistoryRpc.codec()); }; }