Skip to content

Commit

Permalink
557 change to dynamically computed transactionviews (#714)
Browse files Browse the repository at this point in the history
* wip

* wip: dynamically compute txv txp

* wip

* change to dynamically computed transactionviews

* fix format

* review fixes
  • Loading branch information
Valentine1898 authored Mar 12, 2024
1 parent 641b5b1 commit 29d7b2a
Show file tree
Hide file tree
Showing 15 changed files with 491 additions and 1,976 deletions.
2 changes: 1 addition & 1 deletion apps/extension/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

PRAX=lkpmkhpnhknhmibgnmmhdhgdilepfghe
IDB_VERSION=27
IDB_VERSION=28
USDC_ASSET_ID="reum7wQmk/owgvGMWMZn/6RFPV24zIKq3W6In/WwZgg="
MINIFRONT_URL=https://app.testnet.penumbra.zone/
PENUMBRA_NODE_PD_URL=https://grpc.testnet.penumbra.zone/
27 changes: 5 additions & 22 deletions packages/query/src/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
IndexedDbInterface,
ViewServerInterface,
} from '@penumbra-zone/types';
import { computePositionId, decodeSctRoot, transactionInfo } from '@penumbra-zone/wasm';
import { computePositionId, decodeSctRoot } from '@penumbra-zone/wasm';
import {
PositionState,
PositionState_PositionStateEnum,
Expand All @@ -22,12 +22,10 @@ import { StateCommitment } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbr
import {
SpendableNoteRecord,
SwapRecord,
TransactionInfo,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb';
import { backOff } from 'exponential-backoff';

interface QueryClientProps {
fullViewingKey: string;
querier: RootQuerier;
indexedDb: IndexedDbInterface;
viewServer: ViewServerInterface;
Expand All @@ -38,15 +36,13 @@ const blankTxSource = new CommitmentSource({
});

export class BlockProcessor implements BlockProcessorInterface {
private readonly fullViewingKey: string;
private readonly querier: RootQuerier;
private readonly indexedDb: IndexedDbInterface;
private readonly viewServer: ViewServerInterface;
private readonly abortController: AbortController = new AbortController();
private syncPromise: Promise<void> | undefined;

constructor({ indexedDb, viewServer, querier, fullViewingKey }: QueryClientProps) {
this.fullViewingKey = fullViewingKey;
constructor({ indexedDb, viewServer, querier }: QueryClientProps) {
this.indexedDb = indexedDb;
this.viewServer = viewServer;
this.querier = querier;
Expand Down Expand Up @@ -235,7 +231,7 @@ export class BlockProcessor implements BlockProcessorInterface {
// pending broadcasts, and populate the transaction list.
// - calls wasm for each relevant tx
// - saves to idb
await this.saveTransactionInfos(compactBlock.height, relevantTx);
await this.saveTransactions(compactBlock.height, relevantTx);
}

// We only query Tendermint for the latest known block height once, when
Expand Down Expand Up @@ -346,22 +342,9 @@ export class BlockProcessor implements BlockProcessorInterface {
}
}

private async saveTransactionInfos(height: bigint, relevantTx: Map<TransactionId, Transaction>) {
private async saveTransactions(height: bigint, relevantTx: Map<TransactionId, Transaction>) {
for (const [id, transaction] of relevantTx) {
const { txp: perspective, txv: view } = await transactionInfo(
this.fullViewingKey,
transaction,
this.indexedDb.constants(),
);
await this.indexedDb.saveTransactionInfo(
new TransactionInfo({
height,
id,
transaction,
perspective,
view,
}),
);
await this.indexedDb.saveTransaction(id, height, transaction);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/router/src/grpc/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export interface IndexedDbMock {
getSpendableNoteByNullifier?: Mock;
getStateCommitmentTree?: Mock;
getSwapByNullifier?: Mock;
getTransactionInfo?: Mock;
getTransaction?: Mock;
iterateAssetsMetadata?: () => Partial<AsyncIterable<Mock>>;
iterateSpendableNotes?: () => Partial<AsyncIterable<Mock>>;
iterateSwaps?: () => Partial<AsyncIterable<Mock>>;
iterateTransactionInfo?: () => Partial<AsyncIterable<Mock>>;
iterateTransactions?: () => Partial<AsyncIterable<Mock>>;
iterateValidatorInfos?: () => Partial<AsyncIterable<Mock>>;
subscribe?: (table: string) => Partial<AsyncIterable<Mock>>;
getSwapByCommitment?: Mock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ describe('BroadcastTransaction request handler', () => {

mockIndexedDb = {
subscribe: (table: string) => {
if (table === 'TRANSACTION_INFO') return mockTransactionInfoSubscription;
if (table === 'TRANSACTIONS') return mockTransactionInfoSubscription;
throw new Error('Table not supported');
},
};
mockServices = {
getWalletServices: vi.fn(() => Promise.resolve({ indexedDb: mockIndexedDb })),
getWalletServices: vi.fn(() =>
Promise.resolve({ indexedDb: mockIndexedDb }),
) as MockServices['getWalletServices'],
querier: {
tendermint: mockTendermint,
},
Expand Down Expand Up @@ -81,15 +83,15 @@ describe('BroadcastTransaction request handler', () => {

test('should successfully broadcastTransaction with await detection', async () => {
const detectionHeight = 222n;
const txInfo = new TransactionInfo({
const txRecord = new TransactionInfo({
transaction: transactionData,
height: detectionHeight,
id: transactionIdData,
});

mockTendermint.broadcastTx?.mockResolvedValue(transactionIdData);
txSubNext.mockResolvedValueOnce({
value: { value: txInfo.toJson(), table: 'TRANSACTION_INFO' },
value: { value: txRecord.toJson(), table: 'TRANSACTIONS' },
});

broadcastTransactionRequest.awaitDetection = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import type { Impl } from '.';
import { servicesCtx } from '../../ctx';

import { TransactionId } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/txhash/v1/txhash_pb';
import { TransactionInfo } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb';

import { ConnectError, Code } from '@connectrpc/connect';

import { sha256Hash } from '@penumbra-zone/crypto-web';
import { uint8ArrayToHex } from '@penumbra-zone/types';
import { TransactionInfo } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb';

export const broadcastTransaction: Impl['broadcastTransaction'] = async function* (req, ctx) {
const services = ctx.values.get(servicesCtx);
Expand All @@ -17,7 +17,7 @@ export const broadcastTransaction: Impl['broadcastTransaction'] = async function
throw new ConnectError('No transaction provided in request', Code.InvalidArgument);

// start subscription early to prevent race condition
const subscription = indexedDb.subscribe('TRANSACTION_INFO');
const subscription = indexedDb.subscribe('TRANSACTIONS');

const id = new TransactionId({ inner: await sha256Hash(req.transaction.toBinary()) });

Expand Down
Loading

0 comments on commit 29d7b2a

Please sign in to comment.