feature/use-crypto-rng (#190)

* fixes

* fix

* Update waku_types.nim

* fixes

* fix

* Improve the generateRequestId call

Co-authored-by: kdeme <kim.demey@gmail.com>
This commit is contained in:
Dean Eigenmann 2020-09-28 23:44:14 +02:00 committed by GitHub
parent 25b48bb99e
commit 3eb015ee7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 26 additions and 17 deletions

View File

@ -37,7 +37,7 @@ procSuite "Waku Filter":
completionFut.complete(true)
let
proto = WakuFilter.init(dialSwitch, handle)
proto = WakuFilter.init(dialSwitch, crypto.newRng(), handle)
rpc = FilterRequest(contentFilter: @[ContentFilter(topics: @["pew", "pew2"])], topic: "topic")
dialSwitch.mount(proto)
@ -46,7 +46,7 @@ procSuite "Waku Filter":
discard
let
proto2 = WakuFilter.init(listenSwitch, emptyHandle)
proto2 = WakuFilter.init(listenSwitch, crypto.newRng(), emptyHandle)
subscription = proto2.subscription()
var subscriptions = newTable[string, MessageNotificationSubscription]()

View File

@ -30,7 +30,7 @@ procSuite "Waku Store":
discard await listenSwitch.start()
let
proto = WakuStore.init(dialSwitch)
proto = WakuStore.init(dialSwitch, crypto.newRng())
subscription = proto.subscription()
rpc = HistoryQuery(topics: @["topic"])

View File

@ -4,14 +4,13 @@
import
std/tables,
chronos,
random,
chronos, bearssl, stew/byteutils,
libp2p/[switch, peerinfo, multiaddress, crypto/crypto],
libp2p/protobuf/minprotobuf,
libp2p/protocols/protocol,
libp2p/switch,
libp2p/stream/connection,
libp2p/protocols/pubsub/[pubsub, floodsub, gossipsub]
libp2p/protocols/pubsub/[pubsub, gossipsub]
# Common data types -----------------------------------------------------------
@ -63,6 +62,7 @@ type
WakuStore* = ref object of LPProtocol
switch*: Switch
rng*: ref BrHmacDrbgContext
peers*: seq[HistoryPeer]
messages*: seq[WakuMessage]
@ -86,6 +86,7 @@ type
MessagePushHandler* = proc(msg: MessagePush): Future[void] {.gcsafe, closure.}
WakuFilter* = ref object of LPProtocol
rng*: ref BrHmacDrbgContext
switch*: Switch
subscribers*: seq[Subscriber]
pushHandler*: MessagePushHandler
@ -102,6 +103,7 @@ type
messages*: seq[(Topic, WakuMessage)]
filters*: Filters
subscriptions*: MessageNotificationSubscriptions
rng*: ref BrHmacDrbgContext
WakuRelay* = ref object of GossipSub
gossipEnabled*: bool
@ -131,6 +133,7 @@ proc notify*(filters: Filters, msg: WakuMessage) =
if msg.contentTopic in filter.contentFilter.topics:
filter.handler(msg.payload)
proc generateRequestId*(): string =
for _ in .. 10:
add(result, char(rand(int('A') .. int('z'))))
proc generateRequestId*(rng: ref BrHmacDrbgContext): string =
var bytes: array[10, byte]
brHmacDrbgGenerate(rng[], bytes)
toHex(bytes)

View File

@ -84,7 +84,8 @@ proc init*(T: type WakuNode, nodeKey: crypto.PrivateKey,
switch.mount(wakuRelay)
result = WakuNode(
switch: switch,
switch: switch,
rng: crypto.newRng(),
peerInfo: peerInfo,
wakuRelay: wakuRelay,
subscriptions: newTable[string, MessageNotificationSubscription]()
@ -103,14 +104,14 @@ proc start*(node: WakuNode) {.async.} =
# NOTE WakuRelay is being instantiated as part of initing node
node.wakuStore = WakuStore.init(node.switch)
node.wakuStore = WakuStore.init(node.switch, node.rng)
node.switch.mount(node.wakuStore)
node.subscriptions.subscribe(WakuStoreCodec, node.wakuStore.subscription())
proc pushHandler(msg: MessagePush) {.async, gcsafe.} =
info "push received"
node.wakuFilter = WakuFilter.init(node.switch, pushHandler)
node.wakuFilter = WakuFilter.init(node.switch, node.rng, pushHandler)
node.switch.mount(node.wakuFilter)
node.subscriptions.subscribe(WakuFilterCodec, node.wakuFilter.subscription())

View File

@ -1,5 +1,6 @@
import
std/tables,
bearssl,
chronos, chronicles, metrics, stew/results,
libp2p/protocols/pubsub/pubsubpeer,
libp2p/protocols/pubsub/floodsub,
@ -7,6 +8,7 @@ import
libp2p/protocols/protocol,
libp2p/protobuf/minprotobuf,
libp2p/stream/connection,
libp2p/crypto/crypto,
libp2p/switch,
./message_notifier,
./../../node/v2/waku_types
@ -114,8 +116,9 @@ method init*(wf: WakuFilter) =
wf.handler = handle
wf.codec = WakuFilterCodec
proc init*(T: type WakuFilter, switch: Switch, handler: MessagePushHandler): T =
proc init*(T: type WakuFilter, switch: Switch, rng: ref BrHmacDrbgContext, handler: MessagePushHandler): T =
new result
result.rng = crypto.newRng()
result.switch = switch
result.pushHandler = handler
result.init()
@ -139,4 +142,4 @@ proc subscription*(proto: WakuFilter): MessageNotificationSubscription =
proc subscribe*(wf: WakuFilter, peer: PeerInfo, request: FilterRequest) {.async, gcsafe.} =
let conn = await wf.switch.dial(peer.peerId, peer.addrs, WakuFilterCodec)
await conn.writeLP(FilterRPC(requestId: generateRequestId(), request: request).encode().buffer)
await conn.writeLP(FilterRPC(requestId: generateRequestId(wf.rng), request: request).encode().buffer)

View File

@ -4,7 +4,6 @@
## Instead, it should likely be on top of GossipSub with a similar interface.
import
std/strutils,
chronos, chronicles, metrics,
libp2p/protocols/pubsub/[pubsub, floodsub, gossipsub],
libp2p/protocols/pubsub/rpc/messages,

View File

@ -1,7 +1,9 @@
import
std/tables,
bearssl,
chronos, chronicles, metrics, stew/results,
libp2p/switch,
libp2p/crypto/crypto,
libp2p/protocols/protocol,
libp2p/protobuf/minprotobuf,
libp2p/stream/connection,
@ -97,8 +99,9 @@ method init*(ws: WakuStore) =
ws.handler = handle
ws.codec = WakuStoreCodec
proc init*(T: type WakuStore, switch: Switch): T =
proc init*(T: type WakuStore, switch: Switch, rng: ref BrHmacDrbgContext): T =
new result
result.rng = rng
result.switch = switch
result.init()
@ -127,7 +130,7 @@ proc query*(w: WakuStore, query: HistoryQuery, handler: QueryHandlerFunc) {.asyn
let peer = w.peers[0]
let conn = await w.switch.dial(peer.peerInfo.peerId, peer.peerInfo.addrs, WakuStoreCodec)
await conn.writeLP(HistoryRPC(requestId: "foo", query: query).encode().buffer)
await conn.writeLP(HistoryRPC(requestId: generateRequestId(w.rng), query: query).encode().buffer)
var message = await conn.readLp(64*1024)
let response = HistoryRPC.init(message)