-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network): add conversion between protobuf Event and sn api (#2102)
- Loading branch information
1 parent
f045912
commit 719f7d2
Showing
4 changed files
with
114 additions
and
0 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
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 } | ||
} | ||
} |
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
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. | ||
} | ||
} |