-
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.
refactor: support async host and polymorphic ledger (#28)
- Loading branch information
Showing
9 changed files
with
324 additions
and
96 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 |
---|---|---|
@@ -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!() | ||
} | ||
} |
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,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!() | ||
} | ||
} |
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,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, | ||
} | ||
} | ||
} |
Oops, something went wrong.