-
Notifications
You must be signed in to change notification settings - Fork 12
/
block-processor.ts
665 lines (589 loc) · 26 KB
/
block-processor.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import {
AssetId,
Metadata,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb.js';
import { AuctionId } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/auction/v1/auction_pb.js';
import {
PositionState,
PositionState_PositionStateEnum,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/dex/v1/dex_pb.js';
import {
CommitmentSource,
Nullifier,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/sct/v1/sct_pb.js';
import { ValidatorInfoResponse } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/stake/v1/stake_pb.js';
import {
Action,
Transaction,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/transaction/v1/transaction_pb.js';
import { TransactionId } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/txhash/v1/txhash_pb.js';
import { StateCommitment } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/crypto/tct/v1/tct_pb.js';
import {
SpendableNoteRecord,
SwapRecord,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb.js';
import { auctionIdFromBech32 } from '@penumbra-zone/bech32m/pauctid';
import { bech32mIdentityKey } from '@penumbra-zone/bech32m/penumbravalid';
import { sha256Hash } from '@penumbra-zone/crypto-web/sha256';
import { getAssetId } from '@penumbra-zone/getters/metadata';
import {
getExchangeRateFromValidatorInfoResponse,
getIdentityKeyFromValidatorInfoResponse,
} from '@penumbra-zone/getters/validator-info-response';
import { toDecimalExchangeRate } from '@penumbra-zone/types/amount';
import { PRICE_RELEVANCE_THRESHOLDS, assetPatterns } from '@penumbra-zone/types/assets';
import type { BlockProcessorInterface } from '@penumbra-zone/types/block-processor';
import { uint8ArrayToHex } from '@penumbra-zone/types/hex';
import type { IndexedDbInterface } from '@penumbra-zone/types/indexed-db';
import type { ViewServerInterface } from '@penumbra-zone/types/servers';
import { ScanBlockResult } from '@penumbra-zone/types/state-commitment-tree';
import { computePositionId, getLpNftMetadata } from '@penumbra-zone/wasm/dex';
import { customizeSymbol } from '@penumbra-zone/wasm/metadata';
import { backOff } from 'exponential-backoff';
import { updatePricesFromSwaps } from './helpers/price-indexer.js';
import { processActionDutchAuctionEnd } from './helpers/process-action-dutch-auction-end.js';
import { processActionDutchAuctionSchedule } from './helpers/process-action-dutch-auction-schedule.js';
import { processActionDutchAuctionWithdraw } from './helpers/process-action-dutch-auction-withdraw.js';
import { RootQuerier } from './root-querier.js';
import { GasPrices } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/fee/v1/fee_pb.js';
import { IdentityKey } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb.js';
import { getDelegationTokenMetadata } from '@penumbra-zone/wasm/stake';
declare global {
// eslint-disable-next-line no-var
var __DEV__: boolean | undefined;
// eslint-disable-next-line no-var
var __ASSERT_ROOT__: boolean | undefined;
}
interface QueryClientProps {
querier: RootQuerier;
indexedDb: IndexedDbInterface;
viewServer: ViewServerInterface;
numeraires: AssetId[];
stakingAssetId: AssetId;
}
const BLANK_TX_SOURCE = new CommitmentSource({
source: { case: 'transaction', value: { id: new Uint8Array() } },
});
const POSITION_STATES: PositionState[] = [
new PositionState({ state: PositionState_PositionStateEnum.OPENED }),
new PositionState({ state: PositionState_PositionStateEnum.CLOSED }),
new PositionState({ state: PositionState_PositionStateEnum.WITHDRAWN, sequence: 0n }),
];
export class BlockProcessor implements BlockProcessorInterface {
private readonly querier: RootQuerier;
private readonly indexedDb: IndexedDbInterface;
private readonly viewServer: ViewServerInterface;
private readonly abortController: AbortController = new AbortController();
private numeraires: AssetId[];
private readonly stakingAssetId: AssetId;
private syncPromise: Promise<void> | undefined;
constructor({ indexedDb, viewServer, querier, numeraires, stakingAssetId }: QueryClientProps) {
this.indexedDb = indexedDb;
this.viewServer = viewServer;
this.querier = querier;
this.numeraires = numeraires;
this.stakingAssetId = stakingAssetId;
}
// If sync() is called multiple times concurrently, they'll all wait for
// the same promise rather than each starting their own sync process.
public sync = (): Promise<void> =>
(this.syncPromise ??= backOff(() => this.syncAndStore(), {
delayFirstAttempt: false,
startingDelay: 5_000, // 5 seconds
numOfAttempts: Infinity,
maxDelay: 20_000, // 20 seconds
retry: async (e, attemptNumber) => {
if (globalThis.__DEV__) {
console.debug('Sync failure', attemptNumber, e);
}
await this.viewServer.resetTreeToStored();
return !this.abortController.signal.aborted;
},
})).finally(
// if the above promise completes, exponential backoff has ended (aborted).
// reset the rejected promise to allow for a new sync to be started.
() => (this.syncPromise = undefined),
);
public stop = (r: string) => this.abortController.abort(`Sync stop ${r}`);
setNumeraires(numeraires: AssetId[]): void {
this.numeraires = numeraires;
}
private async syncAndStore() {
// start at next block, or genesis if height is undefined
let currentHeight = (await this.indexedDb.getFullSyncHeight()) ?? -1n;
// this is the first network query of the block processor. use backoff to
// delay until network is available
let latestKnownBlockHeight = await backOff(
async () => {
const latest = await this.querier.tendermint.latestBlockHeight();
if (!latest) {
throw new Error('Unknown latest block height');
}
return latest;
},
{ retry: () => true },
);
// TODO: init validator info in a better way, possibly after batch endpoint
// implemented https://github.com/penumbra-zone/penumbra/issues/4688
if (currentHeight === -1n) {
// In the `for` loop below, we only update validator infos once we've
// reached the latest known epoch. This means that, if a user is syncing
// for the first time, they could experience a broken UI until the latest
// known epoch is reached, since they may have delegation tokens but no
// validator info to go with them. So we'll update validator infos at the
// beginning of sync as well, and force the rest of sync to wait until
// it's done.
void this.updateValidatorInfos(0n);
}
// this is an indefinite stream of the (compact) chain from the network
// intended to run continuously
for await (const compactBlock of this.querier.compactBlock.compactBlockRange({
startHeight: currentHeight + 1n,
keepAlive: true,
abortSignal: this.abortController.signal,
})) {
// confirm block height to prevent corruption of local state
if (compactBlock.height === currentHeight + 1n) {
currentHeight = compactBlock.height;
} else {
throw new Error(`Unexpected block height: ${compactBlock.height} at ${currentHeight}`);
}
if (compactBlock.appParametersUpdated) {
await this.indexedDb.saveAppParams(await this.querier.app.appParams());
}
if (compactBlock.fmdParameters) {
await this.indexedDb.saveFmdParams(compactBlock.fmdParameters);
}
if (compactBlock.gasPrices) {
await this.indexedDb.saveGasPrices(
new GasPrices({
assetId: this.stakingAssetId,
...compactBlock.gasPrices,
}),
);
}
if (compactBlock.altGasPrices.length) {
for (const gasPrice of compactBlock.altGasPrices) {
await this.indexedDb.saveGasPrices(gasPrice);
}
}
// wasm view server scan
// - decrypts new notes
// - decrypts new swaps
// - updates idb with advice
const scannerWantsFlush = await this.viewServer.scanBlock(compactBlock);
// flushing is slow, avoid it until
// - wasm says
// - every 1000th block
// - every block at tip
const flushReasons = {
scannerWantsFlush,
interval: compactBlock.height % 1000n === 0n,
new: compactBlock.height > latestKnownBlockHeight,
};
const recordsByCommitment = new Map<StateCommitment, SpendableNoteRecord | SwapRecord>();
let flush: ScanBlockResult | undefined;
if (Object.values(flushReasons).some(Boolean)) {
flush = this.viewServer.flushUpdates();
// in an atomic query, this
// - saves 'sctUpdates'
// - saves new decrypted notes
// - saves new decrypted swaps
// - updates last block synced
await this.indexedDb.saveScanResult(flush);
// - detect unknown asset types
// - shielded pool for asset metadata
// - or, generate default fallback metadata
// - update idb
await this.identifyNewAssets(flush.newNotes);
for (const spendableNoteRecord of flush.newNotes) {
recordsByCommitment.set(spendableNoteRecord.noteCommitment!, spendableNoteRecord);
}
for (const swapRecord of flush.newSwaps) {
recordsByCommitment.set(swapRecord.swapCommitment!, swapRecord);
}
}
// nullifiers on this block may match notes or swaps from db
// - update idb, mark as spent/claimed
// - return nullifiers used in this way
const spentNullifiers = await this.resolveNullifiers(
compactBlock.nullifiers,
compactBlock.height,
);
// if a new record involves a state commitment, scan all block tx
if (spentNullifiers.size || recordsByCommitment.size) {
// this is a network query
const blockTx = await this.querier.app.txsByHeight(compactBlock.height);
// identify tx that involve a new record
// - compare nullifiers
// - compare state commitments
// - collect relevant tx for info generation later
// - if matched by commitment, collect record with recovered source
const { relevantTx, recordsWithSources } = await this.identifyTransactions(
spentNullifiers,
recordsByCommitment,
blockTx,
);
// this simply stores the new records with 'rehydrated' sources to idb
// TODO: this is the second time we save these records, after "saveScanResult"
await this.saveRecoveredCommitmentSources(recordsWithSources);
await this.processTransactions(blockTx);
// at this point txinfo can be generated and saved. this will resolve
// pending broadcasts, and populate the transaction list.
// - calls wasm for each relevant tx
// - saves to idb
await this.saveTransactions(compactBlock.height, relevantTx);
}
/**
* This... really isn't great.
*
* You can see above that we're already iterating over flush.newNotes. So
* why don't we put this call to
* `this.maybeUpsertAuctionWithNoteCommitment()` inside that earlier `for`
* loop?
*
* The problem is, we need to call `this.processTransactions()` before
* calling `this.maybeUpsertAuctionWithNoteCommitment()`, because
* `this.processTransactions()` is what saves the auction NFT metadata to
* the database. `this.maybeUpsertAuctionWithNoteCommitment()` depends on
* that auction NFT metadata being saved already to be able to detect
* whether a given note is for an auction NFT; only then will it save the
* note's commitment to the `AUCTIONS` table.
*
* "So why not just move `this.processTransactions()` to before the block
* where we handle `flush.newNotes`?" Because `this.processTransactions()`
* should only run after we've handled `flush.newNotes`, since we depend
* on the result of the flush to determine whether there are transactions
* to process in the first place. It's a catch-22.
*
* This isn't a problem in core because core isn't going back and forth
* between Rust and TypeScript like we are. If and when we move the block
* processor into Rust, this issue should be resolved.
*/
for (const spendableNoteRecord of flush?.newNotes ?? []) {
await this.maybeUpsertAuctionWithNoteCommitment(spendableNoteRecord);
}
// We do not store historical prices,
// so there is no point in saving prices that would already be considered obsolete at the time of saving
const blockInPriceRelevanceThreshold =
compactBlock.height >= latestKnownBlockHeight - BigInt(PRICE_RELEVANCE_THRESHOLDS.default);
// we can't use third-party price oracles for privacy reasons,
// so we have to get asset prices from swap results during block scans
// and store them locally in indexed-db.
if (blockInPriceRelevanceThreshold && compactBlock.swapOutputs.length) {
await updatePricesFromSwaps(
this.indexedDb,
this.numeraires,
compactBlock.swapOutputs,
compactBlock.height,
);
}
// We only query Tendermint for the latest known block height once, when
// the block processor starts running. Once we're caught up, though, the
// chain will of course continue adding blocks, and we'll keep processing
// them. So, we need to update `latestKnownBlockHeight` once we've passed
// it.
if (compactBlock.height > latestKnownBlockHeight) {
latestKnownBlockHeight = compactBlock.height;
}
const isLastBlockOfEpoch = !!compactBlock.epochRoot;
if (isLastBlockOfEpoch) {
await this.handleEpochTransition(compactBlock.height, latestKnownBlockHeight);
}
if (globalThis.__ASSERT_ROOT__) {
await this.assertRootValid(compactBlock.height);
}
}
}
/*
* Compares the locally stored, filtered TCT root with the actual one on chain. They should match.
* This is expensive to do every block, so should only be done in development for debugging purposes.
*/
private async assertRootValid(blockHeight: bigint): Promise<void> {
const remoteRoot = await this.querier.cnidarium.fetchRemoteRoot(blockHeight);
const inMemoryRoot = this.viewServer.getSctRoot();
if (remoteRoot.equals(inMemoryRoot)) {
console.debug(
`Block height: ${blockHeight} root matches remote ✅ \n`,
`Hash: ${uint8ArrayToHex(inMemoryRoot.inner)}`,
);
} else {
console.warn(
`Block height: ${blockHeight} root does not match remote ❌ \n`,
`Local hash: ${uint8ArrayToHex(inMemoryRoot.inner)} \n`,
`Remote hash: ${uint8ArrayToHex(remoteRoot.inner)}`,
);
}
}
private async saveRecoveredCommitmentSources(recovered: (SpendableNoteRecord | SwapRecord)[]) {
for (const record of recovered) {
if (record instanceof SpendableNoteRecord) {
await this.indexedDb.saveSpendableNote(record);
} else if (record instanceof SwapRecord) {
await this.indexedDb.saveSwap(record);
} else {
throw new Error('Unexpected record type');
}
}
}
private async identifyNewAssets(notes: SpendableNoteRecord[]) {
for (const note of notes) {
const assetId = note.note?.value?.assetId;
if (!assetId) {
continue;
}
await this.saveAndReturnMetadata(assetId);
}
}
private async identifyTransactions(
spentNullifiers: Set<Nullifier>,
commitmentRecordsByStateCommitment: Map<StateCommitment, SpendableNoteRecord | SwapRecord>,
blockTx: Transaction[],
) {
const relevantTx = new Map<TransactionId, Transaction>();
const recordsWithSources = new Array<SpendableNoteRecord | SwapRecord>();
for (const tx of blockTx) {
let txId: TransactionId | undefined;
const txCommitments = (tx.body?.actions ?? []).flatMap(({ action }) => {
switch (action.case) {
case 'output':
return action.value.body?.notePayload?.noteCommitment;
case 'swap':
return action.value.body?.payload?.commitment;
case 'swapClaim':
return [action.value.body?.output1Commitment, action.value.body?.output2Commitment];
default:
return;
}
});
const txNullifiers = (tx.body?.actions ?? []).map(({ action }) => {
switch (action.case) {
case 'spend':
case 'swapClaim':
return action.value.body?.nullifier;
default:
return;
}
});
for (const spentNullifier of spentNullifiers) {
if (txNullifiers.some(txNullifier => spentNullifier.equals(txNullifier))) {
txId = new TransactionId({ inner: await sha256Hash(tx.toBinary()) });
relevantTx.set(txId, tx);
spentNullifiers.delete(spentNullifier);
}
}
for (const [stateCommitment, spendableNoteRecord] of commitmentRecordsByStateCommitment) {
if (txCommitments.some(txCommitment => stateCommitment.equals(txCommitment))) {
txId ??= new TransactionId({ inner: await sha256Hash(tx.toBinary()) });
relevantTx.set(txId, tx);
if (BLANK_TX_SOURCE.equals(spendableNoteRecord.source)) {
spendableNoteRecord.source = new CommitmentSource({
source: { case: 'transaction', value: { id: txId.inner } },
});
recordsWithSources.push(spendableNoteRecord);
}
commitmentRecordsByStateCommitment.delete(stateCommitment);
}
}
}
return { relevantTx, recordsWithSources };
}
// TODO: refactor. there is definitely a better way to do this. batch
// endpoint issue https://github.com/penumbra-zone/penumbra/issues/4688
private async saveAndReturnMetadata(assetId: AssetId): Promise<Metadata | undefined> {
const metadataAlreadyInDb = await this.indexedDb.getAssetsMetadata(assetId);
if (metadataAlreadyInDb) {
return metadataAlreadyInDb;
}
const metadataFromNode = await this.querier.shieldedPool.assetMetadataById(assetId);
//do not save IBC token metadata that are not in the prax registry
const isIbcAsset = metadataFromNode && assetPatterns.ibc.matches(metadataFromNode.display);
if (metadataFromNode && !isIbcAsset) {
await this.indexedDb.saveAssetsMetadata(customizeSymbol(metadataFromNode));
return metadataFromNode;
}
return undefined;
}
private async saveAndReturnDelegationMetadata(
identityKey: IdentityKey,
): Promise<Metadata | undefined> {
const delegationTokenAssetId = new AssetId({
altBaseDenom: `udelegation_${bech32mIdentityKey(identityKey)}`,
});
const metadataAlreadyInDb = await this.indexedDb.getAssetsMetadata(delegationTokenAssetId);
if (metadataAlreadyInDb) {
return metadataAlreadyInDb;
}
const generatedMetadata = getDelegationTokenMetadata(identityKey);
await this.indexedDb.saveAssetsMetadata(customizeSymbol(generatedMetadata));
return generatedMetadata;
}
// Nullifier is published in network when a note is spent or swap is claimed.
private async resolveNullifiers(nullifiers: Nullifier[], height: bigint) {
const spentNullifiers = new Set<Nullifier>();
for (const nullifier of nullifiers) {
const record =
(await this.indexedDb.getSpendableNoteByNullifier(nullifier)) ??
(await this.indexedDb.getSwapByNullifier(nullifier));
if (!record) {
continue;
}
spentNullifiers.add(nullifier);
if (record instanceof SpendableNoteRecord) {
record.heightSpent = height;
await this.indexedDb.saveSpendableNote(record);
} else if (record instanceof SwapRecord) {
record.heightClaimed = height;
await this.indexedDb.saveSwap(record);
}
}
return spentNullifiers;
}
/**
* Identify various pieces of data from the transaction that we need to save,
* such as metadata, liquidity positions, etc.
*/
private async processTransactions(txs: Transaction[]) {
for (const tx of txs) {
for (const { action } of tx.body?.actions ?? []) {
await Promise.all([this.identifyAuctionNfts(action), this.identifyLpNftPositions(action)]);
}
}
}
/**
* during wasm tx info generation later, wasm independently queries idb for
* asset metadata, so we have to pre-populate. Auction NFTs aren't known by
* the chain so aren't populated by identifyNewAssets.
*/
private async identifyAuctionNfts(action: Action['action']) {
if (action.case === 'actionDutchAuctionSchedule' && action.value.description) {
await processActionDutchAuctionSchedule(action.value.description, this.indexedDb);
} else if (action.case === 'actionDutchAuctionEnd' && action.value.auctionId) {
await processActionDutchAuctionEnd(action.value, this.querier.auction, this.indexedDb);
} else if (action.case === 'actionDutchAuctionWithdraw' && action.value.auctionId) {
await processActionDutchAuctionWithdraw(
action.value.auctionId,
action.value.seq,
this.indexedDb,
);
}
}
/**
* during wasm tx info generation later, wasm independently queries idb for
* asset metadata, so we have to pre-populate. LpNft position states aren't
* known by the chain so aren't populated by identifyNewAssets
* - detect LpNft position opens
* - generate all possible position state metadata
* - update idb
*/
private async identifyLpNftPositions(action: Action['action']) {
if (action.case === 'positionOpen' && action.value.position) {
for (const state of POSITION_STATES) {
const metadata = getLpNftMetadata(computePositionId(action.value.position), state);
await this.indexedDb.saveAssetsMetadata(metadata);
}
// to optimize on-chain storage PositionId is not written in the positionOpen action,
// but can be computed via hashing of immutable position fields
await this.indexedDb.addPosition(
computePositionId(action.value.position),
action.value.position,
);
}
if (action.case === 'positionClose' && action.value.positionId) {
await this.indexedDb.updatePosition(
action.value.positionId,
new PositionState({ state: PositionState_PositionStateEnum.CLOSED }),
);
}
if (action.case === 'positionWithdraw' && action.value.positionId) {
// Record the LPNFT for the current sequence number.
const positionState = new PositionState({
state: PositionState_PositionStateEnum.WITHDRAWN,
sequence: action.value.sequence,
});
const metadata = getLpNftMetadata(action.value.positionId, positionState);
await this.indexedDb.saveAssetsMetadata(metadata);
await this.indexedDb.updatePosition(action.value.positionId, positionState);
}
}
private async maybeUpsertAuctionWithNoteCommitment(spendableNoteRecord: SpendableNoteRecord) {
const assetId = spendableNoteRecord.note?.value?.assetId;
if (!assetId) {
return;
}
const metadata = await this.indexedDb.getAssetsMetadata(assetId);
const captureGroups = assetPatterns.auctionNft.capture(metadata?.display ?? '');
if (!captureGroups) {
return;
}
const auctionId = new AuctionId(auctionIdFromBech32(captureGroups.auctionId));
await this.indexedDb.upsertAuction(auctionId, {
noteCommitment: spendableNoteRecord.noteCommitment,
});
}
private async saveTransactions(height: bigint, relevantTx: Map<TransactionId, Transaction>) {
for (const [id, transaction] of relevantTx) {
await this.indexedDb.saveTransaction(id, height, transaction);
}
}
private async handleEpochTransition(
endHeightOfPreviousEpoch: bigint,
latestKnownBlockHeight: bigint,
): Promise<void> {
const nextEpochStartHeight = endHeightOfPreviousEpoch + 1n;
await this.indexedDb.addEpoch(nextEpochStartHeight);
const { sctParams } = (await this.indexedDb.getAppParams()) ?? {};
const nextEpochIsLatestKnownEpoch =
sctParams && latestKnownBlockHeight - nextEpochStartHeight < sctParams.epochDuration;
// If we're doing a full sync from block 0, there could be hundreds or even
// thousands of epoch transitions in the chain already. If we update
// validator infos on every epoch transition, we'd be making tons of
// unnecessary calls to the RPC node for validator infos. Instead, we'll
// only get updated validator infos once we're within the latest known
// epoch.
if (nextEpochIsLatestKnownEpoch) {
void this.updateValidatorInfos(nextEpochStartHeight);
}
}
// TODO: refactor. there is definitely a better way to do this. batch
// endpoint issue https://github.com/penumbra-zone/penumbra/issues/4688
private async updateValidatorInfos(nextEpochStartHeight: bigint): Promise<void> {
for await (const validatorInfoResponse of this.querier.stake.allValidatorInfos()) {
if (!validatorInfoResponse.validatorInfo) {
continue;
}
await this.indexedDb.upsertValidatorInfo(validatorInfoResponse.validatorInfo);
await this.updatePriceForValidatorDelegationToken(
validatorInfoResponse,
nextEpochStartHeight,
);
// this loop requests delegation token metadata for each validator
// individually. there may be very many, so we must artificially delay
// this loop or the RPC may hard-ratelimit us.
await new Promise(resolve =>
setTimeout(
resolve,
// an entire second
1000,
),
);
}
}
private async updatePriceForValidatorDelegationToken(
validatorInfoResponse: ValidatorInfoResponse,
nextEpochStartHeight: bigint,
) {
const identityKey = getIdentityKeyFromValidatorInfoResponse(validatorInfoResponse);
const metadata = await this.saveAndReturnDelegationMetadata(identityKey);
if (metadata) {
const assetId = getAssetId(metadata);
const exchangeRate = getExchangeRateFromValidatorInfoResponse(validatorInfoResponse);
await this.indexedDb.updatePrice(
assetId,
this.stakingAssetId,
toDecimalExchangeRate(exchangeRate),
nextEpochStartHeight,
);
}
}
}