@aztec/p2p 0.47.0 → 0.48.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.
Files changed (37) hide show
  1. package/dest/bootstrap/bootstrap.d.ts +2 -6
  2. package/dest/bootstrap/bootstrap.d.ts.map +1 -1
  3. package/dest/bootstrap/bootstrap.js +1 -1
  4. package/dest/client/index.js +2 -2
  5. package/dest/client/mocks.d.ts +5 -3
  6. package/dest/client/mocks.d.ts.map +1 -1
  7. package/dest/client/mocks.js +18 -9
  8. package/dest/client/p2p_client.d.ts +5 -3
  9. package/dest/client/p2p_client.d.ts.map +1 -1
  10. package/dest/client/p2p_client.js +19 -6
  11. package/dest/config.d.ts +12 -12
  12. package/dest/config.d.ts.map +1 -1
  13. package/dest/config.js +89 -25
  14. package/dest/service/libp2p_service.d.ts +2 -3
  15. package/dest/service/libp2p_service.d.ts.map +1 -1
  16. package/dest/service/libp2p_service.js +20 -32
  17. package/dest/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
  18. package/dest/tx_pool/aztec_kv_tx_pool.js +24 -6
  19. package/dest/tx_pool/instrumentation.d.ts +4 -2
  20. package/dest/tx_pool/instrumentation.d.ts.map +1 -1
  21. package/dest/tx_pool/instrumentation.js +21 -8
  22. package/dest/tx_pool/memory_tx_pool.d.ts.map +1 -1
  23. package/dest/tx_pool/memory_tx_pool.js +13 -5
  24. package/package.json +6 -6
  25. package/src/bootstrap/bootstrap.ts +2 -9
  26. package/src/client/index.ts +1 -1
  27. package/src/client/mocks.ts +20 -7
  28. package/src/client/p2p_client.ts +19 -4
  29. package/src/config.ts +116 -55
  30. package/src/service/libp2p_service.ts +21 -34
  31. package/src/tx_pool/aztec_kv_tx_pool.ts +24 -5
  32. package/src/tx_pool/instrumentation.ts +23 -8
  33. package/src/tx_pool/memory_tx_pool.ts +16 -4
  34. package/dest/service/tx_messages.d.ts +0 -32
  35. package/dest/service/tx_messages.d.ts.map +0 -1
  36. package/dest/service/tx_messages.js +0 -121
  37. package/src/service/tx_messages.ts +0 -143
package/src/config.ts CHANGED
@@ -1,4 +1,10 @@
1
- import { SemVer } from 'semver';
1
+ import {
2
+ type ConfigMappingsType,
3
+ booleanConfigHelper,
4
+ getConfigFromMappings,
5
+ numberConfigHelper,
6
+ pickConfigMappings,
7
+ } from '@aztec/foundation/config';
2
8
 
3
9
  /**
4
10
  * P2P client configuration values.
@@ -12,17 +18,17 @@ export interface P2PConfig {
12
18
  /**
13
19
  * The frequency in which to check for new L2 blocks.
14
20
  */
15
- p2pBlockCheckIntervalMS: number;
21
+ blockCheckIntervalMS: number;
16
22
 
17
23
  /**
18
24
  * The frequency in which to check for new peers.
19
25
  */
20
- p2pPeerCheckIntervalMS: number;
26
+ peerCheckIntervalMS: number;
21
27
 
22
28
  /**
23
29
  * Size of queue of L2 blocks to store.
24
30
  */
25
- p2pL2QueueSize: number;
31
+ l2QueueSize: number;
26
32
 
27
33
  /**
28
34
  * The announce address for TCP.
@@ -59,11 +65,6 @@ export interface P2PConfig {
59
65
  */
60
66
  transactionProtocol: string;
61
67
 
62
- /**
63
- * Whether to enable NAT from libp2p (ignored for bootstrap node).
64
- */
65
- enableNat?: boolean;
66
-
67
68
  /**
68
69
  * The minimum number of peers (a peer count below this will cause the node to look for more peers)
69
70
  */
