11import { type UUID , unknownToError } from "@happy.tech/common"
22import { bigIntToZeroPadded } from "@happy.tech/common"
33import { type Result , ResultAsync } from "neverthrow"
4- import { FINALIZED_STATUSES , type Randomness , type RandomnessStatus } from "./Randomness"
4+ import { type Randomness , RandomnessStatus } from "./Randomness"
5+ import { DIGITS_MAX_UINT256 } from "./constants"
56import { db } from "./db/driver"
67import { randomnessEntityToRow , randomnessRowToEntity } from "./db/types"
78
8- const COMMITMENT_PRUNE_INTERVAL_BLOCKS = 120n // 2 minutes
9+ const COMMITMENT_PRUNE_INTERVAL_BLOCKS = 60n // 2 minutes
910
10- // Quantity of digits in the max uint256 value
11- export const DIGITS_MAX_UINT256 = 78
11+ export const FINALIZED_STATUSES = [
12+ RandomnessStatus . REVEAL_EXECUTED ,
13+ RandomnessStatus . REVEAL_FAILED ,
14+ RandomnessStatus . REVEAL_NOT_SUBMITTED_ON_TIME ,
15+ ]
1216
1317export class RandomnessRepository {
14- private readonly map = new Map < bigint , Randomness > ( )
18+ // In-memory cache that maps block numbers to their corresponding Randomness
19+ private readonly cache = new Map < bigint , Randomness > ( )
1520
1621 async start ( ) : Promise < void > {
1722 const randomnessesDb = ( await db . selectFrom ( "randomnesses" ) . selectAll ( ) . execute ( ) ) . map ( randomnessRowToEntity )
1823 for ( const randomness of randomnessesDb ) {
19- this . map . set ( randomness . blockNumber , randomness )
24+ this . cache . set ( randomness . blockNumber , randomness )
2025 }
2126 }
2227
2328 getRandomnessForBlockNumber ( blockNumber : bigint ) : Randomness | undefined {
24- return this . map . get ( blockNumber )
29+ return this . cache . get ( blockNumber )
2530 }
2631
2732 getRandomnessForIntentId ( intentId : UUID ) : Randomness | undefined {
28- return Array . from ( this . map . values ( ) ) . find (
33+ return Array . from ( this . cache . values ( ) ) . find (
2934 ( randomness ) =>
3035 randomness . commitmentTransactionIntentId === intentId ||
3136 randomness . revealTransactionIntentId === intentId ,
3237 )
3338 }
3439
35- getRandomnessInBlockRange ( start : bigint , end : bigint ) : Randomness [ ] {
36- return Array . from ( this . map . values ( ) ) . filter (
37- ( randomness ) => randomness . blockNumber >= start && randomness . blockNumber <= end ,
38- )
39- }
40-
4140 getRandomnessInStatus ( status : RandomnessStatus ) : Randomness [ ] {
42- return Array . from ( this . map . values ( ) ) . filter ( ( randomness ) => randomness . status === status )
41+ return Array . from ( this . cache . values ( ) ) . filter ( ( randomness ) => randomness . status === status )
4342 }
4443
4544 /**
4645 * Save a randomness to the database
4746 * Even if the operation fails, the randomness will be saved in a in-memory cache
4847 */
4948 async saveRandomness ( randomness : Randomness ) : Promise < Result < void , Error > > {
50- this . map . set ( randomness . blockNumber , randomness )
49+ this . cache . set ( randomness . blockNumber , randomness )
5150 const row = randomnessEntityToRow ( randomness )
5251 return await ResultAsync . fromPromise ( db . insertInto ( "randomnesses" ) . values ( row ) . execute ( ) , unknownToError ) . map (
5352 ( ) => undefined ,
@@ -59,7 +58,7 @@ export class RandomnessRepository {
5958 * Even if the operation fails, the randomness will be updated in a in-memory cache
6059 */
6160 async updateRandomness ( randomness : Randomness ) : Promise < Result < void , Error > > {
62- this . map . set ( randomness . blockNumber , randomness )
61+ this . cache . set ( randomness . blockNumber , randomness )
6362 const row = randomnessEntityToRow ( randomness )
6463 return await ResultAsync . fromPromise (
6564 db
@@ -73,9 +72,9 @@ export class RandomnessRepository {
7372
7473 async pruneRandomnesses ( latestBlock : bigint ) : Promise < Result < void , Error > > {
7574 const cutoffBlock = latestBlock - COMMITMENT_PRUNE_INTERVAL_BLOCKS
76- for ( const blockNumber of this . map . keys ( ) ) {
75+ for ( const blockNumber of this . cache . keys ( ) ) {
7776 if ( blockNumber < cutoffBlock ) {
78- this . map . delete ( blockNumber )
77+ this . cache . delete ( blockNumber )
7978 }
8079 }
8180
0 commit comments