Skip to content

Commit fee3b2d

Browse files
committed
Expose the current best chain tip from ChannelManager + Monitors
Fixes #979
1 parent 3808436 commit fee3b2d

File tree

8 files changed

+60
-44
lines changed

8 files changed

+60
-44
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ use bitcoin::hashes::sha256::Hash as Sha256;
3030
use bitcoin::hash_types::{BlockHash, WPubkeyHash};
3131

3232
use lightning::chain;
33-
use lightning::chain::{chainmonitor, channelmonitor, Confirm, Watch};
33+
use lightning::chain::{BestBlock, chainmonitor, channelmonitor, Confirm, Watch};
3434
use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, MonitorEvent};
3535
use lightning::chain::transaction::OutPoint;
3636
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
3737
use lightning::chain::keysinterface::{KeysInterface, InMemorySigner};
3838
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
39-
use lightning::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, PaymentSendFailure, ChannelManagerReadArgs};
39+
use lightning::ln::channelmanager::{ChainParameters, ChannelManager, PaymentSendFailure, ChannelManagerReadArgs};
4040
use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
4141
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, DecodeError, UpdateAddHTLC, Init};
4242
use lightning::util::enforcing_trait_impls::{EnforcingSigner, INITIAL_REVOKED_COMMITMENT_NUMBER};

fuzz/src/full_stack.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ use bitcoin::hashes::sha256::Hash as Sha256;
2727
use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
2828

2929
use lightning::chain;
30-
use lightning::chain::{Confirm, Listen};
30+
use lightning::chain::{BestBlock, Confirm, Listen};
3131
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
3232
use lightning::chain::chainmonitor;
3333
use lightning::chain::transaction::OutPoint;
3434
use lightning::chain::keysinterface::{InMemorySigner, KeysInterface};
3535
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
36-
use lightning::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager};
36+
use lightning::ln::channelmanager::{ChainParameters, ChannelManager};
3737
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
3838
use lightning::ln::msgs::DecodeError;
3939
use lightning::routing::router::get_route;

lightning-background-processor/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,12 @@ mod tests {
174174
use bitcoin::blockdata::constants::genesis_block;
175175
use bitcoin::blockdata::transaction::{Transaction, TxOut};
176176
use bitcoin::network::constants::Network;
177-
use lightning::chain::Confirm;
178-
use lightning::chain::chainmonitor;
177+
use lightning::chain::{BestBlock, Confirm, chainmonitor};
179178
use lightning::chain::channelmonitor::ANTI_REORG_DELAY;
180179
use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager};
181180
use lightning::chain::transaction::OutPoint;
182181
use lightning::get_event_msg;
183-
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, BestBlock, ChainParameters, ChannelManager, SimpleArcChannelManager};
182+
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, ChannelManager, SimpleArcChannelManager};
184183
use lightning::ln::features::InitFeatures;
185184
use lightning::ln::msgs::ChannelMessageHandler;
186185
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor};

lightning/src/chain/channelmonitor.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ use ln::{PaymentHash, PaymentPreimage};
3737
use ln::msgs::DecodeError;
3838
use ln::chan_utils;
3939
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction};
40-
use ln::channelmanager::{BestBlock, HTLCSource};
40+
use ln::channelmanager::HTLCSource;
4141
use chain;
42-
use chain::WatchedOutput;
42+
use chain::{BestBlock, WatchedOutput};
4343
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
4444
use chain::transaction::{OutPoint, TransactionData};
4545
use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface};
@@ -1189,6 +1189,12 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
11891189
txids.dedup();
11901190
txids
11911191
}
1192+
1193+
/// Gets the latest best block which was connected either via the [`chain::Listen`] or
1194+
/// [`chain::Confirm`] interfaces.
1195+
pub fn current_best_block(&self) -> BestBlock {
1196+
self.inner.lock().unwrap().best_block.clone()
1197+
}
11921198
}
11931199

11941200
impl<Signer: Sign> ChannelMonitorImpl<Signer> {
@@ -2827,11 +2833,11 @@ mod tests {
28272833
use bitcoin::hash_types::Txid;
28282834
use bitcoin::network::constants::Network;
28292835
use hex;
2836+
use chain::BestBlock;
28302837
use chain::channelmonitor::ChannelMonitor;
28312838
use chain::package::{WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC, WEIGHT_REVOKED_OUTPUT};
28322839
use chain::transaction::OutPoint;
28332840
use ln::{PaymentPreimage, PaymentHash};
2834-
use ln::channelmanager::BestBlock;
28352841
use ln::chan_utils;
28362842
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
28372843
use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};

lightning/src/chain/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
//! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
1111
1212
use bitcoin::blockdata::block::{Block, BlockHeader};
13+
use bitcoin::blockdata::constants::genesis_block;
1314
use bitcoin::blockdata::script::Script;
1415
use bitcoin::blockdata::transaction::{Transaction, TxOut};
1516
use bitcoin::hash_types::{BlockHash, Txid};
17+
use bitcoin::network::constants::Network;
1618

1719
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent};
1820
use chain::keysinterface::Sign;
@@ -28,6 +30,36 @@ pub mod keysinterface;
2830
pub(crate) mod onchaintx;
2931
pub(crate) mod package;
3032

