Skip to content

Commit

Permalink
fix: e2e-p2p attestation timeout (#9154)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Oct 10, 2024
1 parent 1323a34 commit 25bd47b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Block Attestation serialization / deserialization', () => {
it('Should serialize / deserialize + recover sender', () => {
const account = Secp256k1Signer.random();

const attestation = makeBlockAttestation(account);
const attestation = makeBlockAttestation({ signer: account });
const serialized = attestation.toBuffer();
const deserialized = BlockAttestation.fromBuffer(serialized);

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuit-types/src/p2p/block_proposal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Block Proposal serialization / deserialization', () => {
it('Should serialize / deserialize + recover sender', () => {
const account = Secp256k1Signer.random();

const proposal = makeBlockProposal(account);
const proposal = makeBlockProposal({ signer: account });
const serialized = proposal.toBuffer();
const deserialized = BlockProposal.fromBuffer(serialized);

Expand Down
31 changes: 23 additions & 8 deletions yarn-project/circuit-types/src/p2p/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type Header } from '@aztec/circuits.js';
import { makeHeader } from '@aztec/circuits.js/testing';
import { Secp256k1Signer } from '@aztec/foundation/crypto';
import { Fr } from '@aztec/foundation/fields';
Expand All @@ -8,11 +9,25 @@ import { BlockProposal } from './block_proposal.js';
import { ConsensusPayload } from './consensus_payload.js';
import { getHashedSignaturePayloadEthSignedMessage } from './signature_utils.js';

const makeAndSignConsensusPayload = (signer = Secp256k1Signer.random()) => {
export interface MakeConsensusPayloadOptions {
signer?: Secp256k1Signer;
header?: Header;
archive?: Fr;
txHashes?: TxHash[];
}

const makeAndSignConsensusPayload = (options?: MakeConsensusPayloadOptions) => {
const {
signer = Secp256k1Signer.random(),
header = makeHeader(1),
archive = Fr.random(),
txHashes = [0, 1, 2, 3, 4, 5].map(() => TxHash.random()),
} = options ?? {};

const payload = ConsensusPayload.fromFields({
header: makeHeader(1),
archive: Fr.random(),
txHashes: [0, 1, 2, 3, 4, 5].map(() => TxHash.random()),
header,
archive,
txHashes,
});

const hash = getHashedSignaturePayloadEthSignedMessage(payload);
Expand All @@ -21,13 +36,13 @@ const makeAndSignConsensusPayload = (signer = Secp256k1Signer.random()) => {
return { payload, signature };
};

export const makeBlockProposal = (signer = Secp256k1Signer.random()): BlockProposal => {
const { payload, signature } = makeAndSignConsensusPayload(signer);
export const makeBlockProposal = (options?: MakeConsensusPayloadOptions): BlockProposal => {
const { payload, signature } = makeAndSignConsensusPayload(options);
return new BlockProposal(payload, signature);
};

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/8028)
export const makeBlockAttestation = (signer?: Secp256k1Signer): BlockAttestation => {
const { payload, signature } = makeAndSignConsensusPayload(signer);
export const makeBlockAttestation = (options?: MakeConsensusPayloadOptions): BlockAttestation => {
const { payload, signature } = makeAndSignConsensusPayload(options);
return new BlockAttestation(payload, signature);
};
35 changes: 34 additions & 1 deletion yarn-project/validator-client/src/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { TxHash } from '@aztec/circuit-types';
import { makeHeader } from '@aztec/circuits.js/testing';
import { Secp256k1Signer } from '@aztec/foundation/crypto';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { type P2P } from '@aztec/p2p';
Expand All @@ -12,7 +13,7 @@ import { describe, expect, it } from '@jest/globals';
import { type MockProxy, mock } from 'jest-mock-extended';
import { type PrivateKeyAccount, generatePrivateKey, privateKeyToAccount } from 'viem/accounts';

import { makeBlockProposal } from '../../circuit-types/src/p2p/mocks.js';
import { makeBlockAttestation, makeBlockProposal } from '../../circuit-types/src/p2p/mocks.js';
import { type ValidatorClientConfig } from './config.js';
import {
AttestationTimeoutError,
Expand Down Expand Up @@ -81,4 +82,36 @@ describe('ValidationService', () => {
TransactionsNotAvailableError,
);
});

it('Should collect attestations for a proposal', async () => {
const signer = Secp256k1Signer.random();
const attestor1 = Secp256k1Signer.random();
const attestor2 = Secp256k1Signer.random();

const archive = Fr.random();
const txHashes = [0, 1, 2, 3, 4, 5].map(() => TxHash.random());

const proposal = makeBlockProposal({ signer, archive, txHashes });

// Mock the attestations to be returned
const expectedAttestations = [
makeBlockAttestation({ signer: attestor1, archive, txHashes }),
makeBlockAttestation({ signer: attestor2, archive, txHashes }),
];
p2pClient.getAttestationsForSlot.mockImplementation((slot, proposalId) => {
if (
slot === proposal.payload.header.globalVariables.slotNumber.toBigInt() &&
proposalId === proposal.archive.toString()
) {
return Promise.resolve(expectedAttestations);
}
return Promise.resolve([]);
});

// Perform the query
const numberOfRequiredAttestations = 3;
const attestations = await validatorClient.collectAttestations(proposal, numberOfRequiredAttestations);

expect(attestations).toHaveLength(numberOfRequiredAttestations);
});
});
2 changes: 1 addition & 1 deletion yarn-project/validator-client/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class ValidatorClient extends WithTracer implements Validator {
const slot = proposal.payload.header.globalVariables.slotNumber.toBigInt();
this.log.info(`Waiting for ${numberOfRequiredAttestations} attestations for slot: ${slot}`);

const proposalId = proposal.p2pMessageIdentifier().toString();
const proposalId = proposal.archive.toString();
const myAttestation = await this.validationService.attestToProposal(proposal);

const startTime = Date.now();
Expand Down

0 comments on commit 25bd47b

Please sign in to comment.