@aztec/p2p 0.49.2 → 0.50.1
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/attestation_pool/attestation_pool.d.ts +39 -0
- package/dest/attestation_pool/attestation_pool.d.ts.map +1 -0
- package/dest/attestation_pool/attestation_pool.js +2 -0
- package/dest/attestation_pool/index.d.ts +3 -0
- package/dest/attestation_pool/index.d.ts.map +1 -0
- package/dest/attestation_pool/index.js +3 -0
- package/dest/attestation_pool/memory_attestation_pool.d.ts +12 -0
- package/dest/attestation_pool/memory_attestation_pool.d.ts.map +1 -0
- package/dest/attestation_pool/memory_attestation_pool.js +56 -0
- package/dest/attestation_pool/mocks.d.ts +16 -0
- package/dest/attestation_pool/mocks.d.ts.map +1 -0
- package/dest/attestation_pool/mocks.js +29 -0
- package/dest/client/index.d.ts +2 -1
- package/dest/client/index.d.ts.map +1 -1
- package/dest/client/index.js +4 -4
- package/dest/client/p2p_client.d.ts +27 -2
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +17 -5
- package/dest/index.d.ts +1 -0
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -1
- package/dest/service/dummy_service.d.ts +8 -4
- package/dest/service/dummy_service.d.ts.map +1 -1
- package/dest/service/dummy_service.js +8 -4
- package/dest/service/libp2p_service.d.ts +23 -6
- package/dest/service/libp2p_service.d.ts.map +1 -1
- package/dest/service/libp2p_service.js +52 -10
- package/dest/service/service.d.ts +4 -3
- package/dest/service/service.d.ts.map +1 -1
- package/package.json +8 -7
- package/src/attestation_pool/attestation_pool.ts +42 -0
- package/src/attestation_pool/index.ts +2 -0
- package/src/attestation_pool/memory_attestation_pool.ts +70 -0
- package/src/attestation_pool/mocks.ts +33 -0
- package/src/client/index.ts +4 -2
- package/src/client/p2p_client.ts +52 -3
- package/src/index.ts +1 -0
- package/src/service/dummy_service.ts +9 -4
- package/src/service/libp2p_service.ts +69 -7
- package/src/service/service.ts +6 -3
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
BlockAttestation,
|
|
3
|
+
BlockProposal,
|
|
4
|
+
type Gossipable,
|
|
5
|
+
type RawGossipMessage,
|
|
6
|
+
TopicType,
|
|
7
|
+
TopicTypeMap,
|
|
8
|
+
Tx,
|
|
9
|
+
} from '@aztec/circuit-types';
|
|
2
10
|
import { createDebugLogger } from '@aztec/foundation/log';
|
|
3
11
|
import { SerialQueue } from '@aztec/foundation/queue';
|
|
4
12
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
@@ -15,6 +23,7 @@ import { createFromJSON, createSecp256k1PeerId } from '@libp2p/peer-id-factory';
|
|
|
15
23
|
import { tcp } from '@libp2p/tcp';
|
|
16
24
|
import { type Libp2p, createLibp2p } from 'libp2p';
|
|
17
25
|
|
|
26
|
+
import { type AttestationPool } from '../attestation_pool/attestation_pool.js';
|
|
18
27
|
import { type P2PConfig } from '../config.js';
|
|
19
28
|
import { type TxPool } from '../tx_pool/index.js';
|
|
20
29
|
import { convertToMultiaddr } from '../util.js';
|
|
@@ -50,14 +59,25 @@ export class LibP2PService implements P2PService {
|
|
|
50
59
|
private jobQueue: SerialQueue = new SerialQueue();
|
|
51
60
|
private peerManager: PeerManager;
|
|
52
61
|
private discoveryRunningPromise?: RunningPromise;
|
|
62
|
+
|
|
63
|
+
private blockReceivedCallback: (block: BlockProposal) => Promise<BlockAttestation | undefined>;
|
|
64
|
+
|
|
53
65
|
constructor(
|
|
54
66
|
private config: P2PConfig,
|
|
55
67
|
private node: PubSubLibp2p,
|
|
56
68
|
private peerDiscoveryService: PeerDiscoveryService,
|
|
57
69
|
private txPool: TxPool,
|
|
70
|
+
private attestationPool: AttestationPool,
|
|
58
71
|
private logger = createDebugLogger('aztec:libp2p_service'),
|
|
59
72
|
) {
|
|
60
73
|
this.peerManager = new PeerManager(node, peerDiscoveryService, config, logger);
|
|
74
|
+
|
|
75
|
+
this.blockReceivedCallback = (block: BlockProposal): Promise<BlockAttestation | undefined> => {
|
|
76
|
+
this.logger.verbose(
|
|
77
|
+
`[WARNING] handler not yet registered: Block received callback not set. Received block ${block.p2pMessageIdentifier()} from peer.`,
|
|
78
|
+
);
|
|
79
|
+
return Promise.resolve(undefined);
|
|
80
|
+
};
|
|
61
81
|
}
|
|
62
82
|
|
|
63
83
|
/**
|
|
@@ -132,6 +152,7 @@ export class LibP2PService implements P2PService {
|
|
|
132
152
|
peerDiscoveryService: PeerDiscoveryService,
|
|
133
153
|
peerId: PeerId,
|
|
134
154
|
txPool: TxPool,
|
|
155
|
+
attestationPool: AttestationPool,
|
|
135
156
|
store: AztecKVStore,
|
|
136
157
|
) {
|
|
137
158
|
const { tcpListenAddress, tcpAnnounceAddress, minPeerCount, maxPeerCount } = config;
|
|
@@ -184,7 +205,12 @@ export class LibP2PService implements P2PService {
|
|
|
184
205
|
},
|
|
185
206
|
});
|
|
186
207
|
|
|
187
|
-
return new LibP2PService(config, node, peerDiscoveryService, txPool);
|
|
208
|
+
return new LibP2PService(config, node, peerDiscoveryService, txPool, attestationPool);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
public registerBlockReceivedCallback(callback: (block: BlockProposal) => Promise<BlockAttestation | undefined>) {
|
|
212
|
+
this.blockReceivedCallback = callback;
|
|
213
|
+
this.logger.verbose('Block received callback registered');
|
|
188
214
|
}
|
|
189
215
|
|
|
190
216
|
/**
|
|
@@ -223,16 +249,52 @@ export class LibP2PService implements P2PService {
|
|
|
223
249
|
const tx = Tx.fromBuffer(Buffer.from(message.data));
|
|
224
250
|
await this.processTxFromPeer(tx);
|
|
225
251
|
}
|
|
252
|
+
if (message.topic === BlockAttestation.p2pTopic) {
|
|
253
|
+
const attestation = BlockAttestation.fromBuffer(Buffer.from(message.data));
|
|
254
|
+
await this.processAttestationFromPeer(attestation);
|
|
255
|
+
}
|
|
256
|
+
if (message.topic == BlockProposal.p2pTopic) {
|
|
257
|
+
const block = BlockProposal.fromBuffer(Buffer.from(message.data));
|
|
258
|
+
await this.processBlockFromPeer(block);
|
|
259
|
+
}
|
|
226
260
|
|
|
227
261
|
return;
|
|
228
262
|
}
|
|
229
263
|
|
|
264
|
+
/**Process Attestation From Peer
|
|
265
|
+
* When a proposal is received from a peer, we add it to the attestation pool, so it can be accessed by other services.
|
|
266
|
+
*
|
|
267
|
+
* @param attestation - The attestation to process.
|
|
268
|
+
*/
|
|
269
|
+
private async processAttestationFromPeer(attestation: BlockAttestation): Promise<void> {
|
|
270
|
+
this.logger.verbose(`Received attestation ${attestation.p2pMessageIdentifier()} from external peer.`);
|
|
271
|
+
await this.attestationPool.addAttestations([attestation]);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**Process block from peer
|
|
275
|
+
*
|
|
276
|
+
* Pass the received block to the validator client
|
|
277
|
+
*
|
|
278
|
+
* @param block - The block to process.
|
|
279
|
+
*/
|
|
280
|
+
// REVIEW: callback pattern https://github.com/AztecProtocol/aztec-packages/issues/7963
|
|
281
|
+
private async processBlockFromPeer(block: BlockProposal): Promise<void> {
|
|
282
|
+
this.logger.verbose(`Received block ${block.p2pMessageIdentifier()} from external peer.`);
|
|
283
|
+
const attestation = await this.blockReceivedCallback(block);
|
|
284
|
+
|
|
285
|
+
// TODO: fix up this pattern - the abstraction is not nice
|
|
286
|
+
// The attestation can be undefined if no handler is registered / the validator deems the block invalid
|
|
287
|
+
if (attestation != undefined) {
|
|
288
|
+
this.propagate(attestation);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
230
292
|
/**
|
|
231
|
-
* Propagates
|
|
232
|
-
* @param
|
|
293
|
+
* Propagates provided message to peers.
|
|
294
|
+
* @param message - The message to propagate.
|
|
233
295
|
*/
|
|
234
|
-
public
|
|
235
|
-
void this.jobQueue.put(() => Promise.resolve(this.sendToPeers(
|
|
296
|
+
public propagate<T extends Gossipable>(message: T): void {
|
|
297
|
+
void this.jobQueue.put(() => Promise.resolve(this.sendToPeers(message)));
|
|
236
298
|
}
|
|
237
299
|
|
|
238
300
|
private async processTxFromPeer(tx: Tx): Promise<void> {
|
|
@@ -246,7 +308,7 @@ export class LibP2PService implements P2PService {
|
|
|
246
308
|
const parent = message.constructor as typeof Gossipable;
|
|
247
309
|
|
|
248
310
|
const identifier = message.p2pMessageIdentifier().toString();
|
|
249
|
-
this.logger.verbose(`Sending
|
|
311
|
+
this.logger.verbose(`Sending message ${identifier} to peers`);
|
|
250
312
|
|
|
251
313
|
const recipientsNum = await this.publishToTopic(parent.p2pTopic, message.toBuffer());
|
|
252
314
|
this.logger.verbose(`Sent tx ${identifier} to ${recipientsNum} peers`);
|
package/src/service/service.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BlockAttestation, BlockProposal, Gossipable } from '@aztec/circuit-types';
|
|
2
2
|
|
|
3
3
|
import type { ENR } from '@chainsafe/enr';
|
|
4
4
|
import type { PeerId } from '@libp2p/interface';
|
|
@@ -27,9 +27,12 @@ export interface P2PService {
|
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* Called to have the given transaction propagated through the P2P network.
|
|
30
|
-
* @param
|
|
30
|
+
* @param message - The message to be propagated.
|
|
31
31
|
*/
|
|
32
|
-
|
|
32
|
+
propagate<T extends Gossipable>(message: T): void;
|
|
33
|
+
|
|
34
|
+
// Leaky abstraction: fix https://github.com/AztecProtocol/aztec-packages/issues/7963
|
|
35
|
+
registerBlockReceivedCallback(callback: (block: BlockProposal) => Promise<BlockAttestation>): void;
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
/**
|