-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathp2p_client.test.ts
315 lines (249 loc) · 10.6 KB
/
p2p_client.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { MockL2BlockSource } from '@aztec/archiver/test';
import { L2Block, P2PClientType, mockEpochProofQuote, mockTx } from '@aztec/circuit-types';
import { Fr } from '@aztec/circuits.js';
import { retryUntil } from '@aztec/foundation/retry';
import { sleep } from '@aztec/foundation/sleep';
import { type AztecAsyncKVStore } from '@aztec/kv-store';
import { openTmpStore } from '@aztec/kv-store/lmdb-v2';
import { expect } from '@jest/globals';
import { type MockProxy, mock } from 'jest-mock-extended';
import { type EpochProofQuotePool, type P2PService } from '../index.js';
import { type AttestationPool } from '../mem_pools/attestation_pool/attestation_pool.js';
import { type MemPools } from '../mem_pools/interface.js';
import { type TxPool } from '../mem_pools/tx_pool/index.js';
import { P2PClient } from './p2p_client.js';
describe('In-Memory P2P Client', () => {
let txPool: MockProxy<TxPool>;
let attestationPool: MockProxy<AttestationPool>;
let epochProofQuotePool: MockProxy<EpochProofQuotePool>;
let mempools: MemPools;
let blockSource: MockL2BlockSource;
let p2pService: MockProxy<P2PService>;
let kvStore: AztecAsyncKVStore;
let client: P2PClient;
beforeEach(async () => {
txPool = mock<TxPool>();
txPool.getAllTxs.mockResolvedValue([]);
txPool.getPendingTxHashes.mockResolvedValue([]);
txPool.getMinedTxHashes.mockResolvedValue([]);
txPool.getAllTxHashes.mockResolvedValue([]);
p2pService = mock<P2PService>();
attestationPool = mock<AttestationPool>();
epochProofQuotePool = mock<EpochProofQuotePool>();
epochProofQuotePool.getQuotes.mockReturnValue([]);
blockSource = new MockL2BlockSource();
await blockSource.createBlocks(100);
mempools = {
txPool,
attestationPool,
epochProofQuotePool,
};
kvStore = await openTmpStore('test');
client = new P2PClient(P2PClientType.Full, kvStore, blockSource, mempools, p2pService);
});
afterEach(async () => {
await kvStore.close();
});
const advanceToProvenBlock = async (getProvenBlockNumber: number, provenEpochNumber = getProvenBlockNumber) => {
blockSource.setProvenBlockNumber(getProvenBlockNumber);
blockSource.setProvenEpochNumber(provenEpochNumber);
await retryUntil(async () => (await client.getSyncedProvenBlockNum()) >= getProvenBlockNumber, 'synced', 10, 0.1);
};
afterEach(async () => {
if (client.isReady()) {
await client.stop();
}
});
it('can start & stop', async () => {
expect(client.isReady()).toEqual(false);
await client.start();
expect(client.isReady()).toEqual(true);
await client.stop();
expect(client.isReady()).toEqual(false);
});
it('adds txs to pool', async () => {
await client.start();
const tx1 = await mockTx();
const tx2 = await mockTx();
await client.sendTx(tx1);
await client.sendTx(tx2);
expect(txPool.addTxs).toHaveBeenCalledTimes(2);
await client.stop();
});
it('rejects txs after being stopped', async () => {
await client.start();
const tx1 = await mockTx();
const tx2 = await mockTx();
await client.sendTx(tx1);
await client.sendTx(tx2);
expect(txPool.addTxs).toHaveBeenCalledTimes(2);
await client.stop();
const tx3 = await mockTx();
await expect(client.sendTx(tx3)).rejects.toThrow();
expect(txPool.addTxs).toHaveBeenCalledTimes(2);
});
it('restores the previous block number it was at', async () => {
await client.start();
const synchedBlock = await client.getSyncedLatestBlockNum();
await client.stop();
const client2 = new P2PClient(P2PClientType.Full, kvStore, blockSource, mempools, p2pService);
await expect(client2.getSyncedLatestBlockNum()).resolves.toEqual(synchedBlock);
});
it('deletes txs once block is proven', async () => {
blockSource.setProvenBlockNumber(0);
await client.start();
expect(txPool.deleteTxs).not.toHaveBeenCalled();
await advanceToProvenBlock(5);
expect(txPool.deleteTxs).toHaveBeenCalledTimes(5);
await client.stop();
});
it('deletes txs after waiting the set number of blocks', async () => {
client = new P2PClient(P2PClientType.Full, kvStore, blockSource, mempools, p2pService, {
keepProvenTxsInPoolFor: 10,
});
blockSource.setProvenBlockNumber(0);
await client.start();
expect(txPool.deleteTxs).not.toHaveBeenCalled();
await advanceToProvenBlock(5);
expect(txPool.deleteTxs).not.toHaveBeenCalled();
await advanceToProvenBlock(12);
expect(txPool.deleteTxs).toHaveBeenCalledTimes(2);
await advanceToProvenBlock(20);
expect(txPool.deleteTxs).toHaveBeenCalledTimes(10);
await client.stop();
});
it('stores and returns epoch proof quotes', async () => {
client = new P2PClient(P2PClientType.Full, kvStore, blockSource, mempools, p2pService);
blockSource.setProvenEpochNumber(2);
await client.start();
const proofQuotes = [
mockEpochProofQuote(3n),
mockEpochProofQuote(2n),
mockEpochProofQuote(3n),
mockEpochProofQuote(4n),
mockEpochProofQuote(2n),
mockEpochProofQuote(3n),
];
for (const quote of proofQuotes) {
await client.addEpochProofQuote(quote);
}
expect(epochProofQuotePool.addQuote).toBeCalledTimes(proofQuotes.length);
for (let i = 0; i < proofQuotes.length; i++) {
expect(epochProofQuotePool.addQuote).toHaveBeenNthCalledWith(i + 1, proofQuotes[i]);
}
expect(epochProofQuotePool.addQuote).toBeCalledTimes(proofQuotes.length);
await client.getEpochProofQuotes(2n);
expect(epochProofQuotePool.getQuotes).toBeCalledTimes(1);
expect(epochProofQuotePool.getQuotes).toBeCalledWith(2n);
});
// TODO(#10737) flake cc Maddiaa0
it.skip('deletes expired proof quotes', async () => {
client = new P2PClient(P2PClientType.Full, kvStore, blockSource, mempools, p2pService);
blockSource.setProvenEpochNumber(1);
blockSource.setProvenBlockNumber(1);
await client.start();
const proofQuotes = [
mockEpochProofQuote(3n),
mockEpochProofQuote(2n),
mockEpochProofQuote(3n),
mockEpochProofQuote(4n),
mockEpochProofQuote(2n),
mockEpochProofQuote(3n),
];
for (const quote of proofQuotes) {
client.broadcastEpochProofQuote(quote);
}
epochProofQuotePool.deleteQuotesToEpoch.mockReset();
await advanceToProvenBlock(3, 3);
expect(epochProofQuotePool.deleteQuotesToEpoch).toBeCalledWith(3n);
});
describe('Chain prunes', () => {
it('moves the tips on a chain reorg', async () => {
blockSource.setProvenBlockNumber(0);
await client.start();
await advanceToProvenBlock(90);
await expect(client.getL2Tips()).resolves.toEqual({
latest: { number: 100, hash: expect.any(String) },
proven: { number: 90, hash: expect.any(String) },
finalized: { number: 90, hash: expect.any(String) },
});
blockSource.removeBlocks(10);
// give the client a chance to react to the reorg
await sleep(100);
await expect(client.getL2Tips()).resolves.toEqual({
latest: { number: 90, hash: expect.any(String) },
proven: { number: 90, hash: expect.any(String) },
finalized: { number: 90, hash: expect.any(String) },
});
blockSource.addBlocks([await L2Block.random(91), await L2Block.random(92)]);
// give the client a chance to react to the new blocks
await sleep(100);
await expect(client.getL2Tips()).resolves.toEqual({
latest: { number: 92, hash: expect.any(String) },
proven: { number: 90, hash: expect.any(String) },
finalized: { number: 90, hash: expect.any(String) },
});
});
it('deletes txs created from a pruned block', async () => {
client = new P2PClient(P2PClientType.Full, kvStore, blockSource, mempools, p2pService, {
keepProvenTxsInPoolFor: 10,
});
blockSource.setProvenBlockNumber(0);
await client.start();
// add two txs to the pool. One build against block 90, one against block 95
// then prune the chain back to block 90
// only one tx should be deleted
const goodTx = await mockTx();
goodTx.data.constants.historicalHeader.globalVariables.blockNumber = new Fr(90);
const badTx = await mockTx();
badTx.data.constants.historicalHeader.globalVariables.blockNumber = new Fr(95);
txPool.getAllTxs.mockResolvedValue([goodTx, badTx]);
blockSource.removeBlocks(10);
await sleep(150);
expect(txPool.deleteTxs).toHaveBeenCalledWith([await badTx.getTxHash()]);
await client.stop();
});
it('moves mined and valid txs back to the pending set', async () => {
client = new P2PClient(P2PClientType.Full, kvStore, blockSource, mempools, p2pService, {
keepProvenTxsInPoolFor: 10,
});
blockSource.setProvenBlockNumber(0);
await client.start();
// add three txs to the pool built against different blocks
// then prune the chain back to block 90
// only one tx should be deleted
const goodButOldTx = await mockTx();
goodButOldTx.data.constants.historicalHeader.globalVariables.blockNumber = new Fr(89);
const goodTx = await mockTx();
goodTx.data.constants.historicalHeader.globalVariables.blockNumber = new Fr(90);
const badTx = await mockTx();
badTx.data.constants.historicalHeader.globalVariables.blockNumber = new Fr(95);
txPool.getAllTxs.mockResolvedValue([goodButOldTx, goodTx, badTx]);
txPool.getMinedTxHashes.mockResolvedValue([
[await goodButOldTx.getTxHash(), 90],
[await goodTx.getTxHash(), 91],
]);
blockSource.removeBlocks(10);
await sleep(150);
expect(txPool.deleteTxs).toHaveBeenCalledWith([await badTx.getTxHash()]);
await sleep(150);
expect(txPool.markMinedAsPending).toHaveBeenCalledWith([await goodTx.getTxHash()]);
await client.stop();
});
});
describe('Attestation pool pruning', () => {
it('deletes attestations older than the number of slots we want to keep in the pool', async () => {
const advanceToProvenBlockNumber = 20;
const keepAttestationsInPoolFor = 12;
blockSource.setProvenBlockNumber(0);
(client as any).keepAttestationsInPoolFor = keepAttestationsInPoolFor;
await client.start();
expect(attestationPool.deleteAttestationsOlderThan).not.toHaveBeenCalled();
await advanceToProvenBlock(advanceToProvenBlockNumber);
expect(attestationPool.deleteAttestationsOlderThan).toHaveBeenCalledTimes(1);
expect(attestationPool.deleteAttestationsOlderThan).toHaveBeenCalledWith(
BigInt(advanceToProvenBlockNumber - keepAttestationsInPoolFor),
);
});
});
});