use std::error::Error;
pub mod node_communication {
use crate::bitcoin::protocol::BitcoinError;
use async_trait::async_trait;
#[async_trait]
pub trait NodeCommunicationPort {
async fn connect(&self) -> Result<(), BitcoinError>;
async fn broadcast_transaction(&self, tx_hex: &str) -> Result<String, BitcoinError>;
async fn get_mempool_stats(&self) -> Result<MempoolStats, BitcoinError>;
async fn is_synced(&self) -> Result<bool, BitcoinError>;
}
pub struct MempoolStats {
pub tx_count: usize,
pub size_bytes: usize,
pub fee_stats: FeeStats,
}
pub struct FeeStats {
pub min_fee_rate: f64,
pub median_fee_rate: f64,
pub next_block_fee_rate: f64,
}
}
pub mod wallet_interface {
use crate::bitcoin::error::Error;
use async_trait::async_trait;
#[async_trait]
pub trait WalletPort {
async fn create_psbt(&self,
recipients: &[Recipient],
fee_rate: f64) -> Result<String, Error>;
async fn sign_psbt(&self, psbt: &str) -> Result<String, Error>;
async fn finalize_psbt(&self, psbt: &str) -> Result<String, Error>;
async fn create_taproot_psbt(&self,
script_tree: &ScriptTree,
recipients: &[Recipient]) -> Result<String, Error>;
}
pub struct Recipient {
pub address: String,
pub amount: u64,
}
pub struct ScriptTree {
pub internal_key: String,
pub script_branches: Vec<ScriptBranch>,
}
pub struct ScriptBranch {
pub script: String,
pub leaf_version: u8,
}
}
pub mod smart_contract {
use crate::bitcoin::error::Error;
use async_trait::async_trait;
#[async_trait]
pub trait SmartContractPort {
async fn compile_miniscript(&self, miniscript: &str) -> Result<String, Error>;
async fn analyze_script(&self, script: &str) -> Result<ScriptAnalysis, Error>;
async fn test_execute(&self, script: &str,
inputs: &[ScriptInput]) -> Result<ScriptExecutionResult, Error>;
}
pub struct ScriptAnalysis {
pub size: usize,
pub opcode_count: usize,
pub sigop_count: usize,
pub taproot_compatible: bool,
}
pub struct ScriptInput {
pub stack_element: String,
}
pub struct ScriptExecutionResult {
pub success: bool,
pub final_stack: Vec<String>,
pub error: Option<String>,
}
}
pub mod metrics {
use crate::bitcoin::error::Error;
use async_trait::async_trait;
#[async_trait]
pub trait MetricsPort {
async fn get_tps(&self) -> Result<f64, Error>;
async fn get_block_propagation_time(&self) -> Result<f64, Error>;
async fn get_mempool_depth(&self) -> Result<MempoolDepth, Error>;
async fn export_prometheus_metrics(&self) -> Result<String, Error>;
}
pub struct MempoolDepth {
pub size_bytes: usize,
pub tx_count: usize,
pub size_categories: MempoolSizeCategories,
}
pub struct MempoolSizeCategories {
pub low_fee_bytes: usize,
pub medium_fee_bytes: usize,
pub high_fee_bytes: usize,
}
}
pub mod p2p;
pub mod wallet;
pub mod contracts;
pub trait Port {
fn name(&self) -> &'static str;
fn version(&self) -> &'static str;
fn is_connected(&self) -> bool;
}
pub struct PortManager {
ports: Vec<Box<dyn Port>>,
}
impl PortManager {
pub fn new() -> Self {
PortManager {
ports: Vec::new(),
}
}
pub fn register_port(&mut self, port: Box<dyn Port>) {
self.ports.push(port);
}
pub fn get_ports(&self) -> &[Box<dyn Port>] {
&self.ports
}
pub fn get_port_by_name(&self, name: &str) -> Option<&Box<dyn Port>> {
self.ports.iter().find(|p| p.name() == name)
}
}