Skip to content

Commit

Permalink
Merge pull request #2025 from TheBlueMatt/2023-02-no-pub-genesis-hashes
Browse files Browse the repository at this point in the history
Remove genesis block hash from public API
  • Loading branch information
TheBlueMatt authored Feb 27, 2023
2 parents f71daed + d7c818a commit 16b3c72
Show file tree
Hide file tree
Showing 19 changed files with 55 additions and 90 deletions.
2 changes: 1 addition & 1 deletion fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
let network = Network::Bitcoin;
let params = ChainParameters {
network,
best_block: BestBlock::from_genesis(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),
monitor, keys_manager)
Expand Down
4 changes: 2 additions & 2 deletions fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,15 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
let network = Network::Bitcoin;
let params = ChainParameters {
network,
best_block: BestBlock::from_genesis(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));
// 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.
keys_manager.counter.fetch_sub(3, Ordering::AcqRel);
let our_id = &keys_manager.get_node_id(Recipient::Node).unwrap();
let network_graph = Arc::new(NetworkGraph::new(genesis_block(network).block_hash(), Arc::clone(&logger)));
let network_graph = Arc::new(NetworkGraph::new(network, Arc::clone(&logger)));
let gossip_sync = Arc::new(P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)));
let scorer = FixedPenaltyScorer::with_penalty(0);

Expand Down
4 changes: 1 addition & 3 deletions fuzz/src/process_network_graph.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Imports that need to be added manually
use lightning_rapid_gossip_sync::RapidGossipSync;
use bitcoin::hashes::Hash as TraitImport;

use crate::utils::test_logger;

/// Actual fuzz test, method signature and name are fixed
fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
let block_hash = bitcoin::BlockHash::all_zeros();
let logger = test_logger::TestLogger::new("".to_owned(), out);
let network_graph = lightning::routing::gossip::NetworkGraph::new(block_hash, &logger);
let network_graph = lightning::routing::gossip::NetworkGraph::new(bitcoin::Network::Bitcoin, &logger);
let rapid_sync = RapidGossipSync::new(&network_graph);
let _ = rapid_sync.update_network_graph(data);
}
Expand Down
3 changes: 1 addition & 2 deletions fuzz/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use lightning::util::ser::Readable;
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::PublicKey;
use bitcoin::network::constants::Network;
use bitcoin::blockdata::constants::genesis_block;

use crate::utils::test_logger;

Expand Down Expand Up @@ -189,7 +188,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
let logger = test_logger::TestLogger::new("".to_owned(), out);

