Expose the current best chain tip from ChannelManager + Monitors

Fixes #979
This commit is contained in:
Matt Corallo 2021-07-03 01:58:30 +00:00
parent 0882655680
commit da298e498f
8 changed files with 58 additions and 44 deletions

View file

@ -30,13 +30,13 @@ use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hash_types::{BlockHash, WPubkeyHash}; use bitcoin::hash_types::{BlockHash, WPubkeyHash};
use lightning::chain; use lightning::chain;
use lightning::chain::{chainmonitor, channelmonitor, Confirm, Watch}; use lightning::chain::{BestBlock, chainmonitor, channelmonitor, Confirm, Watch};
use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, MonitorEvent}; use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, MonitorEvent};
use lightning::chain::transaction::OutPoint; use lightning::chain::transaction::OutPoint;
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
use lightning::chain::keysinterface::{KeysInterface, InMemorySigner}; use lightning::chain::keysinterface::{KeysInterface, InMemorySigner};
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, PaymentSendFailure, ChannelManagerReadArgs}; use lightning::ln::channelmanager::{ChainParameters, ChannelManager, PaymentSendFailure, ChannelManagerReadArgs};
use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, DecodeError, UpdateAddHTLC, Init}; use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, DecodeError, UpdateAddHTLC, Init};
use lightning::util::enforcing_trait_impls::{EnforcingSigner, INITIAL_REVOKED_COMMITMENT_NUMBER}; use lightning::util::enforcing_trait_impls::{EnforcingSigner, INITIAL_REVOKED_COMMITMENT_NUMBER};

View file

@ -27,13 +27,13 @@ use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash}; use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
use lightning::chain; use lightning::chain;
use lightning::chain::{Confirm, Listen}; use lightning::chain::{BestBlock, Confirm, Listen};
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
use lightning::chain::chainmonitor; use lightning::chain::chainmonitor;
use lightning::chain::transaction::OutPoint; use lightning::chain::transaction::OutPoint;
use lightning::chain::keysinterface::{InMemorySigner, KeysInterface}; use lightning::chain::keysinterface::{InMemorySigner, KeysInterface};
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager}; use lightning::ln::channelmanager::{ChainParameters, ChannelManager};
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor}; use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
use lightning::ln::msgs::DecodeError; use lightning::ln::msgs::DecodeError;
use lightning::routing::router::get_route; use lightning::routing::router::get_route;

View file

@ -174,13 +174,12 @@ mod tests {
use bitcoin::blockdata::constants::genesis_block; use bitcoin::blockdata::constants::genesis_block;
use bitcoin::blockdata::transaction::{Transaction, TxOut}; use bitcoin::blockdata::transaction::{Transaction, TxOut};
use bitcoin::network::constants::Network; use bitcoin::network::constants::Network;
use lightning::chain::Confirm; use lightning::chain::{BestBlock, Confirm, chainmonitor};
use lightning::chain::chainmonitor;
use lightning::chain::channelmonitor::ANTI_REORG_DELAY; use lightning::chain::channelmonitor::ANTI_REORG_DELAY;
use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager}; use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager};
use lightning::chain::transaction::OutPoint; use lightning::chain::transaction::OutPoint;
use lightning::get_event_msg; use lightning::get_event_msg;
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, BestBlock, ChainParameters, ChannelManager, SimpleArcChannelManager}; use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, ChannelManager, SimpleArcChannelManager};
use lightning::ln::features::InitFeatures; use lightning::ln::features::InitFeatures;
use lightning::ln::msgs::ChannelMessageHandler; use lightning::ln::msgs::ChannelMessageHandler;
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor}; use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor};

View file

