@aztec/p2p 0.46.7 → 0.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/client/index.d.ts +1 -1
- package/dest/client/index.d.ts.map +1 -1
- package/dest/client/index.js +2 -2
- package/dest/client/p2p_client.d.ts +49 -21
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +111 -36
- package/dest/tx_pool/aztec_kv_tx_pool.d.ts +4 -6
- package/dest/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
- package/dest/tx_pool/aztec_kv_tx_pool.js +47 -16
- package/dest/tx_pool/memory_tx_pool.d.ts +6 -6
- package/dest/tx_pool/memory_tx_pool.d.ts.map +1 -1
- package/dest/tx_pool/memory_tx_pool.js +36 -11
- package/dest/tx_pool/tx_pool.d.ts +19 -4
- package/dest/tx_pool/tx_pool.d.ts.map +1 -1
- package/dest/tx_pool/tx_pool_test_suite.d.ts.map +1 -1
- package/dest/tx_pool/tx_pool_test_suite.js +16 -4
- package/package.json +6 -6
- package/src/client/index.ts +1 -1
- package/src/client/p2p_client.ts +134 -48
- package/src/tx_pool/aztec_kv_tx_pool.ts +49 -15
- package/src/tx_pool/memory_tx_pool.ts +42 -11
- package/src/tx_pool/tx_pool.ts +22 -4
- package/src/tx_pool/tx_pool_test_suite.ts +18 -3
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Tx, TxHash } from '@aztec/circuit-types';
|
|
2
2
|
import { type TxAddedToPoolStats } from '@aztec/circuit-types/stats';
|
|
3
3
|
import { type Logger, createDebugLogger } from '@aztec/foundation/log';
|
|
4
|
-
import { type AztecKVStore, type AztecMap } from '@aztec/kv-store';
|
|
4
|
+
import { type AztecKVStore, type AztecMap, type AztecSet } from '@aztec/kv-store';
|
|
5
5
|
import { type TelemetryClient } from '@aztec/telemetry-client';
|
|
6
6
|
|
|
7
7
|
import { TxPoolInstrumentation } from './instrumentation.js';
|
|
@@ -13,11 +13,14 @@ import { type TxPool } from './tx_pool.js';
|
|
|
13
13
|
export class AztecKVTxPool implements TxPool {
|
|
14
14
|
#store: AztecKVStore;
|
|
15
15
|
|
|
16
|
-
/**
|
|
17
|
-
* Our tx pool, stored as a Map in-memory, with K: tx hash and V: the transaction.
|
|
18
|
-
*/
|
|
16
|
+
/** Our tx pool, stored as a Map, with K: tx hash and V: the transaction. */
|
|
19
17
|
#txs: AztecMap<string, Buffer>;
|
|
20
18
|
|
|
19
|
+
/** Index for pending txs. */
|
|
20
|
+
#pendingTxs: AztecSet<string>;
|
|
21
|
+
/** Index for mined txs. */
|
|
22
|
+
#minedTxs: AztecSet<string>;
|
|
23
|
+
|
|
21
24
|
#log: Logger;
|
|
22
25
|
|
|
23
26
|
#metrics: TxPoolInstrumentation;
|
|
@@ -29,11 +32,43 @@ export class AztecKVTxPool implements TxPool {
|
|
|
29
32
|
*/
|
|
30
33
|
constructor(store: AztecKVStore, telemetry: TelemetryClient, log = createDebugLogger('aztec:tx_pool')) {
|
|
31
34
|
this.#txs = store.openMap('txs');
|
|
35
|
+
this.#minedTxs = store.openSet('minedTxs');
|
|
36
|
+
this.#pendingTxs = store.openSet('pendingTxs');
|
|
37
|
+
|
|
32
38
|
this.#store = store;
|
|
33
39
|
this.#log = log;
|
|
34
40
|
this.#metrics = new TxPoolInstrumentation(telemetry, 'AztecKVTxPool');
|
|
35
41
|
}
|
|
36
42
|
|
|
43
|
+
public markAsMined(txHashes: TxHash[]): Promise<void> {
|
|
44
|
+
return this.#store.transaction(() => {
|
|
45
|
+
for (const hash of txHashes) {
|
|
46
|
+
const key = hash.toString();
|
|
47
|
+
void this.#minedTxs.add(key);
|
|
48
|
+
void this.#pendingTxs.delete(key);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public getPendingTxHashes(): TxHash[] {
|
|
54
|
+
return Array.from(this.#pendingTxs.entries()).map(x => TxHash.fromString(x));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public getMinedTxHashes(): TxHash[] {
|
|
58
|
+
return Array.from(this.#minedTxs.entries()).map(x => TxHash.fromString(x));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public getTxStatus(txHash: TxHash): 'pending' | 'mined' | undefined {
|
|
62
|
+
const key = txHash.toString();
|
|
63
|
+
if (this.#pendingTxs.has(key)) {
|
|
64
|
+
return 'pending';
|
|
65
|
+
} else if (this.#minedTxs.has(key)) {
|
|
66
|
+
return 'mined';
|
|
67
|
+
} else {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
37
72
|
/**
|
|
38
73
|
* Checks if a transaction exists in the pool and returns it.
|
|
39
74
|
* @param txHash - The generated tx hash.
|
|
@@ -59,7 +94,12 @@ export class AztecKVTxPool implements TxPool {
|
|
|
59
94
|
...tx.getStats(),
|
|
60
95
|
} satisfies TxAddedToPoolStats);
|
|
61
96
|
|
|
62
|
-
|
|
97
|
+
const key = txHash.toString();
|
|
98
|
+
void this.#txs.set(key, tx.toBuffer());
|
|
99
|
+
if (!this.#minedTxs.has(key)) {
|
|
100
|
+
// REFACTOR: Use an lmdb conditional write to avoid race conditions with this write tx
|
|
101
|
+
void this.#pendingTxs.add(key);
|
|
102
|
+
}
|
|
63
103
|
}
|
|
64
104
|
|
|
65
105
|
this.#metrics.recordTxs(txs);
|
|
@@ -74,7 +114,10 @@ export class AztecKVTxPool implements TxPool {
|
|
|
74
114
|
public deleteTxs(txHashes: TxHash[]): Promise<void> {
|
|
75
115
|
return this.#store.transaction(() => {
|
|
76
116
|
for (const hash of txHashes) {
|
|
77
|
-
|
|
117
|
+
const key = hash.toString();
|
|
118
|
+
void this.#txs.delete(key);
|
|
119
|
+
void this.#pendingTxs.delete(key);
|
|
120
|
+
void this.#minedTxs.delete(key);
|
|
78
121
|
}
|
|
79
122
|
|
|
80
123
|
this.#metrics.removeTxs(txHashes.length);
|
|
@@ -96,13 +139,4 @@ export class AztecKVTxPool implements TxPool {
|
|
|
96
139
|
public getAllTxHashes(): TxHash[] {
|
|
97
140
|
return Array.from(this.#txs.keys()).map(x => TxHash.fromString(x));
|
|
98
141
|
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Returns a boolean indicating if the transaction is present in the pool.
|
|
102
|
-
* @param txHash - The hash of the transaction to be queried.
|
|
103
|
-
* @returns True if the transaction present, false otherwise.
|
|
104
|
-
*/
|
|
105
|
-
public hasTx(txHash: TxHash): boolean {
|
|
106
|
-
return this.#txs.has(txHash.toString());
|
|
107
|
-
}
|
|
108
142
|
}
|
|
@@ -14,6 +14,8 @@ export class InMemoryTxPool implements TxPool {
|
|
|
14
14
|
* Our tx pool, stored as a Map in-memory, with K: tx hash and V: the transaction.
|
|
15
15
|
*/
|
|
16
16
|
private txs: Map<bigint, Tx>;
|
|
17
|
+
private minedTxs: Set<bigint>;
|
|
18
|
+
private pendingTxs: Set<bigint>;
|
|
17
19
|
|
|
18
20
|
private metrics: TxPoolInstrumentation;
|
|
19
21
|
|
|
@@ -23,9 +25,39 @@ export class InMemoryTxPool implements TxPool {
|
|
|
23
25
|
*/
|
|
24
26
|
constructor(telemetry: TelemetryClient, private log = createDebugLogger('aztec:tx_pool')) {
|
|
25
27
|
this.txs = new Map<bigint, Tx>();
|
|
28
|
+
this.minedTxs = new Set();
|
|
29
|
+
this.pendingTxs = new Set();
|
|
26
30
|
this.metrics = new TxPoolInstrumentation(telemetry, 'InMemoryTxPool');
|
|
27
31
|
}
|
|
28
32
|
|
|
33
|
+
public markAsMined(txHashes: TxHash[]): Promise<void> {
|
|
34
|
+
const keys = txHashes.map(x => x.toBigInt());
|
|
35
|
+
for (const key of keys) {
|
|
36
|
+
this.minedTxs.add(key);
|
|
37
|
+
this.pendingTxs.delete(key);
|
|
38
|
+
}
|
|
39
|
+
return Promise.resolve();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public getPendingTxHashes(): TxHash[] {
|
|
43
|
+
return Array.from(this.pendingTxs).map(x => TxHash.fromBigInt(x));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public getMinedTxHashes(): TxHash[] {
|
|
47
|
+
return Array.from(this.minedTxs).map(x => TxHash.fromBigInt(x));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public getTxStatus(txHash: TxHash): 'pending' | 'mined' | undefined {
|
|
51
|
+
const key = txHash.toBigInt();
|
|
52
|
+
if (this.pendingTxs.has(key)) {
|
|
53
|
+
return 'pending';
|
|
54
|
+
}
|
|
55
|
+
if (this.minedTxs.has(key)) {
|
|
56
|
+
return 'mined';
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
29
61
|
/**
|
|
30
62
|
* Checks if a transaction exists in the pool and returns it.
|
|
31
63
|
* @param txHash - The generated tx hash.
|
|
@@ -49,7 +81,12 @@ export class InMemoryTxPool implements TxPool {
|
|
|
49
81
|
eventName: 'tx-added-to-pool',
|
|
50
82
|
...tx.getStats(),
|
|
51
83
|
} satisfies TxAddedToPoolStats);
|
|
52
|
-
|
|
84
|
+
|
|
85
|
+
const key = txHash.toBigInt();
|
|
86
|
+
this.txs.set(key, tx);
|
|
87
|
+
if (!this.minedTxs.has(key)) {
|
|
88
|
+
this.pendingTxs.add(key);
|
|
89
|
+
}
|
|
53
90
|
}
|
|
54
91
|
return Promise.resolve();
|
|
55
92
|
}
|
|
@@ -62,7 +99,10 @@ export class InMemoryTxPool implements TxPool {
|
|
|
62
99
|
public deleteTxs(txHashes: TxHash[]): Promise<void> {
|
|
63
100
|
this.metrics.removeTxs(txHashes.length);
|
|
64
101
|
for (const txHash of txHashes) {
|
|
65
|
-
|
|
102
|
+
const key = txHash.toBigInt();
|
|
103
|
+
this.txs.delete(key);
|
|
104
|
+
this.pendingTxs.delete(key);
|
|
105
|
+
this.minedTxs.delete(key);
|
|
66
106
|
}
|
|
67
107
|
return Promise.resolve();
|
|
68
108
|
}
|
|
@@ -82,13 +122,4 @@ export class InMemoryTxPool implements TxPool {
|
|
|
82
122
|
public getAllTxHashes(): TxHash[] {
|
|
83
123
|
return Array.from(this.txs.keys()).map(x => TxHash.fromBigInt(x));
|
|
84
124
|
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Returns a boolean indicating if the transaction is present in the pool.
|
|
88
|
-
* @param txHash - The hash of the transaction to be queried.
|
|
89
|
-
* @returns True if the transaction present, false otherwise.
|
|
90
|
-
*/
|
|
91
|
-
public hasTx(txHash: TxHash): boolean {
|
|
92
|
-
return this.txs.has(txHash.toBigInt());
|
|
93
|
-
}
|
|
94
125
|
}
|
package/src/tx_pool/tx_pool.ts
CHANGED
|
@@ -17,6 +17,12 @@ export interface TxPool {
|
|
|
17
17
|
*/
|
|
18
18
|
getTxByHash(txHash: TxHash): Tx | undefined;
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Marks the set of txs as mined, as opposed to pending.
|
|
22
|
+
* @param txHashes - Hashes of the txs to flag as mined.
|
|
23
|
+
*/
|
|
24
|
+
markAsMined(txHashes: TxHash[]): Promise<void>;
|
|
25
|
+
|
|
20
26
|
/**
|
|
21
27
|
* Deletes transactions from the pool. Tx hashes that are not present are ignored.
|
|
22
28
|
* @param txHashes - An array of tx hashes to be removed from the tx pool.
|
|
@@ -36,9 +42,21 @@ export interface TxPool {
|
|
|
36
42
|
getAllTxHashes(): TxHash[];
|
|
37
43
|
|
|
38
44
|
/**
|
|
39
|
-
*
|
|
40
|
-
* @
|
|
41
|
-
|
|
45
|
+
* Gets the hashes of pending transactions currently in the tx pool.
|
|
46
|
+
* @returns An array of pending transaction hashes found in the tx pool.
|
|
47
|
+
*/
|
|
48
|
+
getPendingTxHashes(): TxHash[];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Gets the hashes of mined transactions currently in the tx pool.
|
|
52
|
+
* @returns An array of mined transaction hashes found in the tx pool.
|
|
53
|
+
*/
|
|
54
|
+
getMinedTxHashes(): TxHash[];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns whether the given tx hash is flagged as pending or mined.
|
|
58
|
+
* @param txHash - Hash of the tx to query.
|
|
59
|
+
* @returns Pending or mined depending on its status, or undefined if not found.
|
|
42
60
|
*/
|
|
43
|
-
|
|
61
|
+
getTxStatus(txHash: TxHash): 'pending' | 'mined' | undefined;
|
|
44
62
|
}
|
|
@@ -13,12 +13,14 @@ export function describeTxPool(getTxPool: () => TxPool) {
|
|
|
13
13
|
pool = getTxPool();
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
-
it('Adds txs to the pool', async () => {
|
|
16
|
+
it('Adds txs to the pool as pending', async () => {
|
|
17
17
|
const tx1 = mockTx();
|
|
18
18
|
|
|
19
19
|
await pool.addTxs([tx1]);
|
|
20
20
|
const poolTx = pool.getTxByHash(tx1.getTxHash());
|
|
21
21
|
expect(poolTx!.getTxHash()).toEqual(tx1.getTxHash());
|
|
22
|
+
expect(pool.getTxStatus(tx1.getTxHash())).toEqual('pending');
|
|
23
|
+
expect(pool.getPendingTxHashes()).toEqual([tx1.getTxHash()]);
|
|
22
24
|
});
|
|
23
25
|
|
|
24
26
|
it('Removes txs from the pool', async () => {
|
|
@@ -27,8 +29,21 @@ export function describeTxPool(getTxPool: () => TxPool) {
|
|
|
27
29
|
await pool.addTxs([tx1]);
|
|
28
30
|
await pool.deleteTxs([tx1.getTxHash()]);
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
expect(
|
|
32
|
+
expect(pool.getTxByHash(tx1.getTxHash())).toBeFalsy();
|
|
33
|
+
expect(pool.getTxStatus(tx1.getTxHash())).toBeUndefined();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('Marks txs as mined', async () => {
|
|
37
|
+
const tx1 = mockTx(1);
|
|
38
|
+
const tx2 = mockTx(2);
|
|
39
|
+
|
|
40
|
+
await pool.addTxs([tx1, tx2]);
|
|
41
|
+
await pool.markAsMined([tx1.getTxHash()]);
|
|
42
|
+
|
|
43
|
+
expect(pool.getTxByHash(tx1.getTxHash())).toEqual(tx1);
|
|
44
|
+
expect(pool.getTxStatus(tx1.getTxHash())).toEqual('mined');
|
|
45
|
+
expect(pool.getMinedTxHashes()).toEqual([tx1.getTxHash()]);
|
|
46
|
+
expect(pool.getPendingTxHashes()).toEqual([tx2.getTxHash()]);
|
|
32
47
|
});
|
|
33
48
|
|
|
34
49
|
it('Returns all transactions in the pool', async () => {
|