Skip to content

Commit

Permalink
feat(network): add conversion between protobuf Event and sn api (#2102)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaastarkware authored Jun 13, 2024
1 parent f045912 commit 719f7d2
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/papyrus_protobuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn main() -> Result<()> {
prost_build::compile_protos(
&[
"src/proto/p2p/proto/class.proto",
"src/proto/p2p/proto/event.proto",
"src/proto/p2p/proto/header.proto",
"src/proto/p2p/proto/state.proto",
"src/proto/p2p/proto/transaction.proto",
Expand Down
91 changes: 91 additions & 0 deletions crates/papyrus_protobuf/src/converters/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use prost::Message;
use starknet_api::core::{ContractAddress, PatriciaKey};
use starknet_api::transaction::{Event, EventContent, EventData, EventKey, TransactionHash};
use starknet_types_core::felt::Felt;

use super::ProtobufConversionError;
use crate::sync::DataOrFin;
use crate::{auto_impl_into_and_try_from_vec_u8, protobuf};

impl TryFrom<protobuf::EventsResponse> for DataOrFin<(Event, TransactionHash)> {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::EventsResponse) -> Result<Self, Self::Error> {
match value.event_message {
Some(protobuf::events_response::EventMessage::Event(event)) => {
Ok(Self(Some(event.try_into()?)))
}
Some(protobuf::events_response::EventMessage::Fin(_)) => Ok(Self(None)),
None => Err(ProtobufConversionError::MissingField {
field_description: "EventsResponse::event_message",
}),
}
}
}
impl From<DataOrFin<(Event, TransactionHash)>> for protobuf::EventsResponse {
fn from(value: DataOrFin<(Event, TransactionHash)>) -> Self {
match value.0 {
Some(event_transaction_hash) => protobuf::EventsResponse {
event_message: Some(protobuf::events_response::EventMessage::Event(
event_transaction_hash.into(),
)),
},
None => protobuf::EventsResponse {
event_message: Some(protobuf::events_response::EventMessage::Fin(protobuf::Fin {})),
},
}
}
}

auto_impl_into_and_try_from_vec_u8!(DataOrFin<(Event, TransactionHash)>, protobuf::EventsResponse);

impl TryFrom<protobuf::Event> for (Event, TransactionHash) {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::Event) -> Result<Self, Self::Error> {
let transaction_hash = TransactionHash(
value
.transaction_hash
.ok_or(ProtobufConversionError::MissingField {
field_description: "Event::transaction_hash",
})?
.try_into()?,
);

let from_address_felt =
Felt::try_from(value.from_address.ok_or(ProtobufConversionError::MissingField {
field_description: "Event::from_address",
})?)?;
let from_address =
ContractAddress(PatriciaKey::try_from(from_address_felt).map_err(|_| {
ProtobufConversionError::OutOfRangeValue {
type_description: "PatriciaKey",
value_as_str: format!("{from_address_felt:?}"),
}
})?);

let keys = value
.keys
.into_iter()
.map(Felt::try_from)
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.map(EventKey)
.collect();

let data =
EventData(value.data.into_iter().map(Felt::try_from).collect::<Result<Vec<_>, _>>()?);

Ok((Event { from_address, content: EventContent { keys, data } }, transaction_hash))
}
}

impl From<(Event, TransactionHash)> for protobuf::Event {
fn from(value: (Event, TransactionHash)) -> Self {
let (event, transaction_hash) = value;
let transaction_hash = Some(transaction_hash.0.into());
let from_address = Some(Felt::from(event.from_address).into());
let keys = event.content.keys.into_iter().map(|key| key.0.into()).collect();
let data =
event.content.data.0.into_iter().map(protobuf::Felt252::from).collect::<Vec<_>>();
Self { transaction_hash, from_address, keys, data }
}
}
1 change: 1 addition & 0 deletions crates/papyrus_protobuf/src/converters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod class;
pub mod common;
pub mod consensus;
mod event;
mod header;
mod receipt;
// TODO(shahak): Internalize this once network doesn't depend on protobuf.
Expand Down
21 changes: 21 additions & 0 deletions crates/papyrus_protobuf/src/proto/p2p/proto/event.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";
import "p2p/proto/common.proto";

message Event {
Hash transaction_hash = 1;
Felt252 from_address = 3;
repeated Felt252 keys = 4;
repeated Felt252 data = 5;
}

message EventsRequest {
Iteration iteration = 1;
}

// Responses are sent ordered by the order given in the request.
message EventsResponse {
oneof event_message {
Event event = 1;
Fin fin = 2; // Fin is sent after the peer sent all the data or when it encountered a block that it doesn't have its events.
}
}

0 comments on commit 719f7d2

Please sign in to comment.