Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,21 @@ export async function* watch(
options: WatchOptions = defaultWatchOptions,
): AsyncGenerator<RandomnessBeacon> {
const info = await client.chain().info()
let currentRound = roundAt(Date.now(), info)


while (!abortController.signal.aborted) {
const now = Date.now()
await sleep(roundTime(info, currentRound) - now)

const beacon = await retryOnError(async () => client.get(currentRound), options.retriesOnFailure)
yield validatedBeacon(client, beacon, currentRound)
currentRound = currentRound + 1
const targetRound = roundAt(now, info)

const nextRoundTime = roundTime(info, targetRound)
const waitTime = nextRoundTime - now
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will waitTime ever be > 0 here given we're checking the round at now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think the difference between nextRoundTime (the time of the next round, which will occur in the future) and the current time will be greater than 0.

Is there anything incorrect here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assuming 3 second rounds:
If we start at time 0 and it's currently time 1, roundAt(now, info) returns 1 (which started at time 0), therefore we will wait 0 seconds.
On the next iteration, it will still be time 1(ish), and roundAt(now, info) will still return 1, waiting 0 seconds again
I'd have assumed you need to increment the round to get the next round

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed it seems the tests are failing for the same reason


await sleep(Math.max(0, waitTime))

const beacon = await retryOnError(
async () => client.get(targetRound),
options.retriesOnFailure
)
yield validatedBeacon(client, beacon, targetRound)
}
}

Expand Down
Loading