@@ -79,61 +80,121 @@ export interface P2PConfig {
79
80
  */
80
81
  dataDirectory?: string;
81
82
 
82
- /**
83
- * The transaction gossiping message version.
84
- */
85
- txGossipVersion: SemVer;
86
-
87
83
  /**
88
84
  * If announceUdpAddress or announceTcpAddress are not provided, query for the IP address of the machine. Default is false.
89
85
  */
90
86
  queryForIp: boolean;
87
+
88
+ /** How many blocks have to pass after a block is proven before its txs are deleted (zero to delete immediately once proven) */
89
+ keepProvenTxsInPoolFor: number;
91
90
  }
92
91
 
92
+ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
93
+ p2pEnabled: {
94
+ env: 'P2P_ENABLED',
95
+ description: 'A flag dictating whether the P2P subsystem should be enabled.',
96
+ ...booleanConfigHelper(),
97
+ },
98
+ blockCheckIntervalMS: {
99
+ env: 'P2P_BLOCK_CHECK_INTERVAL_MS',
100
+ description: 'The frequency in which to check for new L2 blocks.',
101
+ ...numberConfigHelper(100),
102
+ },
103
+ peerCheckIntervalMS: {
104
+ env: 'P2P_PEER_CHECK_INTERVAL_MS',
105
+ description: 'The frequency in which to check for new peers.',
106
+ ...numberConfigHelper(1_000),
107
+ },
108
+ l2QueueSize: {
109
+ env: 'P2P_L2_QUEUE_SIZE',
110
+ description: 'Size of queue of L2 blocks to store.',
111
+ ...numberConfigHelper(1_000),
112
+ },
113
+ tcpListenAddress: {
114
+ env: 'TCP_LISTEN_ADDR',
115
+ defaultValue: '0.0.0.0:40400',
116
+ description: 'The listen address for TCP. Format: <IP_ADDRESS>:<PORT>.',
117
+ },
118
+ udpListenAddress: {
119
+ env: 'UDP_LISTEN_ADDR',
120
+ defaultValue: '0.0.0.0:40400',
121
+ description: 'The listen address for UDP. Format: <IP_ADDRESS>:<PORT>.',
122
+ },
123
+ tcpAnnounceAddress: {
124
+ env: 'P2P_TCP_ANNOUNCE_ADDR',
125
+ description:
126
+ 'The announce address for TCP. Format: <IP_ADDRESS>:<PORT>. Leave IP_ADDRESS blank to query for public IP.',
127
+ },
128
+ udpAnnounceAddress: {
129
+ env: 'P2P_UDP_ANNOUNCE_ADDR',
130
+ description:
131
+ 'The announce address for UDP. Format: <IP_ADDRESS>:<PORT>. Leave IP_ADDRESS blank to query for public IP.',
132
+ },
133
+ peerIdPrivateKey: {
134
+ env: 'PEER_ID_PRIVATE_KEY',
135
+ description: 'An optional peer id private key. If blank, will generate a random key.',
136
+ },
137
+ bootstrapNodes: {
138
+ env: 'BOOTSTRAP_NODES',
139
+ parseEnv: (val: string) => val.split(','),
140
+ description: 'A list of bootstrap peer ENRs to connect to. Separated by commas.',
141
+ },
142
+ transactionProtocol: {
143
+ env: 'P2P_TX_PROTOCOL',
144
+ description: 'Protocol identifier for transaction gossiping.',
145
+ defaultValue: '/aztec/0.1.0',
146
+ },
147
+ minPeerCount: {
148
+ env: 'P2P_MIN_PEERS',
149
+ description: 'The minimum number of peers to connect to.',
150
+ ...numberConfigHelper(10),
151
+ },
152
+ maxPeerCount: {
153
+ env: 'P2P_MAX_PEERS',
154
+ description: 'The maximum number of peers to connect to.',
155
+ ...numberConfigHelper(100),
156
+ },
157
+ dataDirectory: {
158
+ env: 'DATA_DIRECTORY',
159
+ description: 'Data directory for peer & tx databases. Will use temporary location if not set.',
160
+ },
161
+ queryForIp: {
162
+ env: 'P2P_QUERY_FOR_IP',
163
+ description:
164
+ 'If announceUdpAddress or announceTcpAddress are not provided, query for the IP address of the machine. Default is false.',
165
+ ...booleanConfigHelper(),
166
+ },
167
+ keepProvenTxsInPoolFor: {
168
+ env: 'P2P_TX_POOL_KEEP_PROVEN_FOR',
169
+ description:
170
+ 'How many blocks have to pass after a block is proven before its txs are deleted (zero to delete immediately once proven)',
171
+ ...numberConfigHelper(0),
172
+ },
173
+ };
174
+
93
175
  /**
94
176
  * Gets the config values for p2p client from environment variables.
95
177
  * @returns The config values for p2p client.
96
178
  */
