Skip to content

Commit

Permalink
refactor: support async host and polymorphic ledger (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Oct 5, 2024
1 parent 1f71139 commit 79c35aa
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 96 deletions.
81 changes: 77 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions balius-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ redb = "2.1.3"
tracing = "0.1.40"
hex = "0.4.3"
itertools = "0.13.0"
utxorpc = { version = "0.6.0", optional = true }
async-trait = "0.1.83"

[dev-dependencies]
tokio = "1.40.0"
60 changes: 18 additions & 42 deletions balius-runtime/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,56 @@
use std::sync::Arc;

use crate::{
ledgers,
router::Router,
wit::balius::app::{driver, kv, ledger, submit},
};

#[derive(Clone)]
pub struct Adapter {
worker_id: String,
router: Router,
pub ledger: ledgers::Ledger,
}

impl Adapter {
pub fn new(worker_id: String, router: Router) -> Self {
Self { worker_id, router }
pub fn new(worker_id: String, router: Router, ledger: ledgers::Ledger) -> Self {
Self {
worker_id,
router,
ledger,
}
}
}

#[async_trait::async_trait]
impl kv::Host for Adapter {
fn get_value(&mut self, key: String) -> Result<kv::Payload, kv::KvError> {
async fn get_value(&mut self, key: String) -> Result<kv::Payload, kv::KvError> {
todo!()
}

fn set_value(&mut self, key: String, value: kv::Payload) -> Result<(), kv::KvError> {
async fn set_value(&mut self, key: String, value: kv::Payload) -> Result<(), kv::KvError> {
println!("{}:{}", key, hex::encode(value));

Ok(())
}

fn list_values(&mut self, prefix: String) -> Result<Vec<String>, kv::KvError> {
async fn list_values(&mut self, prefix: String) -> Result<Vec<String>, kv::KvError> {
todo!()
}
}

#[async_trait::async_trait]
impl submit::Host for Adapter {
fn submit_tx(&mut self, tx: submit::Cbor) -> Result<(), submit::SubmitError> {
async fn submit_tx(&mut self, tx: submit::Cbor) -> Result<(), submit::SubmitError> {
println!("{}", hex::encode(tx));

Ok(())
}
}

#[async_trait::async_trait]
impl driver::Host for Adapter {
fn register_channel(&mut self, id: u32, pattern: driver::EventPattern) -> () {
async fn register_channel(&mut self, id: u32, pattern: driver::EventPattern) -> () {
self.router.register_channel(&self.worker_id, id, &pattern);
}
}

impl ledger::Host for Adapter {
fn read_utxos(
&mut self,
refs: Vec<ledger::TxoRef>,
) -> Result<Vec<ledger::Utxo>, ledger::LedgerError> {
let output = pallas::ledger::primitives::babbage::MintedTransactionOutput::PostAlonzo(pallas::ledger::primitives::babbage::MintedPostAlonzoTransactionOutput {
address: pallas::ledger::addresses::Address::from_bech32("addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x").unwrap().to_vec().into(),
value: pallas::ledger::primitives::babbage::Value::Coin(5_000_000),
datum_option: None,
script_ref: None,
});

let cbor = pallas::codec::minicbor::to_vec(&output).unwrap();

Ok(vec![ledger::Utxo {
ref_: ledger::TxoRef {
tx_hash: hex::decode(
"f7d3837715680f3a170e99cd202b726842d97f82c05af8fcd18053c64e33ec4f",
)
.unwrap(),
tx_index: 0,
},
body: cbor,
}])
}

fn search_utxos(
&mut self,
pattern: ledger::UtxoPattern,
) -> Result<Vec<ledger::Utxo>, ledger::LedgerError> {
todo!()
}
}
39 changes: 39 additions & 0 deletions balius-runtime/src/ledgers/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::wit::balius::app::ledger as wit;

#[derive(Clone)]
pub struct Ledger;

#[async_trait::async_trait]
impl wit::Host for Ledger {
async fn read_utxos(
&mut self,
_refs: Vec<wit::TxoRef>,
) -> Result<Vec<wit::Utxo>, wit::LedgerError> {
let output = pallas::ledger::primitives::babbage::MintedTransactionOutput::PostAlonzo(pallas::ledger::primitives::babbage::MintedPostAlonzoTransactionOutput {
address: pallas::ledger::addresses::Address::from_bech32("addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x").unwrap().to_vec().into(),
value: pallas::ledger::primitives::babbage::Value::Coin(5_000_000),
datum_option: None,
script_ref: None,
});

let cbor = pallas::codec::minicbor::to_vec(&output).unwrap();

Ok(vec![wit::Utxo {
ref_: wit::TxoRef {
tx_hash: hex::decode(
"f7d3837715680f3a170e99cd202b726842d97f82c05af8fcd18053c64e33ec4f",
)
.unwrap(),
tx_index: 0,
},
body: cbor,
}])
}

async fn search_utxos(
&mut self,
_pattern: wit::UtxoPattern,
) -> Result<Vec<wit::Utxo>, wit::LedgerError> {
todo!()
}
}
54 changes: 54 additions & 0 deletions balius-runtime/src/ledgers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::wit::balius::app::ledger as wit;

pub mod mock;

#[cfg(feature = "utxorpc")]
pub mod u5c;

#[derive(Clone)]
pub enum Ledger {
Mock(mock::Ledger),

#[cfg(feature = "utxorpc")]
U5C(u5c::Ledger),
}

impl From<mock::Ledger> for Ledger {
fn from(ledger: mock::Ledger) -> Self {
Ledger::Mock(ledger)
}
}

#[cfg(feature = "utxorpc")]
impl From<u5c::Ledger> for Ledger {
fn from(ledger: u5c::Ledger) -> Self {
Ledger::U5C(ledger)
}
}

#[async_trait::async_trait]
impl wit::Host for Ledger {
async fn read_utxos(
&mut self,
refs: Vec<wit::TxoRef>,
) -> Result<Vec<wit::Utxo>, wit::LedgerError> {
match self {
Ledger::Mock(ledger) => ledger.read_utxos(refs).await,

#[cfg(feature = "utxorpc")]
Ledger::U5C(ledger) => ledger.read_utxos(refs).await,
}
}

async fn search_utxos(
&mut self,
pattern: wit::UtxoPattern,
) -> Result<Vec<wit::Utxo>, wit::LedgerError> {
match self {
Ledger::Mock(ledger) => ledger.search_utxos(pattern).await,

#[cfg(feature = "utxorpc")]
Ledger::U5C(ledger) => ledger.search_utxos(pattern).await,
}
}
}
Loading

0 comments on commit 79c35aa

Please sign in to comment.