@ -37,9 +37,9 @@ use ln::{PaymentHash, PaymentPreimage};
use ln::msgs::DecodeError; use ln::msgs::DecodeError;
use ln::chan_utils; use ln::chan_utils;
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction}; use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction};
use ln::channelmanager::{BestBlock, HTLCSource}; use ln::channelmanager::HTLCSource;
use chain; use chain;
use chain::WatchedOutput; use chain::{BestBlock, WatchedOutput};
use chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use chain::transaction::{OutPoint, TransactionData}; use chain::transaction::{OutPoint, TransactionData};
use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface}; use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface};
@ -1189,6 +1189,12 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
txids.dedup(); txids.dedup();
txids txids
} }
/// Gets the latest best block which was connected either via the [`chain::Listen`] or
/// [`chain::Confirm`] interfaces.
pub fn current_best_block(&self) -> BestBlock {
self.inner.lock().unwrap().best_block.clone()
}
} }
impl<Signer: Sign> ChannelMonitorImpl<Signer> { impl<Signer: Sign> ChannelMonitorImpl<Signer> {
@ -2827,11 +2833,11 @@ mod tests {
use bitcoin::hash_types::Txid; use bitcoin::hash_types::Txid;
use bitcoin::network::constants::Network; use bitcoin::network::constants::Network;
use hex; use hex;
use chain::BestBlock;
use chain::channelmonitor::ChannelMonitor; use chain::channelmonitor::ChannelMonitor;
use chain::package::{WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC, WEIGHT_REVOKED_OUTPUT}; use chain::package::{WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC, WEIGHT_REVOKED_OUTPUT};
use chain::transaction::OutPoint; use chain::transaction::OutPoint;
use ln::{PaymentPreimage, PaymentHash}; use ln::{PaymentPreimage, PaymentHash};
use ln::channelmanager::BestBlock;
use ln::chan_utils; use ln::chan_utils;
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters}; use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator}; use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};

View file

@ -10,9 +10,11 @@
//! Structs and traits which allow other parts of rust-lightning to interact with the blockchain. //! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::constants::genesis_block;
use bitcoin::blockdata::script::Script; use bitcoin::blockdata::script::Script;
use bitcoin::blockdata::transaction::{Transaction, TxOut}; use bitcoin::blockdata::transaction::{Transaction, TxOut};
use bitcoin::hash_types::{BlockHash, Txid}; use bitcoin::hash_types::{BlockHash, Txid};
use bitcoin::network::constants::Network;
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent}; use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent};
use chain::keysinterface::Sign; use chain::keysinterface::Sign;
@ -28,6 +30,34 @@ pub mod keysinterface;
pub(crate) mod onchaintx; pub(crate) mod onchaintx;
pub(crate) mod package; pub(crate) mod package;
/// The best known block as identified by its hash and height.
#[derive(Clone, Copy, PartialEq)]
pub struct BestBlock {
block_hash: BlockHash,
height: u32,
}
impl BestBlock {
/// Returns the best block from the genesis of the given network.
pub fn from_genesis(network: Network) -> Self {
BestBlock {
block_hash: genesis_block(network).header.block_hash(),
height: 0,
}
}
/// Returns the best block as identified by the given block hash and height.
pub fn new(block_hash: BlockHash, height: u32) -> Self {
BestBlock { block_hash, height }
}
/// Returns the best block hash.
pub fn block_hash(&self) -> BlockHash { self.block_hash }
/// Returns the best block height.
pub fn height(&self) -> u32 { self.height }
}
/// An error when accessing the chain via [`Access`]. /// An error when accessing the chain via [`Access`].
#[derive(Clone)] #[derive(Clone)]
pub enum AccessError { pub enum AccessError {

View file

@ -26,9 +26,10 @@ use ln::{PaymentPreimage, PaymentHash};
use ln::features::{ChannelFeatures, InitFeatures}; use ln::features::{ChannelFeatures, InitFeatures};
use ln::msgs; use ln::msgs;
use ln::msgs::{DecodeError, OptionalField, DataLossProtect}; use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
use ln::channelmanager::{BestBlock, PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT}; use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
use ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor}; use ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor};
use ln::chan_utils; use ln::chan_utils;
use chain::BestBlock;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER}; use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER};
use chain::transaction::{OutPoint, TransactionData}; use chain::transaction::{OutPoint, TransactionData};
@ -4988,13 +4989,14 @@ mod tests {
use bitcoin::hashes::hex::FromHex; use bitcoin::hashes::hex::FromHex;
use hex; use hex;
use ln::{PaymentPreimage, PaymentHash}; use ln::{PaymentPreimage, PaymentHash};
use ln::channelmanager::{BestBlock, HTLCSource}; use ln::channelmanager::HTLCSource;
use ln::channel::{Channel,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,HTLCCandidate,HTLCInitiator,TxCreationKeys}; use ln::channel::{Channel,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,HTLCCandidate,HTLCInitiator,TxCreationKeys};
use ln::channel::MAX_FUNDING_SATOSHIS; use ln::channel::MAX_FUNDING_SATOSHIS;
use ln::features::InitFeatures; use ln::features::InitFeatures;
use ln::msgs::{ChannelUpdate, DataLossProtect, DecodeError, OptionalField, UnsignedChannelUpdate}; use ln::msgs::{ChannelUpdate, DataLossProtect, DecodeError, OptionalField, UnsignedChannelUpdate};
use ln::chan_utils; use ln::chan_utils;
use ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT}; use ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT};
use chain::BestBlock;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::keysinterface::{InMemorySigner, KeysInterface, BaseSign}; use chain::keysinterface::{InMemorySigner, KeysInterface, BaseSign};
use chain::transaction::OutPoint; use chain::transaction::OutPoint;

