Skip to content

Commit

Permalink
feat(papyrus_protobuf): add BlockInfo to ProposalPart
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaastarkware committed Feb 4, 2025
1 parent 83555c5 commit 87cbbc9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 37 deletions.
6 changes: 4 additions & 2 deletions crates/papyrus_protobuf/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ pub struct ProposalFin {
pub enum ProposalPart {
/// The initialization part of the proposal.
Init(ProposalInit),
/// Identifies the content of the proposal; contains `id(v)` in Tendermint terms.
Fin(ProposalFin),
/// The block info part of the proposal.
BlockInfo(BlockInfo),
/// A part of the proposal that contains one or more transactions.
Transactions(TransactionBatch),
/// The final part of the proposal, including the block hash.
Fin(ProposalFin),
}

impl TryInto<ProposalInit> for ProposalPart {
Expand Down
54 changes: 29 additions & 25 deletions crates/papyrus_protobuf/src/converters/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,27 +192,6 @@ impl From<ProposalInit> for protobuf::ProposalInit {

auto_impl_into_and_try_from_vec_u8!(ProposalInit, protobuf::ProposalInit);

impl TryFrom<protobuf::TransactionBatch> for TransactionBatch {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::TransactionBatch) -> Result<Self, Self::Error> {
let transactions = value
.transactions
.into_iter()
.map(|tx| tx.try_into())
.collect::<Result<Vec<ConsensusTransaction>, ProtobufConversionError>>()?;
Ok(TransactionBatch { transactions })
}
}

impl From<TransactionBatch> for protobuf::TransactionBatch {
fn from(value: TransactionBatch) -> Self {
let transactions = value.transactions.into_iter().map(Into::into).collect();
protobuf::TransactionBatch { transactions }
}
}

auto_impl_into_and_try_from_vec_u8!(TransactionBatch, protobuf::TransactionBatch);

impl TryFrom<protobuf::BlockInfo> for BlockInfo {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::BlockInfo) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -268,6 +247,27 @@ impl From<BlockInfo> for protobuf::BlockInfo {

auto_impl_into_and_try_from_vec_u8!(BlockInfo, protobuf::BlockInfo);

impl TryFrom<protobuf::TransactionBatch> for TransactionBatch {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::TransactionBatch) -> Result<Self, Self::Error> {
let transactions = value
.transactions
.into_iter()
.map(|tx| tx.try_into())
.collect::<Result<Vec<ConsensusTransaction>, ProtobufConversionError>>()?;
Ok(TransactionBatch { transactions })
}
}

impl From<TransactionBatch> for protobuf::TransactionBatch {
fn from(value: TransactionBatch) -> Self {
let transactions = value.transactions.into_iter().map(Into::into).collect();
protobuf::TransactionBatch { transactions }
}
}

auto_impl_into_and_try_from_vec_u8!(TransactionBatch, protobuf::TransactionBatch);

impl TryFrom<protobuf::ProposalFin> for ProposalFin {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::ProposalFin) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -301,8 +301,9 @@ impl TryFrom<protobuf::ProposalPart> for ProposalPart {

match part {
Message::Init(init) => Ok(ProposalPart::Init(init.try_into()?)),
Message::Transactions(content) => Ok(ProposalPart::Transactions(content.try_into()?)),
Message::Fin(fin) => Ok(ProposalPart::Fin(fin.try_into()?)),
Message::BlockInfo(block_info) => Ok(ProposalPart::BlockInfo(block_info.try_into()?)),
Message::Transactions(content) => Ok(ProposalPart::Transactions(content.try_into()?)),
}
}
}
Expand All @@ -313,12 +314,15 @@ impl From<ProposalPart> for protobuf::ProposalPart {
ProposalPart::Init(init) => protobuf::ProposalPart {
message: Some(protobuf::proposal_part::Message::Init(init.into())),
},
ProposalPart::Transactions(content) => protobuf::ProposalPart {
message: Some(protobuf::proposal_part::Message::Transactions(content.into())),
},
ProposalPart::Fin(fin) => protobuf::ProposalPart {
message: Some(protobuf::proposal_part::Message::Fin(fin.into())),
},
ProposalPart::BlockInfo(block_info) => protobuf::ProposalPart {
message: Some(protobuf::proposal_part::Message::BlockInfo(block_info.into())),
},
ProposalPart::Transactions(content) => protobuf::ProposalPart {
message: Some(protobuf::proposal_part::Message::Transactions(content.into())),
},
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/papyrus_protobuf/src/converters/consensus_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use starknet_api::rpc_transaction::{
};

use crate::consensus::{
BlockInfo,
ProposalFin,
ProposalInit,
ProposalPart,
Expand Down Expand Up @@ -87,6 +88,17 @@ fn convert_proposal_init_to_vec_u8_and_back() {
assert_eq!(proposal_init, res_data);
}

#[test]
fn convert_block_info_to_vec_u8_and_back() {
let mut rng = get_rng();

let block_info = BlockInfo::get_test_instance(&mut rng);

let bytes_data: Vec<u8> = block_info.clone().into();
let res_data = BlockInfo::try_from(bytes_data).unwrap();
assert_eq!(block_info, res_data);
}

#[test]
fn convert_transaction_batch_to_vec_u8_and_back() {
let mut rng = get_rng();
Expand Down
15 changes: 14 additions & 1 deletion crates/papyrus_protobuf/src/converters/test_instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use rand::Rng;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::consensus_transaction::ConsensusTransaction;
use starknet_api::core::ContractAddress;
use starknet_api::data_availability::L1DataAvailabilityMode;

use super::ProtobufConversionError;
use crate::consensus::{
BlockInfo,
ProposalFin,
ProposalInit,
ProposalPart,
Expand Down Expand Up @@ -43,10 +45,21 @@ auto_impl_get_test_instance! {
pub struct TransactionBatch {
pub transactions: Vec<ConsensusTransaction>,
}
pub struct BlockInfo {
pub height: BlockNumber,
pub timestamp: u64,
pub builder: ContractAddress,
pub l1_da_mode: L1DataAvailabilityMode,
pub l2_gas_price_fri: u128,
pub l1_gas_price_wei: u128,
pub l1_data_gas_price_wei: u128,
pub eth_to_strk_rate: u64,
}
pub enum ProposalPart {
Init(ProposalInit) = 0,
Fin(ProposalFin) = 1,
Transactions(TransactionBatch) = 2,
BlockInfo(BlockInfo) = 2,
Transactions(TransactionBatch) = 3,
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ message ProposalPart {
oneof message {
ProposalInit init = 1;
ProposalFin fin = 2;
TransactionBatch transactions = 3;
BlockInfo block_info = 3;
TransactionBatch transactions = 4;
}
}

Expand Down
19 changes: 11 additions & 8 deletions crates/starknet_integration_tests/tests/end_to_end_flow_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ async fn listen_to_broadcasted_messages(
StreamMessageBody::Content(ProposalPart::Init(init)) => {
panic!("Unexpected init: {:?}", init)
}
StreamMessageBody::Content(ProposalPart::Fin(proposal_fin)) => {
assert_eq!(
proposal_fin, expected_proposal_fin,
"Unexpected fin message: {:?}, expected: {:?}",
proposal_fin, expected_proposal_fin
);
got_proposal_fin = true;
}
StreamMessageBody::Content(ProposalPart::BlockInfo(_)) => {
// TODO(Asmaa): Add validation for block info.
}
StreamMessageBody::Content(ProposalPart::Transactions(transactions)) => {
// TODO(Arni): add calculate_transaction_hash to consensus transaction and use it
// here.
Expand All @@ -242,14 +253,6 @@ async fn listen_to_broadcasted_messages(
}
}));
}
StreamMessageBody::Content(ProposalPart::Fin(proposal_fin)) => {
assert_eq!(
proposal_fin, expected_proposal_fin,
"Unexpected fin message: {:?}, expected: {:?}",
proposal_fin, expected_proposal_fin
);
got_proposal_fin = true;
}
StreamMessageBody::Fin => {
got_channel_fin = true;
}
Expand Down

0 comments on commit 87cbbc9

Please sign in to comment.