Skip to content

Commit 45da95f

Browse files
committed
feat: integrate mix protocol with extended kademlia discovery
1 parent eb0c34c commit 45da95f

28 files changed

+544
-175
lines changed

apps/chat2mix/chat2mix.nim

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import
3030
protobuf/minprotobuf, # message serialisation/deserialisation from and to protobufs
3131
nameresolving/dnsresolver,
3232
protocols/mix/curve25519,
33+
protocols/mix/mix_protocol,
3334
] # define DNS resolution
3435
import
3536
waku/[
@@ -38,6 +39,7 @@ import
3839
waku_lightpush/rpc,
3940
waku_enr,
4041
discovery/waku_dnsdisc,
42+
discovery/waku_kademlia,
4143
waku_node,
4244
node/waku_metrics,
4345
node/peer_manager,
@@ -453,14 +455,48 @@ proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} =
453455
(await node.mountMix(conf.clusterId, mixPrivKey, conf.mixnodes)).isOkOr:
454456
error "failed to mount waku mix protocol: ", error = $error
455457
quit(QuitFailure)
456-
await node.mountRendezvousClient(conf.clusterId)
458+
459+
# Setup extended kademlia discovery if bootstrap nodes are provided
460+
if conf.kadBootstrapNodes.len > 0:
461+
var kadBootstrapPeers: seq[(PeerId, seq[MultiAddress])]
462+
for nodeStr in conf.kadBootstrapNodes:
463+
let (peerId, ma) = parseFullAddress(nodeStr).valueOr:
464+
error "Failed to parse kademlia bootstrap node", node = nodeStr, error = error
465+
continue
466+
kadBootstrapPeers.add((peerId, @[ma]))
467+
468+
if kadBootstrapPeers.len > 0:
469+
node.wakuKademlia = WakuKademlia.new(
470+
node.switch,
471+
ExtendedKademliaDiscoveryParams(
472+
bootstrapNodes: kadBootstrapPeers,
473+
mixPubKey: some(mixPubKey),
474+
advertiseMix: false,
475+
),
476+
node.peerManager,
477+
getMixNodePoolSize = proc(): int {.gcsafe, raises: [].} =
478+
if node.wakuMix.isNil():
479+
0
480+
else:
481+
node.getMixNodePoolSize(),
482+
isNodeStarted = proc(): bool {.gcsafe, raises: [].} =
483+
node.started,
484+
).valueOr:
485+
error "failed to setup kademlia discovery", error = error
486+
quit(QuitFailure)
487+
488+
#await node.mountRendezvousClient(conf.clusterId)
457489

458490
await node.start()
459491

460492
node.peerManager.start()
493+
if not node.wakuKademlia.isNil():
494+
(await node.wakuKademlia.start(minMixPeers = MinMixNodePoolSize)).isOkOr:
495+
error "failed to start kademlia discovery", error = error
496+
quit(QuitFailure)
461497

462498
await node.mountLibp2pPing()
463-
await node.mountPeerExchangeClient()
499+
#await node.mountPeerExchangeClient()
464500
let pubsubTopic = conf.getPubsubTopic(node, conf.contentTopic)
465501
echo "pubsub topic is: " & pubsubTopic
466502
let nick = await readNick(transp)
@@ -601,11 +637,6 @@ proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} =
601637
node, pubsubTopic, conf.contentTopic, servicePeerInfo, false
602638
)
603639
echo "waiting for mix nodes to be discovered..."
604-
while true:
605-
if node.getMixNodePoolSize() >= MinMixNodePoolSize:
606-
break
607-
discard await node.fetchPeerExchangePeers()
608-
await sleepAsync(1000)
609640

610641
while node.getMixNodePoolSize() < MinMixNodePoolSize:
611642
info "waiting for mix nodes to be discovered",

apps/chat2mix/config_chat2mix.nim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ type
203203
fleet* {.
204204
desc:
205205
"Select the fleet to connect to. This sets the DNS discovery URL to the selected fleet.",
206-
defaultValue: Fleet.test,
206+
defaultValue: Fleet.none,
207207
name: "fleet"
208208
.}: Fleet
209209