97
179
  export function getP2PConfigEnvVars(): P2PConfig {
98
- const {
99
- P2P_ENABLED,
100
- P2P_BLOCK_CHECK_INTERVAL_MS,
101
- P2P_PEER_CHECK_INTERVAL_MS,
102
- P2P_L2_BLOCK_QUEUE_SIZE,
103
- P2P_TCP_LISTEN_ADDR,
104
- P2P_UDP_LISTEN_ADDR,
105
- P2P_TCP_ANNOUNCE_ADDR,
106
- P2P_UDP_ANNOUNCE_ADDR,
107
- PEER_ID_PRIVATE_KEY,
108
- BOOTSTRAP_NODES,
109
- P2P_NAT_ENABLED,
110
- P2P_MIN_PEERS,
111
- P2P_MAX_PEERS,
112
- DATA_DIRECTORY,
113
- TX_GOSSIP_VERSION,
114
- P2P_TX_PROTOCOL,
115
- P2P_QUERY_FOR_IP,
116
- } = process.env;
117
- // P2P listen & announce addresses passed in format: <IP_ADDRESS>:<PORT>
118
- // P2P announce multiaddrs passed in format: /ip4/<IP_ADDRESS>/<protocol>/<PORT>
119
- const envVars: P2PConfig = {
120
- tcpAnnounceAddress: P2P_TCP_ANNOUNCE_ADDR,
121
- udpAnnounceAddress: P2P_UDP_ANNOUNCE_ADDR,
122
- tcpListenAddress: P2P_TCP_LISTEN_ADDR || '0.0.0.0:40400',
123
- udpListenAddress: P2P_UDP_LISTEN_ADDR || '0.0.0.0:40400',
124
- p2pEnabled: P2P_ENABLED === 'true',
125
- p2pBlockCheckIntervalMS: P2P_BLOCK_CHECK_INTERVAL_MS ? +P2P_BLOCK_CHECK_INTERVAL_MS : 100,
126
- p2pPeerCheckIntervalMS: P2P_PEER_CHECK_INTERVAL_MS ? +P2P_PEER_CHECK_INTERVAL_MS : 1000,
127
- p2pL2QueueSize: P2P_L2_BLOCK_QUEUE_SIZE ? +P2P_L2_BLOCK_QUEUE_SIZE : 1000,
128
- peerIdPrivateKey: PEER_ID_PRIVATE_KEY,
129
- bootstrapNodes: BOOTSTRAP_NODES ? BOOTSTRAP_NODES.split(',') : [],
130
- transactionProtocol: P2P_TX_PROTOCOL ? P2P_TX_PROTOCOL : '/aztec/0.1.0',
131
- enableNat: P2P_NAT_ENABLED === 'true',
132
- minPeerCount: P2P_MIN_PEERS ? +P2P_MIN_PEERS : 10,
133
- maxPeerCount: P2P_MAX_PEERS ? +P2P_MAX_PEERS : 100,
134
- dataDirectory: DATA_DIRECTORY,
135
- txGossipVersion: TX_GOSSIP_VERSION ? new SemVer(TX_GOSSIP_VERSION) : new SemVer('0.1.0'),
136
- queryForIp: P2P_QUERY_FOR_IP === 'true',
137
- };
138
- return envVars;
180
+ return getConfigFromMappings<P2PConfig>(p2pConfigMappings);
139
181
  }
