-
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.
Listen to chain events and update twin on requests
- Loading branch information
1 parent
f35d685
commit f3b1779
Showing
17 changed files
with
165 additions
and
35 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.
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
Binary file not shown.
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 |
---|---|---|
|
@@ -71,4 +71,6 @@ message Envelope { | |
bytes plain = 13; | ||
bytes cipher = 14; | ||
} | ||
|
||
optional string relays = 17; | ||
} |
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
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,40 @@ | ||
use crate::{cache::Cache, tfchain::tfchain, twin::Twin}; | ||
use anyhow::Result; | ||
use futures::StreamExt; | ||
use log; | ||
use subxt::{OnlineClient, PolkadotConfig}; | ||
|
||
#[derive(Clone)] | ||
pub struct Listener<C> | ||
where | ||
C: Cache<Twin>, | ||
{ | ||
cache: C, | ||
api: OnlineClient<PolkadotConfig>, | ||
} | ||
|
||
impl<C> Listener<C> | ||
where | ||
C: Cache<Twin> + Clone, | ||
{ | ||
pub async fn new(url: &str, cache: C) -> Result<Self> { | ||
let api = OnlineClient::<PolkadotConfig>::from_url(url).await?; | ||
Ok(Listener { api, cache }) | ||
} | ||
pub async fn listen(&self) -> Result<()> { | ||
log::info!("started chain events listener"); | ||
let mut blocks_sub = self.api.blocks().subscribe_finalized().await?; | ||
while let Some(block) = blocks_sub.next().await { | ||
let events = block?.events().await?; | ||
for evt in events.iter() { | ||
let evt = evt?; | ||
if let Ok(Some(twin)) = evt.as_event::<tfchain::tfgrid_module::events::TwinStored>() { | ||
self.cache.set(twin.0.id, twin.0.into()).await?; | ||
} else if let Ok(Some(twin)) = evt.as_event::<tfchain::tfgrid_module::events::TwinUpdated>() { | ||
self.cache.set(twin.0.id, twin.0.into()).await?; | ||
} | ||
} | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod events; | ||
|
||
pub use events::Listener; |
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
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,6 @@ | ||
#[subxt::subxt(runtime_metadata_path = "artifacts/network.scale")] | ||
mod tfchain {} | ||
|
||
use subxt::utils::AccountId32; | ||
pub use tfchain::runtime_types::pallet_tfgrid::types::Twin as TwinData; | ||
pub type Twin = TwinData<AccountId32>; |
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