210210
contentTopic* {.
211211
desc: "Content topic for chat messages.",
212-
defaultValue: "/toy-chat-mix/2/huilong/proto",
212+
defaultValue: "/toy-chat/2/baixa-chiado/proto",
213213
name: "content-topic"
214214
.}: string
215215

@@ -228,7 +228,14 @@ type
228228
desc: "WebSocket Secure Support.",
229229
defaultValue: false,
230230
name: "websocket-secure-support"
231-
.}: bool ## rln-relay configuration
231+
.}: bool
232+
233+
## Kademlia Discovery config
234+
kadBootstrapNodes* {.
235+
desc:
236+
"Peer multiaddr for kademlia discovery bootstrap node (must include /p2p/<peerID>). Argument may be repeated.",
237+
name: "kad-bootstrap-node"
238+
.}: seq[string]
232239

233240
proc parseCmdArg*(T: type MixNodePubInfo, p: string): T =
234241
let elements = p.split(":")

nix/default.nix

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ in stdenv.mkDerivation {
4646
];
4747

4848
# Environment variables required for Android builds
49-
ANDROID_SDK_ROOT="${pkgs.androidPkgs.sdk}";
50-
ANDROID_NDK_HOME="${pkgs.androidPkgs.ndk}";
49+
ANDROID_SDK_ROOT = "${pkgs.androidPkgs.sdk}";
50+
ANDROID_NDK_HOME = "${pkgs.androidPkgs.ndk}";
5151
NIMFLAGS = "-d:disableMarchNative -d:git_revision_override=${revision}";
5252
XDG_CACHE_HOME = "/tmp";
5353

