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

refactor: replacing use of L2Tx with TxEffect #4858

Closed
Closed
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
11 changes: 8 additions & 3 deletions yarn-project/archiver/src/archiver/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
L2BlockL2Logs,
L2BlockSource,
L2LogsSource,
L2Tx,
LogFilter,
LogType,
TxEffect,
TxHash,
TxReceipt,
UnencryptedL2Log,
} from '@aztec/circuit-types';
import {
Expand Down Expand Up @@ -419,8 +420,12 @@ export class Archiver implements ArchiveSource {
return blocks.length === 0 ? undefined : blocks[0];
}

public getL2Tx(txHash: TxHash): Promise<L2Tx | undefined> {
return this.store.getL2Tx(txHash);
public getTxEffect(txHash: TxHash): Promise<TxEffect | undefined> {
return this.store.getTxEffect(txHash);
}

public getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
return this.store.getSettledTxReceipt(txHash);
}

/**
Expand Down
18 changes: 13 additions & 5 deletions yarn-project/archiver/src/archiver/archiver_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
L1ToL2Message,
L2Block,
L2BlockL2Logs,
L2Tx,
LogFilter,
LogType,
TxEffect,
TxHash,
TxReceipt,
} from '@aztec/circuit-types';
import { Fr } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
Expand Down Expand Up @@ -47,11 +48,18 @@ export interface ArchiverDataStore {
getBlocks(from: number, limit: number): Promise<L2Block[]>;

/**
* Gets an l2 tx.
* @param txHash - The txHash of the l2 tx.
* @returns The requested L2 tx.
* Gets a tx effect.
* @param txHash - The txHash of the tx corresponding to the tx effect.
* @returns The requested tx effect (or undefined if not found).
*/
getL2Tx(txHash: TxHash): Promise<L2Tx | undefined>;
getTxEffect(txHash: TxHash): Promise<TxEffect | undefined>;

/**
* Gets a receipt of a settled tx.
* @param txHash - The hash of a tx we try to get the receipt for.
* @returns The requested tx receipt (or undefined if not found).
*/
getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined>;

/**
* Append new logs to the store's list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch
});
});

describe('getL2Tx', () => {
describe('getTxEffect', () => {
beforeEach(async () => {
await Promise.all(
blocks.map(block => store.addLogs(block.body.encryptedLogs, block.body.unencryptedLogs, block.number)),
Expand All @@ -167,12 +167,12 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch
() => blocks[1].getTx(0),
])('retrieves a previously stored transaction', async getExpectedTx => {
const expectedTx = getExpectedTx();
const actualTx = await store.getL2Tx(expectedTx.txHash);
const actualTx = await store.getTxEffect(expectedTx.txHash);
expect(actualTx).toEqual(expectedTx);
});

it('returns undefined if tx is not found', async () => {
await expect(store.getL2Tx(new TxHash(Fr.random().toBuffer()))).resolves.toBeUndefined();
await expect(store.getTxEffect(new TxHash(Fr.random().toBuffer()))).resolves.toBeUndefined();
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { INITIAL_L2_BLOCK_NUM, L2Block, L2Tx, TxHash } from '@aztec/circuit-types';
import { INITIAL_L2_BLOCK_NUM, L2Block, TxEffect, TxHash, TxReceipt, TxStatus } from '@aztec/circuit-types';
import { AztecAddress } from '@aztec/circuits.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { AztecKVStore, AztecMap, Range } from '@aztec/kv-store';
Expand Down Expand Up @@ -94,12 +94,12 @@ export class BlockStore {
}

/**
* Gets an l2 tx.
* @param txHash - The txHash of the l2 tx.
* @returns The requested L2 tx.
* Gets a tx effect.
* @param txHash - The txHash of the tx corresponding to the tx effect.
* @returns The requested tx effect (or undefined if not found).
*/
getL2Tx(txHash: TxHash): L2Tx | undefined {
const [blockNumber, txIndex] = this.getL2TxLocation(txHash) ?? [];
getTxEffect(txHash: TxHash): TxEffect | undefined {
const [blockNumber, txIndex] = this.getTxLocation(txHash) ?? [];
if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
return undefined;
}
Expand All @@ -109,11 +109,26 @@ export class BlockStore {
}

/**
* Looks up which block included the requested L2 tx.
* @param txHash - The txHash of the l2 tx.
* Gets a receipt of a settled tx.
* @param txHash - The hash of a tx we try to get the receipt for.
* @returns The requested tx receipt (or undefined if not found).
*/
getSettledTxReceipt(txHash: TxHash): TxReceipt | undefined {
const [blockNumber, txIndex] = this.getTxLocation(txHash) ?? [];
if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
return undefined;
}

const block = this.getBlock(blockNumber)!;
return new TxReceipt(txHash, TxStatus.MINED, '', block.hash().toBuffer(), block.number);
}

/**
* Looks up which block included the requested tx effect.
* @param txHash - The txHash of the tx.
* @returns The block number and index of the tx.
*/
getL2TxLocation(txHash: TxHash): [blockNumber: number, txIndex: number] | undefined {
getTxLocation(txHash: TxHash): [blockNumber: number, txIndex: number] | undefined {
return this.#txIndex.get(txHash.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
L1ToL2Message,
L2Block,
L2BlockL2Logs,
L2Tx,
LogFilter,
LogType,
TxEffect,
TxHash,
TxReceipt,
} from '@aztec/circuit-types';
import { Fr } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
Expand Down Expand Up @@ -93,12 +94,21 @@ export class KVArchiverDataStore implements ArchiverDataStore {
}

/**
* Gets an l2 tx.
* @param txHash - The txHash of the l2 tx.
* @returns The requested L2 tx.
* Gets a tx effect.
* @param txHash - The txHash of the tx corresponding to the tx effect.
* @returns The requested tx effect (or undefined if not found).
*/
getL2Tx(txHash: TxHash): Promise<L2Tx | undefined> {
return Promise.resolve(this.#blockStore.getL2Tx(txHash));
getTxEffect(txHash: TxHash): Promise<TxEffect | undefined> {
return Promise.resolve(this.#blockStore.getTxEffect(txHash));
}

/**
* Gets a receipt of a settled tx.
* @param txHash - The hash of a tx we try to get the receipt for.
* @returns The requested tx receipt (or undefined if not found).
*/
getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
return Promise.resolve(this.#blockStore.getSettledTxReceipt(txHash));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class LogStore {
throw new Error('Missing txHash');
}

const [blockNumber, txIndex] = this.blockStore.getL2TxLocation(filter.txHash) ?? [];
const [blockNumber, txIndex] = this.blockStore.getTxLocation(filter.txHash) ?? [];
if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
return { logs: [], maxLogsHit: false };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import {
L2Block,
L2BlockContext,
L2BlockL2Logs,
L2Tx,
LogFilter,
LogId,
LogType,
TxEffect,
TxHash,
TxReceipt,
TxStatus,
UnencryptedL2Log,
} from '@aztec/circuit-types';
import { Fr, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/circuits.js';
Expand All @@ -32,9 +34,9 @@ export class MemoryArchiverStore implements ArchiverDataStore {
private l2BlockContexts: L2BlockContext[] = [];

/**
* An array containing all the L2 Txs in the L2 blocks that have been fetched so far.
* An array containing all the the effects in the L2 blocks that have been fetched so far.
*/
private l2Txs: L2Tx[] = [];
private txEffects: TxEffect[] = [];

/**
* An array containing all the encrypted logs that have been fetched so far.
Expand Down Expand Up @@ -114,7 +116,7 @@ export class MemoryArchiverStore implements ArchiverDataStore {
*/
public addBlocks(blocks: L2Block[]): Promise<boolean> {
this.l2BlockContexts.push(...blocks.map(block => new L2BlockContext(block)));
this.l2Txs.push(...blocks.flatMap(b => b.getTxs()));
this.txEffects.push(...blocks.flatMap(b => b.getTxs()));
return Promise.resolve(true);
}

Expand Down Expand Up @@ -232,13 +234,31 @@ export class MemoryArchiverStore implements ArchiverDataStore {
}

/**
* Gets an l2 tx.
* @param txHash - The txHash of the l2 tx.
* @returns The requested L2 tx.
* Gets a tx effect.
* @param txHash - The txHash of the tx effect.
* @returns The requested tx effect.
*/
public getL2Tx(txHash: TxHash): Promise<L2Tx | undefined> {
const l2Tx = this.l2Txs.find(tx => tx.txHash.equals(txHash));
return Promise.resolve(l2Tx);
public getTxEffect(txHash: TxHash): Promise<TxEffect | undefined> {
const txEffect = this.txEffects.find(tx => tx.txHash.equals(txHash));
return Promise.resolve(txEffect);
}

/**
* Gets a receipt of a settled tx.
* @param txHash - The hash of a tx we try to get the receipt for.
* @returns The requested tx receipt (or undefined if not found).
*/
public getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
for (const blockContext of this.l2BlockContexts) {
for (const currentTxHash of blockContext.getTxHashes()) {
if (currentTxHash.equals(txHash)) {
return Promise.resolve(
new TxReceipt(txHash, TxStatus.MINED, '', blockContext.block.hash().toBuffer(), blockContext.block.number),
);
}
}
}
return Promise.resolve(undefined);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion yarn-project/archiver/src/rpc/archiver_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
L1ToL2Message,
L2Block,
L2BlockL2Logs,
TxEffect,
TxReceipt,
} from '@aztec/circuit-types';
import { EthAddress, Fr } from '@aztec/circuits.js';
import { JsonRpcServer } from '@aztec/foundation/json-rpc/server';
Expand All @@ -30,8 +32,9 @@ export function createArchiverRpcServer(archiverService: Archiver): JsonRpcServe
L1ToL2Message,
L2Block,
L2BlockL2Logs,
TxEffect,
},
{},
{ TxReceipt },
['start', 'stop'],
);
}
7 changes: 4 additions & 3 deletions yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {
L1ToL2MessageAndIndex,
L2Block,
L2BlockL2Logs,
L2Tx,
LogId,
SiblingPath,
Tx,
TxEffect,
TxHash,
TxReceipt,
} from '@aztec/circuit-types';
import { FunctionSelector, Header } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
Expand All @@ -36,13 +37,13 @@ export function createAztecNodeRpcServer(node: AztecNode) {
FunctionSelector,
Header,
L2Block,
L2Tx,
TxEffect,
LogId,
TxHash,
SiblingPath,
L1ToL2MessageAndIndex,
},
{ Tx, L2BlockL2Logs },
{ Tx, TxReceipt, L2BlockL2Logs },
// disable methods not part of the AztecNode interface
['start', 'stop'],
);
Expand Down
27 changes: 24 additions & 3 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
L2BlockL2Logs,
L2BlockSource,
L2LogsSource,
L2Tx,
LogFilter,
LogType,
MerkleTreeId,
Expand All @@ -21,7 +20,10 @@ import {
SequencerConfig,
SiblingPath,
Tx,
TxEffect,
TxHash,
TxReceipt,
TxStatus,
} from '@aztec/circuit-types';
import {
ARCHIVE_HEIGHT,
Expand Down Expand Up @@ -285,8 +287,27 @@ export class AztecNodeService implements AztecNode {
await this.p2pClient!.sendTx(tx);
}

public getTx(txHash: TxHash): Promise<L2Tx | undefined> {
return this.blockSource.getL2Tx(txHash);
public async getTxReceipt(txHash: TxHash): Promise<TxReceipt> {
let txReceipt = new TxReceipt(txHash, TxStatus.DROPPED, 'Tx dropped by P2P node.');

// We first check if the tx is in pending (instead of first checking if it is mined) because if we first check
// for mined and then for pending there could be a race condition where the tx is mined between the two checks
// and we would incorrectly return a TxReceipt with status DROPPED
const pendingTx = await this.getPendingTxByHash(txHash);
if (pendingTx) {
txReceipt = new TxReceipt(txHash, TxStatus.PENDING, '');
}

const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
if (settledTxReceipt) {
txReceipt = settledTxReceipt;
}

return txReceipt;
}

public getTxEffect(txHash: TxHash): Promise<TxEffect | undefined> {
return this.blockSource.getTxEffect(txHash);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions yarn-project/aztec.js/src/contract/sent_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export class SentTx {
}
if (opts?.debug) {
const txHash = await this.getTxHash();
const tx = (await this.pxe.getTx(txHash))!;
const tx = (await this.pxe.getTxEffect(txHash))!;
const visibleNotes = await this.pxe.getNotes({ txHash });
receipt.debugInfo = {
newNoteHashes: tx.newNoteHashes,
newNullifiers: tx.newNullifiers,
newPublicDataWrites: tx.newPublicDataWrites,
newL2ToL1Msgs: tx.newL2ToL1Msgs,
newContracts: tx.newContracts,
newContractData: tx.newContractData,
newNoteHashes: tx.newNoteHashes.filter(n => !n.isZero()),
newNullifiers: tx.newNullifiers.filter(n => !n.isZero()),
newPublicDataWrites: tx.newPublicDataWrites.filter(p => !p.isEmpty()),
newL2ToL1Msgs: tx.newL2ToL1Msgs.filter(l => !l.isZero()),
newContracts: tx.contractLeaves.filter(c => !c.isZero()),
newContractData: tx.contractData.filter(c => !c.isEmpty()),
visibleNotes,
};
}
Expand Down
Loading
Loading