182
+
183
+ /**
184
+ * Required P2P config values for a bootstrap node.
185
+ */
186
+ export type BootnodeConfig = Pick<
187
+ P2PConfig,
188
+ 'udpAnnounceAddress' | 'peerIdPrivateKey' | 'minPeerCount' | 'maxPeerCount'
189
+ > &
190
+ Required<Pick<P2PConfig, 'udpListenAddress'>>;
191
+
192
+ const bootnodeConfigKeys: (keyof BootnodeConfig)[] = [
193
+ 'udpAnnounceAddress',
194
+ 'peerIdPrivateKey',
195
+ 'minPeerCount',
196
+ 'maxPeerCount',
197
+ 'udpListenAddress',
198
+ ];
199
+
200
+ export const bootnodeConfigMappings = pickConfigMappings(p2pConfigMappings, bootnodeConfigKeys);
@@ -1,6 +1,6 @@
1
- import { type Tx } from '@aztec/circuit-types';
2
- import { SerialQueue } from '@aztec/foundation/fifo';
1
+ import { type Gossipable, type RawGossipMessage, TopicType, TopicTypeMap, Tx } from '@aztec/circuit-types';
3
2
  import { createDebugLogger } from '@aztec/foundation/log';
3
+ import { SerialQueue } from '@aztec/foundation/queue';
4
4
  import { RunningPromise } from '@aztec/foundation/running-promise';
5
5
  import type { AztecKVStore } from '@aztec/kv-store';
6
6
 
@@ -21,7 +21,6 @@ import { convertToMultiaddr } from '../util.js';
21
21
  import { AztecDatastore } from './data_store.js';
22
22
  import { PeerManager } from './peer_manager.js';
23
23
  import type { P2PService, PeerDiscoveryService } from './service.js';
24
- import { AztecTxMessageCreator, fromTxMessage } from './tx_messages.js';
25
24
 
