Skip to content

Commit

Permalink
chore(vc): p2p message cleanup + attestaion + proposal types (#7733)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Aug 6, 2024
1 parent e74108d commit 392cdb1
Show file tree
Hide file tree
Showing 23 changed files with 391 additions and 177 deletions.
2 changes: 2 additions & 0 deletions yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { NoteSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { BaseHashType } from '@aztec/foundation/hash';
import { JsonRpcServer } from '@aztec/foundation/json-rpc/server';

/**
Expand All @@ -40,6 +41,7 @@ export function createAztecNodeRpcServer(node: AztecNode) {
TxEffect,
LogId,
TxHash,
BaseHashType,
PublicDataWitness,
SiblingPath,
},
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/aztec.js/src/rpc_clients/pxe_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
Point,
} from '@aztec/circuits.js';
import { NoteSelector } from '@aztec/foundation/abi';
import { BaseHashType } from '@aztec/foundation/hash';
import { createJsonRpcClient, makeFetch } from '@aztec/foundation/json-rpc/client';

/**
Expand Down Expand Up @@ -54,6 +55,7 @@ export const createPXEClient = (url: string, fetch = makeFetch([1, 2, 3], false)
Point,
TxExecutionRequest,
TxHash,
BaseHashType,
},
{
EncryptedNoteL2BlockL2Logs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EventSelector, NoteSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { BaseHashType } from '@aztec/foundation/hash';
import { createJsonRpcClient, defaultFetch } from '@aztec/foundation/json-rpc/client';

import { type AztecNode } from '../../interfaces/aztec-node.js';
Expand Down Expand Up @@ -40,6 +41,7 @@ export function createAztecNodeClient(url: string, fetch = defaultFetch): AztecN
TxEffect,
LogId,
TxHash,
BaseHashType,
PublicDataWitness,
SiblingPath,
},
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuit-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './simulation_error.js';
export * from './tx/index.js';
export * from './tx_effect.js';
export * from './tx_execution_request.js';
export * from './p2p/index.js';
22 changes: 22 additions & 0 deletions yarn-project/circuit-types/src/p2p/block_attestation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Serde test for the block attestation type
import { makeHeader } from '@aztec/circuits.js/testing';

import { BlockAttestation } from './block_attestation.js';

const makeBlockAttestation = (): BlockAttestation => {
const blockHeader = makeHeader(1);
const signature = Buffer.alloc(64, 1);

return new BlockAttestation(blockHeader, signature);
};

describe('Block Attestation serialization / deserialization', () => {
it('Should serialize / deserialize', () => {
const attestation = makeBlockAttestation();

const serialized = attestation.toBuffer();
const deserialized = BlockAttestation.fromBuffer(serialized);

expect(deserialized).toEqual(attestation);
});
});
48 changes: 48 additions & 0 deletions yarn-project/circuit-types/src/p2p/block_attestation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Header } from '@aztec/circuits.js';
import { BaseHashType } from '@aztec/foundation/hash';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';

import { Gossipable } from './gossipable.js';
import { TopicType, createTopicString } from './topic_type.js';

export class BlockAttestationHash extends BaseHashType {
constructor(hash: Buffer) {
super(hash);
}
}

/**
* BlockAttestation
*
* A validator that has attested to seeing the contents of a block
* will produce a block attestation over the header of the block
*/
export class BlockAttestation extends Gossipable {
static override p2pTopic: string;

constructor(
/** The block header the attestation is made over */
public readonly header: Header,
/** The signature of the block attester */
public readonly signature: Buffer,
) {
super();
}

static {
this.p2pTopic = createTopicString(TopicType.block_attestation);
}

override p2pMessageIdentifier(): BaseHashType {
return BlockAttestationHash.fromField(this.header.hash());
}

toBuffer(): Buffer {
return serializeToBuffer([this.header, this.signature.length, this.signature]);
}

static fromBuffer(buf: Buffer | BufferReader): BlockAttestation {
const reader = BufferReader.asReader(buf);
return new BlockAttestation(reader.readObject(Header), reader.readBuffer());
}
}
24 changes: 24 additions & 0 deletions yarn-project/circuit-types/src/p2p/block_proposal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Serde test for the block proposal type
import { makeHeader } from '@aztec/circuits.js/testing';

import { TxHash } from '../index.js';
import { BlockProposal } from './block_proposal.js';

describe('Block Proposal serialization / deserialization', () => {
const makeBlockProposal = (): BlockProposal => {
const blockHeader = makeHeader(1);
const txs = [0, 1, 2, 3, 4, 5].map(() => TxHash.random());
const signature = Buffer.alloc(64, 1);

return new BlockProposal(blockHeader, txs, signature);
};

it('Should serialize / deserialize', () => {
const proposal = makeBlockProposal();

const serialized = proposal.toBuffer();
const deserialized = BlockProposal.fromBuffer(serialized);

expect(deserialized).toEqual(proposal);
});
});
55 changes: 55 additions & 0 deletions yarn-project/circuit-types/src/p2p/block_proposal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Header } from '@aztec/circuits.js';
import { BaseHashType } from '@aztec/foundation/hash';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';

import { TxHash } from '../index.js';
import { Gossipable } from './gossipable.js';
import { TopicType, createTopicString } from './topic_type.js';

export class BlockProposalHash extends BaseHashType {
constructor(hash: Buffer) {
super(hash);
}
}

/**
* BlockProposal
*
* A block proposal is created by the leader of the chain proposing a sequence of transactions to
* be included in the head of the chain
*/
export class BlockProposal extends Gossipable {
static override p2pTopic: string;

constructor(
/** The block header, after execution of the below sequence of transactions */
public readonly header: Header,
/** The sequence of transactions in the block */
public readonly txs: TxHash[],
/** The signer of the BlockProposal over the header of the new block*/
public readonly signature: Buffer,
) {
super();
}

static {
this.p2pTopic = createTopicString(TopicType.block_proposal);
}

override p2pMessageIdentifier(): BaseHashType {
return BlockProposalHash.fromField(this.header.hash());
}

toBuffer(): Buffer {
return serializeToBuffer([this.header, this.txs.length, this.txs, this.signature.length, this.signature]);
}

static fromBuffer(buf: Buffer | BufferReader): BlockProposal {
const reader = BufferReader.asReader(buf);
return new BlockProposal(
reader.readObject(Header),
reader.readArray(reader.readNumber(), TxHash),
reader.readBuffer(),
);
}
}
26 changes: 26 additions & 0 deletions yarn-project/circuit-types/src/p2p/gossipable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { type BaseHashType } from '@aztec/foundation/hash';

/**
* Gossipable
*
* Any class which extends gossipable will be able to be Gossiped over the p2p network
*/
export abstract class Gossipable {
/** p2p Topic
*
* - The p2p topic identifier, this determines how the message is handled
*/
static p2pTopic: string;

/** p2p Message Identifier
*
* - A digest of the message information, this key is used for deduplication
*/
abstract p2pMessageIdentifier(): BaseHashType;

/** To Buffer
*
* - Serialization method
*/
abstract toBuffer(): Buffer;
}
5 changes: 5 additions & 0 deletions yarn-project/circuit-types/src/p2p/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './block_attestation.js';
export * from './block_proposal.js';
export * from './interface.js';
export * from './gossipable.js';
export * from './topic_type.js';
17 changes: 17 additions & 0 deletions yarn-project/circuit-types/src/p2p/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Tx } from '../tx/tx.js';
import { BlockAttestation } from './block_attestation.js';
import { BlockProposal } from './block_proposal.js';
import { type Gossipable } from './gossipable.js';
import { TopicType } from './topic_type.js';

