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

check height of incoming block #1276

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/sixty-spiders-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@penumbra-zone/query': patch
---

prevent processing the same block twice
124 changes: 64 additions & 60 deletions packages/query/src/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,67 +116,10 @@ export class BlockProcessor implements BlockProcessorInterface {

public stop = (r: string) => this.abortController.abort(`Sync stop ${r}`);

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: // TODO: what other actions have commitments?
return;
}
});

const txNullifiers = (tx.body?.actions ?? []).map(({ action }) => {
switch (action.case) {
case 'spend':
case 'swapClaim':
return action.value.body?.nullifier;
default: // TODO: what other actions have nullifiers?
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 };
}

private async syncAndStore() {
// start at next block, or 0 if height is undefined
const startHeight = ((await this.indexedDb.getFullSyncHeight()) ?? -1n) + 1n;
// start at next block, or genesis if height is undefined
let currentHeight = (await this.indexedDb.getFullSyncHeight()) ?? -1n;
const startHeight = currentHeight + 1n;
turbocrime marked this conversation as resolved.
Show resolved Hide resolved

// this is the first network query of the block processor. use backoff to
// delay until network is available
Expand Down Expand Up @@ -207,6 +150,9 @@ export class BlockProcessor implements BlockProcessorInterface {
keepAlive: true,
abortSignal: this.abortController.signal,
})) {
if (compactBlock.height !== ++currentHeight) {
turbocrime marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Unexpected block: Recieved ${compactBlock.height} at ${currentHeight}`);
turbocrime marked this conversation as resolved.
Show resolved Hide resolved
}
turbocrime marked this conversation as resolved.
Show resolved Hide resolved
if (compactBlock.appParametersUpdated) {
await this.indexedDb.saveAppParams(await this.querier.app.appParams());
}
Expand Down Expand Up @@ -377,6 +323,64 @@ export class BlockProcessor implements BlockProcessorInterface {
}
}

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: // TODO: what other actions have commitments?
return;
}
});

const txNullifiers = (tx.body?.actions ?? []).map(({ action }) => {
switch (action.case) {
case 'spend':
case 'swapClaim':
return action.value.body?.nullifier;
default: // TODO: what other actions have nullifiers?
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 };
}

private async saveAndReturnMetadata(assetId: AssetId): Promise<Metadata | undefined> {
const metadataAlreadyInDb = await this.indexedDb.getAssetsMetadata(assetId);
if (metadataAlreadyInDb) return metadataAlreadyInDb;
Expand Down
Loading