Improvement/bridge improvements (#429)

* General test, import and log improvements

* Bridge improvements
This commit is contained in:
Hanno Cornelius 2021-03-23 10:04:51 +02:00 committed by GitHub
parent 2fe6935623
commit a30d6a8aae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 65 additions and 18 deletions

View File

@ -1,3 +1,5 @@
{.used.}
import
std/[unittest, options, sets, tables, os, strutils, sequtils],
stew/shims/net as stewNet,

View File

@ -1,3 +1,4 @@
{.used.}
import
std/[unittest, options, tables, sets],

View File

@ -1,3 +1,5 @@
{.used.}
import
std/[unittest, options, sets, tables, sequtils],
stew/shims/net as stewNet,

View File

@ -1,7 +1,7 @@
{.used.}
import
std/unittest,
std/[unittest, strutils],
chronicles, chronos, stew/shims/net as stewNet, stew/byteutils,
libp2p/crypto/crypto,
libp2p/crypto/secp,
@ -14,10 +14,10 @@ import
eth/keys,
../../waku/common/wakubridge,
../../waku/v1/protocol/waku_protocol,
../../waku/v2/protocol/[waku_message, message_notifier],
../../waku/v2/protocol/waku_message,
../../waku/v2/protocol/waku_store/waku_store,
../../waku/v2/protocol/waku_filter/waku_filter,
../../waku/v2/node/wakunode2,
../../waku/v2/node/[wakunode2, waku_payload],
../test_helpers
procSuite "WakuBridge":
@ -61,6 +61,10 @@ procSuite "WakuBridge":
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
let msg = WakuMessage.init(data)
if msg.isOk() and msg.value().version == 1:
check:
# Message fields are as expected
msg.value().contentTopic == contentTopic # Topic translation worked
string.fromBytes(msg.value().payload).contains("from V1")
completionFut.complete(true)
v2Node.subscribe(defaultBridgeTopic, relayHandler)
@ -75,6 +79,13 @@ procSuite "WakuBridge":
check:
# v1Node received message published by v2Node
v1Node.protocolState(Waku).queue.items.len == 1
let msg = v1Node.protocolState(Waku).queue.items[0]
check:
# Message fields are as expected
msg.env.topic == topic # Topic translation worked
string.fromBytes(msg.env.data).contains("from V2")
# Test bridging from V1 to V2
check:

View File

@ -1,3 +1,5 @@
{.used.}
import
chronos, chronicles, options, stint, unittest,
web3,

View File

@ -1,3 +1,5 @@
{.used.}
import
std/[unittest, options, tables, sets],
chronos, chronicles, stew/shims/net as stewNet, stew/byteutils,

View File

@ -1,3 +1,5 @@
{.used.}
import web3
proc web3Test() =
var web3: Web3 # an identifier from web3 package

View File

@ -1,28 +1,21 @@
import
std/[strutils, tables],
std/tables,
chronos, confutils, chronicles, chronicles/topics_registry, metrics,
stew/endians2,
stew/shims/net as stewNet, json_rpc/rpcserver,
# Waku v1 imports
eth/[keys, p2p], eth/common/utils,
eth/p2p/[enode, whispernodes],
eth/p2p/enode,
../v1/protocol/waku_protocol,
./utils/nat,
../v1/node/rpc/wakusim,
../v1/node/rpc/waku,
../v1/node/rpc/key_storage,
../v1/node/waku_helpers,
# Waku v2 imports
libp2p/crypto/crypto,
../v2/protocol/waku_filter/waku_filter_types,
../v2/node/wakunode2,
../v2/node/jsonrpc/[debug_api,
filter_api,
relay_api,
store_api],
# Common cli config
./config_bridge
declarePublicCounter waku_bridge_transfers, "Number of messages transferred between Waku v1 and v2 networks", ["type"]
logScope:
topics = "wakubridge"
@ -51,15 +44,17 @@ type
func toWakuMessage(env: Envelope): WakuMessage =
# Translate a Waku v1 envelope to a Waku v2 message
WakuMessage(payload: env.data,
contentTopic: ContentTopic(uint32.fromBytes(env.topic)),
contentTopic: ContentTopic(uint32.fromBytes(env.topic, Endianness.bigEndian)),
version: 1)
proc toWakuV2(bridge: WakuBridge, env: Envelope) {.async.} =
waku_bridge_transfers.inc(labelValues = ["v1_to_v2"])
await bridge.nodev2.publish(defaultBridgeTopic, env.toWakuMessage())
proc toWakuV1(bridge: WakuBridge, msg: WakuMessage) {.gcsafe.} =
waku_bridge_transfers.inc(labelValues = ["v2_to_v1"])
discard bridge.nodev1.postMessage(ttl = defaultTTL,
topic = msg.contentTopic.toBytes(),
topic = msg.contentTopic.toBytes(Endianness.bigEndian),
payload = msg.payload)
##############
@ -144,6 +139,18 @@ proc stop*(bridge: WakuBridge) {.async.} =
await bridge.nodev2.stop()
when isMainModule:
import
eth/p2p/whispernodes,
./utils/nat,
../v1/node/rpc/wakusim,
../v1/node/rpc/waku,
../v1/node/rpc/key_storage,
../v1/node/waku_helpers,
../v2/node/jsonrpc/[debug_api,
filter_api,
relay_api,
store_api]
proc startV2Rpc(node: WakuNode, rpcServer: RpcHttpServer, conf: WakuNodeConf) =
installDebugApiHandlers(node, rpcServer)

View File

@ -14,6 +14,9 @@ import
export jsonrpc_types
logScope:
topics = "admin api"
const futTimeout* = 30.seconds # Max time to wait for futures
proc constructMultiaddrStr*(wireaddr: MultiAddress, peerId: PeerId): string =

View File

@ -2,6 +2,9 @@ import
json_rpc/rpcserver,
../wakunode2
logScope:
topics = "debug api"
proc installDebugApiHandlers*(node: WakuNode, rpcsrv: RpcServer) =
## Debug API version 1 definitions

View File

@ -10,6 +10,9 @@ import
export jsonrpc_types
logScope:
topics = "filter api"
const futTimeout* = 5.seconds # Max time to wait for futures
const maxCache* = 100 # Max number of messages cached per topic @TODO make this configurable

View File

@ -1,5 +1,5 @@
import
std/[options, json, sequtils],
std/[options, json],
eth/keys,
../../../v1/node/rpc/hexstrings,
../../protocol/waku_store/waku_store_types,

View File

@ -10,6 +10,9 @@ import
export waku_payload, jsonrpc_types
logScope:
topics = "private api"
const futTimeout* = 5.seconds # Max time to wait for futures
proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, rng: ref BrHmacDrbgContext, topicCache: TopicCache) =

View File

@ -11,6 +11,9 @@ import
export jsonrpc_types
logScope:
topics = "relay api"
const futTimeout* = 5.seconds # Max time to wait for futures
const maxCache* = 100 # Max number of messages cached per topic @TODO make this configurable

View File

@ -9,6 +9,9 @@ import
export jsonrpc_types
logScope:
topics = "store api"
proc installStoreApiHandlers*(node: WakuNode, rpcsrv: RpcServer) =
const futTimeout = 5.seconds

View File

@ -1,5 +1,5 @@
import
chronicles, options, chronos, stint, sequtils,
chronicles, options, chronos, stint,
web3,
stew/byteutils,
eth/keys,