Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions yarn-project/p2p/src/client/p2p_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MockL2BlockSource } from '@aztec/archiver/test';
import { Signature } from '@aztec/foundation/eth-signature';
import { Fr } from '@aztec/foundation/fields';
import { retryUntil } from '@aztec/foundation/retry';
import { sleep } from '@aztec/foundation/sleep';
Expand All @@ -8,18 +9,18 @@ import { L2Block, randomPublishedL2Block } from '@aztec/stdlib/block';
import { P2PClientType } from '@aztec/stdlib/p2p';
import { mockTx } from '@aztec/stdlib/testing';

import { expect } from '@jest/globals';
import { expect, jest } from '@jest/globals';
import { type MockProxy, mock } from 'jest-mock-extended';

import type { P2PService } from '../index.js';
import { InMemoryAttestationPool, 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 attestationPool: AttestationPool;
let mempools: MemPools;
let blockSource: MockL2BlockSource;
let p2pService: MockProxy<P2PService>;
Expand All @@ -35,7 +36,7 @@ describe('In-Memory P2P Client', () => {

p2pService = mock<P2PService>();

attestationPool = mock<AttestationPool>();
attestationPool = new InMemoryAttestationPool();

blockSource = new MockL2BlockSource();
await blockSource.createBlocks(100);
Expand Down Expand Up @@ -254,10 +255,22 @@ describe('In-Memory P2P Client', () => {
it('adds attestations to the pool', async () => {
await client.start();
const block = await randomPublishedL2Block(1);
const addAttestationsSpy = jest.spyOn(attestationPool, 'addAttestations');
await client.handleBlockStreamEvent({ type: 'blocks-added', blocks: [block] });
expect(attestationPool.addAttestations).toHaveBeenCalledWith(
expect(addAttestationsSpy).toHaveBeenCalledWith(
block.signatures.map(signature => expect.objectContaining({ signature })),
);
});

it('handles empty signatures in block stream events', async () => {
await client.start();
const block = await randomPublishedL2Block(1);
block.signatures[0] = Signature.empty();
const addAttestationsSpy = jest.spyOn(attestationPool, 'addAttestations');
await client.handleBlockStreamEvent({ type: 'blocks-added', blocks: [block] });
expect(addAttestationsSpy).toHaveBeenCalledWith(
block.signatures.filter(sig => !sig.isEmpty).map(signature => expect.objectContaining({ signature })),
);
});
});
});
2 changes: 1 addition & 1 deletion yarn-project/p2p/src/client/p2p_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
private async addAttestationsToPool(blocks: PublishedL2Block[]): Promise<void> {
const attestations = blocks.flatMap(block => {
const payload = ConsensusPayload.fromBlock(block.block);
return block.signatures.map(signature => new BlockAttestation(payload, signature));
return block.signatures.filter(sig => !sig.isEmpty).map(signature => new BlockAttestation(payload, signature));
});
await this.attestationPool?.addAttestations(attestations);
const slots = blocks.map(b => b.block.header.getSlot()).sort((a, b) => Number(a - b));
Expand Down
7 changes: 6 additions & 1 deletion yarn-project/stdlib/src/block/published_l2_block.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Buffer32 } from '@aztec/foundation/buffer';
import { times } from '@aztec/foundation/collection';
import { Secp256k1Signer } from '@aztec/foundation/crypto';
import { Signature } from '@aztec/foundation/eth-signature';
import { schemas } from '@aztec/foundation/schemas';
import { L2Block } from '@aztec/stdlib/block';
Expand Down Expand Up @@ -35,6 +36,10 @@ export async function randomPublishedL2Block(l2BlockNumber: number): Promise<Pub
timestamp: block.header.globalVariables.timestamp.toBigInt(),
blockHash: Buffer32.random().toString(),
};
const signatures = times(3, Signature.random);
// Create valid signatures
const signers = times(3, () => Secp256k1Signer.random());
const signatures = await Promise.all(
times(3, async i => signers[i].signMessage(Buffer32.fromField(await block.hash()))),
);
return { block, l1, signatures };
}