View file

@ -36,8 +36,7 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
use bitcoin::secp256k1; use bitcoin::secp256k1;
use chain; use chain;
use chain::Confirm; use chain::{Confirm, Watch, BestBlock};
use chain::Watch;
use chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID}; use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
use chain::transaction::{OutPoint, TransactionData}; use chain::transaction::{OutPoint, TransactionData};
@ -508,34 +507,6 @@ pub struct ChainParameters {
pub best_block: BestBlock, pub best_block: BestBlock,
} }
/// The best known block as identified by its hash and height.
#[derive(Clone, Copy, PartialEq)]
pub struct BestBlock {
block_hash: BlockHash,
height: u32,
}
impl BestBlock {
/// Returns the best block from the genesis of the given network.
pub fn from_genesis(network: Network) -> Self {
BestBlock {
block_hash: genesis_block(network).header.block_hash(),
height: 0,
}
}
/// Returns the best block as identified by the given block hash and height.
pub fn new(block_hash: BlockHash, height: u32) -> Self {
BestBlock { block_hash, height }
}
/// Returns the best block hash.
pub fn block_hash(&self) -> BlockHash { self.block_hash }
/// Returns the best block height.
pub fn height(&self) -> u32 { self.height }
}
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
enum NotifyOption { enum NotifyOption {
DoPersist, DoPersist,
@ -4085,6 +4056,12 @@ where
let guard = mtx.lock().unwrap(); let guard = mtx.lock().unwrap();
*guard *guard
} }
/// Gets the latest best block which was connected either via the [`chain::Listen`] or
/// [`chain::Confirm`] interfaces.
pub fn current_best_block(&self) -> BestBlock {
self.best_block.read().unwrap().clone()
}
} }
impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref > impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >

View file

@ -10,11 +10,11 @@
//! A bunch of useful utilities for building networks of nodes and exchanging messages between //! A bunch of useful utilities for building networks of nodes and exchanging messages between
//! nodes for functional tests. //! nodes for functional tests.
use chain::{Confirm, Listen, Watch}; use chain::{BestBlock, Confirm, Listen, Watch};
use chain::channelmonitor::ChannelMonitor; use chain::channelmonitor::ChannelMonitor;
use chain::transaction::OutPoint; use chain::transaction::OutPoint;
use ln::{PaymentPreimage, PaymentHash, PaymentSecret}; use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
use ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure}; use ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure};
use routing::router::{Route, get_route}; use routing::router::{Route, get_route};
use routing::network_graph::{NetGraphMsgHandler, NetworkGraph}; use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
use ln::features::{InitFeatures, InvoiceFeatures}; use ln::features::{InitFeatures, InvoiceFeatures};