|
| 1 | +import { type JsonRpcTestContext, createJsonRpcTestSetup } from '@aztec/foundation/json-rpc/test'; |
| 2 | + |
| 3 | +import { BlockAttestation } from '../p2p/block_attestation.js'; |
| 4 | +import { EpochProofQuote } from '../prover_coordination/epoch_proof_quote.js'; |
| 5 | +import { Tx } from '../tx/tx.js'; |
| 6 | +import { type P2PApi, P2PApiSchema, type PeerInfo } from './p2p.js'; |
| 7 | + |
| 8 | +describe('P2PApiSchema', () => { |
| 9 | + let handler: MockP2P; |
| 10 | + let context: JsonRpcTestContext<P2PApi>; |
| 11 | + |
| 12 | + const tested = new Set<string>(); |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + handler = new MockP2P(); |
| 16 | + context = await createJsonRpcTestSetup<P2PApi>(handler, P2PApiSchema); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + tested.add(/^P2PApiSchema\s+([^(]+)/.exec(expect.getState().currentTestName!)![1]); |
| 21 | + context.httpServer.close(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterAll(() => { |
| 25 | + const all = Object.keys(P2PApiSchema); |
| 26 | + expect([...tested].sort()).toEqual(all.sort()); |
| 27 | + }); |
| 28 | + |
| 29 | + it('getAttestationsForSlot', async () => { |
| 30 | + const attestations = await context.client.getAttestationsForSlot(BigInt(1), 'proposalId'); |
| 31 | + expect(attestations).toEqual([BlockAttestation.empty()]); |
| 32 | + expect(attestations[0]).toBeInstanceOf(BlockAttestation); |
| 33 | + }); |
| 34 | + |
| 35 | + it('getEpochProofQuotes', async () => { |
| 36 | + const quotes = await context.client.getEpochProofQuotes(BigInt(1)); |
| 37 | + expect(quotes).toEqual([EpochProofQuote.empty()]); |
| 38 | + expect(quotes[0]).toBeInstanceOf(EpochProofQuote); |
| 39 | + }); |
| 40 | + |
| 41 | + it('getPendingTxs', async () => { |
| 42 | + const txs = await context.client.getPendingTxs(); |
| 43 | + expect(txs[0]).toBeInstanceOf(Tx); |
| 44 | + }); |
| 45 | + |
| 46 | + it('getEncodedEnr', async () => { |
| 47 | + const enr = await context.client.getEncodedEnr(); |
| 48 | + expect(enr).toEqual('enr'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('getPeers', async () => { |
| 52 | + const peers = await context.client.getPeers(); |
| 53 | + expect(peers).toEqual(peers); |
| 54 | + }); |
| 55 | + |
| 56 | + it('getPeers(true)', async () => { |
| 57 | + const peers = await context.client.getPeers(true); |
| 58 | + expect(peers).toEqual(peers); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +const peers: PeerInfo[] = [ |
| 63 | + { status: 'connected', score: 1, id: 'id' }, |
| 64 | + { status: 'dialing', dialStatus: 'dialStatus', id: 'id', addresses: ['address'] }, |
| 65 | + { status: 'cached', id: 'id', addresses: ['address'], enr: 'enr', dialAttempts: 1 }, |
| 66 | +]; |
| 67 | + |
| 68 | +class MockP2P implements P2PApi { |
| 69 | + getAttestationsForSlot(slot: bigint, proposalId?: string | undefined): Promise<BlockAttestation[]> { |
| 70 | + expect(slot).toEqual(1n); |
| 71 | + expect(proposalId).toEqual('proposalId'); |
| 72 | + return Promise.resolve([BlockAttestation.empty()]); |
| 73 | + } |
| 74 | + getEpochProofQuotes(epoch: bigint): Promise<EpochProofQuote[]> { |
| 75 | + expect(epoch).toEqual(1n); |
| 76 | + return Promise.resolve([EpochProofQuote.empty()]); |
| 77 | + } |
| 78 | + getPendingTxs(): Promise<Tx[]> { |
| 79 | + return Promise.resolve([Tx.random()]); |
| 80 | + } |
| 81 | + getEncodedEnr(): Promise<string | undefined> { |
| 82 | + return Promise.resolve('enr'); |
| 83 | + } |
| 84 | + getPeers(includePending?: boolean): Promise<PeerInfo[]> { |
| 85 | + expect(includePending === undefined || includePending === true).toBeTruthy(); |
| 86 | + return Promise.resolve(peers); |
| 87 | + } |
| 88 | +} |
0 commit comments