]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Require best block timestamp within ChannelManager::new
authorWilmer Paulino <wilmer@wilmerpaulino.com>
Thu, 22 Jun 2023 22:19:15 +0000 (15:19 -0700)
committerWilmer Paulino <wilmer@wilmerpaulino.com>
Tue, 27 Jun 2023 20:43:14 +0000 (13:43 -0700)
This ensures freshly initialized nodes can proceed to create unexpired
invoices without a call to `best_block_updated`, since an invoice's
expiration delta is applied to `highest_seen_timestamp`.

fuzz/src/chanmon_consistency.rs
fuzz/src/full_stack.rs
lightning-background-processor/src/lib.rs
lightning-invoice/src/utils.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/functional_test_utils.rs

index 309faaa95522f0773080cd9a82bd0aa822f6165b..8f2c30ffa0bc4b3cc0b0656e62b10d523c39e0ca 100644 (file)
@@ -436,11 +436,12 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        config.channel_config.forwarding_fee_proportional_millionths = 0;
                        config.channel_handshake_config.announced_channel = true;
                        let network = Network::Bitcoin;
+                       let best_block_timestamp = genesis_block(network).header.time;
                        let params = ChainParameters {
                                network,
                                best_block: BestBlock::from_network(network),
                        };
-                       (ChannelManager::new($fee_estimator.clone(), monitor.clone(), broadcast.clone(), &router, Arc::clone(&logger), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), config, params),
+                       (ChannelManager::new($fee_estimator.clone(), monitor.clone(), broadcast.clone(), &router, Arc::clone(&logger), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), config, params, best_block_timestamp),
                        monitor, keys_manager)
                } }
        }
index 1192766bf3e0baadb5c61fef3c0c07dbff0f56f3..2b9449d72b8ed00c978bb3ad817062ccef0a1f9a 100644 (file)
@@ -441,11 +441,12 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
        config.channel_config.forwarding_fee_proportional_millionths =  slice_to_be32(get_slice!(4));
        config.channel_handshake_config.announced_channel = get_slice!(1)[0] != 0;
        let network = Network::Bitcoin;
+       let best_block_timestamp = genesis_block(network).header.time;
        let params = ChainParameters {
                network,
                best_block: BestBlock::from_network(network),
        };
-       let channelmanager = Arc::new(ChannelManager::new(fee_est.clone(), monitor.clone(), broadcast.clone(), &router, Arc::clone(&logger), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), config, params));
+       let channelmanager = Arc::new(ChannelManager::new(fee_est.clone(), monitor.clone(), broadcast.clone(), &router, Arc::clone(&logger), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), config, params, best_block_timestamp));
        // Adding new calls to `EntropySource::get_secure_random_bytes` during startup can change all the
        // keys subsequently generated in this test. Rather than regenerating all the messages manually,
        // it's easier to just increment the counter here so the keys don't change.
