Skip to content

Commit

Permalink
fixup! core: start the migration to async
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Nov 12, 2024
1 parent 06ab2db commit dd5f788
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions lampo-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lightning-net-tokio = { version = "0.0.123" }
lightning-rapid-gossip-sync = { version = "0.0.123" }
lightning-invoice = { version = "0.31" }

async-trait = "0.1"
bitcoin = { version = "0.30.2", features = ["serde"] }
clightningrpc-conf = { git = "https://github.com/laanwj/cln4rust.git", branch = "master" }
crossbeam-channel = "0.5.8"
Expand Down
16 changes: 9 additions & 7 deletions lampo-common/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;
use bitcoin::absolute::Height;
use bitcoin::block::Header as BlockHeader;

use async_trait::async_trait;
pub use bitcoin::consensus::{deserialize, serialize};
pub use bitcoin::{Block, BlockHash, Script, Transaction, Txid};
pub use lightning::chain::WatchedOutput;
Expand Down Expand Up @@ -34,20 +35,21 @@ pub enum BackendKind {

// FIXME: add the BlockSource trait for this
/// Bakend Trait specification
#[async_trait]
pub trait Backend: BlockSource + Send + Sync {
/// Return the kind of backend
fn kind(&self) -> BackendKind;

/// Fetch feerate give a number of blocks
fn fee_rate_estimation(&self, blocks: u64) -> error::Result<u32>;
async fn fee_rate_estimation(&self, blocks: u64) -> error::Result<u32>;

fn minimum_mempool_fee(&self) -> error::Result<u32>;
async fn minimum_mempool_fee(&self) -> error::Result<u32>;

fn brodcast_tx(&self, tx: &Transaction);
async fn brodcast_tx(&self, tx: &Transaction);

fn get_utxo(&self, block: &BlockHash, idx: u64) -> UtxoResult;
async fn get_utxo(&self, block: &BlockHash, idx: u64) -> UtxoResult;

fn get_utxo_by_txid(&self, txid: &Txid, script: &Script) -> error::Result<TxResult>;
async fn get_utxo_by_txid(&self, txid: &Txid, script: &Script) -> error::Result<TxResult>;

fn set_handler(&self, _: Arc<dyn Handler>) {}

Expand All @@ -56,9 +58,9 @@ pub trait Backend: BlockSource + Send + Sync {
fn set_chain_monitor(&self, _: Arc<LampoChainMonitor>) {}

/// Get the information of a transaction inside the blockchain.
fn get_transaction(&self, txid: &Txid) -> error::Result<TxResult>;
async fn get_transaction(&self, txid: &Txid) -> error::Result<TxResult>;

/// Spawn a thread and start polling the backend and notify
/// the listener through the handler.
fn listen(self: Arc<Self>) -> error::Result<()>;
async fn listen(self: Arc<Self>) -> error::Result<()>;
}
1 change: 1 addition & 0 deletions lampo-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub mod chan {
pub use crossbeam_channel::*;
}

pub use async_trait::async_trait;
pub use bitcoin;
pub use bitcoin::secp256k1;

Expand Down
70 changes: 34 additions & 36 deletions lampod/src/chain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::sync;
use std::collections::HashMap;
use std::sync::Arc;

use lampo_common::async_trait;
use lampo_common::backend::Backend;
use lampo_common::bitcoin;
use lampo_common::bitcoin::blockdata::constants::ChainHash;
Expand Down Expand Up @@ -70,36 +71,28 @@ impl LampoChainManager {
}

/// Rust lightning FeeEstimator implementation
#[async_trait]
impl FeeEstimator for LampoChainManager {
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
//FIXME: use cache to avoid return default value (that is 0) on u32
match confirmation_target {
ConfirmationTarget::OnChainSweep => {
self.backend.fee_rate_estimation(1).unwrap_or_default()
}
ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee
| ConfirmationTarget::AnchorChannelFee
| ConfirmationTarget::NonAnchorChannelFee => {
self.backend.fee_rate_estimation(6).unwrap_or_default()
}
ConfirmationTarget::MinAllowedAnchorChannelRemoteFee => {
self.backend.minimum_mempool_fee().unwrap()
}
ConfirmationTarget::ChannelCloseMinimum => {
self.backend.fee_rate_estimation(100).unwrap_or_default()
}
ConfirmationTarget::OutputSpendingFee => {
self.backend.fee_rate_estimation(12).unwrap_or_default()
}
}
// FIXME: have the result in the cache to make easy the query of it.
unimplemented!()
}
}

/// Brodcaster Interface implementation for Lampo.
impl BroadcasterInterface for LampoChainManager {
fn broadcast_transactions(&self, tx: &[&Transaction]) {
// FIXME: change the brodcasting
self.backend.brodcast_tx(tx.first().unwrap());
fn broadcast_transactions(&self, txs: &[&Transaction]) {
// FIXME: support brodcast_txs for multiple tx
// FIXME: we are missing any error in the brodcast_tx, we should
// fix that
for tx in txs.to_vec() {
let tx = tx.clone();
let backend = self.backend.clone();
tokio::spawn(async move {
let tx = tx.clone();
backend.brodcast_tx(&tx).await;
});
}
}
}

Expand Down Expand Up @@ -136,44 +129,49 @@ impl UtxoLookup for LampoChainManager {
unsafe impl Send for LampoChainManager {}
unsafe impl Sync for LampoChainManager {}

#[async_trait]
impl Backend for LampoChainManager {
fn brodcast_tx(&self, tx: &Transaction) {
async fn brodcast_tx(&self, tx: &Transaction) {
self.backend.brodcast_tx(tx);
}

fn fee_rate_estimation(&self, blocks: u64) -> lampo_common::error::Result<u32> {
self.backend.fee_rate_estimation(blocks)
async fn fee_rate_estimation(&self, blocks: u64) -> lampo_common::error::Result<u32> {
self.backend.fee_rate_estimation(blocks).await
}

fn get_transaction(
async fn get_transaction(
&self,
txid: &bitcoin::Txid,
) -> lampo_common::error::Result<lampo_common::backend::TxResult> {
self.backend.get_transaction(txid)
self.backend.get_transaction(txid).await
}

fn get_utxo(&self, block: &bitcoin::BlockHash, idx: u64) -> lampo_common::backend::UtxoResult {
Backend::get_utxo(self.backend.as_ref(), block, idx)
async fn get_utxo(
&self,
block: &bitcoin::BlockHash,
idx: u64,
) -> lampo_common::backend::UtxoResult {
Backend::get_utxo(self.backend.as_ref(), block, idx).await
}

fn get_utxo_by_txid(
async fn get_utxo_by_txid(
&self,
txid: &bitcoin::Txid,
script: &bitcoin::Script,
) -> lampo_common::error::Result<lampo_common::backend::TxResult> {
self.backend.get_utxo_by_txid(txid, script)
self.backend.get_utxo_by_txid(txid, script).await
}

fn kind(&self) -> lampo_common::backend::BackendKind {
self.backend.kind()
}

fn listen(self: Arc<Self>) -> lampo_common::error::Result<()> {
self.backend.clone().listen()
async fn listen(self: Arc<Self>) -> lampo_common::error::Result<()> {
self.backend.clone().listen().await
}

fn minimum_mempool_fee(&self) -> lampo_common::error::Result<u32> {
self.backend.minimum_mempool_fee()
async fn minimum_mempool_fee(&self) -> lampo_common::error::Result<u32> {
self.backend.minimum_mempool_fee().await
}

fn set_handler(&self, arc: Arc<dyn lampo_common::handler::Handler>) {
Expand Down

0 comments on commit dd5f788

Please sign in to comment.