-
Notifications
You must be signed in to change notification settings - Fork 72
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
8 changed files
with
580 additions
and
8 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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 @@ | ||
*.db |
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,4 @@ | ||
create_db: | ||
sqlite3 mydatabase.db < init.sql | ||
|
||
.PHONY: create_db |
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,17 @@ | ||
[source] | ||
type = "N2N" | ||
peers = ["relays-new.cardano-mainnet.iohk.io:3001"] | ||
|
||
[intersect] | ||
type = "Point" | ||
value = [4493860, "ce7f821d2140419fea1a7900cf71b0c0a0e94afbb1f814a6717cff071c3b6afc"] | ||
|
||
[[filters]] | ||
type = "SplitBlock" | ||
|
||
[sink] | ||
type = "SqlDb" | ||
connection = "sqlite:./mydatabase.db" | ||
apply_template = "INSERT INTO txs (slot, cbor) VALUES ('{{point.slot}}', X'{{record.hex}}');" | ||
undo_template = "DELETE FROM txs WHERE slot = {{point.slot}}" | ||
reset_template = "DELETE FROM txs WHERE slot > {{point.slot}}" |
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 @@ | ||
CREATE TABLE txs ( | ||
slot INTEGER NOT NULL, | ||
cbor BLOB | ||
); | ||
|
||
CREATE INDEX idx_txs_slot ON txs(slot); |
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,128 @@ | ||
use gasket::framework::*; | ||
use pallas::network::miniprotocols::Point; | ||
use serde::Deserialize; | ||
use tracing::debug; | ||
|
||
use crate::framework::*; | ||
|
||
pub struct Worker { | ||
db: sqlx::Pool<sqlx::Any>, | ||
} | ||
|
||
fn hbs_data(point: Point, record: Option<Record>) -> serde_json::Value { | ||
serde_json::json!({ | ||
"point": match point { | ||
Point::Origin => serde_json::Value::Null, | ||
Point::Specific(slot, hash) => serde_json::json!({ | ||
"slot": slot, | ||
"hash": hex::encode(hash), | ||
}), | ||
}, | ||
"record": serde_json::Value::from(record), | ||
}) | ||
} | ||
|
||
#[async_trait::async_trait(?Send)] | ||
impl gasket::framework::Worker<Stage> for Worker { | ||
async fn bootstrap(stage: &Stage) -> Result<Self, WorkerError> { | ||
let db = sqlx::AnyPool::connect(&stage.config.connection) | ||
.await | ||
.or_retry()?; | ||
|
||
Ok(Self { db }) | ||
} | ||
|
||
async fn schedule( | ||
&mut self, | ||
stage: &mut Stage, | ||
) -> Result<WorkSchedule<ChainEvent>, WorkerError> { | ||
let msg = stage.input.recv().await.or_panic()?; | ||
Ok(WorkSchedule::Unit(msg.payload)) | ||
} | ||
|
||
async fn execute(&mut self, unit: &ChainEvent, stage: &mut Stage) -> Result<(), WorkerError> { | ||
let point = unit.point().clone(); | ||
|
||
let template = match unit { | ||
ChainEvent::Apply(p, r) => { | ||
let data = hbs_data(p.clone(), Some(r.clone())); | ||
stage.templates.render("apply", &data) | ||
} | ||
ChainEvent::Undo(p, r) => { | ||
let data = hbs_data(p.clone(), Some(r.clone())); | ||
stage.templates.render("undo", &data) | ||
} | ||
ChainEvent::Reset(p) => { | ||
let data = hbs_data(p.clone(), None); | ||
stage.templates.render("reset", &data) | ||
} | ||
}; | ||
|
||
let statement = template.or_panic()?; | ||
|
||
let result = sqlx::query(&statement).execute(&self.db).await.or_retry()?; | ||
debug!(rows = result.rows_affected(), "sql statement executed"); | ||
|
||
stage.ops_count.inc(1); | ||
stage.latest_block.set(point.slot_or_default() as i64); | ||
stage.cursor.send(point.clone().into()).await.or_panic()?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Stage)] | ||
#[stage(name = "sql", unit = "ChainEvent", worker = "Worker")] | ||
pub struct Stage { | ||
config: Config, | ||
templates: handlebars::Handlebars<'static>, | ||
|
||
pub input: MapperInputPort, | ||
pub cursor: SinkCursorPort, | ||
|
||
#[metric] | ||
ops_count: gasket::metrics::Counter, | ||
|
||
#[metric] | ||
latest_block: gasket::metrics::Gauge, | ||
} | ||
|
||
#[derive(Default, Debug, Deserialize)] | ||
pub struct Config { | ||
/// eg: sqlite::memory: | ||
pub connection: String, | ||
pub apply_template: String, | ||
pub undo_template: String, | ||
pub reset_template: String, | ||
} | ||
|
||
impl Config { | ||
pub fn bootstrapper(self, _ctx: &Context) -> Result<Stage, Error> { | ||
sqlx::any::install_default_drivers(); | ||
|
||
let mut templates = handlebars::Handlebars::new(); | ||
|
||
templates | ||
.register_template_string("apply", &self.apply_template) | ||
.map_err(Error::config)?; | ||
|
||
templates | ||
.register_template_string("undo", &self.undo_template) | ||
.map_err(Error::config)?; | ||
|
||
templates | ||
.register_template_string("reset", &self.reset_template) | ||
.map_err(Error::config)?; | ||
|
||
let stage = Stage { | ||
config: self, | ||
templates, | ||
ops_count: Default::default(), | ||
latest_block: Default::default(), | ||
input: Default::default(), | ||
cursor: Default::default(), | ||
}; | ||
|
||
Ok(stage) | ||
} | ||
} |