-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
122 additions
and
15 deletions.
There are no files selected for viewing
File renamed without changes.
107 changes: 107 additions & 0 deletions
107
relayer/src/message_relayer/common/message_paid_listener.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,107 @@ | ||
use std::sync::mpsc::{channel, Receiver, Sender}; | ||
|
||
use bridging_payment::services::BridgingPaymentEvents; | ||
use futures::executor::block_on; | ||
use gear_rpc_client::GearApi; | ||
use parity_scale_codec::Decode; | ||
use primitive_types::{H256, U256}; | ||
use prometheus::IntCounter; | ||
|
||
use utils_prometheus::{impl_metered_service, MeteredService}; | ||
|
||
use super::block_listener::BlockNumber; | ||
|
||
pub struct PaidMessage { | ||
pub nonce: U256, | ||
Check failure on line 15 in relayer/src/message_relayer/common/message_paid_listener.rs GitHub Actions / lints
|
||
} | ||
|
||
pub struct MessagePaidListener { | ||
bridging_payment_address: H256, | ||
|
||
gear_api: GearApi, | ||
|
||
metrics: MessagePaidListenerMetrics, | ||
} | ||
|
||
impl MeteredService for MessagePaidListener { | ||
fn get_sources(&self) -> impl IntoIterator<Item = Box<dyn prometheus::core::Collector>> { | ||
self.metrics.get_sources() | ||
} | ||
} | ||
|
||
impl_metered_service! { | ||
struct MessagePaidListenerMetrics { | ||
total_paid_messages_found: IntCounter, | ||
} | ||
} | ||
|
||
impl MessagePaidListenerMetrics { | ||
fn new() -> Self { | ||
Self::new_inner().expect("Failed to create metrics") | ||
} | ||
|
||
fn new_inner() -> prometheus::Result<Self> { | ||
Ok(Self { | ||
total_paid_messages_found: IntCounter::new( | ||
"message_relayer_message_paid_listener_total_paid_messages_found", | ||
"Total amount of paid messages found by event listener", | ||
)?, | ||
}) | ||
} | ||
} | ||
|
||
impl MessagePaidListener { | ||
pub fn new(gear_api: GearApi, bridging_payment_address: H256) -> Self { | ||
Self { | ||
bridging_payment_address, | ||
gear_api, | ||
metrics: MessagePaidListenerMetrics::new(), | ||
} | ||
} | ||
|
||
pub fn run(self, blocks: Receiver<BlockNumber>) -> Receiver<PaidMessage> { | ||
let (sender, receiver) = channel(); | ||
|
||
tokio::spawn(async move { | ||
loop { | ||
for block in blocks.try_iter() { | ||
let res = block_on(self.process_block_events(block.0, &sender)); | ||
if let Err(err) = res { | ||
log::error!("Event processor failed: {}", err); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
receiver | ||
} | ||
|
||
async fn process_block_events( | ||
&self, | ||
block: u32, | ||
sender: &Sender<PaidMessage>, | ||
) -> anyhow::Result<()> { | ||
log::info!("Processing gear block #{}", block); | ||
let block_hash = self.gear_api.block_number_to_hash(block).await?; | ||
|
||
let messages = self | ||
.gear_api | ||
.user_message_sent_events(self.bridging_payment_address, block_hash) | ||
.await?; | ||
if !messages.is_empty() { | ||
log::info!("Found {} paid messages", messages.len()); | ||
self.metrics | ||
.total_paid_messages_found | ||
.inc_by(messages.len() as u64); | ||
|
||
for message in messages { | ||
let user_reply = BridgingPaymentEvents::decode(&mut &message.payload[..])?; | ||
let BridgingPaymentEvents::TeleportVaraToEth { nonce, .. } = user_reply; | ||
|
||
sender.send(PaidMessage { nonce })?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod gear_event_listener; | ||
pub mod block_listener; | ||
pub mod merkle_root_listener; | ||
pub mod message_paid_listener; | ||
pub mod message_queued_listener; | ||
pub mod message_sender; |
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