Skip to content

Refactor dependencies #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ stdin_fuzz = []
[dependencies]
afl = { version = "0.4", optional = true }
lightning = { path = "../lightning", features = ["fuzztarget"] }
bitcoin = { version = "0.21", features = ["fuzztarget"] }
bitcoin_hashes = { version = "0.7", features = ["fuzztarget"] }
bitcoin = { version = "0.23", features = ["fuzztarget"] }
hex = "0.3"
honggfuzz = { version = "0.5", optional = true }
secp256k1 = { version = "0.15", features=["fuzztarget"] }
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git", optional = true }

[build-dependencies]
Expand Down
19 changes: 9 additions & 10 deletions fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use bitcoin::blockdata::script::{Builder, Script};
use bitcoin::blockdata::opcodes;
use bitcoin::network::constants::Network;

use bitcoin_hashes::Hash as TraitImport;
use bitcoin_hashes::hash160::Hash as Hash160;
use bitcoin_hashes::sha256::Hash as Sha256;
use bitcoin_hashes::sha256d::Hash as Sha256d;
use bitcoin::hashes::Hash as TraitImport;
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hash_types::{BlockHash, WPubkeyHash};

use lightning::chain::chaininterface;
use lightning::chain::transaction::OutPoint;
Expand All @@ -40,8 +39,8 @@ use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
use bitcoin::secp256k1::key::{PublicKey,SecretKey};
use bitcoin::secp256k1::Secp256k1;

use std::mem;
use std::cmp::Ordering;
Expand Down Expand Up @@ -114,7 +113,7 @@ impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMon
hash_map::Entry::Occupied(entry) => entry,
hash_map::Entry::Vacant(_) => panic!("Didn't have monitor on update call"),
};
let mut deserialized_monitor = <(Sha256d, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::
let mut deserialized_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::
read(&mut Cursor::new(&map_entry.get().1), Arc::clone(&self.logger)).unwrap().1;
deserialized_monitor.update_monitor(update.clone(), &&TestBroadcaster {}).unwrap();
let mut ser = VecWriter(Vec::new());
Expand Down Expand Up @@ -144,7 +143,7 @@ impl KeysInterface for KeyProvider {
fn get_destination_script(&self) -> Script {
let secp_ctx = Secp256k1::signing_only();
let channel_monitor_claim_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, self.node_id]).unwrap();
let our_channel_monitor_claim_key_hash = Hash160::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
}

Expand Down Expand Up @@ -215,7 +214,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
let mut monitors = HashMap::new();
let mut old_monitors = $old_monitors.latest_monitors.lock().unwrap();
for (outpoint, (update_id, monitor_ser)) in old_monitors.drain() {
monitors.insert(outpoint, <(Sha256d, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
monitors.insert(outpoint, <(BlockHash, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
monitor.latest_monitors.lock().unwrap().insert(outpoint, (update_id, monitor_ser));
}
let mut monitor_refs = HashMap::new();
Expand All @@ -233,7 +232,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
channel_monitors: &mut monitor_refs,
};

(<(Sha256d, ChannelManager<EnforcingChannelKeys, Arc<TestChannelMonitor>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor)
(<(BlockHash, ChannelManager<EnforcingChannelKeys, Arc<TestChannelMonitor>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor)
} }
}

Expand Down
6 changes: 3 additions & 3 deletions fuzz/src/chanmon_deser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is auto-generated by gen_target.sh based on msg_target_template.txt
// To modify it, modify msg_target_template.txt and run gen_target.sh instead.

use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use bitcoin::hash_types::BlockHash;

use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
use lightning::ln::channelmonitor;
Expand All @@ -26,10 +26,10 @@ impl Writer for VecWriter {
#[inline]
pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
let logger = Arc::new(test_logger::TestLogger::new("".to_owned(), out));
if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data), logger.clone()) {
if let Ok((latest_block_hash, monitor)) = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data), logger.clone()) {
let mut w = VecWriter(Vec::new());
monitor.write_for_disk(&mut w).unwrap();
let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
let deserialized_copy = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
assert!(latest_block_hash == deserialized_copy.0);
assert!(monitor == deserialized_copy.1);
}
Expand Down
22 changes: 10 additions & 12 deletions fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use bitcoin::consensus::encode::deserialize;
use bitcoin::network::constants::Network;
use bitcoin::util::hash::BitcoinHash;