26
25
  export interface PubSubLibp2p extends Libp2p {
27
26
  services: {
@@ -49,7 +48,6 @@ export async function createLibP2PPeerId(privateKey?: string): Promise<PeerId> {
49
48
  */
50
49
  export class LibP2PService implements P2PService {
51
50
  private jobQueue: SerialQueue = new SerialQueue();
52
- private messageCreator: AztecTxMessageCreator;
53
51
  private peerManager: PeerManager;
54
52
  private discoveryRunningPromise?: RunningPromise;
55
53
  constructor(
@@ -59,7 +57,6 @@ export class LibP2PService implements P2PService {
59
57
  private txPool: TxPool,
60
58
  private logger = createDebugLogger('aztec:libp2p_service'),
61
59
  ) {
62
- this.messageCreator = new AztecTxMessageCreator(config.txGossipVersion);
63
60
  this.peerManager = new PeerManager(node, peerDiscoveryService, config, logger);
64
61
  }
65
62
 
@@ -89,20 +86,22 @@ export class LibP2PService implements P2PService {
89
86
  this.logger.info(`Started P2P client with Peer ID ${this.node.peerId.toString()}`);
90
87
 
91
88
  // Subscribe to standard GossipSub topics by default
92
- this.subscribeToTopic(this.messageCreator.getTopic());
89
+ for (const topic in TopicType) {
90
+ this.subscribeToTopic(TopicTypeMap[topic].p2pTopic);
91
+ }
93
92
 
94
93
  // add GossipSub listener
95
94
  this.node.services.pubsub.addEventListener('gossipsub:message', async e => {
96
95
  const { msg } = e.detail;
97
96
  this.logger.debug(`Received PUBSUB message.`);
98
97
 
99
- await this.jobQueue.put(() => this.handleNewGossipMessage(msg.topic, msg.data));
98
+ await this.jobQueue.put(() => this.handleNewGossipMessage(msg));
100
99
  });
101
100
 
102
101
  // Start running promise for peer discovery
103
102
  this.discoveryRunningPromise = new RunningPromise(() => {
104
103
  this.peerManager.discover();
105
- }, this.config.p2pPeerCheckIntervalMS);
104
+ }, this.config.peerCheckIntervalMS);
106
105
  this.discoveryRunningPromise.start();
107
106
  }
108
107
 
@@ -142,20 +141,6 @@ export class LibP2PService implements P2PService {
142
141
 
143
142
  const datastore = new AztecDatastore(store);
144
143
 
145
- // The autonat service seems quite problematic in that using it seems to cause a lot of attempts
146
- // to dial ephemeral ports. I suspect that it works better if you can get the uPNPnat service to
147
- // work as then you would have a permanent port to be dialled.
148
- // Alas, I struggled to get this to work reliably either. I find there is a race between the
149
- // service that reads our listener addresses and the uPnP service.
150
- // The result being the uPnP service can't find an address to use for the port forward.
151
- // Need to investigate further.
152
- // if (enableNat) {
153
- // services.autoNAT = autoNATService({
154
- // protocolPrefix: 'aztec',
155
- // });
156
- // services.uPnPNAT = uPnPNATService();
157
- // }
158
-
159
144
  const node = await createLibp2p({
160
145
  start: false,
161
146
  peerId,
@@ -233,14 +218,13 @@ export class LibP2PService implements P2PService {
233
218
  * @param topic - The message's topic.
234
219
  * @param data - The message data
235
220
  */
236
- private async handleNewGossipMessage(topic: string, data: Uint8Array) {
237
- if (topic !== this.messageCreator.getTopic()) {
238
- // Invalid TX Topic, ignore
239
- return;
221
+ private async handleNewGossipMessage(message: RawGossipMessage) {
222
+ if (message.topic === Tx.p2pTopic) {
223
+ const tx = Tx.fromBuffer(Buffer.from(message.data));
224
+ await this.processTxFromPeer(tx);
240
225
  }
241
226
 
242
- const tx = fromTxMessage(Buffer.from(data));
243
- await this.processTxFromPeer(tx);
227
+ return;
244
228
  }
245
229
 
246
230
  /**
@@ -248,7 +232,7 @@ export class LibP2PService implements P2PService {
248
232
  * @param tx - The transaction to propagate.
249
233
  */
250
234
  public propagateTx(tx: Tx): void {
251
- void this.jobQueue.put(() => Promise.resolve(this.sendTxToPeers(tx)));
235
+ void this.jobQueue.put(() => Promise.resolve(this.sendToPeers(tx)));
252
236
  }
253
237
 
254
238
  private async processTxFromPeer(tx: Tx): Promise<void> {
@@ -258,11 +242,14 @@ export class LibP2PService implements P2PService {
258
242
  await this.txPool.addTxs([tx]);
259
243
  }
260
244
 
261
- private async sendTxToPeers(tx: Tx) {
262
- const { data: txData } = this.messageCreator.createTxMessage(tx);
263
- this.logger.verbose(`Sending tx ${tx.getTxHash().toString()} to peers`);
264
- const recipientsNum = await this.publishToTopic(this.messageCreator.getTopic(), txData);
265
- this.logger.verbose(`Sent tx ${tx.getTxHash().toString()} to ${recipientsNum} peers`);
245
+ private async sendToPeers<T extends Gossipable>(message: T) {
246
+ const parent = message.constructor as typeof Gossipable;
247
+
248
+ const identifier = message.p2pMessageIdentifier().toString();
249
+ this.logger.verbose(`Sending tx ${identifier} to peers`);
250
+
251
+ const recipientsNum = await this.publishToTopic(parent.p2pTopic, message.toBuffer());
252
+ this.logger.verbose(`Sent tx ${identifier} to ${recipientsNum} peers`);
266
253
  }
267
254
 
268
255
  // Libp2p seems to hang sometimes if new peers are initiating connections.
@@ -42,11 +42,17 @@ export class AztecKVTxPool implements TxPool {
42
42
 
43
43
  public markAsMined(txHashes: TxHash[]): Promise<void> {
44
44
  return this.#store.transaction(() => {
45
+ let deleted = 0;
45
46
  for (const hash of txHashes) {
46
47
  const key = hash.toString();
47
48
  void this.#minedTxs.add(key);
48
- void this.#pendingTxs.delete(key);
49
+ if (this.#pendingTxs.has(key)) {
50
+ deleted++;
51
+ void this.#pendingTxs.delete(key);
52
+ }
49
53
  }
54
+ this.#metrics.recordRemovedTxs('pending', deleted);
55
+ this.#metrics.recordAddedTxs('mined', txHashes.length);
50
56
  });
51
57
  }
52
58
 
@@ -87,6 +93,7 @@ export class AztecKVTxPool implements TxPool {
87
93
  public addTxs(txs: Tx[]): Promise<void> {
88
94
  const txHashes = txs.map(tx => tx.getTxHash());
89
95
  return this.#store.transaction(() => {
96
+ let pendingCount = 0;
90
97
  for (const [i, tx] of txs.entries()) {
91
98
  const txHash = txHashes[i];
92
99
  this.#log.info(`Adding tx with id ${txHash.toString()}`, {
@@ -97,12 +104,14 @@ export class AztecKVTxPool implements TxPool {
97
104
  const key = txHash.toString();
98
105
  void this.#txs.set(key, tx.toBuffer());
99
106
  if (!this.#minedTxs.has(key)) {
107
+ pendingCount++;
100
108
  // REFACTOR: Use an lmdb conditional write to avoid race conditions with this write tx
101
109
  void this.#pendingTxs.add(key);
110
+ this.#metrics.recordTxSize(tx);
102
111
  }
103
112
  }
104
113
 
105
- this.#metrics.recordTxs(txs);
114
+ this.#metrics.recordAddedTxs('pending', pendingCount);
106
115
  });
107
116
  }
108
117
 
@@ -113,14 +122,24 @@ export class AztecKVTxPool implements TxPool {
113
122
  */
114
123
  public deleteTxs(txHashes: TxHash[]): Promise<void> {
115
124
  return this.#store.transaction(() => {
125
+ let pendingDeleted = 0;
126
+ let minedDeleted = 0;
116
127
  for (const hash of txHashes) {
117
128
  const key = hash.toString();
118
129
  void this.#txs.delete(key);
119
- void this.#pendingTxs.delete(key);
120
- void this.#minedTxs.delete(key);
130
+ if (this.#pendingTxs.has(key)) {
131
+ pendingDeleted++;
132
+ void this.#pendingTxs.delete(key);
133
+ }
134
+
135
+ if (this.#minedTxs.has(key)) {
136
+ minedDeleted++;
137
+ void this.#minedTxs.delete(key);
138
+ }
121
139
  }
122
140
 
123
- this.#metrics.removeTxs(txHashes.length);
141
+ this.#metrics.recordRemovedTxs('pending', pendingDeleted);
142
+ this.#metrics.recordRemovedTxs('mined', minedDeleted);
124
143
  });
125
144
  }
126
145
 
@@ -1,5 +1,7 @@
1
1
  import { type Tx } from '@aztec/circuit-types';
2
- import { type Histogram, Metrics, type TelemetryClient, type UpDownCounter } from '@aztec/telemetry-client';
2
+ import { Attributes, type Histogram, Metrics, type TelemetryClient, type UpDownCounter } from '@aztec/telemetry-client';
3
+
4
+ export type TxStatus = 'pending' | 'mined';
3
5
 
4
6
  /**
5
7
  * Instrumentation class for the TxPool.
@@ -33,26 +35,39 @@ export class TxPoolInstrumentation {
33
35
  });
34
36
  }
35
37
 
38
+ public recordTxSize(tx: Tx) {
39
+ this.txSize.record(tx.getSize());
40
+ }
41
+
36
42
  /**
37
43
  * Updates the metrics with the new transactions.
38
44
  * @param txs - The transactions to record
39
45
  */
40
- public recordTxs(txs: Tx[]) {
41
- for (const tx of txs) {
42
- this.txSize.record(tx.getSize());
46
+ public recordAddedTxs(status: string, count = 1) {
47
+ if (count < 0) {
48
+ throw new Error('Count must be positive');
43
49
  }
44
-
45
- this.txInMempool.add(txs.length);
50
+ if (count === 0) {
51
+ return;
52
+ }
53
+ this.txInMempool.add(count, {
54
+ [Attributes.STATUS]: status,
55
+ });
46
56
  }
47
57
 
48
58
  /**
49
59
  * Updates the metrics by removing transactions from the mempool.
50
60
  * @param count - The number of transactions to remove from the mempool
51
61
  */
52
- public removeTxs(count = 1) {
62
+ public recordRemovedTxs(status: string, count = 1) {
53
63
  if (count < 0) {
54
64
  throw new Error('Count must be positive');
55
65
  }
56
- this.txInMempool.add(-1 * count);
66
+ if (count === 0) {
67
+ return;
68
+ }
69
+ this.txInMempool.add(-1 * count, {
70
+ [Attributes.STATUS]: status,
71
+ });
57
72
  }
58
73
  }
@@ -36,6 +36,8 @@ export class InMemoryTxPool implements TxPool {
36
36
  this.minedTxs.add(key);
37
37
  this.pendingTxs.delete(key);
38
38
  }
39
+ this.metrics.recordRemovedTxs('pending', txHashes.length);
40
+ this.metrics.recordAddedTxs('mined', txHashes.length);
39
41
  return Promise.resolve();
40
42
  }
41
43
 
@@ -74,7 +76,7 @@ export class InMemoryTxPool implements TxPool {
74
76
  * @returns Empty promise.
75
77
  */
76
78
  public addTxs(txs: Tx[]): Promise<void> {
77
- this.metrics.recordTxs(txs);
79
+ let pending = 0;
78
80
  for (const tx of txs) {
79
81
  const txHash = tx.getTxHash();
80
82
  this.log.debug(`Adding tx with id ${txHash.toString()}`, {
@@ -85,9 +87,13 @@ export class InMemoryTxPool implements TxPool {
85
87
  const key = txHash.toBigInt();
86
88
  this.txs.set(key, tx);
87
89
  if (!this.minedTxs.has(key)) {
90
+ pending++;
91
+ this.metrics.recordTxSize(tx);
88
92
  this.pendingTxs.add(key);
89
93
  }
90
94
  }
95
+
96
+ this.metrics.recordAddedTxs('pending', pending);
91
97
  return Promise.resolve();
92
98
  }
93
99
 
@@ -97,13 +103,19 @@ export class InMemoryTxPool implements TxPool {
97
103
  * @returns The number of transactions that was deleted from the pool.
98
104
  */
99
105
  public deleteTxs(txHashes: TxHash[]): Promise<void> {
100
- this.metrics.removeTxs(txHashes.length);
106
+ let deletedMined = 0;
107
+ let deletedPending = 0;
108
+
101
109
  for (const txHash of txHashes) {
102
110
  const key = txHash.toBigInt();
103
111
  this.txs.delete(key);
104
- this.pendingTxs.delete(key);
105
- this.minedTxs.delete(key);
112
+ deletedPending += this.pendingTxs.delete(key) ? 1 : 0;
113
+ deletedMined += this.minedTxs.delete(key) ? 1 : 0;
106
114
  }
115
+
116
+ this.metrics.recordRemovedTxs('pending', deletedPending);
117
+ this.metrics.recordRemovedTxs('mined', deletedMined);
118
+
107
119
  return Promise.resolve();
108
120
  }
109
121
 
@@ -1,32 +0,0 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- import { Tx } from '@aztec/circuit-types';
3
- import { type SemVer } from 'semver';
4
- export declare const TX_MESSAGE_TOPIC = "";
5
- export declare class AztecTxMessageCreator {
6
- private readonly topic;
7
- constructor(version: SemVer);
8
- createTxMessage(tx: Tx): {
9
- topic: string;
10
- data: Buffer;
11
- };
12
- getTopic(): string;
13
- }
14
- /**
15
- * Decode a POOLED_TRANSACTIONS message into the original transaction objects.
16
- * @param message - The binary message to be decoded.
17
- * @returns - The array of transactions originally encoded into the message.
18
- */
19
- export declare function decodeTransactionsMessage(message: Buffer): Tx[];
20
- /**
21
- * Creates a tx 'message' for sending to a peer.
22
- * @param tx - The transaction to convert to a message.
23
- * @returns - The message.
24
- */
25
- export declare function toTxMessage(tx: Tx): Buffer;
26
- /**
27
- * Reproduces a transaction from a transaction 'message'
28
- * @param buffer - The message buffer to convert to a tx.
29
- * @returns - The reproduced transaction.
30
- */
31
- export declare function fromTxMessage(buffer: Buffer): Tx;
32
- //# sourceMappingURL=tx_messages.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tx_messages.d.ts","sourceRoot":"","sources":["../../src/service/tx_messages.ts"],"names":[],"mappings":";AAAA,OAAO,EAA4C,EAAE,EAAuB,MAAM,sBAAsB,CAAC;AAIzG,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAEnC,qBAAa,qBAAqB;IAChC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;gBACnB,OAAO,EAAE,MAAM;IAI3B,eAAe,CAAC,EAAE,EAAE,EAAE;;;;IAMtB,QAAQ;CAGT;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,MAAM,QAWxD;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,EAAE,GAAG,MAAM,CA8B1C;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,EAAE,CAyDhD"}