-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.ts
More file actions
47 lines (40 loc) · 1.44 KB
/
cache.ts
File metadata and controls
47 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { assert, ChainClient } from "./deps.ts";
export class BeaconCache {
/** A map from round to signature */
private data: Map<number, string> = new Map();
private entryLimit: number;
private client: ChainClient;
public constructor(client: ChainClient, entryLimit: number) {
this.client = client;
this.entryLimit = entryLimit;
}
public add(round: number, signature: string) {
this.data.set(round, signature);
// Remove the first elements (by insertion order)
while (this.data.size > this.entryLimit) {
const key = this.data.keys().next();
const removed = this.data.delete(key.value);
assert(removed);
}
}
public async get(requestRound: number): Promise<string> {
const got = this.data.get(requestRound);
if (got) {
return got;
}
const { signature, round } = await this.client.get(requestRound);
assert(typeof signature === "string", "Got unexpected signature type at runtime");
assert(typeof round === "number", "Got unexpected round type at runtime");
assert(
round === requestRound,
`Got differerent round than expected from drand client (requested: ${requestRound}, got: ${round}). This is likely a server-side error.`,
);
this.data.set(requestRound, signature);
return signature;
}
public debug(): string {
return `BeaconCache { entry count: ${this.data.size}, entries: ${
Array.from(this.data.keys()).join(", ")
}}`;
}
}