-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement poll-based event subscription for Starknet (#221)
* Add receipt block info to MessageResponse * Managed to poll SendPacket events successfully * Add StarknetEventEncoding field * Implement HasEncoding<AsStarknetEvent> for StarknetChain * Implementing CanPollStarknetIbcEvents * Finish draft for CanPollStarknetIbcEvents * Define CanQueryBlockEvents component * Drafting Starknet event stream * Done initial implementation of CanCreateStarknetSubscription * Multiplex event subscription * Add wait and retry block events querier * Implement CanQueryBlockEvents and CanCreateStarknetSubscription for StarknetChain * Fix clippy * Remove sleep in test * Add address argument for get_block_events * Rename providers * Implement StarknetEvent::from_emitted_event * Implement GetStarknetBlockEvents
- Loading branch information
1 parent
0a14290
commit 4c63248
Showing
27 changed files
with
537 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
relayer/crates/cairo-encoding-components/src/types/as_starknet_event.rs
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 @@ | ||
pub struct AsStarknetEvent; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod as_felt; | ||
pub mod as_starknet_event; | ||
pub mod nat; |
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
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 |
---|---|---|
|
@@ -94,6 +94,5 @@ cgp_preset! { | |
PacketDstChannelIdGetterComponent, | ||
]: | ||
ReadPacketDstStarknetFields, | ||
|
||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
relayer/crates/starknet-chain-components/src/impls/queries/block_events/default.rs
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,38 @@ | ||
use cgp::prelude::CanRaiseAsyncError; | ||
use hermes_chain_components::traits::queries::chain_status::CanQueryChainHeight; | ||
use hermes_chain_components::traits::types::event::HasEventType; | ||
use hermes_chain_components::traits::types::height::HasHeightType; | ||
use hermes_chain_type_components::traits::types::address::HasAddressType; | ||
use hermes_runtime_components::traits::runtime::HasRuntime; | ||
use hermes_runtime_components::traits::sleep::CanSleep; | ||
use starknet::core::types::Felt; | ||
use starknet::providers::ProviderError; | ||
|
||
use crate::impls::queries::block_events::get::GetStarknetBlockEvents; | ||
use crate::impls::queries::block_events::retry::RetryQueryBlockEvents; | ||
use crate::impls::queries::block_events::wait::WaitBlockHeightAndQueryEvents; | ||
use crate::traits::provider::HasStarknetProvider; | ||
use crate::traits::queries::block_events::BlockEventsQuerier; | ||
use crate::types::event::StarknetEvent; | ||
|
||
pub struct DefaultQueryBlockEvents; | ||
|
||
impl<Chain> BlockEventsQuerier<Chain> for DefaultQueryBlockEvents | ||
where | ||
Chain: HasRuntime | ||
+ HasHeightType<Height = u64> | ||
+ HasEventType<Event = StarknetEvent> | ||
+ HasAddressType<Address = Felt> | ||
+ HasStarknetProvider | ||
+ CanQueryChainHeight | ||
+ CanRaiseAsyncError<ProviderError>, | ||
Chain::Runtime: CanSleep, | ||
{ | ||
async fn query_block_events( | ||
chain: &Chain, | ||
height: &Chain::Height, | ||
address: &Chain::Address, | ||
) -> Result<Vec<Chain::Event>, Chain::Error> { | ||
<RetryQueryBlockEvents<WaitBlockHeightAndQueryEvents<GetStarknetBlockEvents>>>::query_block_events(chain, height, address).await | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
relayer/crates/starknet-chain-components/src/impls/queries/block_events/get.rs
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,51 @@ | ||
use cgp::prelude::*; | ||
use hermes_chain_components::traits::types::event::HasEventType; | ||
use hermes_chain_components::traits::types::height::HasHeightType; | ||
use hermes_chain_type_components::traits::types::address::HasAddressType; | ||
use starknet::core::types::{BlockId, EventFilter, Felt}; | ||
use starknet::providers::{Provider, ProviderError}; | ||
|
||
use crate::traits::provider::HasStarknetProvider; | ||
use crate::traits::queries::block_events::BlockEventsQuerier; | ||
use crate::types::event::StarknetEvent; | ||
|
||
pub struct GetStarknetBlockEvents; | ||
|
||
impl<Chain> BlockEventsQuerier<Chain> for GetStarknetBlockEvents | ||
where | ||
Chain: HasHeightType<Height = u64> | ||
+ HasEventType<Event = StarknetEvent> | ||
+ HasAddressType<Address = Felt> | ||
+ HasStarknetProvider | ||
+ CanRaiseAsyncError<ProviderError>, | ||
{ | ||
async fn query_block_events( | ||
chain: &Chain, | ||
height: &u64, | ||
address: &Felt, | ||
) -> Result<Vec<StarknetEvent>, Chain::Error> { | ||
let provider = chain.provider(); | ||
|
||
let raw_events = provider | ||
.get_events( | ||
EventFilter { | ||
from_block: Some(BlockId::Number(*height)), | ||
to_block: Some(BlockId::Number(*height)), | ||
address: Some(*address), | ||
keys: None, | ||
}, | ||
None, | ||
1000, | ||
) | ||
.await | ||
.map_err(Chain::raise_error)?; | ||
|
||
let events = raw_events | ||
.events | ||
.into_iter() | ||
.map(StarknetEvent::from) | ||
.collect(); | ||
|
||
Ok(events) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
relayer/crates/starknet-chain-components/src/impls/queries/block_events/mod.rs
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,5 @@ | ||
pub mod default; | ||
pub mod get; | ||
pub mod retry; | ||
pub mod traces; | ||
pub mod wait; |
41 changes: 41 additions & 0 deletions
41
relayer/crates/starknet-chain-components/src/impls/queries/block_events/retry.rs
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,41 @@ | ||
use core::marker::PhantomData; | ||
use core::time::Duration; | ||
|
||
use cgp::prelude::HasAsyncErrorType; | ||
use hermes_chain_components::traits::types::event::HasEventType; | ||
use hermes_chain_components::traits::types::height::HasHeightType; | ||
use hermes_chain_type_components::traits::types::address::HasAddressType; | ||
use hermes_runtime_components::traits::runtime::HasRuntime; | ||
use hermes_runtime_components::traits::sleep::CanSleep; | ||
|
||
use crate::traits::queries::block_events::BlockEventsQuerier; | ||
|
||
pub struct RetryQueryBlockEvents<InQuerier>(pub PhantomData<InQuerier>); | ||
|
||
impl<Chain, InQuerier> BlockEventsQuerier<Chain> for RetryQueryBlockEvents<InQuerier> | ||
where | ||
Chain: HasRuntime + HasHeightType + HasAddressType + HasEventType + HasAsyncErrorType, | ||
InQuerier: BlockEventsQuerier<Chain>, | ||
Chain::Runtime: CanSleep, | ||
{ | ||
async fn query_block_events( | ||
chain: &Chain, | ||
height: &Chain::Height, | ||
address: &Chain::Address, | ||
) -> Result<Vec<Chain::Event>, Chain::Error> { | ||
let runtime = chain.runtime(); | ||
let mut sleep_time = Duration::from_millis(500); | ||
|
||
for _ in 0..5 { | ||
let res = InQuerier::query_block_events(chain, height, address).await; | ||
if let Ok(events) = res { | ||
return Ok(events); | ||
} | ||
|
||
runtime.sleep(sleep_time).await; | ||
sleep_time *= 2; | ||
} | ||
|
||
InQuerier::query_block_events(chain, height, address).await | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
relayer/crates/starknet-chain-components/src/impls/queries/block_events/traces.rs
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,55 @@ | ||
use cgp::prelude::*; | ||
use hermes_chain_components::traits::types::event::HasEventType; | ||
use hermes_chain_components::traits::types::height::HasHeightType; | ||
use hermes_chain_type_components::traits::types::address::HasAddressType; | ||
use starknet::core::types::{BlockId, ExecuteInvocation, Felt, TransactionTrace}; | ||
use starknet::providers::{Provider, ProviderError}; | ||
|
||
use crate::impls::send_message::extract_events_from_function_invocation; | ||
use crate::traits::provider::HasStarknetProvider; | ||
use crate::traits::queries::block_events::BlockEventsQuerier; | ||
use crate::types::event::StarknetEvent; | ||
|
||
pub struct QueryStarknetBlockEventsFromTraces; | ||
|
||
impl<Chain> BlockEventsQuerier<Chain> for QueryStarknetBlockEventsFromTraces | ||
where | ||
Chain: HasHeightType<Height = u64> | ||
+ HasEventType<Event = StarknetEvent> | ||
+ HasAddressType<Address = Felt> | ||
+ HasStarknetProvider | ||
+ CanRaiseAsyncError<ProviderError>, | ||
{ | ||
async fn query_block_events( | ||
chain: &Chain, | ||
height: &u64, | ||
address: &Felt, | ||
) -> Result<Vec<StarknetEvent>, Chain::Error> { | ||
let provider = chain.provider(); | ||
|
||
let traces = provider | ||
.trace_block_transactions(BlockId::Number(*height)) | ||
.await | ||
.map_err(Chain::raise_error)?; | ||
|
||
let events: Vec<StarknetEvent> = traces | ||
.into_iter() | ||
.filter_map(|trace| match trace.trace_root { | ||
TransactionTrace::Invoke(invoke) => match invoke.execute_invocation { | ||
ExecuteInvocation::Success(invoke) => { | ||
if &invoke.contract_address == address { | ||
Some(extract_events_from_function_invocation(invoke)) | ||
} else { | ||
None | ||
} | ||
} | ||
_ => None, | ||
}, | ||
_ => None, | ||
}) | ||
.flatten() | ||
.collect(); | ||
|
||
Ok(events) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
relayer/crates/starknet-chain-components/src/impls/queries/block_events/wait.rs
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,38 @@ | ||
use core::marker::PhantomData; | ||
use core::time::Duration; | ||
|
||
use hermes_chain_components::traits::queries::chain_status::CanQueryChainHeight; | ||
use hermes_chain_components::traits::types::event::HasEventType; | ||
use hermes_chain_type_components::traits::types::address::HasAddressType; | ||
use hermes_runtime_components::traits::runtime::HasRuntime; | ||
use hermes_runtime_components::traits::sleep::CanSleep; | ||
|
||
use crate::traits::queries::block_events::BlockEventsQuerier; | ||
|
||
pub struct WaitBlockHeightAndQueryEvents<InQuerier>(pub PhantomData<InQuerier>); | ||
|
||
impl<Chain, InQuerier> BlockEventsQuerier<Chain> for WaitBlockHeightAndQueryEvents<InQuerier> | ||
where | ||
Chain: HasRuntime + HasAddressType + HasEventType + CanQueryChainHeight, | ||
InQuerier: BlockEventsQuerier<Chain>, | ||
Chain::Runtime: CanSleep, | ||
{ | ||
async fn query_block_events( | ||
chain: &Chain, | ||
height: &Chain::Height, | ||
address: &Chain::Address, | ||
) -> Result<Vec<Chain::Event>, Chain::Error> { | ||
let runtime = chain.runtime(); | ||
|
||
loop { | ||
let current_height = chain.query_chain_height().await?; | ||
if ¤t_height >= height { | ||
break; | ||
} else { | ||
runtime.sleep(Duration::from_millis(200)).await; | ||
} | ||
} | ||
|
||
InQuerier::query_block_events(chain, height, address).await | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
relayer/crates/starknet-chain-components/src/impls/queries/mod.rs
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
Oops, something went wrong.