Skip to content

Commit

Permalink
remove enum
Browse files Browse the repository at this point in the history
  • Loading branch information
puhtaytow committed Dec 1, 2024
1 parent 61387a7 commit 39f5894
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 52 deletions.
13 changes: 8 additions & 5 deletions ampd/src/handlers/starknet_verify_verifier_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use voting_verifier::msg::ExecuteMsg;

use crate::event_processor::EventHandler;
use crate::handlers::errors::Error;
use crate::starknet::json_rpc::{EventType, StarknetClient};
use crate::starknet::verifier::{verify_event, verify_verifier_set};
use crate::starknet::json_rpc::StarknetClient;
use crate::starknet::verifier::verify_verifier_set;
use crate::types::TMAddress;

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -123,7 +123,9 @@ where
// TODO: it should be enum handled here with two variants: ContractCallEvent or SignersRotatedEvent
let transaction_response = self
.rpc_client
.get_event_by_hash_with_enum(Felt::from_bytes_be(&verifier_set.message_id.tx_hash))
.get_event_by_hash_signers_rotated(Felt::from_bytes_be(
&verifier_set.message_id.tx_hash,
))
.await
.unwrap();

Expand All @@ -135,8 +137,9 @@ where
.in_scope(|| {
info!("ready to verify verifier set in poll",);

let vote = transaction_response
.map_or(Vote::NotFound, |tx_receipt| verify_event(&tx_receipt.1));
let vote = transaction_response.map_or(Vote::NotFound, |tx_receipt| {
verify_verifier_set(&tx_receipt.1, &verifier_set, &source_gateway_address)
});

info!(
vote = vote.as_value(),
Expand Down
56 changes: 23 additions & 33 deletions ampd/src/starknet/json_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,11 @@ use starknet_core::types::{ExecutionResult, Felt, FromStrError, TransactionRecei
use starknet_providers::jsonrpc::JsonRpcTransport;
use starknet_providers::{JsonRpcClient, Provider, ProviderError};
use starknet_types::events::contract_call::ContractCallEvent;
use starknet_types::events::signers_rotated::SignersRotated;
use starknet_types::events::signers_rotated::SignersRotatedEvent;
use thiserror::Error;

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

/// Represents different types of events that can be emitted by Starknet contracts
///
/// # Variants
///
/// * `ContractCall` - Contains a contract call event with details about the call
/// * `SignersRotated` - Contains information about signers being rotated/updated
#[derive(Debug)]

pub enum EventType {
ContractCall(ContractCallEvent),
SignersRotated(SignersRotated),
}

#[derive(Debug, Error)]
pub enum StarknetClientError {
#[error(transparent)]
Expand Down Expand Up @@ -73,9 +60,12 @@ pub trait StarknetClient {
/// Returns a tuple `(tx_hash, event)` or a `StarknetClientError`.
async fn get_event_by_hash(&self, tx_hash: Felt) -> Result<Option<(Felt, ContractCallEvent)>>;

/// this one should replace the above
async fn get_event_by_hash_with_enum(&self, tx_hash: Felt)
-> Result<Option<(Felt, EventType)>>;
/// Attempts to fetch a SignersRotated event, by a given `tx_hash`.
/// Returns a tuple `(tx_hash, event)` or a `StarknetClientError`.
async fn get_event_by_hash_signers_rotated(
&self,
tx_hash: Felt,
) -> Result<Option<(Felt, SignersRotatedEvent)>>;
}

#[async_trait]
Expand Down Expand Up @@ -125,10 +115,10 @@ where
Ok(event)
}

async fn get_event_by_hash_with_enum(
async fn get_event_by_hash_signers_rotated(
&self,
tx_hash: Felt,
) -> Result<Option<(Felt, EventType)>> {
) -> Result<Option<(Felt, SignersRotatedEvent)>> {
let receipt_with_block_info = self
.client
.get_transaction_receipt(tx_hash)
Expand All @@ -139,20 +129,20 @@ where
return Err(Report::new(StarknetClientError::UnsuccessfulTx));
}

let event: Option<(Felt, EventType)> = match receipt_with_block_info.receipt {
TransactionReceipt::Invoke(tx) => tx
.events
.iter()
.filter_map(|e| {
if let Ok(cce) = ContractCallEvent::try_from(e.clone()) {
Some((tx.transaction_hash, EventType::ContractCall(cce)))
} else if let Ok(sre) = SignersRotated::try_from(e.clone()) {
Some((tx.transaction_hash, EventType::SignersRotated(sre)))
} else {
None
}
})
.next(),
let event: Option<(Felt, SignersRotatedEvent)> = match receipt_with_block_info.receipt {
TransactionReceipt::Invoke(tx) => {
// NOTE: There should be only one ContractCall event per gateway tx
tx.events
.iter()
.filter_map(|e| {
if let Ok(sre) = SignersRotatedEvent::try_from(e.clone()) {
Some((tx.transaction_hash, sre))
} else {
None
}
})
.next()
}
TransactionReceipt::L1Handler(_) => None,
TransactionReceipt::Declare(_) => None,
TransactionReceipt::Deploy(_) => None,
Expand Down
13 changes: 2 additions & 11 deletions ampd/src/starknet/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@ use prost_types::Field;
use starknet_core::types::{Felt, TransactionReceipt};
use starknet_core::utils::CairoShortStringToFeltError;
use starknet_types::events::contract_call::ContractCallEvent;
use starknet_types::events::signers_rotated::SignersRotated;
use starknet_types::events::signers_rotated::SignersRotatedEvent;

use super::json_rpc::EventType;
use crate::handlers::starknet_verify_msg::Message;
use crate::handlers::starknet_verify_verifier_set::VerifierSetConfirmation;

/// Entrypoint for verifying events from Starknet.
pub fn verify_event(event: &EventType) -> Vote {
match event {
EventType::ContractCall(_) => Vote::FailedOnChain,
EventType::SignersRotated(_) => Vote::SucceededOnChain,
}
}

/// Attempts to fetch the tx provided in `axl_msg.tx_id`.
/// If successful, extracts and parses the ContractCall event
/// and compares it to the message from the relayer (via PollStarted event).
Expand Down Expand Up @@ -46,7 +37,7 @@ impl PartialEq<Message> for ContractCallEvent {

// Verifies that the event data matches the verifier set confirmation data
pub fn verify_verifier_set(
event: &SignersRotated,
event: &SignersRotatedEvent,
confirmation: &VerifierSetConfirmation,
source_gateway_address: &str,
) -> Vote {
Expand Down
6 changes: 3 additions & 3 deletions packages/starknet-types/src/events/signers_rotated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub struct WeightedSigners {

/// Represents a Starknet SignersRotated event
#[derive(Debug, Clone)]
pub struct SignersRotated {
pub struct SignersRotatedEvent {
/// The epoch number when this rotation occurred
pub epoch: u64,
/// The hash of the new signers
Expand All @@ -111,7 +111,7 @@ pub struct SignersRotated {
pub signers: WeightedSigners,
}

impl TryFrom<starknet_core::types::Event> for SignersRotated {
impl TryFrom<starknet_core::types::Event> for SignersRotatedEvent {
type Error = SignersRotatedErrors;

/// Attempts to convert a Starknet event to a SignersRotated event
Expand Down Expand Up @@ -240,7 +240,7 @@ impl TryFrom<starknet_core::types::Event> for SignersRotated {
nonce[..16].copy_from_slice(&msb[16..]);
nonce[16..].copy_from_slice(&lsb[16..]);

Ok(SignersRotated {
Ok(SignersRotatedEvent {
epoch,
signers_hash,
signers: WeightedSigners {
Expand Down

0 comments on commit 39f5894

Please sign in to comment.