From 7491250a2a8113ae0785490c6dab8ea97bbdc5fc Mon Sep 17 00:00:00 2001 From: Hanno Cornelius <68783915+jm-clius@users.noreply.github.com> Date: Mon, 17 Jan 2022 19:37:06 +0100 Subject: [PATCH] Round receiver time (#815) --- CHANGELOG.md | 1 + waku/v2/protocol/waku_store/waku_store.nim | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 087683a84..4b12034f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ which is a sequence of string. - Increased maximum length for reading from a libp2p input stream to allow largest possible protocol messages, including `HistoryResponse` messages at max size. - Significantly improved store node query performance - Added GossipSub `MessageIdProvider` for `11/WAKU2-RELAY` messages. +- Store: timestamps of message reception, used for indexing, now have consistent millisecond resolution. ## 2021-11-05 v0.6 diff --git a/waku/v2/protocol/waku_store/waku_store.nim b/waku/v2/protocol/waku_store/waku_store.nim index ecbab596c..c9294085c 100644 --- a/waku/v2/protocol/waku_store/waku_store.nim +++ b/waku/v2/protocol/waku_store/waku_store.nim @@ -7,7 +7,7 @@ # Group by std, external then internal imports import # std imports - std/[tables, times, sequtils, algorithm, options], + std/[tables, times, sequtils, algorithm, options, math], # external imports bearssl, chronicles, @@ -55,15 +55,18 @@ const # TODO Move serialization function to separate file, too noisy # TODO Move pagination to separate file, self-contained logic -proc computeIndex*(msg: WakuMessage): Index = - ## Takes a WakuMessage and returns its Index +proc computeIndex*(msg: WakuMessage, receivedTime = getTime().toUnixFloat()): Index = + ## Takes a WakuMessage with received timestamp and returns its Index. + ## Received timestamp will default to system time if not provided. var ctx: sha256 ctx.init() ctx.update(msg.contentTopic.toBytes()) # converts the contentTopic to bytes ctx.update(msg.payload) let digest = ctx.finish() # computes the hash ctx.clear() - var index = Index(digest:digest, receiverTime: epochTime(), senderTime: msg.timestamp) + + let receiverTime = receivedTime.round(3) # Ensure timestamp has (only) millisecond resolution + var index = Index(digest:digest, receiverTime: receiverTime, senderTime: msg.timestamp) return index proc encode*(index: Index): ProtoBuffer = @@ -466,7 +469,7 @@ proc init*(ws: WakuStore, capacity = DefaultStoreCapacity) = proc onData(receiverTime: float64, msg: WakuMessage, pubsubTopic: string) = # TODO index should not be recalculated - ws.messages.add(IndexedWakuMessage(msg: msg, index: msg.computeIndex(), pubsubTopic: pubsubTopic)) + ws.messages.add(IndexedWakuMessage(msg: msg, index: msg.computeIndex(receiverTime), pubsubTopic: pubsubTopic)) info "attempting to load messages from persistent storage"