use bitcoin_hashes::Hash as TraitImport;
use bitcoin_hashes::HashEngine as TraitImportEngine;
use bitcoin_hashes::sha256::Hash as Sha256;
use bitcoin_hashes::hash160::Hash as Hash160;
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use bitcoin::hashes::Hash as TraitImport;
use bitcoin::hashes::HashEngine as TraitImportEngine;
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};

use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
use lightning::chain::transaction::OutPoint;
Expand All @@ -32,13 +31,12 @@ use lightning::util::config::UserConfig;

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
use bitcoin::secp256k1::key::{PublicKey,SecretKey};
use bitcoin::secp256k1::Secp256k1;

use std::cell::RefCell;
use std::collections::{HashMap, hash_map};
use std::cmp;
use std::hash::Hash;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64,AtomicUsize,Ordering};

Expand Down Expand Up @@ -129,7 +127,7 @@ impl<'a> PartialEq for Peer<'a> {
}
}
impl<'a> Eq for Peer<'a> {}
impl<'a> Hash for Peer<'a> {
impl<'a> std::hash::Hash for Peer<'a> {
fn hash<H : std::hash::Hasher>(&self, h: &mut H) {
self.id.hash(h)
}
Expand All @@ -142,8 +140,8 @@ struct MoneyLossDetector<'a> {

peers: &'a RefCell<[bool; 256]>,
funding_txn: Vec<Transaction>,
txids_confirmed: HashMap<Sha256dHash, usize>,
header_hashes: Vec<Sha256dHash>,
txids_confirmed: HashMap<Txid, usize>,
header_hashes: Vec<BlockHash>,
height: usize,
max_height: usize,
blocks_connected: u32,
Expand Down Expand Up @@ -241,7 +239,7 @@ impl KeysInterface for KeyProvider {
fn get_destination_script(&self) -> Script {
let secp_ctx = Secp256k1::signing_only();
let channel_monitor_claim_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
let our_channel_monitor_claim_key_hash = <Hash160 as bitcoin_hashes::Hash>::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
}

Expand Down
2 changes: 0 additions & 2 deletions fuzz/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
extern crate bitcoin;
extern crate bitcoin_hashes;
extern crate lightning;
extern crate secp256k1;
extern crate hex;

pub mod utils;
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/peer_crypt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor;

use secp256k1::key::{PublicKey,SecretKey};
use bitcoin::secp256k1::key::{PublicKey,SecretKey};

use utils::test_logger;

Expand Down
10 changes: 5 additions & 5 deletions fuzz/src/router.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use bitcoin::blockdata::script::{Script, Builder};
use bitcoin::blockdata::block::Block;
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::hash_types::{Txid, BlockHash};

use lightning::chain::chaininterface::{ChainError,ChainWatchInterface};
use lightning::ln::channelmanager::ChannelDetails;
Expand All @@ -12,7 +12,7 @@ use lightning::ln::router::{Router, RouteHint};
use lightning::util::logger::Logger;
use lightning::util::ser::Readable;

use secp256k1::key::PublicKey;
use bitcoin::secp256k1::key::PublicKey;

use utils::test_logger;

Expand Down Expand Up @@ -72,15 +72,15 @@ struct DummyChainWatcher {
}

impl ChainWatchInterface for DummyChainWatcher {
fn install_watch_tx(&self, _txid: &Sha256dHash, _script_pub_key: &Script) { }
fn install_watch_outpoint(&self, _outpoint: (Sha256dHash, u32), _out_script: &Script) { }
fn install_watch_tx(&self, _txid: &Txid, _script_pub_key: &Script) { }
fn install_watch_outpoint(&self, _outpoint: (Txid, u32), _out_script: &Script) { }
fn watch_all_txn(&self) { }
fn filter_block<'a>(&self, _block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>) {
(Vec::new(), Vec::new())
}
fn reentered(&self) -> usize { 0 }

fn get_chain_utxo(&self, _genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
fn get_chain_utxo(&self, _genesis_hash: BlockHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
match self.input.get_slice(2) {
Some(&[0, _]) => Err(ChainError::NotSupported),
Some(&[1, _]) => Err(ChainError::NotWatched),
Expand Down
4 changes: 1 addition & 3 deletions lightning-net-tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ For Rust-Lightning clients which wish to make direct connections to Lightning P2
"""

[dependencies]
bitcoin = "0.21"
bitcoin_hashes = "0.7"
bitcoin = "0.23"
lightning = { version = "0.0.11", path = "../lightning" }
secp256k1 = "0.15"
tokio = { version = ">=0.2.12", features = [ "io-util", "macros", "rt-core", "sync", "tcp", "time" ] }

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions lightning-net-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! ```
//! use tokio::sync::mpsc;
//! use tokio::net::TcpStream;
//! use secp256k1::key::PublicKey;
//! use bitcoin::secp256k1::key::PublicKey;
//! use lightning::util::events::EventsProvider;
//! use std::net::SocketAddr;
//! use std::sync::Arc;
Expand Down Expand Up @@ -59,7 +59,7 @@
//! }
//! ```

use secp256k1::key::PublicKey;
use bitcoin::secp256k1::key::PublicKey;

use tokio::net::TcpStream;
use tokio::{io, time};
Expand Down Expand Up @@ -481,7 +481,7 @@ mod tests {
use lightning::ln::msgs::*;
use lightning::ln::peer_handler::{MessageHandler, PeerManager};
use lightning::util::events::*;
use secp256k1::{Secp256k1, SecretKey, PublicKey};
use bitcoin::secp256k1::{Secp256k1, SecretKey, PublicKey};

use tokio::sync::mpsc;

Expand Down
8 changes: 3 additions & 5 deletions lightning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Still missing tons of error-handling. See GitHub issues for suggested projects i
[features]
# Supports tracking channels with a non-bitcoin chain hashes. Currently enables all kinds of fun DoS attacks.
non_bitcoin_chain_hash_routing = []
fuzztarget = ["secp256k1/fuzztarget", "bitcoin/fuzztarget", "bitcoin_hashes/fuzztarget"]
fuzztarget = ["bitcoin/fuzztarget"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
Expand All @@ -22,12 +22,10 @@ max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.21"
bitcoin_hashes = "0.7"
secp256k1 = "0.15"
bitcoin = "0.23"

[dev-dependencies.bitcoin]
version = "0.21"
version = "0.23"
features = ["bitcoinconsensus"]

[dev-dependencies]
Expand Down
22 changes: 11 additions & 11 deletions lightning/src/chain/chaininterface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::blockdata::constants::genesis_block;
use bitcoin::util::hash::BitcoinHash;
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use bitcoin::network::constants::Network;
use bitcoin::hash_types::{Txid, BlockHash};

use util::logger::Logger;

Expand Down Expand Up @@ -40,11 +40,11 @@ pub enum ChainError {
/// events).
pub trait ChainWatchInterface: Sync + Send {
/// Provides a txid/random-scriptPubKey-in-the-tx which much be watched for.
fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script);
fn install_watch_tx(&self, txid: &Txid, script_pub_key: &Script);

/// Provides an outpoint which must be watched for, providing any transactions which spend the
/// given outpoint.
fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script);
fn install_watch_outpoint(&self, outpoint: (Txid, u32), out_script: &Script);

/// Indicates that a listener needs to see all transactions.
fn watch_all_txn(&self);
Expand All @@ -53,7 +53,7 @@ pub trait ChainWatchInterface: Sync + Send {
/// short_channel_id (aka unspent_tx_output_identier). For BTC/tBTC channels the top three
/// bytes are the block height, the next 3 the transaction index within the block, and the
/// final two the output within the transaction.
fn get_chain_utxo(&self, genesis_hash: Sha256dHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>;
fn get_chain_utxo(&self, genesis_hash: BlockHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>;

/// Gets the list of transactions and transaction indices that the ChainWatchInterface is
/// watching for.
Expand Down Expand Up @@ -135,11 +135,11 @@ pub struct ChainWatchedUtil {
// We are more conservative in matching during testing to ensure everything matches *exactly*,
// even though during normal runtime we take more optimized match approaches...
#[cfg(test)]
watched_txn: HashSet<(Sha256dHash, Script)>,
watched_txn: HashSet<(Txid, Script)>,
#[cfg(not(test))]
watched_txn: HashSet<Script>,

watched_outpoints: HashSet<(Sha256dHash, u32)>,
watched_outpoints: HashSet<(Txid, u32)>,
}

impl ChainWatchedUtil {
Expand All @@ -154,7 +154,7 @@ impl ChainWatchedUtil {

/// Registers a tx for monitoring, returning true if it was a new tx and false if we'd already
/// been watching for it.
pub fn register_tx(&mut self, txid: &Sha256dHash, script_pub_key: &Script) -> bool {
pub fn register_tx(&mut self, txid: &Txid, script_pub_key: &Script) -> bool {
if self.watch_all { return false; }
#[cfg(test)]
{
Expand All @@ -169,7 +169,7 @@ impl ChainWatchedUtil {

/// Registers an outpoint for monitoring, returning true if it was a new outpoint and false if
/// we'd already been watching for it
pub fn register_outpoint(&mut self, outpoint: (Sha256dHash, u32), _script_pub_key: &Script) -> bool {
pub fn register_outpoint(&mut self, outpoint: (Txid, u32), _script_pub_key: &Script) -> bool {
if self.watch_all { return false; }
self.watched_outpoints.insert(outpoint)
}
Expand Down Expand Up @@ -332,14 +332,14 @@ impl PartialEq for ChainWatchInterfaceUtil {

/// Register listener
impl ChainWatchInterface for ChainWatchInterfaceUtil {
fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script) {
fn install_watch_tx(&self, txid: &Txid, script_pub_key: &Script) {
let mut watched = self.watched.lock().unwrap();
if watched.register_tx(txid, script_pub_key) {
self.reentered.fetch_add(1, Ordering::Relaxed);
}
}

fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script) {
fn install_watch_outpoint(&self, outpoint: (Txid, u32), out_script: &Script) {
let mut watched = self.watched.lock().unwrap();
if watched.register_outpoint(outpoint, out_script) {
self.reentered.fetch_add(1, Ordering::Relaxed);
Expand All @@ -353,7 +353,7 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
}
}

fn get_chain_utxo(&self, genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
fn get_chain_utxo(&self, genesis_hash: BlockHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
if genesis_hash != genesis_block(self.network).header.bitcoin_hash() {
return Err(ChainError::NotWatched);
}
Expand Down
20 changes: 10 additions & 10 deletions lightning/src/chain/keysinterface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use bitcoin::network::constants::Network;
use bitcoin::util::bip32::{ExtendedPrivKey, ExtendedPubKey, ChildNumber};
use bitcoin::util::bip143;

use bitcoin_hashes::{Hash, HashEngine};
use bitcoin_hashes::sha256::HashEngine as Sha256State;
use bitcoin_hashes::sha256::Hash as Sha256;
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use bitcoin_hashes::hash160::Hash as Hash160;
use bitcoin::hashes::{Hash, HashEngine};
use bitcoin::hashes::sha256::HashEngine as Sha256State;
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
use bitcoin::hash_types::WPubkeyHash;

use secp256k1::key::{SecretKey, PublicKey};
use secp256k1::{Secp256k1, Signature, Signing};
use secp256k1;
use bitcoin::secp256k1::key::{SecretKey, PublicKey};
use bitcoin::secp256k1::{Secp256k1, Signature, Signing};
use bitcoin::secp256k1;

use util::byte_utils;
use util::logger::Logger;
Expand Down Expand Up @@ -513,9 +513,9 @@ impl KeysManager {
let node_secret = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(0).unwrap()).expect("Your RNG is busted").private_key.key;
let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) {
Ok(destination_key) => {
let pubkey_hash160 = Hash160::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.key.serialize()[..]);
let wpubkey_hash = WPubkeyHash::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.to_bytes());
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
.push_slice(&pubkey_hash160.into_inner())
.push_slice(&wpubkey_hash.into_inner())
.into_script()
},
Err(_) => panic!("Your RNG is busted"),
Expand Down
Loading