Merge pull request #819 from TheBlueMatt/2021-03-810-rebased
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Wed, 3 Mar 2021 00:04:23 +0000 (16:04 -0800)
committerGitHub <noreply@github.com>
Wed, 3 Mar 2021 00:04:23 +0000 (16:04 -0800)
Change ChannelManager deserialization to return an optional blockhash

background-processor/src/lib.rs
lightning-block-sync/src/init.rs
lightning-net-tokio/src/lib.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/peer_handler.rs

index 0a09ae95c209e41e6040f85445b8bad4062beb91..0c7191fbad2bd74fc8ebeafebfe044dbfa97ab96 100644 (file)
@@ -124,7 +124,7 @@ mod tests {
        type ChainMonitor = chainmonitor::ChainMonitor<InMemorySigner, Arc<test_utils::TestChainSource>, Arc<test_utils::TestBroadcaster>, Arc<test_utils::TestFeeEstimator>, Arc<test_utils::TestLogger>, Arc<FilesystemPersister>>;
 
        struct Node {
-               node: SimpleArcChannelManager<ChainMonitor, test_utils::TestBroadcaster, test_utils::TestFeeEstimator, test_utils::TestLogger>,
+               node: Arc<SimpleArcChannelManager<ChainMonitor, test_utils::TestBroadcaster, test_utils::TestFeeEstimator, test_utils::TestLogger>>,
                persister: Arc<FilesystemPersister>,
                logger: Arc<test_utils::TestLogger>,
        }
index 3e421fc44a9542b7e0a9fb5aa963d2c4bcca4c0d..59c057157ef14bd6332d0bad1c2207b2919e741c 100644 (file)
@@ -7,6 +7,18 @@ use bitcoin::network::constants::Network;
 
 use lightning::chain;
 
+/// Returns a validated block header of the source's best chain tip.
+///
+/// Upon success, the returned header can be used to initialize [`SpvClient`]. Useful during a fresh
+/// start when there are no chain listeners to sync yet.
+pub async fn validate_best_block_header<B: BlockSource>(block_source: &mut B) ->
+BlockSourceResult<ValidatedBlockHeader> {
+       let (best_block_hash, best_block_height) = block_source.get_best_block().await?;
+       block_source
+               .get_header(&best_block_hash, best_block_height).await?
+               .validate(best_block_hash)
+}
+
 /// Performs a one-time sync of chain listeners using a single *trusted* block source, bringing each
 /// listener's view of the chain from its paired block hash to `block_source`'s best chain tip.
 ///
@@ -113,10 +125,7 @@ pub async fn synchronize_listeners<B: BlockSource, C: Cache>(
        header_cache: &mut C,
        mut chain_listeners: Vec<(BlockHash, &mut dyn chain::Listen)>,
 ) -> BlockSourceResult<ValidatedBlockHeader> {
-       let (best_block_hash, best_block_height) = block_source.get_best_block().await?;
-       let best_header = block_source
-               .get_header(&best_block_hash, best_block_height).await?
-               .validate(best_block_hash)?;
+       let best_header = validate_best_block_header(block_source).await?;
 
        // Fetch the header for the block hash paired with each listener.
        let mut chain_listeners_with_old_headers = Vec::new();
index 051341ee98cb230b16114ee743a87a80c749e5cd..8c98333080f941b5102bf415e9cb4e54a17a8a76 100644 (file)
@@ -38,8 +38,8 @@
 //! type ChainFilter = dyn lightning::chain::Filter;
 //! type DataPersister = dyn lightning::chain::channelmonitor::Persist<lightning::chain::keysinterface::InMemorySigner>;
 //! type ChainMonitor = lightning::chain::chainmonitor::ChainMonitor<lightning::chain::keysinterface::InMemorySigner, Arc<ChainFilter>, Arc<TxBroadcaster>, Arc<FeeEstimator>, Arc<Logger>, Arc<DataPersister>>;
-//! type ChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager<ChainMonitor, TxBroadcaster, FeeEstimator, Logger>;
-//! type PeerManager = lightning::ln::peer_handler::SimpleArcPeerManager<lightning_net_tokio::SocketDescriptor, ChainMonitor, TxBroadcaster, FeeEstimator, ChainAccess, Logger>;
+//! type ChannelManager = Arc<lightning::ln::channelmanager::SimpleArcChannelManager<ChainMonitor, TxBroadcaster, FeeEstimator, Logger>>;
+//! type PeerManager = Arc<lightning::ln::peer_handler::SimpleArcPeerManager<lightning_net_tokio::SocketDescriptor, ChainMonitor, TxBroadcaster, FeeEstimator, ChainAccess, Logger>>;
 //!
 //! // Connect to node with pubkey their_node_id at addr:
 //! async fn connect_to_node(peer_manager: PeerManager, chain_monitor: Arc<ChainMonitor>, channel_manager: ChannelManager, their_node_id: PublicKey, addr: SocketAddr) {
index d3e520f7409a0aee5f8a5b40e4e8d469039fcda1..b56ce5d9d052aa38c955d172e498d6cde4260713 100644 (file)
@@ -349,7 +349,7 @@ const ERR: () = "You need at least 32 bit pointers (well, usize, but we'll assum
 /// issues such as overly long function definitions. Note that the ChannelManager can take any
 /// type that implements KeysInterface for its keys manager, but this type alias chooses the
 /// concrete type of the KeysManager.
-pub type SimpleArcChannelManager<M, T, F, L> = Arc<ChannelManager<InMemorySigner, Arc<M>, Arc<T>, Arc<KeysManager>, Arc<F>, Arc<L>>>;
+pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<InMemorySigner, Arc<M>, Arc<T>, Arc<KeysManager>, Arc<F>, Arc<L>>;
 
 /// SimpleRefChannelManager is a type alias for a ChannelManager reference, and is the reference
 /// counterpart to the SimpleArcChannelManager type alias. Use this type by default when you don't
index b977f791389f2eadade27b0d739539f441341290..d37476327ba02750a34f2fa0e8a8bce0dc62a167 100644 (file)
@@ -293,7 +293,7 @@ fn _check_usize_is_32_or_64() {
 /// lifetimes). Other times you can afford a reference, which is more efficient, in which case
 /// SimpleRefPeerManager is the more appropriate type. Defining these type aliases prevents
 /// issues such as overly long function definitions.
-pub type SimpleArcPeerManager<SD, M, T, F, C, L> = Arc<PeerManager<SD, SimpleArcChannelManager<M, T, F, L>, Arc<NetGraphMsgHandler<Arc<C>, Arc<L>>>, Arc<L>>>;
+pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArcChannelManager<M, T, F, L>>, Arc<NetGraphMsgHandler<Arc<C>, Arc<L>>>, Arc<L>>;
 
 /// SimpleRefPeerManager is a type alias for a PeerManager reference, and is the reference
 /// counterpart to the SimpleArcPeerManager type alias. Use this type by default when you don't