Skip to content

Commit

Permalink
add burst and auto-registration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpavlin committed Jun 19, 2024
1 parent 8aa76dc commit afc4594
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
60 changes: 45 additions & 15 deletions apps/wakunode2/spammer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,72 @@ proc send(

return ok()

proc burstPublish(
waku: Waku, conf: WakuNodeConf, contentTopic: ContentTopic
) {.async.} =
var futures: seq[Future[Result[void, string]]]
var i: uint64 = 0
var start = getTime().toUnixFloat()

while i < conf.rlnRelayUserMessageLimit:
futures.add(send(waku, contentTopic))
inc i

let results = await allFinished(futures)

var current = getTime().toUnixFloat()
var tillNextBurst =
int(int64(conf.rlnEpochSizeSec * 1000) - int64((current - start) * 1000))
info "Published messages",
sleep = tillNextBurst, msgCount = conf.rlnRelayUserMessageLimit

await sleepAsync(tillNextBurst)

proc iterativePublish(
waku: Waku, conf: WakuNodeConf, contentTopic: ContentTopic
) {.async.} =
var start = getTime().toUnixFloat()

(await send(waku, contentTopic)).isOkOr:
error "Failed to publish", err = error

#echo await (waku.node.isReady())
var current = getTime().toUnixFloat()
var tillNextMsg = int(int64(conf.spammerDelay) - int64((current - start) * 1000))
info "Published message", sleep = tillNextMsg

await sleepAsync(tillNextMsg)

proc runSpammer*(
waku: Waku, conf: WakuNodeConf, contentTopic: ContentTopic = "/spammer/0/test/plain"
) {.async.} =
if not conf.enable:
if not conf.spammerEnable:
return

if not conf.rlnRelay:
error "RLN not configured!"
quit(QuitFailure)

var gotPeers = false
while not gotPeers:
while true:
var (inRelayPeers, outRelayPeers) =
waku.node.peerManager.connectedPeers(WakuRelayCodec)

var numPeers = len(inRelayPeers) + len(outRelayPeers)
if numPeers > 0:
gotPeers = true
break
info "Waiting for peers", numPeers = numPeers
await sleepAsync(1000)

#var rate = int(float(1000) / float(conf.msgRate))
#var delayBetweenMsg =
# float(conf.rlnEpochSizeSec * 1000) /
# (float(conf.rlnRelayUserMessageLimit) * conf.msgRateMultiplier)

info "Sending message with delay", delay = conf.delay
info "Sending message with delay", delay = conf.spammerDelay

while true:
var start = getTime().toUnix()

(await send(waku, contentTopic)).isOkOr:
error "Failed to publish", err = error

#echo await (waku.node.isReady())
var current = getTime().toUnix()
var tillNextMsg = int(int64(conf.delay) - (current - start))
info "Published messages", sleep = tillNextMsg

await sleepAsync(tillNextMsg)
if conf.spammerBurst:
await burstPublish(waku, conf, contentTopic)
else:
await iterativePublish(waku, conf, contentTopic)
2 changes: 2 additions & 0 deletions apps/wakunode2/wakunode2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ when isMainModule:
of inspectRlnDb:
doInspectRlnDb(conf)
of noCommand:
if conf.spammerEnable:
doRlnKeystoreGenerator(conf, false)
# NOTE: {.threadvar.} is used to make the global variable GC safe for the closure uses it
# It will always be called from main thread anyway.
# Ref: https://nim-lang.org/docs/manual.html#threads-gc-safety
Expand Down
9 changes: 5 additions & 4 deletions tools/rln_keystore_generator/rln_keystore_generator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import
logScope:
topics = "rln_keystore_generator"

proc doRlnKeystoreGenerator*(conf: WakuNodeConf) =
proc doRlnKeystoreGenerator*(conf: WakuNodeConf, quitOnSucces: bool = true) =
# 1. load configuration
trace "configuration", conf = $conf

Expand All @@ -42,7 +42,7 @@ proc doRlnKeystoreGenerator*(conf: WakuNodeConf) =
idSecretHash = credential.idSecretHash.inHex(),
idCommitment = credential.idCommitment.inHex()

if not conf.execute:
if quitOnSucces and not conf.execute:
info "not executing, exiting"
quit(0)

Expand Down Expand Up @@ -91,7 +91,6 @@ proc doRlnKeystoreGenerator*(conf: WakuNodeConf) =
userMessageLimit: conf.rlnRelayUserMessageLimit,
)


let persistRes = addMembershipCredentials(
conf.rlnRelayCredPath, keystoreCred, conf.rlnRelayCredPassword, RLNAppInfo
)
Expand All @@ -106,4 +105,6 @@ proc doRlnKeystoreGenerator*(conf: WakuNodeConf) =
except CatchableError:
error "failure while stopping OnchainGroupManager", error = getCurrentExceptionMsg()
quit(0) # 0 because we already registered on-chain
quit(0)

if quitOnSucces:
quit(0)
11 changes: 9 additions & 2 deletions waku/factory/external_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ type WakuNodeConf* = object
name: "request-rate-period"
.}: int64

enable* {.desc: "Enable spammer", defaultValue: false, name: "spammer".}: bool
spammerEnable* {.desc: "Enable spammer", defaultValue: false, name: "spammer".}:
bool
# msgRate* {.
# desc: "Number of messages published per epoch",
# defaultValue: 10,
Expand All @@ -607,12 +608,18 @@ type WakuNodeConf* = object
# defaultValue: 1,
# name: "spammer-msg-multiplier"
# .}: float
delay* {.
spammerDelay* {.
desc: "Delay between spawning a publish method (in miliseconds)",
defaultValue: 0,
name: "spammer-delay-between-msg"
.}: int

spammerBurst* {.
desc: "Send messages in burst instead of one by one",
defaultValue: false,
name: "spammer-burst"
.}: bool

## Parsing

# NOTE: Keys are different in nim-libp2p
Expand Down

0 comments on commit afc4594

Please sign in to comment.