Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move epoch genesis to block processor #1720

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/late-crabs-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@penumbra-zone/storage': minor
'@penumbra-zone/query': minor
---

perform idb genesis in the block processor
23 changes: 15 additions & 8 deletions packages/query/src/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,16 @@ export class BlockProcessor implements BlockProcessorInterface {
* Sync local state to present. This method will
* - identify current synced height (or `-1n` to represent a 'pre-genesis' state)
TalDerei marked this conversation as resolved.
Show resolved Hide resolved
* - query remote rpc for the chain's latest block height
* - pre-genesis, initialize validator info
* - pre-genesis, initialize 0th epoch and validator info
* - pre-genesis, process a local genesis block if provided
* - query remote rpc to begin streaming at the next block
* - iterate
*/
private async syncAndStore() {
const PRE_GENESIS_SYNC_HEIGHT = -1n;

// start at next block, or genesis if height is undefined
let currentHeight = (await this.indexedDb.getFullSyncHeight()) ?? -1n;
let currentHeight = (await this.indexedDb.getFullSyncHeight()) ?? PRE_GENESIS_SYNC_HEIGHT;

// this is the first network query of the block processor. use backoff to
// delay until network is available
Expand All @@ -160,13 +162,18 @@ export class BlockProcessor implements BlockProcessorInterface {
{ retry: () => true },
);

// special case genesis sync
if (currentHeight === -1n) {
// handle the special case where no syncing has been done yet, and
// prepares for syncing and checks for a bundled genesis block,
// which can save time by avoiding an initial network request.
if (currentHeight === PRE_GENESIS_SYNC_HEIGHT) {
// create first epoch
await this.indexedDb.addEpoch(0n);

// initialize validator info at genesis
// TODO: use batch endpoint https://github.com/penumbra-zone/penumbra/issues/4688
TalDerei marked this conversation as resolved.
Show resolved Hide resolved
void this.updateValidatorInfos(currentHeight + 1n);
void this.updateValidatorInfos(0n);

// begin the chain with local genesis block if provided
TalDerei marked this conversation as resolved.
Show resolved Hide resolved
// conditional only runs if there is a bundled genesis block provided for the chain
if (this.genesisBlock?.height === currentHeight + 1n) {
currentHeight = this.genesisBlock.height;

Expand Down Expand Up @@ -373,8 +380,8 @@ export class BlockProcessor implements BlockProcessorInterface {
);
}

const isLastBlockOfEpoch = !!compactBlock.epochRoot;
if (isLastBlockOfEpoch) {
// The presence of `epochRoot` indicates that this is the final block of the current epoch.
if (compactBlock.epochRoot) {
TalDerei marked this conversation as resolved.
Show resolved Hide resolved
await this.handleEpochTransition(compactBlock.height, latestKnownBlockHeight);
}

Expand Down
5 changes: 0 additions & 5 deletions packages/storage/src/indexed-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,6 @@ export class IndexedDb implements IndexedDbInterface {
);
await instance.saveRegistryAssets(registryClient, chainId); // Pre-load asset metadata from registry

const existing0thEpoch = await instance.getEpochByHeight(0n);
if (!existing0thEpoch) {
await instance.addEpoch(0n);
} // Create first epoch

return instance;
}

Expand Down
64 changes: 0 additions & 64 deletions packages/storage/src/indexed-db/indexed-db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import {
delegationMetadataA,
delegationMetadataB,
emptyScanResult,
epoch1,
epoch2,
epoch3,
metadataA,
metadataB,
metadataC,
Expand Down Expand Up @@ -565,67 +562,6 @@ describe('IndexedDb', () => {
});
});

describe('epochs', () => {
TalDerei marked this conversation as resolved.
Show resolved Hide resolved
let db: IndexedDb;

beforeEach(async () => {
db = await IndexedDb.initialize({ ...generateInitialProps() });
});

it('prepopulates the 0th epoch', async () => {
const epoch = await db.getEpochByHeight(0n);
expect(epoch?.index).toBe(0n);
expect(epoch?.startHeight).toBe(0n);
});

describe('addEpoch', () => {
beforeEach(async () => {
await db.addEpoch(epoch1.startHeight);
await db.addEpoch(epoch2.startHeight);
await db.addEpoch(epoch3.startHeight);
});

it('auto-increments the epoch index', async () => {
const [result1, result2, result3] = await Promise.all([
db.getEpochByHeight(150n),
db.getEpochByHeight(250n),
db.getEpochByHeight(350n),
]);

expect(result1?.index).toBe(1n);
expect(result2?.index).toBe(2n);
expect(result3?.index).toBe(3n);
});

it('should not save the epoch with the same startHeight twice', async () => {
await db.addEpoch(epoch3.startHeight);

const result = await db.getEpochByHeight(350n);
expect(result?.index).toBe(3n);
});
});

describe('getEpochByHeight', () => {
beforeEach(async () => {
await db.addEpoch(epoch1.startHeight);
await db.addEpoch(epoch2.startHeight);
await db.addEpoch(epoch3.startHeight);
});

it('returns the epoch containing the given block height', async () => {
const [result1, result2, result3] = await Promise.all([
db.getEpochByHeight(150n),
db.getEpochByHeight(250n),
db.getEpochByHeight(350n),
]);

expect(result1?.toJson()).toEqual(epoch1.toJson());
expect(result2?.toJson()).toEqual(epoch2.toJson());
expect(result3?.toJson()).toEqual(epoch3.toJson());
});
});
});

describe('prices', () => {
let db: IndexedDb;

Expand Down
Loading