-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(vc): p2p message cleanup + attestaion + proposal types (#7733)
- Loading branch information
Showing
23 changed files
with
391 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
yarn-project/circuit-types/src/p2p/block_attestation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.