From f8029e73594a5b9fa15c2a99baf1216165085c16 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 18 May 2020 14:49:49 -0600 Subject: [PATCH] use sha256 digest as cache keys (#135) * use sha256 digest as cache keys * rebasing master --- libp2p/protocols/pubsub/gossipsub.nim | 2 +- libp2p/protocols/pubsub/pubsubpeer.nim | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index 4f7e412..9036183 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -391,7 +391,7 @@ method publish*(g: GossipSub, await procCall PubSub(g).publish(topic, data) trace "about to publish message on topic", name = topic, - data = data.toHex() + data = data.shortLog if data.len > 0 and topic.len > 0: var peers: HashSet[string] if topic in g.topics: # if we're subscribed to the topic attempt to build a mesh diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index 3b761d9..e81aa1a 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -8,7 +8,7 @@ ## those terms. import options, hashes, strutils, tables, hashes -import chronos, chronicles +import chronos, chronicles, nimcrypto/sha2 import rpc/[messages, message, protobuf], timedcache, ../../peer, @@ -57,9 +57,9 @@ proc handle*(p: PubSubPeer, conn: Connection) {.async.} = while not conn.closed: trace "waiting for data", peer = p.id, closed = conn.closed let data = await conn.readLp(64 * 1024) - let hexData = data.toHex() + let digest = $(sha256.digest(data)) trace "read data from peer", peer = p.id, data = data.shortLog - if $hexData.hash in p.recvdRpcCache: + if digest in p.recvdRpcCache: trace "message already received, skipping", peer = p.id continue @@ -69,7 +69,7 @@ proc handle*(p: PubSubPeer, conn: Connection) {.async.} = for obs in p.observers[]: obs.onRecv(p, msg) await p.handler(p, @[msg]) - p.recvdRpcCache.put($hexData.hash) + p.recvdRpcCache.put(digest) except CatchableError as exc: trace "Exception occurred in PubSubPeer.handle", exc = exc.msg finally: @@ -87,19 +87,21 @@ proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} = var mm = m for obs in p.observers[]: obs.onSend(p, mm) - let encodedHex = encoded.buffer.toHex() + if encoded.buffer.len <= 0: trace "empty message, skipping", peer = p.id return - if $encodedHex.hash in p.sentRpcCache: + let digest = $(sha256.digest(encoded.buffer)) + if digest in p.sentRpcCache: trace "message already sent to peer, skipping", peer = p.id continue proc sendToRemote() {.async.} = - trace "sending encoded msgs to peer", peer = p.id, encoded = encoded.buffer.shortLog + trace "sending encoded msgs to peer", peer = p.id, + encoded = encoded.buffer.shortLog await p.sendConn.writeLp(encoded.buffer) - p.sentRpcCache.put($encodedHex.hash) + p.sentRpcCache.put(digest) # if no connection has been set, # queue messages untill a connection