From c1bdead94ddf74948f4e8c96e344458ba42d1c9d Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 12 Oct 2021 09:12:54 -0400 Subject: [PATCH] revert: utils.GetUnixEpoch() --- examples/basic2/main.go | 3 ++- examples/chat2/chat.go | 3 ++- examples/filter2/main.go | 3 ++- examples/peer_events/main.go | 3 ++- tests/connection_test.go | 4 ++-- waku/v2/protocol/store/waku_store.go | 5 ++--- waku/v2/utils/time.go | 7 +++++++ 7 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 waku/v2/utils/time.go diff --git a/examples/basic2/main.go b/examples/basic2/main.go index d4837c25..734c7567 100644 --- a/examples/basic2/main.go +++ b/examples/basic2/main.go @@ -15,6 +15,7 @@ import ( logging "github.com/ipfs/go-log" "github.com/status-im/go-waku/waku/v2/node" "github.com/status-im/go-waku/waku/v2/protocol/pb" + "github.com/status-im/go-waku/waku/v2/utils" ) var log = logging.Logger("basic2") @@ -77,7 +78,7 @@ func randomHex(n int) (string, error) { func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) { var contentTopic string = "test" var version uint32 = 0 - var timestamp float64 = float64(time.Now().Unix()) + var timestamp float64 = utils.GetUnixEpoch() p := new(node.Payload) p.Data = []byte(wakuNode.ID() + ": " + msgContent) diff --git a/examples/chat2/chat.go b/examples/chat2/chat.go index 1b5f67f0..c565d575 100644 --- a/examples/chat2/chat.go +++ b/examples/chat2/chat.go @@ -13,6 +13,7 @@ import ( "github.com/status-im/go-waku/waku/v2/protocol/filter" wpb "github.com/status-im/go-waku/waku/v2/protocol/pb" "github.com/status-im/go-waku/waku/v2/protocol/relay" + "github.com/status-im/go-waku/waku/v2/utils" "golang.org/x/crypto/pbkdf2" ) @@ -84,7 +85,7 @@ func (cr *Chat) Publish(ctx context.Context, message string) error { } var version uint32 - var timestamp float64 = float64(time.Now().Unix()) + var timestamp float64 = utils.GetUnixEpoch() var keyInfo *node.KeyInfo = &node.KeyInfo{} if cr.useV1Payload { // Use WakuV1 encryption diff --git a/examples/filter2/main.go b/examples/filter2/main.go index f2394485..70920ead 100644 --- a/examples/filter2/main.go +++ b/examples/filter2/main.go @@ -17,6 +17,7 @@ import ( "github.com/status-im/go-waku/waku/v2/protocol/filter" "github.com/status-im/go-waku/waku/v2/protocol/pb" "github.com/status-im/go-waku/waku/v2/protocol/relay" + "github.com/status-im/go-waku/waku/v2/utils" ) var log = logging.Logger("filter2") @@ -125,7 +126,7 @@ func randomHex(n int) (string, error) { func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) { var version uint32 = 0 - var timestamp float64 = float64(time.Now().UnixNano()) + var timestamp float64 = utils.GetUnixEpoch() p := new(node.Payload) p.Data = []byte(wakuNode.ID() + ": " + msgContent) diff --git a/examples/peer_events/main.go b/examples/peer_events/main.go index 1335b4cb..b28e5d3c 100644 --- a/examples/peer_events/main.go +++ b/examples/peer_events/main.go @@ -17,6 +17,7 @@ import ( "github.com/status-im/go-waku/waku/v2/node" "github.com/status-im/go-waku/waku/v2/protocol/pb" "github.com/status-im/go-waku/waku/v2/protocol/relay" + "github.com/status-im/go-waku/waku/v2/utils" //"github.com/status-im/go-waku/waku/v2/protocol/store" ) @@ -182,7 +183,7 @@ func randomHex(n int) (string, error) { func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) { var version uint32 = 0 - var timestamp float64 = float64(time.Now().Unix()) + var timestamp float64 = utils.GetUnixEpoch() p := new(node.Payload) p.Data = []byte(wakuNode.ID() + ": " + msgContent) diff --git a/tests/connection_test.go b/tests/connection_test.go index 88eb837f..eefd45cb 100644 --- a/tests/connection_test.go +++ b/tests/connection_test.go @@ -6,13 +6,13 @@ import ( "encoding/hex" "net" "testing" - "time" "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/require" "github.com/status-im/go-waku/waku/v2/node" "github.com/status-im/go-waku/waku/v2/protocol/pb" + "github.com/status-im/go-waku/waku/v2/utils" ) func TestBasicSendingReceiving(t *testing.T) { @@ -61,7 +61,7 @@ func randomHex(n int) (string, error) { func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) error { var contentTopic string = "test" var version uint32 = 0 - var timestamp float64 = float64(time.Now().Unix()) + var timestamp float64 = utils.GetUnixEpoch() p := new(node.Payload) p.Data = []byte(wakuNode.ID() + ": " + msgContent) diff --git a/waku/v2/protocol/store/waku_store.go b/waku/v2/protocol/store/waku_store.go index 45560d25..2f428ead 100644 --- a/waku/v2/protocol/store/waku_store.go +++ b/waku/v2/protocol/store/waku_store.go @@ -10,7 +10,6 @@ import ( "math" "sort" "sync" - "time" logging "github.com/ipfs/go-log" "github.com/libp2p/go-libp2p-core/host" @@ -360,7 +359,7 @@ func computeIndex(msg *pb.WakuMessage) (*pb.Index, error) { digest := sha256.Sum256(data) return &pb.Index{ Digest: digest[:], - ReceiverTime: float64(time.Now().Unix()), + ReceiverTime: utils.GetUnixEpoch(), SenderTime: msg.Timestamp, }, nil } @@ -575,7 +574,7 @@ func (store *WakuStore) findLastSeen() float64 { // the resume proc returns the number of retrieved messages if no error occurs, otherwise returns the error string func (store *WakuStore) Resume(ctx context.Context, pubsubTopic string, peerList []peer.ID) (int, error) { - currentTime := float64(time.Now().UnixNano()) + currentTime := utils.GetUnixEpoch() lastSeenTime := store.findLastSeen() var offset float64 = 200000 diff --git a/waku/v2/utils/time.go b/waku/v2/utils/time.go new file mode 100644 index 00000000..2d7b0aba --- /dev/null +++ b/waku/v2/utils/time.go @@ -0,0 +1,7 @@ +package utils + +import "time" + +func GetUnixEpoch() float64 { + return float64(time.Now().UnixNano()) / float64(time.Second) +}