index 0cfa9801badb3a8d31a30cbdce81b93cbaab1f5a..401c3c19ec2200cf9fd101cbd1fdb2f5b3dcce5b 100644 (file)
@@ -1143,7 +1143,7 @@ mod tests {
                        let chain_monitor = Arc::new(chainmonitor::ChainMonitor::new(Some(chain_source.clone()), tx_broadcaster.clone(), logger.clone(), fee_estimator.clone(), persister.clone()));
                        let best_block = BestBlock::from_network(network);
                        let params = ChainParameters { network, best_block };
-                       let manager = Arc::new(ChannelManager::new(fee_estimator.clone(), chain_monitor.clone(), tx_broadcaster.clone(), router.clone(), logger.clone(), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), UserConfig::default(), params));
+                       let manager = Arc::new(ChannelManager::new(fee_estimator.clone(), chain_monitor.clone(), tx_broadcaster.clone(), router.clone(), logger.clone(), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), UserConfig::default(), params, genesis_block.header.time));
                        let p2p_gossip_sync = Arc::new(P2PGossipSync::new(network_graph.clone(), Some(chain_source.clone()), logger.clone()));
                        let rapid_gossip_sync = Arc::new(RapidGossipSync::new(network_graph.clone(), logger.clone()));
                        let msg_handler = MessageHandler {
index d8e7bf12726454cfec9ac0134e7f3ab9fd6a1406..c052706975968105c95b7dbf08c4ec7132b6c6c5 100644 (file)
@@ -1299,13 +1299,14 @@ mod test {
                } else {
                        None
                };
+               let genesis_timestamp = bitcoin::blockdata::constants::genesis_block(bitcoin::Network::Testnet).header.time as u64;
                let non_default_invoice_expiry_secs = 4200;
 
                let invoice =
                        crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestLogger>(
                                Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs,
                                route_hints, nodes[1].keys_manager, nodes[1].keys_manager, nodes[1].logger,
-                               Currency::BitcoinTestnet, None, Duration::from_secs(1234567)
+                               Currency::BitcoinTestnet, None, Duration::from_secs(genesis_timestamp)
                        ).unwrap();
                let (payment_hash, payment_secret) = (PaymentHash(invoice.payment_hash().into_inner()), *invoice.payment_secret());
                let payment_preimage = if user_generated_pmt_hash {
index fcc672a867c04419ae33c1a4bacc01d22f49dbfe..fab6a2d1340289be0a1fa8d5c7c8752a25ca0bbb 100644 (file)
@@ -1973,6 +1973,8 @@ where
 {
        /// Constructs a new `ChannelManager` to hold several channels and route between them.
        ///
+       /// The current time or latest block header time can be provided as the `current_timestamp`.
+       ///
        /// This is the main "logic hub" for all channel-related actions, and implements
        /// [`ChannelMessageHandler`].
        ///
@@ -1986,7 +1988,11 @@ where
        /// [`block_connected`]: chain::Listen::block_connected
        /// [`block_disconnected`]: chain::Listen::block_disconnected
        /// [`params.best_block.block_hash`]: chain::BestBlock::block_hash
-       pub fn new(fee_est: F, chain_monitor: M, tx_broadcaster: T, router: R, logger: L, entropy_source: ES, node_signer: NS, signer_provider: SP, config: UserConfig, params: ChainParameters) -> Self {
+       pub fn new(
+               fee_est: F, chain_monitor: M, tx_broadcaster: T, router: R, logger: L, entropy_source: ES,
+               node_signer: NS, signer_provider: SP, config: UserConfig, params: ChainParameters,
+               current_timestamp: u32,
+       ) -> Self {
                let mut secp_ctx = Secp256k1::new();
                secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
                let inbound_pmt_key_material = node_signer.get_inbound_payment_key_material();
@@ -2018,7 +2024,7 @@ where
 
                        probing_cookie_secret: entropy_source.get_secure_random_bytes(),
 
-                       highest_seen_timestamp: AtomicUsize::new(0),
+                       highest_seen_timestamp: AtomicUsize::new(current_timestamp as usize),
 
                        per_peer_state: FairRwLock::new(HashMap::new()),
 
@@ -9874,6 +9880,7 @@ pub mod bench {
                // Note that this is unrealistic as each payment send will require at least two fsync
                // calls per node.
                let network = bitcoin::Network::Testnet;
+               let genesis_block = bitcoin::blockdata::constants::genesis_block(network);
 
                let tx_broadcaster = test_utils::TestBroadcaster::new(network);
                let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
@@ -9890,7 +9897,7 @@ pub mod bench {
                let node_a = ChannelManager::new(&fee_estimator, &chain_monitor_a, &tx_broadcaster, &router, &logger_a, &keys_manager_a, &keys_manager_a, &keys_manager_a, config.clone(), ChainParameters {
                        network,
                        best_block: BestBlock::from_network(network),
-               });
+               }, genesis_block.header.time);
                let node_a_holder = ANodeHolder { node: &node_a };
 
                let logger_b = test_utils::TestLogger::with_id("node a".to_owned());
@@ -9900,7 +9907,7 @@ pub mod bench {
                let node_b = ChannelManager::new(&fee_estimator, &chain_monitor_b, &tx_broadcaster, &router, &logger_b, &keys_manager_b, &keys_manager_b, &keys_manager_b, config.clone(), ChainParameters {
                        network,
                        best_block: BestBlock::from_network(network),
-               });
+               }, genesis_block.header.time);
                let node_b_holder = ANodeHolder { node: &node_b };
 
                node_a.peer_connected(&node_b.get_our_node_id(), &Init {
index dc5f2b41fe5055e96d5acf10637eac4d1d4c5109..cdb555c473b0264717f9df9f7593c22fb52ad0a1 100644 (file)
@@ -2572,12 +2572,13 @@ pub fn create_node_chanmgrs<'a, 'b>(node_count: usize, cfgs: &'a Vec<NodeCfg<'b>
        let mut chanmgrs = Vec::new();
        for i in 0..node_count {
                let network = Network::Testnet;
+               let genesis_block = bitcoin::blockdata::constants::genesis_block(network);
                let params = ChainParameters {
                        network,
                        best_block: BestBlock::from_network(network),
                };
                let node = ChannelManager::new(cfgs[i].fee_estimator, &cfgs[i].chain_monitor, cfgs[i].tx_broadcaster, &cfgs[i].router, cfgs[i].logger, cfgs[i].keys_manager,
-                       cfgs[i].keys_manager, cfgs[i].keys_manager, if node_config[i].is_some() { node_config[i].clone().unwrap() } else { test_default_channel_config() }, params);
+                       cfgs[i].keys_manager, cfgs[i].keys_manager, if node_config[i].is_some() { node_config[i].clone().unwrap() } else { test_default_channel_config() }, params, genesis_block.header.time);
                chanmgrs.push(node);
        }