Skip to content

Commit 729fcdf

Browse files
committed
fixes
1 parent 4a678d7 commit 729fcdf

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

libp2p/protocols/service_discovery/advertiser.nim

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,19 @@ proc sendRegister*(
228228

229229
proc ticketRetryDelay*(ticket: Ticket, now: Moment): Duration {.raises: [].} =
230230
## Time to sleep before presenting ``ticket`` so the registrar sees it inside
231-
## the eligibility window ``[tMod + tWaitFor, tMod + tWaitFor + Δ]``.
232-
##
233-
## Uses absolute ticket timestamps (not a relative wait from response receipt,
234-
## and not capped by advert expiry) so retries under load are not rejected as
235-
## "ticket outside valid time window".
236-
let eligibility = ticket.tMod.get() + ticket.tWaitFor.get()
231+
## the eligibility window ``[tMod + tWaitFor, tMod + tWaitFor + delta]``.
232+
let tMod = ticket.tMod.valueOr:
233+
return ZeroDuration
234+
235+
let tWaitFor = ticket.tWaitFor.valueOr:
236+
return ZeroDuration
237+
238+
let eligibility = tMod + tWaitFor
239+
237240
if now >= eligibility:
238241
return ZeroDuration
239-
eligibility - now
242+
243+
return eligibility - now
240244

241245
proc advertiseToRegistrar*(
242246
disco: ServiceDiscovery,
@@ -295,9 +299,8 @@ proc advertiseToRegistrar*(
295299

296300
let waitSecs = ticketRetryDelay(newTicket, Moment.now())
297301

298-
debug "waiting for registrar", serviceId, registrar, wait = $waitSecs
299-
300302
if waitSecs > ZeroDuration:
303+
debug "waiting for registrar", serviceId, registrar, wait = $waitSecs
301304
await sleepAsync(waitSecs)
302305
of kademlia_protobuf.RegistrationStatus.Rejected:
303306
debug "registrar rejection, aborting", serviceId, registrar

libp2p/protocols/service_discovery/registrar.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ proc registration*(disco: ServiceDiscovery, peerId: PeerId, inMsg: Message): Mes
490490

491491
# Floor to whole seconds so in-memory ticket times match wire encode/decode
492492
# and the signature payload (epochSeconds / Duration.seconds).
493+
# tWait is already rounded.
493494
let nowSec = Moment.init(now.epochSeconds, Second)
494495
var ticket = Ticket(
495496
advertisement: regMsg.advertisement,

0 commit comments

Comments
 (0)