Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add events and messages checks #30

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
22 changes: 22 additions & 0 deletions rpc-state-reader/src/blockifier_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,21 @@ mod tests {
RpcChain::MainNet
=> ignore
)]
#[test_case(
"0xaa8c451a3f231f3eeba8bc48236eecd5fe42cc1513f175c7d52b41d35ecba2",
657074, // real block 657075
RpcChain::MainNet
)]
#[test_case(
"0x2101910b5baddb23888337377872e08e01d75070486f010373b48d7c8366f11",
657069, // real block 657070
RpcChain::MainNet
)]
#[test_case(
"0x7b41f9942a4d4641e6118a00e0629880845215e4445221139b5d32fa3ab7766",
657043, // real block 657044
RpcChain::MainNet
)]
#[test_case(
"0x73ef9cde09f005ff6f411de510ecad4cdcf6c4d0dfc59137cff34a4fc74dfd",
654000, // real block 654001
Expand Down Expand Up @@ -642,6 +657,13 @@ mod tests {
internal_calls: value.inner_calls.iter().map(|ci| ci.into()).collect(),
// We don't have the revert reason string in the trace so we just make sure it doesn't revert
revert_reason: value.execution.failed.then_some("Default String".into()),
events: value.execution.events.iter().map(|e| e.into()).collect(),
messages: value
.execution
.l2_to_l1_messages
.iter()
.map(|llm| llm.into())
.collect(),
}
}
}
Expand Down
77 changes: 74 additions & 3 deletions rpc-state-reader/src/rpc_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use blockifier::blockifier::block::GasPrices;
use blockifier::{
blockifier::block::GasPrices,
execution::call_info::{OrderedEvent, OrderedL2ToL1Message},
};
use cairo_vm::vm::runners::{
builtin_runner::{
BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, KECCAK_BUILTIN_NAME,
Expand All @@ -14,10 +17,12 @@ use serde_json::json;
use starknet::core::types::ContractClass as SNContractClass;
use starknet_api::{
block::{BlockNumber, BlockTimestamp},
core::{ChainId, ClassHash, ContractAddress},
core::{ChainId, ClassHash, ContractAddress, EthAddress},
hash::{StarkFelt, StarkHash},
state::StorageKey,
transaction::{Transaction as SNTransaction, TransactionHash},
transaction::{
EventData, EventKey, L2ToL1Payload, Transaction as SNTransaction, TransactionHash,
},
};
use std::{collections::HashMap, env, fmt::Display, num::NonZeroU128};

Expand Down Expand Up @@ -95,6 +100,40 @@ pub enum BlockValue {
Hash(StarkHash),
}

#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
pub struct Event {
pub order: usize,
pub keys: Vec<EventKey>,
pub data: EventData,
}

#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
pub struct L2ToL1Msg {
pub order: usize,
pub to_address: EthAddress,
pub payload: L2ToL1Payload,
}

impl From<&OrderedL2ToL1Message> for L2ToL1Msg {
fn from(value: &OrderedL2ToL1Message) -> Self {
Self {
order: value.order,
to_address: value.message.to_address,
payload: value.message.payload.clone(),
}
}
}

impl From<&OrderedEvent> for Event {
fn from(value: &OrderedEvent) -> Self {
Self {
order: value.order,
keys: value.event.keys.clone(),
data: value.event.data.clone(),
}
}
}

impl From<BlockTag> for BlockValue {
fn from(value: BlockTag) -> Self {
BlockValue::Tag(value)
Expand Down Expand Up @@ -167,6 +206,8 @@ pub struct RpcCallInfo {
pub calldata: Option<Vec<StarkFelt>>,
pub internal_calls: Vec<RpcCallInfo>,
pub revert_reason: Option<String>,
pub events: Vec<Event>,
pub messages: Vec<L2ToL1Msg>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -303,11 +344,41 @@ impl<'de> Deserialize<'de> for RpcCallInfo {
.push(serde_json::from_value(call.clone()).map_err(serde::de::Error::custom)?);
}

let events_value = value
.get("events")
.ok_or(serde::de::Error::custom(
RpcStateError::MissingRpcResponseField("events".to_string()),
))?
.clone();
let mut events = vec![];

for event in events_value.as_array().ok_or(serde::de::Error::custom(
RpcStateError::RpcResponseWrongType("events".to_string()),
))? {
events.push(serde_json::from_value(event.clone()).map_err(serde::de::Error::custom)?)
}

let messages_value = value
.get("messages")
.ok_or(serde::de::Error::custom(
RpcStateError::MissingRpcResponseField("messages".to_string()),
))?
.clone();
let mut messages = vec![];

for msg in messages_value.as_array().ok_or(serde::de::Error::custom(
RpcStateError::RpcResponseWrongType("messages".to_string()),
))? {
messages.push(serde_json::from_value(msg.clone()).map_err(serde::de::Error::custom)?)
}

Ok(RpcCallInfo {
retdata,
calldata,
internal_calls,
revert_reason: None,
events,
messages,
})
}
}
Expand Down
Loading