Skip to content

Commit

Permalink
feat: contract_call event capture (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoyan committed Sep 9, 2024
1 parent c1bc456 commit 3760ec5
Show file tree
Hide file tree
Showing 9 changed files with 875 additions and 72 deletions.
1 change: 1 addition & 0 deletions ampd/src/handlers/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use connection_router_api::ChainName;
use itertools::Itertools;
use router_api::ChainName;
use serde::de::{self, Deserializer};
Expand Down
73 changes: 73 additions & 0 deletions ampd/src/handlers/starknet_verify_msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use async_trait::async_trait;
use axelar_wasm_std::voting::{PollId, Vote};
use serde::Deserialize;
use tokio::sync::watch::Receiver;

use crate::event_processor::EventHandler;
use crate::handlers::errors::Error;
use crate::queue::queued_broadcaster::BroadcasterClient;
use crate::starknet::verifier::MessageVerifier;
use crate::types::{Hash, TMAddress};

type Result<T> = error_stack::Result<T, Error>;

#[derive(Deserialize, Debug)]
pub struct Message {
pub tx_id: String,
pub event_index: u64,
pub destination_address: String,
pub destination_chain: String,
pub source_address: String,
pub payload_hash: Hash,
}

pub struct Handler<V, B>
where
V: MessageVerifier,
B: BroadcasterClient,
{
worker: TMAddress,
voting_verifier: TMAddress,
msg_verifier: V,
broadcast_client: B,
latest_block_height: Receiver<u64>,
}

impl<V, B> Handler<V, B>
where
V: MessageVerifier + Send + Sync,
B: BroadcasterClient,
{
pub fn new(
worker: TMAddress,
voting_verifier: TMAddress,
msg_verifier: V,
broadcast_client: B,
latest_block_height: Receiver<u64>,
) -> Self {
Self {
worker,
voting_verifier,
msg_verifier,
broadcast_client,
latest_block_height,
}
}

async fn broadcast_votes(&self, poll_id: PollId, votes: Vec<Vote>) -> Result<()> {
unimplemented!()
}
}

#[async_trait]
impl<V, B> EventHandler for Handler<V, B>
where
V: MessageVerifier + Send + Sync,
B: BroadcasterClient + Send + Sync,
{
type Err = Error;

async fn handle(&self, event: &events::Event) -> Result<()> {
unimplemented!()
}
}
15 changes: 15 additions & 0 deletions ampd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@ where
),
event_processor_config.clone(),
),
handlers::config::Config::StarknetMsgVerifier {
cosmwasm_contract,
rpc_url,
rpc_timeout: _,
} => self.create_handler_task(
"starknet-msg-verifier",
handlers::starknet_verify_msg::Handler::new(
worker.clone(),
cosmwasm_contract,
starknet::verifier::RPCMessageVerifier::new(rpc_url.as_str()),
self.broadcaster.client(),
self.block_height_monitor.latest_block_height(),
),
stream_timeout,
),
};
self.event_processor = self.event_processor.add_task(task);
}
Expand Down
74 changes: 23 additions & 51 deletions ampd/src/starknet/events/contract_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ mod tests {
use std::str::FromStr;

use ethers::types::H256;
use starknet_core::types::FieldElement;
use starknet_core::types::{FieldElement, FromStrError};
use starknet_core::utils::starknet_keccak;

use super::ContractCallEvent;
Expand Down Expand Up @@ -217,6 +217,27 @@ mod tests {
}

fn get_dummy_event() -> starknet_core::types::Event {
// "hello" as payload
// "hello" as destination address
// "some_from_address" as source address
// "destination_chain" as destination_chain
let event_data: Result<Vec<FieldElement>, FromStrError> = vec![
"0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca",
"0x0000000000000000000000000000000000000000000000000000000000000000", // 0 datas
"0x00000000000000000000000000000000000000000000000000000068656c6c6f", // "hello"
"0x0000000000000000000000000000000000000000000000000000000000000005", // 5 bytes
"0x0000000000000000000000000000000056d9517b9c948127319a09a7a36deac8", // keccak256(hello)
"0x000000000000000000000000000000001c8aff950685c2ed4bc3174f3472287b",
"0x0000000000000000000000000000000000000000000000000000000000000005", // 5 bytes
"0x0000000000000000000000000000000000000000000000000000000000000068", // h
"0x0000000000000000000000000000000000000000000000000000000000000065", // e
"0x000000000000000000000000000000000000000000000000000000000000006c", // l
"0x000000000000000000000000000000000000000000000000000000000000006c", // l
"0x000000000000000000000000000000000000000000000000000000000000006f", // o
]
.into_iter()
.map(FieldElement::from_str)
.collect();
starknet_core::types::Event {
// I think it's a pedersen hash, but we don't use it, so any value should do
from_address: starknet_keccak("some_from_address".as_bytes()),
Expand All @@ -227,56 +248,7 @@ mod tests {
)
.unwrap(),
],
data: vec![
FieldElement::from_str(
"0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca",
)
.unwrap(),
FieldElement::from_str(
"0x0000000000000000000000000000000000000000000000000000000000000000",
)
.unwrap(),
FieldElement::from_str(
"0x00000000000000000000000000000000000000000000000000000068656c6c6f",
)
.unwrap(),
FieldElement::from_str(
"0x0000000000000000000000000000000000000000000000000000000000000005",
)
.unwrap(),
FieldElement::from_str(
"0x0000000000000000000000000000000056d9517b9c948127319a09a7a36deac8",
)
.unwrap(),
FieldElement::from_str(
"0x000000000000000000000000000000001c8aff950685c2ed4bc3174f3472287b",
)
.unwrap(),
FieldElement::from_str(
"0x0000000000000000000000000000000000000000000000000000000000000005",
)
.unwrap(),
FieldElement::from_str(
"0x0000000000000000000000000000000000000000000000000000000000000068",
)
.unwrap(),
FieldElement::from_str(
"0x0000000000000000000000000000000000000000000000000000000000000065",
)
.unwrap(),
FieldElement::from_str(
"0x000000000000000000000000000000000000000000000000000000000000006c",
)
.unwrap(),
FieldElement::from_str(
"0x000000000000000000000000000000000000000000000000000000000000006c",
)
.unwrap(),
FieldElement::from_str(
"0x000000000000000000000000000000000000000000000000000000000000006f",
)
.unwrap(),
],
data: event_data.unwrap(),
}
}
}
Loading

0 comments on commit 3760ec5

Please sign in to comment.