Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

chore(network): change the way protobuf types are being imported to improve readability #1233

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/.vscode
# Git hooks
/.husky
/crates/papyrus_network/protoc
40 changes: 15 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ primitive-types = "0.12.1"
parity-scale-codec = "3.5.0"
pretty_assertions = "1.3.0"
prometheus-parse = "0.2.4"
prost = "0.11.9"
prost-build = "0.11.9"
prost-types = "0.11.9"
prost = "0.12.1"
prost-build = "0.12.1"
prost-types = "0.12.1"
rand = "0.8.5"
rand_chacha = "0.3.1"
regex = "1.9.0"
Expand Down
4 changes: 4 additions & 0 deletions crates/papyrus_network/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::env;
use std::io::Result;

fn main() -> Result<()> {
println!("Building");
let dir = env::current_dir().unwrap();
println!("{dir:?}");
env::set_var("PROTOC", "./protoc");
prost_build::compile_protos(
&["src/messages/proto/p2p/proto/block.proto"],
&["src/messages/proto/"],
Expand Down
24 changes: 4 additions & 20 deletions crates/papyrus_network/src/messages/block.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
// TODO(shahak): Think of a way to auto-generate these.
/// Signatures from the Starknet feeder gateway that show this block and state diffs are part of
/// Starknet
pub type Signatures = crate::messages::proto::p2p::proto::Signatures;
/// Header of a block. Contains hashes for the proof and state diffs and the rest of the block
/// data.
pub type BlockHeader = crate::messages::proto::p2p::proto::BlockHeader;
/// A proof for the Kth block behind the current block.
pub type BlockProof = crate::messages::proto::p2p::proto::BlockProof;
/// A message announcing that a new block was created.
pub type NewBlock = crate::messages::proto::p2p::proto::NewBlock;
/// A request to get a range of block headers and state diffs.
pub type GetBlocks = crate::messages::proto::p2p::proto::GetBlocks;
/// A response of a [`GetBlocks`] request.
pub type GetBlocksResponse = crate::messages::proto::p2p::proto::GetBlocksResponse;
/// A request to get signatures on a block.
pub type GetSignatures = crate::messages::proto::p2p::proto::GetSignatures;
use super::protobuf;

impl GetBlocksResponse {
impl protobuf::BlockHeadersResponse {
pub fn is_fin(&self) -> bool {
self.response.as_ref().map_or(false, |response| {
self.header_message.as_ref().map_or(false, |response| {
matches!(
response,
crate::messages::proto::p2p::proto::get_blocks_response::Response::Fin(_)
crate::messages::proto::p2p::proto::block_headers_response::HeaderMessage::Fin(_)
)
})
}
Expand Down
17 changes: 0 additions & 17 deletions crates/papyrus_network/src/messages/common.rs

This file was deleted.

8 changes: 5 additions & 3 deletions crates/papyrus_network/src/messages/messages_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Duration;
use futures::AsyncWriteExt;
use pretty_assertions::assert_eq;

use super::block::GetBlocksResponse;
use super::{read_message, write_message};
use crate::messages::protobuf;
use crate::test_utils::{get_connected_streams, hardcoded_data};

#[tokio::test]
Expand All @@ -23,7 +23,9 @@ async fn read_write_positive_flow() {
async fn read_message_returns_none_when_other_stream_is_closed() {
let (mut stream1, mut stream2, _) = get_connected_streams().await;
stream1.close().await.unwrap();
assert!(read_message::<GetBlocksResponse, _>(&mut stream2).await.unwrap().is_none());
assert!(
read_message::<protobuf::BlockHeadersResponse, _>(&mut stream2).await.unwrap().is_none()
);
}

#[tokio::test]
Expand All @@ -32,7 +34,7 @@ async fn read_message_is_pending_when_other_stream_didnt_send() {
assert!(
tokio::time::timeout(
Duration::from_millis(10),
read_message::<GetBlocksResponse, _>(&mut stream2)
read_message::<protobuf::BlockHeadersResponse, _>(&mut stream2)
)
.await
.is_err()
Expand Down
3 changes: 2 additions & 1 deletion crates/papyrus_network/src/messages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod block;
pub mod common;
#[cfg(test)]
mod messages_test;

Expand All @@ -17,6 +16,8 @@ use futures::{AsyncRead, AsyncWrite};
use libp2p::core::upgrade::{read_length_prefixed, write_length_prefixed};
use prost::Message;

pub use crate::messages::proto::p2p::proto as protobuf;

pub const MAX_MESSAGE_SIZE: usize = 1 << 20;

pub async fn write_message<T: Message, Stream: AsyncWrite + Unpin>(
Expand Down
95 changes: 51 additions & 44 deletions crates/papyrus_network/src/messages/proto/p2p/proto/block.proto
Original file line number Diff line number Diff line change
@@ -1,73 +1,80 @@
syntax = "proto3";
import "p2p/proto/common.proto";
import "p2p/proto/state.proto";
import "google/protobuf/timestamp.proto";

// for now, we assume a small consensus, so this fits in 1M. Else, these will be repeated
message Signatures {
BlockID id = 1;
repeated Signature signatures = 2; // can be more explicit here about the signature structure as this is not part of account abstraction
uint64 block_number = 1;

repeated ConsensusSignature signatures = 2; //
// can be more explicit here about the signature structure as this is not part of account abstraction
}

// Note: commitments may change to be for the previous blocks like comet/tendermint
// hash of block header sent to L1
message BlockHeader {
BlockID parent_block = 1;

google.protobuf.Timestamp time = 2; // TODO: see if this needs to be Felt252 or can be converted

Address sequencer_address = 3;

Merkle state_diffs = 4; // By order of (contract, key), taking last in case of duplicates.
// This means the proposer needs to sort after finishing the block (TBD: patricia? )

// State is optional and appears every X blocks for the last block. This is to support
// snapshot sync and also so that light nodes can sync on state without state diffs.
Merkle state = 5; // hash of contract and class patricia tries. Same as in L1. Later more trees will be included

Hash proof_fact = 6; // for Kth block behind. A hash of the output of the proof
Hash parent_header = 1;
uint64 number = 2;
google.protobuf.Timestamp time = 3; // TODO: see if this needs to be Felt252 or can be converted
Address sequencer_address = 4;
Merkle state_diffs = 5; // By order of (contract, key), taking last in case of duplicates.
// This means the proposer needs to sort after finishing the block (TBD: patricia? )
// State is optional and appears every X blocks for the last block. This is to support
// snapshot sync and also so that light nodes can sync on state without state diffs.
Patricia state = 6; // hash of contract and class patricia tries. Same as in L1. Later more trees will be included
Hash proof_fact = 7; // for Kth block behind. A hash of the output of the proof

// The following merkles can be built on the fly while sequencing/validating txs.
Merkle transactions = 7; // By order of execution

Merkle events = 8; // By order of issuance.

Merkle receipts = 9; // By order of issuance.

uint32 protocol_version = 10;

ChainID chain_id = 11;
Merkle transactions = 8; // By order of execution. TBD: required? the client can execute (powerful machine) and match state diff
Merkle events = 9; // By order of issuance. TBD: in receipts?
Merkle receipts = 10; // By order of issuance.
}

message BlockProof {
bytes proof = 1; // proof size is currently 142K
}

// sent to all peers (except the ones this was received from, if any).
// for a fraction of peers, also send the GetBlocks response (as if they asked for it for this block)
// for a fraction of peers, also send the GetBlockHeaders and GetBlockBodies response (as if they asked for it for this block)
message NewBlock {
BlockID id = 1;
oneof maybe_full {
uint64 block_number = 1;
BlockHeadersResponse header = 2;
BlockBodiesResponse body = 3;
}
}

// result is (BlockHeader, StateDiff*)*
message GetBlocks {
enum Direction {
Forward = 0;
Backward = 1;
}
BlockID start = 1; // exclude start from the result
Direction direction = 2;
uint64 limit = 3;
uint64 skip = 4; // when starting, can send to first node skip=0, limit=100, second skip=100, limit=100, etc.
uint64 step = 5; // to allow interleaving from several nodes

// result is (BlockHeader, Signature?)* in order of creation (incr/dec)
message BlockHeadersRequest {
Iteration iteration = 1;
}

message GetBlocksResponse {
oneof Response {
BlockHeader header = 1;
Fin fin = 2;
message BlockHeadersResponse {
uint64 block_number = 1;

oneof header_message {
BlockHeader header = 2;
Signatures signatures = 3;
Fin fin = 4;
}
}

message GetSignatures {
BlockID id = 1;
// result is (StateDiff*, Classes*, BlockProof?)* currently in creation order (incr/dec), but may change in the future
message BlockBodiesRequest {
Iteration iteration = 1;
}

message BlockBodiesResponse {
uint64 block_number = 1;

oneof body_message {
StateDiff diff = 2;
Classes classes = 3;
BlockProof proof = 4;
Fin fin = 5;
}
}


Loading