Skip to content

Commit 7dfa295

Browse files
authored
feat: add insturmentation to attestation and epoch quote mem pools (#9055)
1 parent 7677ca5 commit 7dfa295

20 files changed

+301
-120
lines changed

yarn-project/circuit-types/src/p2p/block_attestation.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@ import { BlockAttestation } from './block_attestation.js';
55
import { makeBlockAttestation } from './mocks.js';
66

77
describe('Block Attestation serialization / deserialization', () => {
8+
const checkEquivalence = (serialized: BlockAttestation, deserialized: BlockAttestation) => {
9+
expect(deserialized.getSize()).toEqual(serialized.getSize());
10+
expect(deserialized).toEqual(serialized);
11+
};
12+
813
it('Should serialize / deserialize', () => {
914
const attestation = makeBlockAttestation();
1015

1116
const serialized = attestation.toBuffer();
1217
const deserialized = BlockAttestation.fromBuffer(serialized);
13-
14-
expect(deserialized).toEqual(attestation);
18+
checkEquivalence(attestation, deserialized);
1519
});
1620

1721
it('Should serialize / deserialize + recover sender', () => {
1822
const account = Secp256k1Signer.random();
1923

20-
const proposal = makeBlockAttestation(account);
21-
const serialized = proposal.toBuffer();
24+
const attestation = makeBlockAttestation(account);
25+
const serialized = attestation.toBuffer();
2226
const deserialized = BlockAttestation.fromBuffer(serialized);
2327

24-
expect(deserialized).toEqual(proposal);
28+
checkEquivalence(attestation, deserialized);
2529

2630
// Recover signature
2731
const sender = deserialized.getSender();

yarn-project/circuit-types/src/p2p/block_attestation.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,8 @@ export class BlockAttestation extends Gossipable {
7272
static empty(): BlockAttestation {
7373
return new BlockAttestation(ConsensusPayload.empty(), Signature.empty());
7474
}
75+
76+
getSize(): number {
77+
return this.payload.getSize() + this.signature.getSize();
78+
}
7579
}

yarn-project/circuit-types/src/p2p/block_proposal.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import { BlockProposal } from './block_proposal.js';
55
import { makeBlockProposal } from './mocks.js';
66

77
describe('Block Proposal serialization / deserialization', () => {
8+
const checkEquivalence = (serialized: BlockProposal, deserialized: BlockProposal) => {
9+
expect(deserialized.getSize()).toEqual(serialized.getSize());
10+
expect(deserialized).toEqual(serialized);
11+
};
12+
813
it('Should serialize / deserialize', () => {
914
const proposal = makeBlockProposal();
1015

1116
const serialized = proposal.toBuffer();
1217
const deserialized = BlockProposal.fromBuffer(serialized);
13-
14-
expect(deserialized).toEqual(proposal);
18+
checkEquivalence(proposal, deserialized);
1519
});
1620

1721
it('Should serialize / deserialize + recover sender', () => {
@@ -21,7 +25,7 @@ describe('Block Proposal serialization / deserialization', () => {
2125
const serialized = proposal.toBuffer();
2226
const deserialized = BlockProposal.fromBuffer(serialized);
2327

24-
expect(deserialized).toEqual(proposal);
28+
checkEquivalence(proposal, deserialized);
2529

2630
// Recover signature
2731
const sender = deserialized.getSender();

yarn-project/circuit-types/src/p2p/block_proposal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@ export class BlockProposal extends Gossipable {
7575
const reader = BufferReader.asReader(buf);
7676
return new BlockProposal(reader.readObject(ConsensusPayload), reader.readObject(Signature));
7777
}
78+
79+
getSize(): number {
80+
return this.payload.getSize() + this.signature.getSize();
81+
}
7882
}

yarn-project/circuit-types/src/p2p/consensus_payload.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { TxHash } from '../tx/tx_hash.js';
99
import { type Signable } from './signature_utils.js';
1010

1111
export class ConsensusPayload implements Signable {
12+
private size: number | undefined;
13+
1214
constructor(
1315
/** The block header the attestation is made over */
1416
public readonly header: Header,
@@ -31,7 +33,9 @@ export class ConsensusPayload implements Signable {
3133
}
3234

3335
toBuffer(): Buffer {
34-
return serializeToBuffer([this.header, this.archive, this.txHashes.length, this.txHashes]);
36+
const buffer = serializeToBuffer([this.header, this.archive, this.txHashes.length, this.txHashes]);
37+
this.size = buffer.length;
38+
return buffer;
3539
}
3640

3741
static fromBuffer(buf: Buffer | BufferReader): ConsensusPayload {
@@ -50,4 +54,17 @@ export class ConsensusPayload implements Signable {
5054
static empty(): ConsensusPayload {
5155
return new ConsensusPayload(Header.empty(), Fr.ZERO, []);
5256
}
57+
58+
/**
59+
* Get the size of the consensus payload in bytes.
60+
* @returns The size of the consensus payload.
61+
*/
62+
getSize(): number {
63+
// We cache size to avoid recalculating it
64+
if (this.size) {
65+
return this.size;
66+
}
67+
this.size = this.toBuffer().length;
68+
return this.size;
69+
}
5370
}

yarn-project/circuit-types/src/p2p/gossipable.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ export abstract class Gossipable {
2323
* - Serialization method
2424
*/
2525
abstract toBuffer(): Buffer;
26+
27+
/**
28+
* Get the size of the gossipable object.
29+
*
30+
* This is used for metrics recording.
31+
*/
32+
abstract getSize(): number;
2633
}

yarn-project/circuit-types/src/prover_coordination/epoch_proof_quote.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ describe('epoch proof quote', () => {
1919
quote = new EpochProofQuote(payload, Signature.random());
2020
});
2121

22+
const checkEquivalence = (serialized: EpochProofQuote, deserialized: EpochProofQuote) => {
23+
expect(deserialized.getSize()).toEqual(serialized.getSize());
24+
expect(deserialized).toEqual(serialized);
25+
};
26+
2227
it('should serialize and deserialize from buffer', () => {
23-
expect(EpochProofQuote.fromBuffer(quote.toBuffer())).toEqual(quote);
28+
const deserialised = EpochProofQuote.fromBuffer(quote.toBuffer());
29+
checkEquivalence(quote, deserialised);
2430
});
2531

2632
it('should serialize and deserialize from JSON', () => {
27-
expect(EpochProofQuote.fromJSON(quote.toJSON())).toEqual(quote);
33+
const deserialised = EpochProofQuote.fromJSON(quote.toJSON());
34+
checkEquivalence(quote, deserialised);
2835
});
2936
});

yarn-project/circuit-types/src/prover_coordination/epoch_proof_quote.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,12 @@ export class EpochProofQuote extends Gossipable {
6969
signature: this.signature.toViemSignature(),
7070
};
7171
}
72+
73+
/**
74+
* Get the size of the epoch proof quote in bytes.
75+
* @returns The size of the epoch proof quote in bytes.
76+
*/
77+
getSize(): number {
78+
return this.payload.getSize() + this.signature.getSize();
79+
}
7280
}

yarn-project/circuit-types/src/prover_coordination/epoch_proof_quote_payload.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { type FieldsOf } from '@aztec/foundation/types';
55
import { inspect } from 'util';
66

77
export class EpochProofQuotePayload {
8+
// Cached values
9+
private asBuffer: Buffer | undefined;
10+
private size: number | undefined;
11+
812
constructor(
913
public readonly epochToProve: bigint,
1014
public readonly validUntilSlot: bigint,
@@ -24,7 +28,13 @@ export class EpochProofQuotePayload {
2428
}
2529

2630
toBuffer(): Buffer {
27-
return serializeToBuffer(...EpochProofQuotePayload.getFields(this));
31+
// We cache the buffer to avoid recalculating it
32+
if (this.asBuffer) {
33+
return this.asBuffer;
34+
}
35+
this.asBuffer = serializeToBuffer(...EpochProofQuotePayload.getFields(this));
36+
this.size = this.asBuffer.length;
37+
return this.asBuffer;
2838
}
2939

3040
static fromBuffer(buf: Buffer | BufferReader): EpochProofQuotePayload {
@@ -84,6 +94,16 @@ export class EpochProofQuotePayload {
8494
};
8595
}
8696

97+
getSize(): number {
98+
// We cache size to avoid recalculating it
99+
if (this.size) {
100+
return this.size;
101+
}
102+
// Size is cached when calling toBuffer
103+
this.toBuffer();
104+
return this.size!;
105+
}
106+
87107
[inspect.custom](): string {
88108
return `EpochProofQuotePayload { epochToProve: ${this.epochToProve}, validUntilSlot: ${this.validUntilSlot}, bondAmount: ${this.bondAmount}, prover: ${this.prover}, basisPointFee: ${this.basisPointFee} }`;
89109
}

yarn-project/foundation/src/eth-signature/eth_signature.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,21 @@ describe('eth signature', () => {
2020
signature = signer.sign(message);
2121
});
2222

23+
const checkEquivalence = (serialized: Signature, deserialized: Signature) => {
24+
expect(deserialized.getSize()).toEqual(serialized.getSize());
25+
expect(deserialized).toEqual(serialized);
26+
};
27+
2328
it('should serialize / deserialize to buffer', () => {
2429
const serialized = signature.toBuffer();
2530
const deserialized = Signature.fromBuffer(serialized);
26-
expect(deserialized).toEqual(signature);
31+
checkEquivalence(signature, deserialized);
2732
});
2833

2934
it('should serialize / deserialize real signature to hex string', () => {
3035
const serialized = signature.to0xString();
3136
const deserialized = Signature.from0xString(serialized);
32-
expect(deserialized).toEqual(signature);
37+
checkEquivalence(signature, deserialized);
3338
});
3439

3540
it('should recover signer from signature', () => {
@@ -41,13 +46,13 @@ describe('eth signature', () => {
4146
const signature = new Signature(Buffer32.random(), Buffer32.random(), 1, false);
4247
const serialized = signature.to0xString();
4348
const deserialized = Signature.from0xString(serialized);
44-
expect(deserialized).toEqual(signature);
49+
checkEquivalence(signature, deserialized);
4550
});
4651

4752
it('should serialize / deserialize to hex string with 2-digit v', () => {
4853
const signature = new Signature(Buffer32.random(), Buffer32.random(), 26, false);
4954
const serialized = signature.to0xString();
5055
const deserialized = Signature.from0xString(serialized);
51-
expect(deserialized).toEqual(signature);
56+
checkEquivalence(signature, deserialized);
5257
});
5358
});

0 commit comments

Comments
 (0)