@@ -61,6 +61,15 @@ in stdenv.mkDerivation {
6161

6262
configurePhase = ''
6363
patchShebangs . vendor/nimbus-build-system > /dev/null
64+
65+
# build_nim.sh guards "rm -rf dist/checksums" with NIX_BUILD_TOP != "/build",
66+
# but on macOS the nix sandbox uses /private/tmp/... so the check fails and
67+
# dist/checksums (provided via preBuild) gets deleted. Fix the check to skip
68+
# the removal whenever NIX_BUILD_TOP is set (i.e. any nix build).
69+
substituteInPlace vendor/nimbus-build-system/scripts/build_nim.sh \
70+
--replace 'if [[ "''${NIX_BUILD_TOP}" != "/build" ]]; then' \
71+
'if [[ -z "''${NIX_BUILD_TOP}" ]]; then'
72+
6473
make nimbus-build-system-paths
6574
make nimbus-build-system-nimble-dir
6675
'';

simulations/mixnet/config.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
log-level = "INFO"
1+
log-level = "TRACE"
22
relay = true
33
mix = true
44
filter = true
5-
store = false
5+
store = true
66
lightpush = true
77
max-connections = 150
8-
peer-exchange = true
8+
peer-exchange = false
99
metrics-logging = false
1010
cluster-id = 2
11-
discv5-discovery = true
11+
discv5-discovery = false
1212
discv5-udp-port = 9000
1313
discv5-enr-auto-update = true
14+
enable-kad-discovery = true
1415
rest = true
1516
rest-admin = true
1617
ports-shift = 1
@@ -19,7 +20,9 @@ shard = [0]
1920
agent-string = "nwaku-mix"
2021
nodekey = "f98e3fba96c32e8d1967d460f1b79457380e1a895f7971cecc8528abe733781a"
2122
mixkey = "a87db88246ec0eedda347b9b643864bee3d6933eb15ba41e6d58cb678d813258"
22-
rendezvous = true
23+
rendezvous = false
2324
listen-address = "127.0.0.1"
2425
nat = "extip:127.0.0.1"
26+
ext-multiaddr = ["/ip4/127.0.0.1/tcp/60001"]
27+
ext-multiaddr-only = true
2528
ip-colocation-limit=0

simulations/mixnet/config1.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
log-level = "INFO"
1+
log-level = "TRACE"
22
relay = true
33
mix = true
44
filter = true
55
store = false
66
lightpush = true
77
max-connections = 150
8-
peer-exchange = true
8+
peer-exchange = false
99
metrics-logging = false
1010
cluster-id = 2
11-
discv5-discovery = true
11+
discv5-discovery = false
1212
discv5-udp-port = 9001
1313
discv5-enr-auto-update = true
1414
discv5-bootstrap-node = ["enr:-LG4QBaAbcA921hmu3IrreLqGZ4y3VWCjBCgNN9mpX9vqkkbSrM3HJHZTXnb5iVXgc5pPtDhWLxkB6F3yY25hSwMezkEgmlkgnY0gmlwhH8AAAGKbXVsdGlhZGRyc4oACATAqEQ-BuphgnJzhQACAQAAiXNlY3AyNTZrMaEDpEW1UlUGHRJg6g_zGuCddKWmIUBGZCQX13xGfh9J6KiDdGNwguphg3VkcIIjKYV3YWt1Mg0"]
15+
kad-bootstrap-node = ["/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o"]
1516
rest = true
1617
rest-admin = true
1718
ports-shift = 2
@@ -20,8 +21,10 @@ shard = [0]
2021
agent-string = "nwaku-mix"
2122
nodekey = "09e9d134331953357bd38bbfce8edb377f4b6308b4f3bfbe85c610497053d684"
2223
mixkey = "c86029e02c05a7e25182974b519d0d52fcbafeca6fe191fbb64857fb05be1a53"
23-
rendezvous = true
24+
rendezvous = false
2425
listen-address = "127.0.0.1"
2526
nat = "extip:127.0.0.1"
27+
ext-multiaddr = ["/ip4/127.0.0.1/tcp/60002"]
28+
ext-multiaddr-only = true
2629
ip-colocation-limit=0
2730
#staticnode = ["/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o", "/ip4/127.0.0.1/tcp/60003/p2p/16Uiu2HAmTEDHwAziWUSz6ZE23h5vxG2o4Nn7GazhMor4bVuMXTrA","/ip4/127.0.0.1/tcp/60004/p2p/16Uiu2HAmPwRKZajXtfb1Qsv45VVfRZgK3ENdfmnqzSrVm3BczF6f","/ip4/127.0.0.1/tcp/60005/p2p/16Uiu2HAmRhxmCHBYdXt1RibXrjAUNJbduAhzaTHwFCZT4qWnqZAu"]

simulations/mixnet/config2.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
log-level = "INFO"
1+
log-level = "TRACE"
22
relay = true
33
mix = true
44
filter = true
55
store = false
66
lightpush = true
77
max-connections = 150
8-
peer-exchange = true
8+
peer-exchange = false
99
metrics-logging = false
1010
cluster-id = 2
11-
discv5-discovery = true
11+
discv5-discovery = false
1212
discv5-udp-port = 9002
1313
discv5-enr-auto-update = true
1414
discv5-bootstrap-node = ["enr:-LG4QBaAbcA921hmu3IrreLqGZ4y3VWCjBCgNN9mpX9vqkkbSrM3HJHZTXnb5iVXgc5pPtDhWLxkB6F3yY25hSwMezkEgmlkgnY0gmlwhH8AAAGKbXVsdGlhZGRyc4oACATAqEQ-BuphgnJzhQACAQAAiXNlY3AyNTZrMaEDpEW1UlUGHRJg6g_zGuCddKWmIUBGZCQX13xGfh9J6KiDdGNwguphg3VkcIIjKYV3YWt1Mg0"]
15+
kad-bootstrap-node = ["/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o"]
1516
rest = false
1617
rest-admin = false
1718
ports-shift = 3
@@ -20,8 +21,10 @@ shard = [0]
2021
agent-string = "nwaku-mix"
2122
nodekey = "ed54db994682e857d77cd6fb81be697382dc43aa5cd78e16b0ec8098549f860e"
2223
mixkey = "b858ac16bbb551c4b2973313b1c8c8f7ea469fca03f1608d200bbf58d388ec7f"
23-
rendezvous = true
24+
rendezvous = false
2425
listen-address = "127.0.0.1"
2526
nat = "extip:127.0.0.1"
27+
ext-multiaddr = ["/ip4/127.0.0.1/tcp/60003"]
28+
ext-multiaddr-only = true
2629
ip-colocation-limit=0
2730
#staticnode = ["/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o", "/ip4/127.0.0.1/tcp/60002/p2p/16Uiu2HAmLtKaFaSWDohToWhWUZFLtqzYZGPFuXwKrojFVF6az5UF","/ip4/127.0.0.1/tcp/60004/p2p/16Uiu2HAmPwRKZajXtfb1Qsv45VVfRZgK3ENdfmnqzSrVm3BczF6f","/ip4/127.0.0.1/tcp/60005/p2p/16Uiu2HAmRhxmCHBYdXt1RibXrjAUNJbduAhzaTHwFCZT4qWnqZAu"]

simulations/mixnet/config3.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
log-level = "INFO"
1+
log-level = "TRACE"
22
relay = true
33
mix = true
44
filter = true
55
store = false
66
lightpush = true
77
max-connections = 150
8-
peer-exchange = true
8+
peer-exchange = false
99
metrics-logging = false
1010
cluster-id = 2
11-
discv5-discovery = true
11+
discv5-discovery = false
1212
discv5-udp-port = 9003
1313
discv5-enr-auto-update = true
1414
discv5-bootstrap-node = ["enr:-LG4QBaAbcA921hmu3IrreLqGZ4y3VWCjBCgNN9mpX9vqkkbSrM3HJHZTXnb5iVXgc5pPtDhWLxkB6F3yY25hSwMezkEgmlkgnY0gmlwhH8AAAGKbXVsdGlhZGRyc4oACATAqEQ-BuphgnJzhQACAQAAiXNlY3AyNTZrMaEDpEW1UlUGHRJg6g_zGuCddKWmIUBGZCQX13xGfh9J6KiDdGNwguphg3VkcIIjKYV3YWt1Mg0"]
15+
kad-bootstrap-node = ["/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o"]
1516
rest = false
1617
rest-admin = false
1718
ports-shift = 4
@@ -20,8 +21,10 @@ shard = [0]
2021
agent-string = "nwaku-mix"
2122
nodekey = "42f96f29f2d6670938b0864aced65a332dcf5774103b4c44ec4d0ea4ef3c47d6"
2223
mixkey = "d8bd379bb394b0f22dd236d63af9f1a9bc45266beffc3fbbe19e8b6575f2535b"
23-
rendezvous = true
24+
rendezvous = false
2425
listen-address = "127.0.0.1"
2526
nat = "extip:127.0.0.1"
27+
ext-multiaddr = ["/ip4/127.0.0.1/tcp/60004"]
28+
ext-multiaddr-only = true
2629
ip-colocation-limit=0
2730
#staticnode = ["/ip4/127.0.0.1/tcp/60002/p2p/16Uiu2HAmLtKaFaSWDohToWhWUZFLtqzYZGPFuXwKrojFVF6az5UF", "/ip4/127.0.0.1/tcp/60003/p2p/16Uiu2HAmTEDHwAziWUSz6ZE23h5vxG2o4Nn7GazhMor4bVuMXTrA","/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o","/ip4/127.0.0.1/tcp/60005/p2p/16Uiu2HAmRhxmCHBYdXt1RibXrjAUNJbduAhzaTHwFCZT4qWnqZAu"]

simulations/mixnet/config4.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
log-level = "INFO"
1+
log-level = "TRACE"
22
relay = true
33
mix = true
44
filter = true
55
store = false
66
lightpush = true
77
max-connections = 150
8-
peer-exchange = true
8+
peer-exchange = false
99
metrics-logging = false
1010
cluster-id = 2
11-
discv5-discovery = true
11+
discv5-discovery = false
1212
discv5-udp-port = 9004
1313
discv5-enr-auto-update = true
1414
discv5-bootstrap-node = ["enr:-LG4QBaAbcA921hmu3IrreLqGZ4y3VWCjBCgNN9mpX9vqkkbSrM3HJHZTXnb5iVXgc5pPtDhWLxkB6F3yY25hSwMezkEgmlkgnY0gmlwhH8AAAGKbXVsdGlhZGRyc4oACATAqEQ-BuphgnJzhQACAQAAiXNlY3AyNTZrMaEDpEW1UlUGHRJg6g_zGuCddKWmIUBGZCQX13xGfh9J6KiDdGNwguphg3VkcIIjKYV3YWt1Mg0"]
15+
kad-bootstrap-node = ["/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o"]
1516
rest = false
1617
rest-admin = false
1718
ports-shift = 5
@@ -20,8 +21,10 @@ shard = [0]
2021
agent-string = "nwaku-mix"
2122
nodekey = "3ce887b3c34b7a92dd2868af33941ed1dbec4893b054572cd5078da09dd923d4"
2223
mixkey = "780fff09e51e98df574e266bf3266ec6a3a1ddfcf7da826a349a29c137009d49"
23-
rendezvous = true
24+
rendezvous = false
2425
listen-address = "127.0.0.1"
2526
nat = "extip:127.0.0.1"
27+
ext-multiaddr = ["/ip4/127.0.0.1/tcp/60005"]
28+
ext-multiaddr-only = true
2629
ip-colocation-limit=0
2730
#staticnode = ["/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o", "/ip4/127.0.0.1/tcp/60003/p2p/16Uiu2HAmTEDHwAziWUSz6ZE23h5vxG2o4Nn7GazhMor4bVuMXTrA","/ip4/127.0.0.1/tcp/60004/p2p/16Uiu2HAmPwRKZajXtfb1Qsv45VVfRZgK3ENdfmnqzSrVm3BczF6f","/ip4/127.0.0.1/tcp/60002/p2p/16Uiu2HAmLtKaFaSWDohToWhWUZFLtqzYZGPFuXwKrojFVF6az5UF"]

simulations/mixnet/run_chat_mix.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
../../build/chat2mix --cluster-id=2 --num-shards-in-network=1 --shard=0 --servicenode="/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o" --log-level=TRACE
1+
../../build/chat2mix --cluster-id=2 --num-shards-in-network=1 --shard=0 --servicenode="/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o" --log-level=TRACE --kad-bootstrap-node="/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o"
22
#--mixnode="/ip4/127.0.0.1/tcp/60002/p2p/16Uiu2HAmLtKaFaSWDohToWhWUZFLtqzYZGPFuXwKrojFVF6az5UF:9231e86da6432502900a84f867004ce78632ab52cd8e30b1ec322cd795710c2a" --mixnode="/ip4/127.0.0.1/tcp/60003/p2p/16Uiu2HAmTEDHwAziWUSz6ZE23h5vxG2o4Nn7GazhMor4bVuMXTrA:275cd6889e1f29ca48e5b9edb800d1a94f49f13d393a0ecf1a07af753506de6c" --mixnode="/ip4/127.0.0.1/tcp/60004/p2p/16Uiu2HAmPwRKZajXtfb1Qsv45VVfRZgK3ENdfmnqzSrVm3BczF6f:e0ed594a8d506681be075e8e23723478388fb182477f7a469309a25e7076fc18" --mixnode="/ip4/127.0.0.1/tcp/60005/p2p/16Uiu2HAmRhxmCHBYdXt1RibXrjAUNJbduAhzaTHwFCZT4qWnqZAu:8fd7a1a7c19b403d231452a9b1ea40eb1cc76f455d918ef8980e7685f9eeeb1f"

simulations/mixnet/run_mix_node.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../build/wakunode2 --config-file="config.toml"
1+
../../build/wakunode2 --config-file="config.toml" 2>&1 | tee mix_node.log

0 commit comments

Comments
 (0)