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

feat(query): update block processor to include subaccounts into LPs #279

Merged
merged 4 commits into from
Jan 30, 2025
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
20 changes: 10 additions & 10 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
"@bufbuild/protobuf": "^1.x",
"@connectrpc/connect": "^1.x",
"@connectrpc/connect-web": "^1.x",
"@penumbra-labs/registry": "^12.1.0",
"@penumbra-zone/bech32m": "11.0.0",
"@penumbra-labs/registry": "^12.1.1",
"@penumbra-zone/bech32m": "12.0.0",
"@penumbra-zone/client": "22.0.0",
"@penumbra-zone/crypto-web": "33.0.0",
"@penumbra-zone/getters": "21.0.0",
"@penumbra-zone/crypto-web": "34.0.0",
"@penumbra-zone/getters": "22.0.0",
"@penumbra-zone/keys": "4.2.1",
"@penumbra-zone/perspective": "44.0.0",
"@penumbra-zone/protobuf": "7.0.0",
"@penumbra-zone/perspective": "45.0.0",
"@penumbra-zone/protobuf": "7.1.0",
"@penumbra-zone/query": "workspace:*",
"@penumbra-zone/services": "49.0.0",
"@penumbra-zone/storage": "44.0.0",
"@penumbra-zone/services": "50.0.0",
"@penumbra-zone/storage": "45.0.0",
"@penumbra-zone/transport-chrome": "8.0.1",
"@penumbra-zone/transport-dom": "7.5.0",
"@penumbra-zone/types": "27.1.0",
"@penumbra-zone/wasm": "37.1.0",
"@penumbra-zone/types": "28.0.0",
"@penumbra-zone/wasm": "38.0.0",
"@radix-ui/react-icons": "^1.3.2",
"@repo/context": "workspace:*",
"@repo/ui": "workspace:*",
Expand Down
20 changes: 10 additions & 10 deletions packages/context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
},
"peerDependencies": {
"@bufbuild/protobuf": "^1.x",
"@penumbra-labs/registry": "^12.1.0",
"@penumbra-zone/bech32m": "11.0.0",
"@penumbra-zone/crypto-web": "33.0.0",
"@penumbra-zone/getters": "21.0.0",
"@penumbra-labs/registry": "^12.1.1",
"@penumbra-zone/bech32m": "12.0.0",
"@penumbra-zone/crypto-web": "34.0.0",
"@penumbra-zone/getters": "22.0.0",
"@penumbra-zone/keys": "4.2.1",
"@penumbra-zone/perspective": "44.0.0",
"@penumbra-zone/protobuf": "7.0.0",
"@penumbra-zone/perspective": "45.0.0",
"@penumbra-zone/protobuf": "7.1.0",
"@penumbra-zone/query": "workspace:*",
"@penumbra-zone/services": "49.0.0",
"@penumbra-zone/storage": "44.0.0",
"@penumbra-zone/services": "50.0.0",
"@penumbra-zone/storage": "45.0.0",
"@penumbra-zone/transport-chrome": "8.0.1",
"@penumbra-zone/transport-dom": "7.5.0",
"@penumbra-zone/types": "27.1.0",
"@penumbra-zone/wasm": "37.1.0",
"@penumbra-zone/types": "28.0.0",
"@penumbra-zone/wasm": "38.0.0",
"exponential-backoff": "^3.1.1"
}
}
6 changes: 3 additions & 3 deletions packages/noble/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
},
"dependencies": {
"@cosmjs/stargate": "^0.32.4",
"@penumbra-zone/bech32m": "11.0.0",
"@penumbra-zone/protobuf": "7.0.0",
"@penumbra-zone/wasm": "37.1.0"
"@penumbra-zone/bech32m": "12.0.0",
"@penumbra-zone/protobuf": "7.1.0",
"@penumbra-zone/wasm": "38.0.0"
}
}
12 changes: 6 additions & 6 deletions packages/query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"@bufbuild/protobuf": "^1.x",
"@connectrpc/connect": "^1.x",
"@connectrpc/connect-web": "^1.x",
"@penumbra-zone/bech32m": "11.0.0",
"@penumbra-zone/crypto-web": "33.0.0",
"@penumbra-zone/getters": "21.0.0",
"@penumbra-zone/protobuf": "7.0.0",
"@penumbra-zone/types": "27.1.0",
"@penumbra-zone/wasm": "37.1.0",
"@penumbra-zone/bech32m": "12.0.0",
"@penumbra-zone/crypto-web": "34.0.0",
"@penumbra-zone/getters": "22.0.0",
"@penumbra-zone/protobuf": "7.1.0",
"@penumbra-zone/types": "28.0.0",
"@penumbra-zone/wasm": "38.0.0",
"exponential-backoff": "^3.1.1"
},
"engine": {
Expand Down
23 changes: 14 additions & 9 deletions packages/query/src/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { processActionDutchAuctionEnd } from './helpers/process-action-dutch-auc
import { processActionDutchAuctionSchedule } from './helpers/process-action-dutch-auction-schedule';
import { processActionDutchAuctionWithdraw } from './helpers/process-action-dutch-auction-withdraw';
import { RootQuerier } from './root-querier';
import { IdentityKey } from '@penumbra-zone/protobuf/penumbra/core/keys/v1/keys_pb';
import { AddressIndex, IdentityKey } from '@penumbra-zone/protobuf/penumbra/core/keys/v1/keys_pb';
import { getDelegationTokenMetadata } from '@penumbra-zone/wasm/stake';
import { toPlainMessage } from '@bufbuild/protobuf';
import { getAssetIdFromGasPrices } from '@penumbra-zone/getters/compact-block';
Expand Down Expand Up @@ -309,7 +309,7 @@ export class BlockProcessor implements BlockProcessorInterface {

// if a new record involves a state commitment, scan all block tx
if (spentNullifiers.size || recordsByCommitment.size) {
// this is a network query
// compact block doesn't store transactions data, this query request it by rpc call
const blockTx = await this.querier.app.txsByHeight(compactBlock.height);

// Filter down to transactions & note records in block relevant to user
Expand Down Expand Up @@ -494,7 +494,7 @@ export class BlockProcessor implements BlockProcessorInterface {

// 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>();
const spentNullifiers = new Map<Nullifier, SpendableNoteRecord | SwapRecord>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we merge, I want to circle back to this and a few other things with the data modeling. What's the lifetime of the spent nullifier map? How big is a SNR? An OOM seems somewhat unlikely but I don't totally understand the approach yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good flag, still thinking through the proposed data structure.

the spentNullifiers map is a local variable that only lives for the duration of processing a single block in processBlock(). It isn’t a class field or stored anywhere long term. SNRs are stored in the spendable_notes indexeddb table using the saveSpendableNote() storage method, and the state payload is marked as spent by appending a "heightSpent" field in storage.

SNRs are ~700 bytes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK this seems totally fine, round it to 1KB, that's 1M SNRs. sgtm

Copy link
Contributor

@TalDerei TalDerei Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after reviewing the code, Map<Nullifier, SpendableNoteRecord | SwapRecord> is actually the optimal and clever approach to minimize db fetches required to understand what subaccount a particular LP position is eventually associated with.

stepping through the code, prior to calling resolveNullifiers() , indexdb stores the successfully trial decrypted spendable notes in a table who's primary key is the commitment and secondary index is the nullifier. So in theory, you have all the information to know which SNR is associated with what commitment and nullifier directly in storage, but don't yet know which transaction that commitment is associated with, because we haven't processed and saved the transaction information at this stage of the block processor.

resolveNullifiers() will subsequently query any SNR / SwapRecord stored in indexeddb that match any nullifier in the incoming compact block, and the record <> nullifier pair are stored in the spentNullifiers map.

up until this point in the block processing, we know a few things:

  • which SNR / SwapRecord is associated with the user
  • what commitment + nullifier is associated with what SNR / SwapRecord
  • don't know which transaction is associated with the SNR / SwapRecord or commitment

then, if we found a commitment or nullifier relevant to the user in that block, we request all the transactions for that block height from the full node, and filter the specific transaction relevant to the user. This is where the original spentNullifiers map comes into play. We have a spentNullifiers map (Map<Nullifier, SpendableNoteRecord | SwapRecord>) and commitmentRecords map (Map<StateCommitment, SpendableNoteRecord | SwapRecord>), and once we know which commitment / nullifier inside the transaction's actions is associated with the user's commitment / nullifier (stored in those maps in memory), we know we transaction is the user's, and we return the transaction associated with it's relevant subacount by deriving the AddressIndex from the SNR itself.

After identifying the transaction, we have a special method called processTransactions that will identify the LP position relevant to the user and save it alongside the subaccount.

anyways, if we didn't originally have this map, once we knew which nullifier is associated with the user, we'd need to perform another db query to fetch the relevant SNR from the table, and subsequently derive the AddressIndex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this great explanation, @TalDerei! This is exactly what the PR does in the smallest details

const readOperations = [];
const writeOperations = [];

Expand All @@ -518,8 +518,6 @@ export class BlockProcessor implements BlockProcessorInterface {
continue;
}

spentNullifiers.add(nullifier);

if (record instanceof SpendableNoteRecord) {
record.heightSpent = height;
const writePromise = this.indexedDb.saveSpendableNote({
Expand All @@ -535,6 +533,8 @@ export class BlockProcessor implements BlockProcessorInterface {
});
writeOperations.push(writePromise);
}

spentNullifiers.set(nullifier, record);
}

// Await all writes in parallel
Expand All @@ -548,9 +548,12 @@ export class BlockProcessor implements BlockProcessorInterface {
* such as metadata, liquidity positions, etc.
*/
private async processTransactions(txs: RelevantTx[]) {
for (const { data } of txs) {
for (const { data, subaccount } of txs) {
for (const { action } of data.body?.actions ?? []) {
await Promise.all([this.identifyAuctionNfts(action), this.identifyLpNftPositions(action)]);
await Promise.all([
this.identifyAuctionNfts(action),
this.identifyLpNftPositions(action, subaccount),
]);
}
}
}
Expand Down Expand Up @@ -582,7 +585,7 @@ export class BlockProcessor implements BlockProcessorInterface {
* - generate all possible position state metadata
* - update idb
*/
private async identifyLpNftPositions(action: Action['action']) {
private async identifyLpNftPositions(action: Action['action'], subaccount?: AddressIndex) {
if (action.case === 'positionOpen' && action.value.position) {
for (const state of POSITION_STATES) {
const metadata = getLpNftMetadata(computePositionId(action.value.position), state);
Expand All @@ -597,12 +600,14 @@ export class BlockProcessor implements BlockProcessorInterface {
await this.indexedDb.addPosition(
computePositionId(action.value.position),
action.value.position,
subaccount,
);
}
if (action.case === 'positionClose' && action.value.positionId) {
await this.indexedDb.updatePosition(
action.value.positionId,
new PositionState({ state: PositionState_PositionStateEnum.CLOSED }),
subaccount,
);
}
if (action.case === 'positionWithdraw' && action.value.positionId) {
Expand All @@ -618,7 +623,7 @@ export class BlockProcessor implements BlockProcessorInterface {
penumbraAssetId: getAssetId(metadata),
});

await this.indexedDb.updatePosition(action.value.positionId, positionState);
await this.indexedDb.updatePosition(action.value.positionId, positionState, subaccount);
}
}

Expand Down
Loading