Skip to content

Commit 62ee648

Browse files
feat(tandomnness): improve performance and reability
1 parent a98fbb3 commit 62ee648

4 files changed

Lines changed: 20 additions & 15 deletions

File tree

packages/contracts/src/randomness/Random.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ contract Random is RandomCommitment, Drand {
1111
* This is necessary, because whenever a Drand value is generated at time T, it is not possible to guarantee it
1212
* will be posted on a block with timestamp T (even if such a block exists) because of network delays.
1313
*/
14-
uint256 public constant DRAND_DELAY = 2;
14+
uint256 public constant DRAND_DELAY = 4;
1515
/**
1616
* The minimum amount of time (in seconds) that commitments to future Drand randomness must be made
1717
* in advance. This delay is relative to Drand timestamp (so DRAND_DELAY must also be added).

packages/randomness-service/src/CustomGasEstimator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export class CustomGasEstimator extends DefaultGasLimitEstimator {
1818
return ok(100000n)
1919
}
2020

21+
if (transaction.functionName === "postDrand") {
22+
return ok(220000n)
23+
}
24+
2125
return this.simulateTransactionForGas(transactionManager, transaction)
2226
}
2327
}

packages/randomness-service/src/DrandService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class DrandService {
3838
}
3939

4040
const url = `${env.EVM_DRAND_URL}/rounds/${round}`
41-
const response = await ResultAsync.fromPromise(fetchWithRetry(url, {}, 2, 500), unknownToError)
41+
const response = await ResultAsync.fromPromise(fetchWithRetry(url, {}, 2, 1000), unknownToError)
4242

4343
if (response.isErr()) {
4444
return err(DrandError.NetworkError)

packages/randomness-service/src/index.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class RandomnessService {
5151
console.error(description)
5252
})
5353

54+
// Start fetching Drand beacons for past rounds. Subsequently, beacons will be fetched at regular intervals
55+
// to maintain synchronization with the Drand network
56+
this.handleNewDrandBeacons()
57+
5458
// Synchronize the retrieval of new Drand beacons with the Drand network to request them as soon as they become available.
5559
const periodMs = Number(env.EVM_DRAND_PERIOD_SECONDS) * MS_IN_SECOND
5660
const drandGenesisTimestampMs = Number(env.EVM_DRAND_GENESIS_TIMESTAMP_SECONDS) * MS_IN_SECOND
@@ -255,22 +259,19 @@ class RandomnessService {
255259

256260
const randomnessToReveal = this.randomnessRepository.getRandomnessForBlockNumber(nextBlockNumber)
257261

258-
if (!randomnessToReveal) {
259-
console.warn("Not found randomness to reveal with block number", nextBlockNumber)
260-
return transactions
261-
}
262-
263-
const revealValueTransaction = this.transactionFactory.createRevealValueTransaction(randomnessToReveal)
262+
if (randomnessToReveal) {
263+
const revealValueTransaction = this.transactionFactory.createRevealValueTransaction(randomnessToReveal)
264264

265-
transactions.unshift(revealValueTransaction)
265+
transactions.unshift(revealValueTransaction)
266266

267-
randomnessToReveal.addRevealTransactionIntentId(revealValueTransaction.intentId)
267+
randomnessToReveal.addRevealTransactionIntentId(revealValueTransaction.intentId)
268268

269-
this.randomnessRepository.updateRandomness(randomnessToReveal).then((result) => {
270-
if (result.isErr()) {
271-
console.error("Failed to update randomness", result.error)
272-
}
273-
})
269+
this.randomnessRepository.updateRandomness(randomnessToReveal).then((result) => {
270+
if (result.isErr()) {
271+
console.error("Failed to update randomness", result.error)
272+
}
273+
})
274+
}
274275

275276
transactions.push(...this.pendingPostDrandTransactions)
276277

0 commit comments

Comments
 (0)