From 800e4975857648d4069ca81b456a9dabf251a7b1 Mon Sep 17 00:00:00 2001 From: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com> Date: Wed, 17 Dec 2025 14:55:58 +0100 Subject: [PATCH] Change BrokerContext from random number to counter --- waku/common/broker/broker_context.nim | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/waku/common/broker/broker_context.nim b/waku/common/broker/broker_context.nim index 1b8235f6a..bdc5fc73a 100644 --- a/waku/common/broker/broker_context.nim +++ b/waku/common/broker/broker_context.nim @@ -1,4 +1,4 @@ -import std/[strutils, sysrand] +import std/[strutils, concurrency/atomics] type BrokerContext* = distinct uint32 @@ -9,18 +9,10 @@ func `$`*(bc: BrokerContext): string = const DefaultBrokerContext* = BrokerContext(0xCAFFE14E'u32) +var gContextCounter: Atomic[uint32] + proc NewBrokerContext*(): BrokerContext = - ## Generates a random non-default broker context (as a raw uint32). - ## - ## The default broker context is reserved for the provider at index 0. - ## This helper never returns that value. - for _ in 0 ..< 16: - let b = urandom(4) - if b.len != 4: - continue - let key = - (uint32(b[0]) shl 24) or (uint32(b[1]) shl 16) or (uint32(b[2]) shl 8) or - uint32(b[3]) - if key != uint32(DefaultBrokerContext): - return BrokerContext(key) - BrokerContext(1'u32) + var nextId = gContextCounter.fetchAdd(1, moRelaxed) + if nextId == uint32(DefaultBrokerContext): + nextId = gContextCounter.fetchAdd(1, moRelaxed) + return BrokerContext(nextId)