-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce chain-sync driver (#36)
- Loading branch information
Showing
18 changed files
with
416 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use pallas::codec::minicbor::decode::info; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::select; | ||
use tokio_util::sync::CancellationToken; | ||
use tracing::{info, warn}; | ||
use utxorpc::CardanoSyncClient; | ||
|
||
use crate::{ChainPoint, Error, Runtime}; | ||
|
||
impl From<ChainPoint> for utxorpc::spec::sync::BlockRef { | ||
fn from(point: ChainPoint) -> Self { | ||
utxorpc::spec::sync::BlockRef { | ||
index: point.0, | ||
hash: point.1.to_vec().into(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Serialize, Deserialize, Debug)] | ||
pub struct Config { | ||
pub endpoint_url: String, | ||
pub api_key: String, | ||
} | ||
|
||
pub async fn run(config: Config, runtime: Runtime, cancel: CancellationToken) -> Result<(), Error> { | ||
let mut sync = utxorpc::ClientBuilder::new() | ||
.uri(&config.endpoint_url)? | ||
.metadata("dmtr-api-key", config.api_key)? | ||
.build::<CardanoSyncClient>() | ||
.await; | ||
|
||
let cursor = runtime | ||
.chain_cursor() | ||
.await? | ||
.map(Into::into) | ||
.into_iter() | ||
.collect(); | ||
|
||
// TODO: handle disconnections and retry logic | ||
|
||
let mut tip = sync.follow_tip(cursor).await?; | ||
|
||
info!("starting follow-tip loop"); | ||
|
||
loop { | ||
select! { | ||
_ = cancel.cancelled() => { | ||
warn!("chainsync driver cancelled"); | ||
break Ok(()) | ||
}, | ||
event = tip.event() => { | ||
match event { | ||
Ok(utxorpc::TipEvent::Apply(block)) => { | ||
let block = pallas::ledger::traverse::MultiEraBlock::decode(&block.native).unwrap(); | ||
runtime.apply_block(&block).await?; | ||
} | ||
Ok(utxorpc::TipEvent::Undo(block)) => { | ||
let block = pallas::ledger::traverse::MultiEraBlock::decode(&block.native).unwrap(); | ||
runtime.undo_block(&block).await?; | ||
} | ||
Ok(utxorpc::TipEvent::Reset(point)) => { | ||
warn!(slot=point.index, "TODO: handle reset"); | ||
continue; | ||
}, | ||
Err(_) => todo!(), | ||
} | ||
} | ||
} | ||
} | ||
} |
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 +1,11 @@ | ||
//! Control-flow components on top of the Runtime. | ||
//! | ||
//! Drivers are responsible for implementing the control-flows that | ||
//! handle external interactions (e.g. handling requests, syncing from the | ||
//! blockchain, etc) and funnels them into the Runtime. | ||
//! | ||
//! Each of these drivers has a way to trigger a forever-loop that should be | ||
//! spawn as an independent tokio task running on the background. | ||
|
||
pub mod chainsync; | ||
pub mod jsonrpc; |
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.