Arseniy Klempner b40ee69e0f
feat(mix,rln-gifter): LEZ-backed RLN mix + 2-phase gifter protocol (rebased onto PR #3807; plugin via nimble)
Ports commit 14562878 onto PR #3807's logos_delivery/ layout.

Changes vs original 14562878:
- Plugin consumed via nimble dep (no submodule add); the PR #3807 nimble
  resolution already provides it.
- mountMix signature carries both PR #3807's disableSpamProtection AND
  the patch's useOnchainLEZ parameters (callsite passes both).
- WakuMix.new branches on disableSpamProtection while also setting
  useOnchainLEZ on the spam-protection config. Cover-traffic params come
  from the plugin config when RLN is enabled, fall back to waku defaults
  when disabled.
- start() retains HEAD's split between local-only init and the separate
  registerDoSProtectionWithNetwork retry loop. publishGossipsubTrigger is
  promoted to a standalone proc instead of inline in an else branch.
- MixProtocol.init uses libp2p_mix's new Opt-wrapped coverTraffic and
  the MixRlnSpamProtection.new constructor (not the renamed newMixRlnSpamProtection).
- Makefile reverted to HEAD: PR #3807 dropped the separate mix-librln
  archive in favor of a single stateless zerokit, so the patch's
  MIX_LIBRLN_* additions are obsolete.
- README/sim helper config files: HEAD's values retained (env-specific
  paths from patch author dropped).

Adds:
- logos_delivery/waku/waku_mix/logos_core_client.nim (RLN client layer)
- logos_delivery/waku/waku_mix/coordination handler (via waku_node)
- logos_delivery/waku/waku_rln_relay/rln_gifter/{client,protocol,rpc,rpc_codec}.nim
- 5 new C FFI exports (logosdelivery_set_rln_fetcher, _set_rln_config,
  _set_rln_identity, _push_roots, _push_proof)
- --mix-gifter-* CLI flags + MixConf fields + MixConfBuilder methods
- tests/waku_rln_relay/test_rln_gifter.nim
2026-06-19 18:54:41 -04:00

79 lines
2.5 KiB
Nim

import ffi
import std/locks
import logos_delivery/waku/factory/waku
import logos_delivery/waku/waku_mix/logos_core_client as mix_rln_client
declareLibrary("logosdelivery")
var eventCallbackLock: Lock
initLock(eventCallbackLock)
template requireInitializedNode*(
ctx: ptr FFIContext[Waku], opName: string, onError: untyped
) =
if isNil(ctx):
let errMsg {.inject.} = opName & " failed: invalid context"
onError
elif isNil(ctx.myLib) or isNil(ctx.myLib[]):
let errMsg {.inject.} = opName & " failed: node is not initialized"
onError
proc logosdelivery_set_event_callback(
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
) {.dynlib, exportc, cdecl.} =
if isNil(ctx):
echo "error: invalid context in logosdelivery_set_event_callback"
return
# prevent race conditions that might happen due incorrect usage.
eventCallbackLock.acquire()
defer:
eventCallbackLock.release()
ctx[].eventCallback = cast[pointer](callback)
ctx[].eventUserData = userData
proc logosdelivery_init(): cint {.dynlib, exportc, cdecl.} =
initializeLibrary()
# Default log level is configured at compile time via chronicles_log_level;
# the runtime setLogLevel resolution conflicts with std/termios's two-arg
# template under ff8d518 — leave the compile-time default in effect.
return RET_OK
proc logosdelivery_set_rln_fetcher(
ctx: ptr FFIContext[Waku], fetcher: mix_rln_client.RlnFetcherFunc, fetcherData: pointer
) {.dynlib, exportc, cdecl.} =
if fetcher.isNil:
echo "error: nil fetcher in logosdelivery_set_rln_fetcher"
return
mix_rln_client.setRlnFetcher(fetcher, fetcherData)
proc logosdelivery_set_rln_config(
ctx: ptr FFIContext[Waku], configAccountId: cstring, leafIndex: cint
): cint {.dynlib, exportc, cdecl.} =
if configAccountId.isNil:
return RET_ERR
mix_rln_client.setRlnConfig($configAccountId, leafIndex.int)
return RET_OK
proc logosdelivery_set_rln_identity(
ctx: ptr FFIContext[Waku], idSecretHashHex: cstring
) {.dynlib, exportc, cdecl.} =
if idSecretHashHex.isNil:
return
mix_rln_client.setRlnIdentity($idSecretHashHex)
proc logosdelivery_push_roots(
ctx: ptr FFIContext[Waku], rootsJson: cstring
) {.dynlib, exportc, cdecl.} =
if rootsJson.isNil:
return
mix_rln_client.pushRoots($rootsJson)
proc logosdelivery_push_proof(
ctx: ptr FFIContext[Waku], proofJson: cstring
) {.dynlib, exportc, cdecl.} =
if proofJson.isNil:
return
mix_rln_client.pushProof($proofJson)