[wip] rev formatting in "simple_" examples (easier to read)
This commit is contained in:
parent
80be251c8f
commit
9678605be7
|
@ -1,8 +1,15 @@
|
|||
import std/strutils, stew/byteutils, parseutils, hashes, tables, sequtils, sets
|
||||
import libp2p
|
||||
import libp2p/protocols/pubsub/rpc/messages
|
||||
import libp2p/protocols/pubsub/peertable
|
||||
import testground_sdk
|
||||
import std/hashes
|
||||
import std/parseutils
|
||||
import std/sequtils
|
||||
import std/sets
|
||||
import std/strutils
|
||||
import std/tables
|
||||
|
||||
import pkg/libp2p
|
||||
import pkg/libp2p/protocols/pubsub/peertable
|
||||
import pkg/libp2p/protocols/pubsub/rpc/messages
|
||||
import pkg/stew/byteutils
|
||||
import pkg/testground_sdk
|
||||
|
||||
proc msgIdProvider(m: Message): Result[MessageID, ValidationResult] =
|
||||
return ok(($m.data.hash).toBytes())
|
||||
|
@ -12,6 +19,7 @@ testground(client):
|
|||
myId = await client.signalAndWait("setup", client.testInstanceCount)
|
||||
# Ugly IP generation
|
||||
myIp = client.testSubnet.split('.')[0..1].join(".") & ".1." & $myId
|
||||
|
||||
await client.updateNetworkParameter(
|
||||
NetworkConf(
|
||||
network: "default",
|
||||
|
@ -22,15 +30,15 @@ testground(client):
|
|||
routing_policy: "accept_all",
|
||||
default: LinkShape(
|
||||
latency: 100000000,
|
||||
# jitter: 100000000,
|
||||
# jitter: 100000000,
|
||||
)
|
||||
|
||||
)
|
||||
)
|
||||
|
||||
# Subscribing early to the pubsub topic to make sure we don't
|
||||
# miss any address
|
||||
let addressQueue = client.subscribe("addresses")
|
||||
let
|
||||
addressQueue = client.subscribe("addresses")
|
||||
|
||||
# The sidecar will signal for "network_setup" when the network is ready
|
||||
# (callback_state above)
|
||||
|
@ -39,6 +47,7 @@ testground(client):
|
|||
let
|
||||
ma = MultiAddress.init("/ip4/" & myIp & "/tcp/0").tryGet()
|
||||
rng = libp2p.crypto.newRng()
|
||||
|
||||
switch =
|
||||
SwitchBuilder.new()
|
||||
.withRng(rng)
|
||||
|
@ -55,24 +64,31 @@ testground(client):
|
|||
verifySignature = false,
|
||||
anonymize = true, # Signing take a few milliseconds
|
||||
triggerSelf = true)
|
||||
|
||||
switch.mount(pubsub)
|
||||
|
||||
await switch.start()
|
||||
await pubsub.start()
|
||||
|
||||
let gotMsg = newFuture[void]()
|
||||
let
|
||||
gotMsg = newFuture[void]()
|
||||
|
||||
pubsub.subscribe("letopic",
|
||||
pubsub.subscribe(
|
||||
"letopic",
|
||||
proc (topic: string, data: seq[byte]) {.async.} =
|
||||
doAssert data[0] == 12
|
||||
gotMsg.complete()
|
||||
)
|
||||
|
||||
# Ugly broadcast
|
||||
await client.publish("addresses", $myId & "," & $switch.peerInfo.addrs[0] & "," & $switch.peerInfo.peerId)
|
||||
await client.publish(
|
||||
"addresses",
|
||||
$myId & "," & $switch.peerInfo.addrs[0] & "," & $switch.peerInfo.peerId)
|
||||
|
||||
var
|
||||
peersInfo: seq[string]
|
||||
toConnect = client.testInstanceCount
|
||||
|
||||
# Retrieve every addresses
|
||||
while peersInfo.len < toConnect:
|
||||
peersInfo.add(await addressQueue.popLast())
|
||||
|
@ -82,12 +98,19 @@ testground(client):
|
|||
# too many connections (>2500), caused "No route to host" errors.)
|
||||
if peersInfo.len > 15:
|
||||
peersInfo = peersInfo[0..<15]
|
||||
|
||||
for p in peersInfo:
|
||||
let dress = p.split(",")
|
||||
let pid = PeerId.init(dress[2]).tryGet()
|
||||
var otherId: int
|
||||
let
|
||||
dress = p.split(",")
|
||||
pid = PeerId.init(dress[2]).tryGet()
|
||||
|
||||
var
|
||||
otherId: int
|
||||
|
||||
discard parseInt(dress[0], otherId)
|
||||
|
||||
toConnect.dec
|
||||
|
||||
if otherId < myId and pid != switch.peerInfo.peerId:
|
||||
try:
|
||||
await switch.connect(pid, @[MultiAddress.init(dress[1]).tryGet()])
|
||||
|
@ -100,13 +123,15 @@ testground(client):
|
|||
await sleepAsync(1.seconds)
|
||||
discard await client.signalAndWait("readyforaction", client.testInstanceCount)
|
||||
|
||||
let start = Moment.now()
|
||||
let
|
||||
start = Moment.now()
|
||||
|
||||
if myId == 1:
|
||||
# First node publishes
|
||||
discard await pubsub.publish("letopic", @[12.byte])
|
||||
|
||||
if await gotMsg.withTimeout(60.seconds):
|
||||
echo "FINISHED ",myId, ": ", Moment.now() - start
|
||||
echo "FINISHED ", myId, ": ", Moment.now() - start
|
||||
else:
|
||||
echo myId, " never got the message :'("
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
name = "simple_libp2p"
|
||||
|
||||
[defaults]
|
||||
builder = "docker:generic"
|
||||
runner = "local:docker"
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
import std/strutils, testground_sdk, chronos, stew/byteutils
|
||||
import std/strutils
|
||||
|
||||
import pkg/chronos
|
||||
import pkg/stew/byteutils
|
||||
import pkg/testground_sdk
|
||||
|
||||
testground(client):
|
||||
let
|
||||
myId = await client.signalAndWait("setup", client.testInstanceCount)
|
||||
myIp = client.testSubnet.split('.')[0..1].join(".") & ".1." & $myId
|
||||
serverIp = client.testSubnet.split('.')[0..1].join(".") & ".1.1"
|
||||
|
||||
await client.updateNetworkParameter(
|
||||
NetworkConf(
|
||||
network: "default",
|
||||
|
@ -22,22 +27,30 @@ testground(client):
|
|||
payload = client.param(string, "payload")
|
||||
count = client.param(int, "count")
|
||||
printResult = client.param(bool, "printResult")
|
||||
|
||||
if myId == 1: # server
|
||||
let
|
||||
server = createStreamServer(initTAddress(myIp & ":5050"), flags = {ReuseAddr})
|
||||
server = createStreamServer(
|
||||
initTAddress(myIp & ":5050"), flags = {ReuseAddr})
|
||||
|
||||
connection = await server.accept()
|
||||
|
||||
for _ in 0 ..< count:
|
||||
doAssert (await connection.write(payload.toBytes())) == payload.len
|
||||
|
||||
connection.close()
|
||||
|
||||
else: # client
|
||||
let connection = await connect(initTAddress(serverIp & ":5050"))
|
||||
var buffer = newSeq[byte](payload.len)
|
||||
let
|
||||
connection = await connect(initTAddress(serverIp & ":5050"))
|
||||
|
||||
var
|
||||
buffer = newSeq[byte](payload.len)
|
||||
|
||||
for _ in 0 ..< count:
|
||||
await connection.readExactly(addr buffer[0], payload.len)
|
||||
doAssert string.fromBytes(buffer) == payload
|
||||
|
||||
connection.close()
|
||||
|
||||
if printResult:
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
name = "simple_tcp_ping"
|
||||
|
||||
[defaults]
|
||||
builder = "docker:generic"
|
||||
runner = "local:docker"
|
||||
|
|
Loading…
Reference in New Issue