Skip to content

Commit

Permalink
fix: fix hooker method in starknet messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Dec 17, 2023
1 parent 5dd0b42 commit 4d8c089
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions crates/katana/core/src/service/messaging/starknet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// SOLIS
use tokio::sync::RwLock as AsyncRwLock;
use crate::hooker::KatanaHooker;
//

use std::collections::HashMap;
use std::sync::Arc;

Expand All @@ -16,8 +21,6 @@ use starknet::signers::{LocalWallet, SigningKey};
use tracing::{debug, error, trace, warn};
use url::Url;

use crate::hooker::KatanaHooker;

use super::{Error, MessagingConfig, Messenger, MessengerResult, LOG_TARGET};

/// As messaging in starknet is only possible with EthAddress in the `to_address`
Expand Down Expand Up @@ -197,13 +200,12 @@ impl Messenger for StarknetMessaging {

let mut l1_handler_txs: Vec<L1HandlerTx> = vec![];

for (block_number, block_events) in self
.fetch_events(BlockId::Number(from_block), BlockId::Number(to_block))
let events = self.fetch_events(BlockId::Number(from_block), BlockId::Number(to_block))
.await
.map_err(|_| Error::SendError)
.unwrap()
.iter()
{
.unwrap();

for (block_number, block_events) in events.iter() {
debug!(
target: LOG_TARGET,
"Converting events of block {} into L1HandlerTx ({} events)",
Expand All @@ -212,25 +214,18 @@ impl Messenger for StarknetMessaging {
);

for e in block_events.iter() {
if let Ok((tx, info)) = l1_handler_tx_from_event(e, chain_id) {
let is_message_accepted = self
.hooker
.read()
.await
.verify_message_to_appchain(
info.from_address,
info.to_address,
info.selector,
)
.await;

if is_message_accepted {
l1_handler_txs.push(tx)
} else {
warn!(target: LOG_TARGET, "Message to appchain rejected by hooker: from:{:?} to:{:?} selector:{:?}",
info.from_address,
info.to_address,
info.selector);
if let Ok(tx) = l1_handler_tx_from_event(e, chain_id) {
if let Ok((from, to, selector)) = info_from_event(e) {
let is_message_accepted = self
.hooker
.read()
.await
.verify_message_to_appchain(from, to, selector)
.await;

if is_message_accepted {
l1_handler_txs.push(tx)
}
}
}
}
Expand Down Expand Up @@ -382,6 +377,27 @@ fn l1_handler_tx_from_event(event: &EmittedEvent, chain_id: FieldElement) -> Res
})
}

fn info_from_event(event: &EmittedEvent) -> Result<(FieldElement, FieldElement, FieldElement)> {
if event.keys[0] != selector!("MessageSentToAppchain") {
debug!(
target: LOG_TARGET,
"Event with key {:?} can't be converted into L1HandlerTx", event.keys[0],
);
return Err(Error::GatherError.into());
}

if event.keys.len() != 4 || event.data.len() < 2 {
error!(target: LOG_TARGET, "Event MessageSentToAppchain is not well formatted");
}

// See contrat appchain_messaging.cairo for MessageSentToAppchain event.
let from_address = event.keys[2];
let to_address = event.keys[3];
let entry_point_selector = event.data[0];

Ok((from_address, to_address, entry_point_selector))
}

#[cfg(test)]
mod tests {

Expand Down

0 comments on commit 4d8c089

Please sign in to comment.