export interface RawGossipMessage {
topic: string;
data: Uint8Array;
}

// Force casts as we know that each field here extends Gossipable, and we just want types from Gossipable
export const TopicTypeMap: Record<string, typeof Gossipable> = {
[TopicType.tx]: Tx as unknown as typeof Gossipable,
[TopicType.block_proposal]: BlockProposal as unknown as typeof Gossipable,
[TopicType.block_attestation]: BlockAttestation as unknown as typeof Gossipable,
};
18 changes: 18 additions & 0 deletions yarn-project/circuit-types/src/p2p/topic_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** Create Topic String
*
* The topic channel identifier
* @param topicType
* @returns
*/
export function createTopicString(topicType: TopicType) {
return '/aztec/' + topicType + '/0.1.0';
}

/**
*
*/
export enum TopicType {
tx = 'tx',
block_proposal = 'block_proposal',
block_attestation = 'block_attestation',
}
21 changes: 19 additions & 2 deletions yarn-project/circuit-types/src/tx/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ import {
type PublicKernelCircuitPublicInputs,
} from '@aztec/circuits.js';
import { arraySerializedSizeOfNonEmpty } from '@aztec/foundation/collection';
import { type BaseHashType } from '@aztec/foundation/hash';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';

import { type GetUnencryptedLogsResponse } from '../logs/get_unencrypted_logs_response.js';
import { type L2LogsSource } from '../logs/l2_logs_source.js';
import { EncryptedNoteTxL2Logs, EncryptedTxL2Logs, UnencryptedTxL2Logs } from '../logs/tx_l2_logs.js';
import { Gossipable } from '../p2p/gossipable.js';
import { TopicType, createTopicString } from '../p2p/topic_type.js';
import { PublicExecutionRequest } from '../public_execution_request.js';
import { type TxStats } from '../stats/stats.js';
import { TxHash } from './tx_hash.js';

/**
* The interface of an L2 transaction.
*/
export class Tx {
export class Tx extends Gossipable {
static override p2pTopic: string;

constructor(
/**
* Output of the private kernel circuit for this tx.
Expand Down Expand Up @@ -49,7 +54,19 @@ export class Tx {
* Public function call to be run by the sequencer as part of teardown.
*/
public readonly publicTeardownFunctionCall: PublicExecutionRequest,
) {}
) {
super();
}

// Gossipable method
static {
this.p2pTopic = createTopicString(TopicType.tx);
}

// Gossipable method
override p2pMessageIdentifier(): BaseHashType {
return this.getTxHash();
}

hasPublicCalls() {
return this.data.numberOfPublicCallRequests() > 0;
Expand Down
Loading

0 comments on commit 392cdb1

Please sign in to comment.