use bitcoin::{
blockdata::{
block::{Block, BlockHeader},
transaction::Transaction,
script::Script,
},
network::{
message::NetworkMessage,
constants::Network
}
};
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use lightning::{
chain::chaininterface::{ChainListener, ChainWatchInterface, ChainWatchInterfaceUtil,ChainError},
util::logger::{Level, Logger, Record}
};
use downstream::Downstream;
use p2p::P2PControlSender;
use std::sync::{Arc, Weak, Mutex};
struct LightningLogger{
level: Level
}
impl Logger for LightningLogger {
fn log(&self, record: &Record) {
if self.level >= record.level {
debug!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
}
}
}
pub type SharedLightningConnector = Arc<Mutex<LightningConnector>>;
pub struct LightningConnector {
util: ChainWatchInterfaceUtil,
p2p: P2PControlSender
}
impl Downstream for LightningConnector {
fn block_connected(&mut self, block: &Block, height: u32) {
self.util.block_connected_with_filtering(block, height)
}
fn header_connected(&mut self, block: &BlockHeader, height: u32) {}
fn block_disconnected(&mut self, header: &BlockHeader) {
self.util.block_disconnected(header)
}
}
impl LightningConnector {
pub fn new (network: Network, p2p: P2PControlSender) -> LightningConnector {
LightningConnector {
util: ChainWatchInterfaceUtil::new(network, Arc::new(LightningLogger{level: Level::Info})),
p2p
}
}
pub fn broadcast (&self, tx: Transaction) {
self.p2p.broadcast(NetworkMessage::Tx(tx))
}
}
impl ChainWatchInterface for LightningConnector {
fn install_watch_tx(&self, _txid: &Sha256dHash, _script_pub_key: &Script) {
unimplemented!()
}
fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script) {
self.util.install_watch_outpoint(outpoint, out_script)
}
fn watch_all_txn(&self) {
self.util.watch_all_txn()
}
fn register_listener(&self, listener: Weak<ChainListener>) {
self.util.register_listener(listener)
}
fn get_chain_utxo(&self, _genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
Err(ChainError::NotSupported)
}
}