-
Notifications
You must be signed in to change notification settings - Fork 54
/
DbSyncNetworkInfoProvider.ts
140 lines (120 loc) · 4.46 KB
/
DbSyncNetworkInfoProvider.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as NetworkInfoCacheKey from './keys';
import {
Cardano,
CardanoNode,
CardanoNodeUtil,
EraSummary,
NetworkInfoProvider,
StakeSummary,
SupplySummary,
createSlotEpochCalc
} from '@cardano-sdk/core';
import { DbSyncProvider } from '../../util/DbSyncProvider';
import { Disposer, EpochMonitor } from '../../util/polling/types';
import { GenesisData } from './types';
import { InMemoryCache, UNLIMITED_CACHE_TTL } from '../../InMemoryCache';
import { Logger } from 'ts-log';
import { NetworkInfoBuilder } from './NetworkInfoBuilder';
import { Pool } from 'pg';
import { RunnableModule } from '@cardano-sdk/util';
import { loadGenesisData, toGenesisParams, toLedgerTip, toProtocolParams, toSupply } from './mappers';
export interface NetworkInfoProviderProps {
cardanoNodeConfigPath: string;
}
export interface NetworkInfoProviderDependencies {
db: Pool;
cache: InMemoryCache;
logger: Logger;
cardanoNode: CardanoNode;
epochMonitor: EpochMonitor;
}
export class DbSyncNetworkInfoProvider extends DbSyncProvider(RunnableModule) implements NetworkInfoProvider {
#logger: Logger;
#cache: InMemoryCache;
#currentEpoch: Cardano.EpochNo;
#currentHash: Cardano.BlockId | undefined;
#builder: NetworkInfoBuilder;
#genesisDataReady: Promise<GenesisData>;
#epochMonitor: EpochMonitor;
#epochRolloverDisposer: Disposer;
constructor(
{ cardanoNodeConfigPath }: NetworkInfoProviderProps,
{ db, cache, logger, cardanoNode, epochMonitor }: NetworkInfoProviderDependencies
) {
super(db, cardanoNode, 'DbSyncNetworkInfoProvider', logger);
this.#logger = logger;
this.#cache = cache;
this.#currentEpoch = Cardano.EpochNo(0);
this.#epochMonitor = epochMonitor;
this.#builder = new NetworkInfoBuilder(db, logger);
this.#genesisDataReady = loadGenesisData(cardanoNodeConfigPath);
}
public async ledgerTip(): Promise<Cardano.Tip> {
const tip = await this.#builder.queryLedgerTip();
const result = toLedgerTip(tip);
// Perform computation only on changed tip
if (this.#currentHash !== result.hash) {
this.#currentHash = result.hash;
const slotEpochCalc = createSlotEpochCalc(await this.eraSummaries());
const currentEpoch = slotEpochCalc(result.slot);
// On epoch rollover, invalidate the cache before returning
if (this.#currentEpoch.valueOf() !== currentEpoch.valueOf()) {
// The first time, no need to invalidate the cache
if (this.#currentEpoch.valueOf() !== 0) this.#epochMonitor.onEpoch(currentEpoch);
this.#currentEpoch = currentEpoch;
}
}
return result;
}
public async protocolParameters(): Promise<Cardano.ProtocolParameters> {
const currentProtocolParams = await this.#builder.queryProtocolParams();
return toProtocolParams(currentProtocolParams);
}
public async genesisParameters(): Promise<Cardano.CompactGenesis> {
const genesisData = await this.#genesisDataReady;
return toGenesisParams(genesisData);
}
public async lovelaceSupply(): Promise<SupplySummary> {
const { maxLovelaceSupply } = await this.#genesisDataReady;
const [circulatingSupply, totalSupply] = await Promise.all([
this.#cache.get(NetworkInfoCacheKey.CIRCULATING_SUPPLY, () => this.#builder.queryCirculatingSupply()),
this.#cache.get(
NetworkInfoCacheKey.TOTAL_SUPPLY,
() => this.#builder.queryTotalSupply(maxLovelaceSupply),
UNLIMITED_CACHE_TTL
)
]);
return toSupply({ circulatingSupply, totalSupply });
}
public async stake(): Promise<StakeSummary> {
this.#logger.debug('About to query stake data');
const [live, activeStake] = await Promise.all([
this.#cache.get(NetworkInfoCacheKey.LIVE_STAKE, () =>
this.cardanoNode.stakeDistribution().then(CardanoNodeUtil.toLiveStake)
),
this.#cache.get(NetworkInfoCacheKey.ACTIVE_STAKE, () => this.#builder.queryActiveStake(), UNLIMITED_CACHE_TTL)
]);
return {
active: BigInt(activeStake),
live
};
}
public async eraSummaries(): Promise<EraSummary[]> {
return await this.#cache.get(
NetworkInfoCacheKey.ERA_SUMMARIES,
() => this.cardanoNode.eraSummaries(),
UNLIMITED_CACHE_TTL
);
}
async initializeImpl() {
return Promise.resolve();
}
async startImpl() {
this.#epochRolloverDisposer = this.#epochMonitor.onEpochRollover(() => this.#cache.clear());
}
async shutdownImpl() {
this.#cache.shutdown();
await this.#genesisDataReady;
this.#epochRolloverDisposer();
}
}