33+
/// The best known block as identified by its hash and height.
34+
#[derive(Clone, Copy, PartialEq)]
35+
pub struct BestBlock {
36+
block_hash: BlockHash,
37+
height: u32,
38+
}
39+
40+
impl BestBlock {
41+
/// Returns the best block from the genesis of the given network.
42+
pub fn from_genesis(network: Network) -> Self {
43+
BestBlock {
44+
block_hash: genesis_block(network).header.block_hash(),
45+
height: 0,
46+
}
47+
}
48+
49+
/// Returns the best block as identified by the given block hash and height.
50+
pub fn new(block_hash: BlockHash, height: u32) -> Self {
51+
BestBlock { block_hash, height }
52+
}
53+
54+
/// Returns the best block hash.
55+
pub fn block_hash(&self) -> BlockHash { self.block_hash }
56+
57+
/// Returns the best block height.
58+
pub fn height(&self) -> u32 { self.height }
59+
}
60+
61+
62+
3163
/// An error when accessing the chain via [`Access`].
3264
#[derive(Clone)]
3365
pub enum AccessError {

lightning/src/ln/channel.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ use ln::{PaymentPreimage, PaymentHash};
2626
use ln::features::{ChannelFeatures, InitFeatures};
2727
use ln::msgs;
2828
use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
29-
use ln::channelmanager::{BestBlock, PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
29+
use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
3030
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};
3131
use ln::chan_utils;
32+
use chain::BestBlock;
3233
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
3334
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER};
3435
use chain::transaction::{OutPoint, TransactionData};
@@ -4983,13 +4984,14 @@ mod tests {
49834984
use bitcoin::hashes::hex::FromHex;
49844985
use hex;
49854986
use ln::{PaymentPreimage, PaymentHash};
4986-
use ln::channelmanager::{BestBlock, HTLCSource};
4987+
use ln::channelmanager::HTLCSource;
49874988
use ln::channel::{Channel,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,HTLCCandidate,HTLCInitiator,TxCreationKeys};
49884989
use ln::channel::MAX_FUNDING_SATOSHIS;
49894990
use ln::features::InitFeatures;
49904991
use ln::msgs::{ChannelUpdate, DataLossProtect, DecodeError, OptionalField, UnsignedChannelUpdate};
49914992
use ln::chan_utils;
49924993
use ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT};
4994+
use chain::BestBlock;
49934995
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
49944996
use chain::keysinterface::{InMemorySigner, KeysInterface, BaseSign};
49954997
use chain::transaction::OutPoint;

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
3636
use bitcoin::secp256k1;
3737

3838
use chain;
39-
use chain::Confirm;
40-
use chain::Watch;
39+
use chain::{Confirm, Watch, BestBlock};
4140
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
4241
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};
4342
use chain::transaction::{OutPoint, TransactionData};
@@ -508,34 +507,6 @@ pub struct ChainParameters {
508507
pub best_block: BestBlock,
509508
}
510509

511-
/// The best known block as identified by its hash and height.
512-
#[derive(Clone, Copy, PartialEq)]
513-
pub struct BestBlock {
514-
block_hash: BlockHash,
515-
height: u32,
516-
}
517-
518-
impl BestBlock {
519-
/// Returns the best block from the genesis of the given network.
520-
pub fn from_genesis(network: Network) -> Self {
521-
BestBlock {
522-
block_hash: genesis_block(network).header.block_hash(),
523-
height: 0,
524-
}
525-
}
526-
527-
/// Returns the best block as identified by the given block hash and height.
528-
pub fn new(block_hash: BlockHash, height: u32) -> Self {
529-
BestBlock { block_hash, height }
530-
}
531-
532-
/// Returns the best block hash.
533-
pub fn block_hash(&self) -> BlockHash { self.block_hash }
534-
535-
/// Returns the best block height.
536-
pub fn height(&self) -> u32 { self.height }
537-
}
538-
539510
#[derive(Copy, Clone, PartialEq)]
540511
enum NotifyOption {
541512
DoPersist,
@@ -4085,6 +4056,12 @@ where
40854056
let guard = mtx.lock().unwrap();
40864057
*guard
40874058
}
4059+
4060+
/// Gets the latest best block which was connected either via the [`chain::Listen`] or
4061+
/// [`chain::Confirm`] interfaces.
4062+
pub fn current_best_block(&self) -> BestBlock {
4063+
self.best_block.read().unwrap().clone()
4064+
}
40884065
}
40894066

40904067
impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >

lightning/src/ln/functional_test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
//! A bunch of useful utilities for building networks of nodes and exchanging messages between
1111
//! nodes for functional tests.
1212
13-
use chain::{Confirm, Listen, Watch};
13+
use chain::{BestBlock, Confirm, Listen, Watch};
1414
use chain::channelmonitor::ChannelMonitor;
1515
use chain::transaction::OutPoint;
1616
use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
17-
use ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure};
17+
use ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure};
1818
use routing::router::{Route, get_route};
1919
use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
2020
use ln::features::{InitFeatures, InvoiceFeatures};

0 commit comments

Comments
 (0)