let our_pubkey = get_pubkey!();
let net_graph = NetworkGraph::new(genesis_block(Network::Bitcoin).header.block_hash(), &logger);
let net_graph = NetworkGraph::new(Network::Bitcoin, &logger);
let chain_source = FuzzChainSource {
input: Arc::clone(&input),
net_graph: &net_graph,
Expand Down
4 changes: 2 additions & 2 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ mod tests {
let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
let network = Network::Testnet;
let genesis_block = genesis_block(network);
let network_graph = Arc::new(NetworkGraph::new(genesis_block.header.block_hash(), logger.clone()));
let network_graph = Arc::new(NetworkGraph::new(network, logger.clone()));
let scorer = Arc::new(Mutex::new(TestScorer::new()));
let seed = [i as u8; 32];
let router = Arc::new(DefaultRouter::new(network_graph.clone(), logger.clone(), seed, scorer.clone()));
Expand All @@ -949,7 +949,7 @@ mod tests {
let now = Duration::from_secs(genesis_block.header.time as u64);
let keys_manager = Arc::new(KeysManager::new(&seed, now.as_secs(), now.subsec_nanos()));
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_genesis(network);
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 p2p_gossip_sync = Arc::new(P2PGossipSync::new(network_graph.clone(), Some(chain_source.clone()), logger.clone()));
Expand Down
14 changes: 4 additions & 10 deletions lightning-rapid-gossip-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
//! # }
//! # let logger = FakeLogger {};
//!
//! let block_hash = genesis_block(Network::Bitcoin).header.block_hash();
//! let network_graph = NetworkGraph::new(block_hash, &logger);
//! let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
//! let rapid_sync = RapidGossipSync::new(&network_graph);
//! let snapshot_contents: &[u8] = &[0; 0];
//! let new_last_sync_timestamp_result = rapid_sync.update_network_graph(snapshot_contents);
Expand Down Expand Up @@ -161,7 +160,6 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
mod tests {
use std::fs;

use bitcoin::blockdata::constants::genesis_block;
use bitcoin::Network;

use lightning::ln::msgs::DecodeError;
Expand Down Expand Up @@ -225,9 +223,8 @@ mod tests {
let sync_test = FileSyncTest::new(tmp_directory, &valid_response);
let graph_sync_test_file = sync_test.get_test_file_path();

let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

Expand Down Expand Up @@ -258,9 +255,8 @@ mod tests {

#[test]
fn measure_native_read_from_file() {
let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

Expand Down Expand Up @@ -290,7 +286,6 @@ mod tests {
pub mod bench {
use test::Bencher;

use bitcoin::blockdata::constants::genesis_block;
use bitcoin::Network;

use lightning::ln::msgs::DecodeError;
Expand All @@ -301,10 +296,9 @@ pub mod bench {

#[bench]
fn bench_reading_full_graph_from_file(b: &mut Bencher) {
let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
b.iter(|| {
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
let rapid_sync = RapidGossipSync::new(&network_graph);
let sync_result = rapid_sync.sync_network_graph_with_file_path("./res/full_graph.lngossip");
if let Err(crate::error::GraphSyncError::DecodeError(DecodeError::Io(io_error))) = &sync_result {
Expand Down
33 changes: 11 additions & 22 deletions lightning-rapid-gossip-sync/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L

#[cfg(test)]
mod tests {
use bitcoin::blockdata::constants::genesis_block;
use bitcoin::Network;

use lightning::ln::msgs::DecodeError;
Expand Down Expand Up @@ -269,9 +268,8 @@ mod tests {

#[test]
fn network_graph_fails_to_update_from_clipped_input() {
let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

let example_input = vec![
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
Expand Down Expand Up @@ -309,9 +307,8 @@ mod tests {
68, 226, 0, 6, 11, 0, 1, 128,
];

let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

Expand All @@ -338,9 +335,8 @@ mod tests {
2, 68, 226, 0, 6, 11, 0, 1, 128,
];

let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

Expand Down Expand Up @@ -375,9 +371,8 @@ mod tests {
0, 1, 0, 0, 0, 125, 255, 2, 68, 226, 0, 6, 11, 0, 1, 5, 0, 0, 0, 0, 29, 129, 25, 192,
];

let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

Expand Down Expand Up @@ -442,9 +437,8 @@ mod tests {
25, 192,
];

let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

Expand Down Expand Up @@ -502,9 +496,8 @@ mod tests {
25, 192,
];

let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

Expand All @@ -528,9 +521,8 @@ mod tests {

#[test]
fn full_update_succeeds() {
let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

Expand Down Expand Up @@ -560,9 +552,8 @@ mod tests {

#[test]
fn full_update_succeeds_at_the_beginning_of_the_unix_era() {
let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

Expand All @@ -576,14 +567,13 @@ mod tests {
#[test]
fn timestamp_edge_cases_are_handled_correctly() {
// this is the timestamp encoded in the binary data of valid_input below
let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();

let latest_succeeding_time = VALID_BINARY_TIMESTAMP + STALE_RGS_UPDATE_AGE_LIMIT_SECS;
let earliest_failing_time = latest_succeeding_time + 1;

{
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
assert_eq!(network_graph.read_only().channels().len(), 0);

let rapid_sync = RapidGossipSync::new(&network_graph);
Expand All @@ -593,7 +583,7 @@ mod tests {
}

{
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
assert_eq!(network_graph.read_only().channels().len(), 0);

let rapid_sync = RapidGossipSync::new(&network_graph);
Expand Down Expand Up @@ -630,9 +620,8 @@ mod tests {
0, 0, 1,
];

let block_hash = genesis_block(Network::Bitcoin).block_hash();
let logger = TestLogger::new();
let network_graph = NetworkGraph::new(block_hash, &logger);
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
let rapid_sync = RapidGossipSync::new(&network_graph);
let update_result = rapid_sync.update_network_graph(&unknown_version_input[..]);

Expand Down
2 changes: 1 addition & 1 deletion lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4155,7 +4155,7 @@ mod tests {
// Prune with one old state and a holder commitment tx holding a few overlaps with the
// old state.
let shutdown_pubkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
let best_block = BestBlock::from_genesis(Network::Testnet);
let best_block = BestBlock::from_network(Network::Testnet);
let monitor = ChannelMonitor::new(Secp256k1::new(), keys,
Some(ShutdownScript::new_p2wpkh_from_pubkey(shutdown_pubkey).into_inner()), 0, &Script::new(),
(OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct BestBlock {
impl BestBlock {
/// Constructs a `BestBlock` that represents the genesis block at height 0 of the given
/// network.
pub fn from_genesis(network: Network) -> Self {
pub fn from_network(network: Network) -> Self {
BestBlock {
block_hash: genesis_block(network).header.block_hash(),
height: 0,
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7175,7 +7175,7 @@ mod tests {
let secp_ctx = Secp256k1::new();
let seed = [42; 32];
let network = Network::Testnet;
let best_block = BestBlock::from_genesis(network);
let best_block = BestBlock::from_network(network);
let chain_hash = best_block.block_hash();
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);

Expand Down
11 changes: 5 additions & 6 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8673,13 +8673,12 @@ 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_hash = bitcoin::blockdata::constants::genesis_block(network).header.block_hash();

let tx_broadcaster = test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))};
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
let logger_a = test_utils::TestLogger::with_id("node a".to_owned());
let scorer = Mutex::new(test_utils::TestScorer::new());
let router = test_utils::TestRouter::new(Arc::new(NetworkGraph::new(genesis_hash, &logger_a)), &scorer);
let router = test_utils::TestRouter::new(Arc::new(NetworkGraph::new(network, &logger_a)), &scorer);

let mut config: UserConfig = Default::default();
config.channel_handshake_config.minimum_depth = 1;
Expand All @@ -8689,7 +8688,7 @@ pub mod bench {
let keys_manager_a = KeysManager::new(&seed_a, 42, 42);
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_genesis(network),
best_block: BestBlock::from_network(network),
});
let node_a_holder = NodeHolder { node: &node_a };

Expand All @@ -8699,7 +8698,7 @@ pub mod bench {
let keys_manager_b = KeysManager::new(&seed_b, 42, 42);
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_genesis(network),
best_block: BestBlock::from_network(network),
});
let node_b_holder = NodeHolder { node: &node_b };

Expand All @@ -8723,7 +8722,7 @@ pub mod bench {
assert_eq!(&tx_broadcaster.txn_broadcasted.lock().unwrap()[..], &[tx.clone()]);

let block = Block {
header: BlockHeader { version: 0x20000000, prev_blockhash: genesis_hash, merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 },
header: BlockHeader { version: 0x20000000, prev_blockhash: BestBlock::from_network(network).block_hash(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 },
txdata: vec![tx],
};
Listen::block_connected(&node_a, &block, 1);
Expand Down Expand Up @@ -8762,7 +8761,7 @@ pub mod bench {
_ => panic!("Unexpected event"),
}

let dummy_graph = NetworkGraph::new(genesis_hash, &logger_a);
let dummy_graph = NetworkGraph::new(network, &logger_a);

let mut payment_count: u64 = 0;
macro_rules! send_payment {
Expand Down
4 changes: 2 additions & 2 deletions lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,7 +2300,7 @@ pub fn create_node_cfgs<'a>(node_count: usize, chanmon_cfgs: &'a Vec<TestChanMon

for i in 0..node_count {
let chain_monitor = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[i].chain_source), &chanmon_cfgs[i].tx_broadcaster, &chanmon_cfgs[i].logger, &chanmon_cfgs[i].fee_estimator, &chanmon_cfgs[i].persister, &chanmon_cfgs[i].keys_manager);
let network_graph = Arc::new(NetworkGraph::new(chanmon_cfgs[i].chain_source.genesis_hash, &chanmon_cfgs[i].logger));
let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, &chanmon_cfgs[i].logger));
let seed = [i as u8; 32];
nodes.push(NodeCfg {
chain_source: &chanmon_cfgs[i].chain_source,
Expand Down Expand Up @@ -2341,7 +2341,7 @@ pub fn create_node_chanmgrs<'a, 'b>(node_count: usize, cfgs: &'a Vec<NodeCfg<'b>
let network = Network::Testnet;
let params = ChainParameters {
network,
best_block: BestBlock::from_genesis(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);
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5280,7 +5280,7 @@ fn test_key_derivation_params() {
let seed = [42; 32];
let keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
let chain_monitor = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[0].chain_source), &chanmon_cfgs[0].tx_broadcaster, &chanmon_cfgs[0].logger, &chanmon_cfgs[0].fee_estimator, &chanmon_cfgs[0].persister, &keys_manager);
let network_graph = Arc::new(NetworkGraph::new(chanmon_cfgs[0].chain_source.genesis_hash, &chanmon_cfgs[0].logger));
let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, &chanmon_cfgs[0].logger));
let scorer = Mutex::new(test_utils::TestScorer::new());
let router = test_utils::TestRouter::new(network_graph.clone(), &scorer);
let node = NodeCfg { chain_source: &chanmon_cfgs[0].chain_source, logger: &chanmon_cfgs[0].logger, tx_broadcaster: &chanmon_cfgs[0].tx_broadcaster, fee_estimator: &chanmon_cfgs[0].fee_estimator, router, chain_monitor, keys_manager: &keys_manager, network_graph, node_seed: seed, override_init_features: alloc::rc::Rc::new(core::cell::RefCell::new(None)) };
Expand Down
Loading

0 comments on commit 16b3c72

Please sign in to comment.