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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,12 @@ build = "build.rs"
# 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"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
max_level_warn = []
max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.13"
Expand Down
12 changes: 10 additions & 2 deletions fuzz/fuzz_targets/channel_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,17 @@ use lightning::ln::msgs::{MsgDecodable, ErrorAction};
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
use lightning::chain::transaction::OutPoint;
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

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

use std::sync::atomic::{AtomicUsize,Ordering};
use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
Expand DownExpand Up@@ -101,6 +107,8 @@ pub fn do_test(data: &[u8]) {
input: &input,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

macro_rules! get_slice {
($len: expr) => {
match input.get_slice($len as usize) {
Expand DownExpand Up@@ -191,7 +199,7 @@ pub fn do_test(data: &[u8]) {
let mut channel = if get_slice!(1)[0] != 0 {
let chan_value = slice_to_be24(get_slice!(3));

let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger));
chan.get_open_channel(Sha256dHash::from(get_slice!(32)), &fee_est).unwrap();
let accept_chan = if get_slice!(1)[0] == 0 {
decode_msg_with_len16!(msgs::AcceptChannel, 270, 1)
Expand All@@ -213,7 +221,7 @@ pub fn do_test(data: &[u8]) {
} else {
decode_msg!(msgs::OpenChannel, 2*32+6*8+4+2*2+6*33+1)
};
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0) {
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger)) {
Ok(chan) => chan,
Err(_) => return,
};
Expand Down
14 changes: 10 additions & 4 deletions fuzz/fuzz_targets/full_stack_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,11 @@ use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
use lightning::ln::router::Router;
use lightning::util::events::{EventsProvider,Event};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
Expand DownExpand Up@@ -169,18 +174,19 @@ pub fn do_test(data: &[u8]) {
Err(_) => return,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());
let monitor = Arc::new(TestChannelMonitor{});
let watch = Arc::new(ChainWatchInterfaceUtil::new());
let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
let broadcast = Arc::new(TestBroadcaster{});

let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone()).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap()));
let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger)));

let peers = RefCell::new([false; 256]);
let handler = PeerManager::new(MessageHandler {
chan_handler: channelmanager.clone(),
route_handler: router.clone(),
}, our_network_key);
}, our_network_key, Arc::clone(&logger));

let mut should_forward = false;
let mut payments_received = Vec::new();
Expand Down
11 changes: 10 additions & 1 deletion fuzz/fuzz_targets/router_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,10 +7,17 @@ use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
use lightning::ln::router::{Router, RouteHint};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

use secp256k1::key::PublicKey;
use secp256k1::Secp256k1;

mod utils;

use utils::test_logger;

use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
((v[0] as u16) << 8*1) |
Expand DownExpand Up@@ -98,8 +105,10 @@ pub fn do_test(data: &[u8]) {
}
}

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

let our_pubkey = get_pubkey!();
let router = Router::new(our_pubkey.clone());
let router = Router::new(our_pubkey.clone(), Arc::clone(&logger));

loop {
match get_slice!(1)[0] {
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/utils/mod.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
pub(crate) mod test_logger;
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/utils/test_logger.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
use lightning::util::logger::{Logger, Level, Record};

pub struct TestLogger {
level: Level,
}

impl TestLogger {
pub fn new() -> TestLogger {
TestLogger {
level: Level::Off,
}
}
pub fn enable(&mut self, level: Level) {
self.level = level;
}
}

impl Logger for TestLogger {
fn log(&self, record: &Record) {
#[cfg(any(test, not(feature = "fuzztarget")))]
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
}
}
11 changes: 7 additions & 4 deletions src/chain/chaininterface.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::util::hash::Sha256dHash;
use std::sync::{Mutex,Weak,MutexGuard};
use util::logger::Logger;
use std::sync::{Mutex,Weak,MutexGuard,Arc};
use std::sync::atomic::{AtomicUsize, Ordering};

/// An interface to request notification of certain scripts as they appear the
Expand DownExpand Up@@ -70,7 +71,8 @@ pub trait FeeEstimator: Sync + Send {
pub struct ChainWatchInterfaceUtil {
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
listeners: Mutex<Vec<Weak<ChainListener>>>,
reentered: AtomicUsize
reentered: AtomicUsize,
logger: Arc<Logger>,
}

/// Register listener
Expand DownExpand Up@@ -100,11 +102,12 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
}

impl ChainWatchInterfaceUtil {
pub fn new() -> ChainWatchInterfaceUtil {
pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
ChainWatchInterfaceUtil {
watched: Mutex::new((Vec::new(), Vec::new(), false)),
listeners: Mutex::new(Vec::new()),
reentered: AtomicUsize::new(1)
reentered: AtomicUsize::new(1),
logger: logger,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ extern crate rand;
extern crate secp256k1;
#[cfg(test)] extern crate hex;

#[macro_use]
pub mod util;
pub mod chain;
pub mod ln;
pub mod util;
21 changes: 17 additions & 4 deletions src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,11 +23,13 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::{transaction_utils,rng};
use util::sha2::Sha256;
use util::logger::{Logger, Record};

use std;
use std::default::Default;
use std::{cmp,mem};
use std::time::Instant;
use std::sync::{Arc};

pub struct ChannelKeys {
pub funding_key: SecretKey,
Expand DownExpand Up@@ -303,6 +305,8 @@ pub struct Channel {
their_shutdown_scriptpubkey: Option<Script>,

channel_monitor: ChannelMonitor,

logger: Arc<Logger>,
}

const OUR_MAX_HTLCS: u16 = 5; //TODO
Expand DownExpand Up@@ -361,7 +365,7 @@ impl Channel {
// Constructors:

/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Channel {
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
panic!("funding value > 2^24");
}
Expand DownExpand Up@@ -429,6 +433,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
}
}

Expand All@@ -446,7 +452,7 @@ impl Channel {
/// Assumes chain_hash has already been checked and corresponds with what we expect!
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
/// that we're rejecting the new channel.
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool) -> Result<Channel, HandleError> {
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
// Check sanity of message fields:
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
Expand DownExpand Up@@ -548,6 +554,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
};

let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
Expand DownExpand Up@@ -1748,7 +1756,7 @@ impl Channel {

match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey) {
Ok(_) => {},
Err(_) => {
Err(_e) => {
// The remote end may have decided to revoke their output due to inconsistent dust
// limits, so check for that case by re-checking the signature here.
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
Expand DownExpand Up@@ -2111,6 +2119,7 @@ impl Channel {
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
Ok(res) => res,
Err(e) => {
log_error!(self, "Got bad signatures: {}!", e.err);
self.channel_monitor.unset_funding_info();
return Err(e);
}
Expand DownExpand Up@@ -2409,10 +2418,13 @@ mod tests {
use ln::chan_utils;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::test_utils;
use util::logger::Logger;
use secp256k1::{Secp256k1,Message,Signature};
use secp256k1::key::{SecretKey,PublicKey};
use crypto::sha2::Sha256;
use crypto::digest::Digest;
use std::sync::Arc;

struct TestFeeEstimator {
fee_est: u64
Expand All@@ -2433,6 +2445,7 @@ mod tests {
fn outbound_commitment_test() {
// Test vectors from BOLT 3 Appendix C:
let feeest = TestFeeEstimator{fee_est: 15000};
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let secp_ctx = Secp256k1::new();

let chan_keys = ChannelKeys {
Expand All@@ -2450,7 +2463,7 @@ mod tests {
assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).unwrap().serialize()[..],
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);

let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42); // Nothing uses their network key in this test
let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42, Arc::clone(&logger)); // Nothing uses their network key in this test
chan.their_to_self_delay = 144;
chan.our_dust_limit_satoshis = 546;

Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,12 @@ build = "build.rs"
# 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"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
max_level_warn = []
max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.13"
Expand Down
12 changes: 10 additions & 2 deletions fuzz/fuzz_targets/channel_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,17 @@ use lightning::ln::msgs::{MsgDecodable, ErrorAction};
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
use lightning::chain::transaction::OutPoint;
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

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

use std::sync::atomic::{AtomicUsize,Ordering};
use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
Expand DownExpand Up@@ -101,6 +107,8 @@ pub fn do_test(data: &[u8]) {
input: &input,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

macro_rules! get_slice {
($len: expr) => {
match input.get_slice($len as usize) {
Expand DownExpand Up@@ -191,7 +199,7 @@ pub fn do_test(data: &[u8]) {
let mut channel = if get_slice!(1)[0] != 0 {
let chan_value = slice_to_be24(get_slice!(3));

let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger));
chan.get_open_channel(Sha256dHash::from(get_slice!(32)), &fee_est).unwrap();
let accept_chan = if get_slice!(1)[0] == 0 {
decode_msg_with_len16!(msgs::AcceptChannel, 270, 1)
Expand All@@ -213,7 +221,7 @@ pub fn do_test(data: &[u8]) {
} else {
decode_msg!(msgs::OpenChannel, 2*32+6*8+4+2*2+6*33+1)
};
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0) {
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger)) {
Ok(chan) => chan,
Err(_) => return,
};
Expand Down
14 changes: 10 additions & 4 deletions fuzz/fuzz_targets/full_stack_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,11 @@ use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
use lightning::ln::router::Router;
use lightning::util::events::{EventsProvider,Event};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
Expand DownExpand Up@@ -169,18 +174,19 @@ pub fn do_test(data: &[u8]) {
Err(_) => return,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());
let monitor = Arc::new(TestChannelMonitor{});
let watch = Arc::new(ChainWatchInterfaceUtil::new());
let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
let broadcast = Arc::new(TestBroadcaster{});

let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone()).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap()));
let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger)));

let peers = RefCell::new([false; 256]);
let handler = PeerManager::new(MessageHandler {
chan_handler: channelmanager.clone(),
route_handler: router.clone(),
}, our_network_key);
}, our_network_key, Arc::clone(&logger));

let mut should_forward = false;
let mut payments_received = Vec::new();
Expand Down
11 changes: 10 additions & 1 deletion fuzz/fuzz_targets/router_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,10 +7,17 @@ use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
use lightning::ln::router::{Router, RouteHint};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

use secp256k1::key::PublicKey;
use secp256k1::Secp256k1;

mod utils;

use utils::test_logger;

use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
((v[0] as u16) << 8*1) |
Expand DownExpand Up@@ -98,8 +105,10 @@ pub fn do_test(data: &[u8]) {
}
}

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

let our_pubkey = get_pubkey!();
let router = Router::new(our_pubkey.clone());
let router = Router::new(our_pubkey.clone(), Arc::clone(&logger));

loop {
match get_slice!(1)[0] {
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/utils/mod.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
pub(crate) mod test_logger;
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/utils/test_logger.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
use lightning::util::logger::{Logger, Level, Record};

pub struct TestLogger {
level: Level,
}

impl TestLogger {
pub fn new() -> TestLogger {
TestLogger {
level: Level::Off,
}
}
pub fn enable(&mut self, level: Level) {
self.level = level;
}
}

impl Logger for TestLogger {
fn log(&self, record: &Record) {
#[cfg(any(test, not(feature = "fuzztarget")))]
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
}
}
11 changes: 7 additions & 4 deletions src/chain/chaininterface.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::util::hash::Sha256dHash;
use std::sync::{Mutex,Weak,MutexGuard};
use util::logger::Logger;
use std::sync::{Mutex,Weak,MutexGuard,Arc};
use std::sync::atomic::{AtomicUsize, Ordering};

/// An interface to request notification of certain scripts as they appear the
Expand DownExpand Up@@ -70,7 +71,8 @@ pub trait FeeEstimator: Sync + Send {
pub struct ChainWatchInterfaceUtil {
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
listeners: Mutex<Vec<Weak<ChainListener>>>,
reentered: AtomicUsize
reentered: AtomicUsize,
logger: Arc<Logger>,
}

/// Register listener
Expand DownExpand Up@@ -100,11 +102,12 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
}

impl ChainWatchInterfaceUtil {
pub fn new() -> ChainWatchInterfaceUtil {
pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
ChainWatchInterfaceUtil {
watched: Mutex::new((Vec::new(), Vec::new(), false)),
listeners: Mutex::new(Vec::new()),
reentered: AtomicUsize::new(1)
reentered: AtomicUsize::new(1),
logger: logger,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ extern crate rand;
extern crate secp256k1;
#[cfg(test)] extern crate hex;

#[macro_use]
pub mod util;
pub mod chain;
pub mod ln;
pub mod util;
21 changes: 17 additions & 4 deletions src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,11 +23,13 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::{transaction_utils,rng};
use util::sha2::Sha256;
use util::logger::{Logger, Record};

use std;
use std::default::Default;
use std::{cmp,mem};
use std::time::Instant;
use std::sync::{Arc};

pub struct ChannelKeys {
pub funding_key: SecretKey,
Expand DownExpand Up@@ -303,6 +305,8 @@ pub struct Channel {
their_shutdown_scriptpubkey: Option<Script>,

channel_monitor: ChannelMonitor,

logger: Arc<Logger>,
}

const OUR_MAX_HTLCS: u16 = 5; //TODO
Expand DownExpand Up@@ -361,7 +365,7 @@ impl Channel {
// Constructors:

/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Channel {
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
panic!("funding value > 2^24");
}
Expand DownExpand Up@@ -429,6 +433,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
}
}

Expand All@@ -446,7 +452,7 @@ impl Channel {
/// Assumes chain_hash has already been checked and corresponds with what we expect!
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
/// that we're rejecting the new channel.
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool) -> Result<Channel, HandleError> {
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
// Check sanity of message fields:
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
Expand DownExpand Up@@ -548,6 +554,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
};

let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
Expand DownExpand Up@@ -1748,7 +1756,7 @@ impl Channel {

match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey) {
Ok(_) => {},
Err(_) => {
Err(_e) => {
// The remote end may have decided to revoke their output due to inconsistent dust
// limits, so check for that case by re-checking the signature here.
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
Expand DownExpand Up@@ -2111,6 +2119,7 @@ impl Channel {
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
Ok(res) => res,
Err(e) => {
log_error!(self, "Got bad signatures: {}!", e.err);
self.channel_monitor.unset_funding_info();
return Err(e);
}
Expand DownExpand Up@@ -2409,10 +2418,13 @@ mod tests {
use ln::chan_utils;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::test_utils;
use util::logger::Logger;
use secp256k1::{Secp256k1,Message,Signature};
use secp256k1::key::{SecretKey,PublicKey};
use crypto::sha2::Sha256;
use crypto::digest::Digest;
use std::sync::Arc;

struct TestFeeEstimator {
fee_est: u64
Expand All@@ -2433,6 +2445,7 @@ mod tests {
fn outbound_commitment_test() {
// Test vectors from BOLT 3 Appendix C:
let feeest = TestFeeEstimator{fee_est: 15000};
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let secp_ctx = Secp256k1::new();

let chan_keys = ChannelKeys {
Expand All@@ -2450,7 +2463,7 @@ mod tests {
assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).unwrap().serialize()[..],
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);

let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42); // Nothing uses their network key in this test
let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42, Arc::clone(&logger)); // Nothing uses their network key in this test
chan.their_to_self_delay = 144;
chan.our_dust_limit_satoshis = 546;

Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,12 @@ build = "build.rs"
# 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"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
max_level_warn = []
max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.13"
Expand Down
12 changes: 10 additions & 2 deletions fuzz/fuzz_targets/channel_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,17 @@ use lightning::ln::msgs::{MsgDecodable, ErrorAction};
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
use lightning::chain::transaction::OutPoint;
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

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

use std::sync::atomic::{AtomicUsize,Ordering};
use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
Expand DownExpand Up@@ -101,6 +107,8 @@ pub fn do_test(data: &[u8]) {
input: &input,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

macro_rules! get_slice {
($len: expr) => {
match input.get_slice($len as usize) {
Expand DownExpand Up@@ -191,7 +199,7 @@ pub fn do_test(data: &[u8]) {
let mut channel = if get_slice!(1)[0] != 0 {
let chan_value = slice_to_be24(get_slice!(3));

let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger));
chan.get_open_channel(Sha256dHash::from(get_slice!(32)), &fee_est).unwrap();
let accept_chan = if get_slice!(1)[0] == 0 {
decode_msg_with_len16!(msgs::AcceptChannel, 270, 1)
Expand All@@ -213,7 +221,7 @@ pub fn do_test(data: &[u8]) {
} else {
decode_msg!(msgs::OpenChannel, 2*32+6*8+4+2*2+6*33+1)
};
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0) {
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger)) {
Ok(chan) => chan,
Err(_) => return,
};
Expand Down
14 changes: 10 additions & 4 deletions fuzz/fuzz_targets/full_stack_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,11 @@ use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
use lightning::ln::router::Router;
use lightning::util::events::{EventsProvider,Event};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
Expand DownExpand Up@@ -169,18 +174,19 @@ pub fn do_test(data: &[u8]) {
Err(_) => return,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());
let monitor = Arc::new(TestChannelMonitor{});
let watch = Arc::new(ChainWatchInterfaceUtil::new());
let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
let broadcast = Arc::new(TestBroadcaster{});

let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone()).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap()));
let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger)));

let peers = RefCell::new([false; 256]);
let handler = PeerManager::new(MessageHandler {
chan_handler: channelmanager.clone(),
route_handler: router.clone(),
}, our_network_key);
}, our_network_key, Arc::clone(&logger));

let mut should_forward = false;
let mut payments_received = Vec::new();
Expand Down
11 changes: 10 additions & 1 deletion fuzz/fuzz_targets/router_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,10 +7,17 @@ use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
use lightning::ln::router::{Router, RouteHint};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

use secp256k1::key::PublicKey;
use secp256k1::Secp256k1;

mod utils;

use utils::test_logger;

use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
((v[0] as u16) << 8*1) |
Expand DownExpand Up@@ -98,8 +105,10 @@ pub fn do_test(data: &[u8]) {
}
}

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

let our_pubkey = get_pubkey!();
let router = Router::new(our_pubkey.clone());
let router = Router::new(our_pubkey.clone(), Arc::clone(&logger));

loop {
match get_slice!(1)[0] {
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/utils/mod.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
pub(crate) mod test_logger;
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/utils/test_logger.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
use lightning::util::logger::{Logger, Level, Record};

pub struct TestLogger {
level: Level,
}

impl TestLogger {
pub fn new() -> TestLogger {
TestLogger {
level: Level::Off,
}
}
pub fn enable(&mut self, level: Level) {
self.level = level;
}
}

impl Logger for TestLogger {
fn log(&self, record: &Record) {
#[cfg(any(test, not(feature = "fuzztarget")))]
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
}
}
11 changes: 7 additions & 4 deletions src/chain/chaininterface.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::util::hash::Sha256dHash;
use std::sync::{Mutex,Weak,MutexGuard};
use util::logger::Logger;
use std::sync::{Mutex,Weak,MutexGuard,Arc};
use std::sync::atomic::{AtomicUsize, Ordering};

/// An interface to request notification of certain scripts as they appear the
Expand DownExpand Up@@ -70,7 +71,8 @@ pub trait FeeEstimator: Sync + Send {
pub struct ChainWatchInterfaceUtil {
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
listeners: Mutex<Vec<Weak<ChainListener>>>,
reentered: AtomicUsize
reentered: AtomicUsize,
logger: Arc<Logger>,
}

/// Register listener
Expand DownExpand Up@@ -100,11 +102,12 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
}

impl ChainWatchInterfaceUtil {
pub fn new() -> ChainWatchInterfaceUtil {
pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
ChainWatchInterfaceUtil {
watched: Mutex::new((Vec::new(), Vec::new(), false)),
listeners: Mutex::new(Vec::new()),
reentered: AtomicUsize::new(1)
reentered: AtomicUsize::new(1),
logger: logger,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ extern crate rand;
extern crate secp256k1;
#[cfg(test)] extern crate hex;

#[macro_use]
pub mod util;
pub mod chain;
pub mod ln;
pub mod util;
21 changes: 17 additions & 4 deletions src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,11 +23,13 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::{transaction_utils,rng};
use util::sha2::Sha256;
use util::logger::{Logger, Record};

use std;
use std::default::Default;
use std::{cmp,mem};
use std::time::Instant;
use std::sync::{Arc};

pub struct ChannelKeys {
pub funding_key: SecretKey,
Expand DownExpand Up@@ -303,6 +305,8 @@ pub struct Channel {
their_shutdown_scriptpubkey: Option<Script>,

channel_monitor: ChannelMonitor,

logger: Arc<Logger>,
}

const OUR_MAX_HTLCS: u16 = 5; //TODO
Expand DownExpand Up@@ -361,7 +365,7 @@ impl Channel {
// Constructors:

/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Channel {
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
panic!("funding value > 2^24");
}
Expand DownExpand Up@@ -429,6 +433,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
}
}

Expand All@@ -446,7 +452,7 @@ impl Channel {
/// Assumes chain_hash has already been checked and corresponds with what we expect!
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
/// that we're rejecting the new channel.
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool) -> Result<Channel, HandleError> {
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
// Check sanity of message fields:
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
Expand DownExpand Up@@ -548,6 +554,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
};

let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
Expand DownExpand Up@@ -1748,7 +1756,7 @@ impl Channel {

match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey) {
Ok(_) => {},
Err(_) => {
Err(_e) => {
// The remote end may have decided to revoke their output due to inconsistent dust
// limits, so check for that case by re-checking the signature here.
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
Expand DownExpand Up@@ -2111,6 +2119,7 @@ impl Channel {
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
Ok(res) => res,
Err(e) => {
log_error!(self, "Got bad signatures: {}!", e.err);
self.channel_monitor.unset_funding_info();
return Err(e);
}
Expand DownExpand Up@@ -2409,10 +2418,13 @@ mod tests {
use ln::chan_utils;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::test_utils;
use util::logger::Logger;
use secp256k1::{Secp256k1,Message,Signature};
use secp256k1::key::{SecretKey,PublicKey};
use crypto::sha2::Sha256;
use crypto::digest::Digest;
use std::sync::Arc;

struct TestFeeEstimator {
fee_est: u64
Expand All@@ -2433,6 +2445,7 @@ mod tests {
fn outbound_commitment_test() {
// Test vectors from BOLT 3 Appendix C:
let feeest = TestFeeEstimator{fee_est: 15000};
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let secp_ctx = Secp256k1::new();

let chan_keys = ChannelKeys {
Expand All@@ -2450,7 +2463,7 @@ mod tests {
assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).unwrap().serialize()[..],
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);

let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42); // Nothing uses their network key in this test
let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42, Arc::clone(&logger)); // Nothing uses their network key in this test
chan.their_to_self_delay = 144;
chan.our_dust_limit_satoshis = 546;

Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,12 @@ build = "build.rs"
# 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"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
max_level_warn = []
max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.13"
Expand Down
12 changes: 10 additions & 2 deletions fuzz/fuzz_targets/channel_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,17 @@ use lightning::ln::msgs::{MsgDecodable, ErrorAction};
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
use lightning::chain::transaction::OutPoint;
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

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

use std::sync::atomic::{AtomicUsize,Ordering};
use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
Expand DownExpand Up@@ -101,6 +107,8 @@ pub fn do_test(data: &[u8]) {
input: &input,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

macro_rules! get_slice {
($len: expr) => {
match input.get_slice($len as usize) {
Expand DownExpand Up@@ -191,7 +199,7 @@ pub fn do_test(data: &[u8]) {
let mut channel = if get_slice!(1)[0] != 0 {
let chan_value = slice_to_be24(get_slice!(3));

let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger));
chan.get_open_channel(Sha256dHash::from(get_slice!(32)), &fee_est).unwrap();
let accept_chan = if get_slice!(1)[0] == 0 {
decode_msg_with_len16!(msgs::AcceptChannel, 270, 1)
Expand All@@ -213,7 +221,7 @@ pub fn do_test(data: &[u8]) {
} else {
decode_msg!(msgs::OpenChannel, 2*32+6*8+4+2*2+6*33+1)
};
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0) {
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger)) {
Ok(chan) => chan,
Err(_) => return,
};
Expand Down
14 changes: 10 additions & 4 deletions fuzz/fuzz_targets/full_stack_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,11 @@ use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
use lightning::ln::router::Router;
use lightning::util::events::{EventsProvider,Event};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
Expand DownExpand Up@@ -169,18 +174,19 @@ pub fn do_test(data: &[u8]) {
Err(_) => return,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());
let monitor = Arc::new(TestChannelMonitor{});
let watch = Arc::new(ChainWatchInterfaceUtil::new());
let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
let broadcast = Arc::new(TestBroadcaster{});

let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone()).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap()));
let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger)));

let peers = RefCell::new([false; 256]);
let handler = PeerManager::new(MessageHandler {
chan_handler: channelmanager.clone(),
route_handler: router.clone(),
}, our_network_key);
}, our_network_key, Arc::clone(&logger));

let mut should_forward = false;
let mut payments_received = Vec::new();
Expand Down
11 changes: 10 additions & 1 deletion fuzz/fuzz_targets/router_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,10 +7,17 @@ use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
use lightning::ln::router::{Router, RouteHint};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

use secp256k1::key::PublicKey;
use secp256k1::Secp256k1;

mod utils;

use utils::test_logger;

use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
((v[0] as u16) << 8*1) |
Expand DownExpand Up@@ -98,8 +105,10 @@ pub fn do_test(data: &[u8]) {
}
}

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

let our_pubkey = get_pubkey!();
let router = Router::new(our_pubkey.clone());
let router = Router::new(our_pubkey.clone(), Arc::clone(&logger));

loop {
match get_slice!(1)[0] {
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/utils/mod.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
pub(crate) mod test_logger;
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/utils/test_logger.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
use lightning::util::logger::{Logger, Level, Record};

pub struct TestLogger {
level: Level,
}

impl TestLogger {
pub fn new() -> TestLogger {
TestLogger {
level: Level::Off,
}
}
pub fn enable(&mut self, level: Level) {
self.level = level;
}
}

impl Logger for TestLogger {
fn log(&self, record: &Record) {
#[cfg(any(test, not(feature = "fuzztarget")))]
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
}
}
11 changes: 7 additions & 4 deletions src/chain/chaininterface.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::util::hash::Sha256dHash;
use std::sync::{Mutex,Weak,MutexGuard};
use util::logger::Logger;
use std::sync::{Mutex,Weak,MutexGuard,Arc};
use std::sync::atomic::{AtomicUsize, Ordering};

/// An interface to request notification of certain scripts as they appear the
Expand DownExpand Up@@ -70,7 +71,8 @@ pub trait FeeEstimator: Sync + Send {
pub struct ChainWatchInterfaceUtil {
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
listeners: Mutex<Vec<Weak<ChainListener>>>,
reentered: AtomicUsize
reentered: AtomicUsize,
logger: Arc<Logger>,
}

/// Register listener
Expand DownExpand Up@@ -100,11 +102,12 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
}

impl ChainWatchInterfaceUtil {
pub fn new() -> ChainWatchInterfaceUtil {
pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
ChainWatchInterfaceUtil {
watched: Mutex::new((Vec::new(), Vec::new(), false)),
listeners: Mutex::new(Vec::new()),
reentered: AtomicUsize::new(1)
reentered: AtomicUsize::new(1),
logger: logger,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ extern crate rand;
extern crate secp256k1;
#[cfg(test)] extern crate hex;

#[macro_use]
pub mod util;
pub mod chain;
pub mod ln;
pub mod util;
21 changes: 17 additions & 4 deletions src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,11 +23,13 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::{transaction_utils,rng};
use util::sha2::Sha256;
use util::logger::{Logger, Record};

use std;
use std::default::Default;
use std::{cmp,mem};
use std::time::Instant;
use std::sync::{Arc};

pub struct ChannelKeys {
pub funding_key: SecretKey,
Expand DownExpand Up@@ -303,6 +305,8 @@ pub struct Channel {
their_shutdown_scriptpubkey: Option<Script>,

channel_monitor: ChannelMonitor,

logger: Arc<Logger>,
}

const OUR_MAX_HTLCS: u16 = 5; //TODO
Expand DownExpand Up@@ -361,7 +365,7 @@ impl Channel {
// Constructors:

/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Channel {
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
panic!("funding value > 2^24");
}
Expand DownExpand Up@@ -429,6 +433,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
}
}

Expand All@@ -446,7 +452,7 @@ impl Channel {
/// Assumes chain_hash has already been checked and corresponds with what we expect!
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
/// that we're rejecting the new channel.
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool) -> Result<Channel, HandleError> {
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
// Check sanity of message fields:
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
Expand DownExpand Up@@ -548,6 +554,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
};

let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
Expand DownExpand Up@@ -1748,7 +1756,7 @@ impl Channel {

match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey) {
Ok(_) => {},
Err(_) => {
Err(_e) => {
// The remote end may have decided to revoke their output due to inconsistent dust
// limits, so check for that case by re-checking the signature here.
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
Expand DownExpand Up@@ -2111,6 +2119,7 @@ impl Channel {
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
Ok(res) => res,
Err(e) => {
log_error!(self, "Got bad signatures: {}!", e.err);
self.channel_monitor.unset_funding_info();
return Err(e);
}
Expand DownExpand Up@@ -2409,10 +2418,13 @@ mod tests {
use ln::chan_utils;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::test_utils;
use util::logger::Logger;
use secp256k1::{Secp256k1,Message,Signature};
use secp256k1::key::{SecretKey,PublicKey};
use crypto::sha2::Sha256;
use crypto::digest::Digest;
use std::sync::Arc;

struct TestFeeEstimator {
fee_est: u64
Expand All@@ -2433,6 +2445,7 @@ mod tests {
fn outbound_commitment_test() {
// Test vectors from BOLT 3 Appendix C:
let feeest = TestFeeEstimator{fee_est: 15000};
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let secp_ctx = Secp256k1::new();

let chan_keys = ChannelKeys {
Expand All@@ -2450,7 +2463,7 @@ mod tests {
assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).unwrap().serialize()[..],
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);

let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42); // Nothing uses their network key in this test
let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42, Arc::clone(&logger)); // Nothing uses their network key in this test
chan.their_to_self_delay = 144;
chan.our_dust_limit_satoshis = 546;

Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,12 @@ build = "build.rs"
# 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"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
max_level_warn = []
max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.13"
Expand Down
12 changes: 10 additions & 2 deletions fuzz/fuzz_targets/channel_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,17 @@ use lightning::ln::msgs::{MsgDecodable, ErrorAction};
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
use lightning::chain::transaction::OutPoint;
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

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

use std::sync::atomic::{AtomicUsize,Ordering};
use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
Expand DownExpand Up@@ -101,6 +107,8 @@ pub fn do_test(data: &[u8]) {
input: &input,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

macro_rules! get_slice {
($len: expr) => {
match input.get_slice($len as usize) {
Expand DownExpand Up@@ -191,7 +199,7 @@ pub fn do_test(data: &[u8]) {
let mut channel = if get_slice!(1)[0] != 0 {
let chan_value = slice_to_be24(get_slice!(3));

let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger));
chan.get_open_channel(Sha256dHash::from(get_slice!(32)), &fee_est).unwrap();
let accept_chan = if get_slice!(1)[0] == 0 {
decode_msg_with_len16!(msgs::AcceptChannel, 270, 1)
Expand All@@ -213,7 +221,7 @@ pub fn do_test(data: &[u8]) {
} else {
decode_msg!(msgs::OpenChannel, 2*32+6*8+4+2*2+6*33+1)
};
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0) {
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger)) {
Ok(chan) => chan,
Err(_) => return,
};
Expand Down
14 changes: 10 additions & 4 deletions fuzz/fuzz_targets/full_stack_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,11 @@ use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
use lightning::ln::router::Router;
use lightning::util::events::{EventsProvider,Event};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
Expand DownExpand Up@@ -169,18 +174,19 @@ pub fn do_test(data: &[u8]) {
Err(_) => return,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());
let monitor = Arc::new(TestChannelMonitor{});
let watch = Arc::new(ChainWatchInterfaceUtil::new());
let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
let broadcast = Arc::new(TestBroadcaster{});

let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone()).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap()));
let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger)));

let peers = RefCell::new([false; 256]);
let handler = PeerManager::new(MessageHandler {
chan_handler: channelmanager.clone(),
route_handler: router.clone(),
}, our_network_key);
}, our_network_key, Arc::clone(&logger));

let mut should_forward = false;
let mut payments_received = Vec::new();
Expand Down
11 changes: 10 additions & 1 deletion fuzz/fuzz_targets/router_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,10 +7,17 @@ use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
use lightning::ln::router::{Router, RouteHint};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

use secp256k1::key::PublicKey;
use secp256k1::Secp256k1;

mod utils;

use utils::test_logger;

use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
((v[0] as u16) << 8*1) |
Expand DownExpand Up@@ -98,8 +105,10 @@ pub fn do_test(data: &[u8]) {
}
}

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

let our_pubkey = get_pubkey!();
let router = Router::new(our_pubkey.clone());
let router = Router::new(our_pubkey.clone(), Arc::clone(&logger));

loop {
match get_slice!(1)[0] {
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/utils/mod.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
pub(crate) mod test_logger;
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/utils/test_logger.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
use lightning::util::logger::{Logger, Level, Record};

pub struct TestLogger {
level: Level,
}

impl TestLogger {
pub fn new() -> TestLogger {
TestLogger {
level: Level::Off,
}
}
pub fn enable(&mut self, level: Level) {
self.level = level;
}
}

impl Logger for TestLogger {
fn log(&self, record: &Record) {
#[cfg(any(test, not(feature = "fuzztarget")))]
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
}
}
11 changes: 7 additions & 4 deletions src/chain/chaininterface.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::util::hash::Sha256dHash;
use std::sync::{Mutex,Weak,MutexGuard};
use util::logger::Logger;
use std::sync::{Mutex,Weak,MutexGuard,Arc};
use std::sync::atomic::{AtomicUsize, Ordering};

/// An interface to request notification of certain scripts as they appear the
Expand DownExpand Up@@ -70,7 +71,8 @@ pub trait FeeEstimator: Sync + Send {
pub struct ChainWatchInterfaceUtil {
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
listeners: Mutex<Vec<Weak<ChainListener>>>,
reentered: AtomicUsize
reentered: AtomicUsize,
logger: Arc<Logger>,
}

/// Register listener
Expand DownExpand Up@@ -100,11 +102,12 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
}

impl ChainWatchInterfaceUtil {
pub fn new() -> ChainWatchInterfaceUtil {
pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
ChainWatchInterfaceUtil {
watched: Mutex::new((Vec::new(), Vec::new(), false)),
listeners: Mutex::new(Vec::new()),
reentered: AtomicUsize::new(1)
reentered: AtomicUsize::new(1),
logger: logger,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ extern crate rand;
extern crate secp256k1;
#[cfg(test)] extern crate hex;

#[macro_use]
pub mod util;
pub mod chain;
pub mod ln;
pub mod util;
21 changes: 17 additions & 4 deletions src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,11 +23,13 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::{transaction_utils,rng};
use util::sha2::Sha256;
use util::logger::{Logger, Record};

use std;
use std::default::Default;
use std::{cmp,mem};
use std::time::Instant;
use std::sync::{Arc};

pub struct ChannelKeys {
pub funding_key: SecretKey,
Expand DownExpand Up@@ -303,6 +305,8 @@ pub struct Channel {
their_shutdown_scriptpubkey: Option<Script>,

channel_monitor: ChannelMonitor,

logger: Arc<Logger>,
}

const OUR_MAX_HTLCS: u16 = 5; //TODO
Expand DownExpand Up@@ -361,7 +365,7 @@ impl Channel {
// Constructors:

/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Channel {
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
panic!("funding value > 2^24");
}
Expand DownExpand Up@@ -429,6 +433,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
}
}

Expand All@@ -446,7 +452,7 @@ impl Channel {
/// Assumes chain_hash has already been checked and corresponds with what we expect!
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
/// that we're rejecting the new channel.
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool) -> Result<Channel, HandleError> {
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
// Check sanity of message fields:
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
Expand DownExpand Up@@ -548,6 +554,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
};

let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
Expand DownExpand Up@@ -1748,7 +1756,7 @@ impl Channel {

match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey) {
Ok(_) => {},
Err(_) => {
Err(_e) => {
// The remote end may have decided to revoke their output due to inconsistent dust
// limits, so check for that case by re-checking the signature here.
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
Expand DownExpand Up@@ -2111,6 +2119,7 @@ impl Channel {
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
Ok(res) => res,
Err(e) => {
log_error!(self, "Got bad signatures: {}!", e.err);
self.channel_monitor.unset_funding_info();
return Err(e);
}
Expand DownExpand Up@@ -2409,10 +2418,13 @@ mod tests {
use ln::chan_utils;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::test_utils;
use util::logger::Logger;
use secp256k1::{Secp256k1,Message,Signature};
use secp256k1::key::{SecretKey,PublicKey};
use crypto::sha2::Sha256;
use crypto::digest::Digest;
use std::sync::Arc;

struct TestFeeEstimator {
fee_est: u64
Expand All@@ -2433,6 +2445,7 @@ mod tests {
fn outbound_commitment_test() {
// Test vectors from BOLT 3 Appendix C:
let feeest = TestFeeEstimator{fee_est: 15000};
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let secp_ctx = Secp256k1::new();

let chan_keys = ChannelKeys {
Expand All@@ -2450,7 +2463,7 @@ mod tests {
assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).unwrap().serialize()[..],
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);

let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42); // Nothing uses their network key in this test
let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42, Arc::clone(&logger)); // Nothing uses their network key in this test
chan.their_to_self_delay = 144;
chan.our_dust_limit_satoshis = 546;

Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,12 @@ build = "build.rs"
# 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"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
max_level_warn = []
max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.13"
Expand Down
12 changes: 10 additions & 2 deletions fuzz/fuzz_targets/channel_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,17 @@ use lightning::ln::msgs::{MsgDecodable, ErrorAction};
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
use lightning::chain::transaction::OutPoint;
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

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

use std::sync::atomic::{AtomicUsize,Ordering};
use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
Expand DownExpand Up@@ -101,6 +107,8 @@ pub fn do_test(data: &[u8]) {
input: &input,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

macro_rules! get_slice {
($len: expr) => {
match input.get_slice($len as usize) {
Expand DownExpand Up@@ -191,7 +199,7 @@ pub fn do_test(data: &[u8]) {
let mut channel = if get_slice!(1)[0] != 0 {
let chan_value = slice_to_be24(get_slice!(3));

let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger));
chan.get_open_channel(Sha256dHash::from(get_slice!(32)), &fee_est).unwrap();
let accept_chan = if get_slice!(1)[0] == 0 {
decode_msg_with_len16!(msgs::AcceptChannel, 270, 1)
Expand All@@ -213,7 +221,7 @@ pub fn do_test(data: &[u8]) {
} else {
decode_msg!(msgs::OpenChannel, 2*32+6*8+4+2*2+6*33+1)
};
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0) {
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger)) {
Ok(chan) => chan,
Err(_) => return,
};
Expand Down
14 changes: 10 additions & 4 deletions fuzz/fuzz_targets/full_stack_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,11 @@ use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
use lightning::ln::router::Router;
use lightning::util::events::{EventsProvider,Event};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
Expand DownExpand Up@@ -169,18 +174,19 @@ pub fn do_test(data: &[u8]) {
Err(_) => return,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());
let monitor = Arc::new(TestChannelMonitor{});
let watch = Arc::new(ChainWatchInterfaceUtil::new());
let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
let broadcast = Arc::new(TestBroadcaster{});

let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone()).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap()));
let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger)));

let peers = RefCell::new([false; 256]);
let handler = PeerManager::new(MessageHandler {
chan_handler: channelmanager.clone(),
route_handler: router.clone(),
}, our_network_key);
}, our_network_key, Arc::clone(&logger));

let mut should_forward = false;
let mut payments_received = Vec::new();
Expand Down
11 changes: 10 additions & 1 deletion fuzz/fuzz_targets/router_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,10 +7,17 @@ use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
use lightning::ln::router::{Router, RouteHint};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

use secp256k1::key::PublicKey;
use secp256k1::Secp256k1;

mod utils;

use utils::test_logger;

use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
((v[0] as u16) << 8*1) |
Expand DownExpand Up@@ -98,8 +105,10 @@ pub fn do_test(data: &[u8]) {
}
}

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

let our_pubkey = get_pubkey!();
let router = Router::new(our_pubkey.clone());
let router = Router::new(our_pubkey.clone(), Arc::clone(&logger));

loop {
match get_slice!(1)[0] {
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/utils/mod.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
pub(crate) mod test_logger;
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/utils/test_logger.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
use lightning::util::logger::{Logger, Level, Record};

pub struct TestLogger {
level: Level,
}

impl TestLogger {
pub fn new() -> TestLogger {
TestLogger {
level: Level::Off,
}
}
pub fn enable(&mut self, level: Level) {
self.level = level;
}
}

impl Logger for TestLogger {
fn log(&self, record: &Record) {
#[cfg(any(test, not(feature = "fuzztarget")))]
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
}
}
11 changes: 7 additions & 4 deletions src/chain/chaininterface.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::util::hash::Sha256dHash;
use std::sync::{Mutex,Weak,MutexGuard};
use util::logger::Logger;
use std::sync::{Mutex,Weak,MutexGuard,Arc};
use std::sync::atomic::{AtomicUsize, Ordering};

/// An interface to request notification of certain scripts as they appear the
Expand DownExpand Up@@ -70,7 +71,8 @@ pub trait FeeEstimator: Sync + Send {
pub struct ChainWatchInterfaceUtil {
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
listeners: Mutex<Vec<Weak<ChainListener>>>,
reentered: AtomicUsize
reentered: AtomicUsize,
logger: Arc<Logger>,
}

/// Register listener
Expand DownExpand Up@@ -100,11 +102,12 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
}

impl ChainWatchInterfaceUtil {
pub fn new() -> ChainWatchInterfaceUtil {
pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
ChainWatchInterfaceUtil {
watched: Mutex::new((Vec::new(), Vec::new(), false)),
listeners: Mutex::new(Vec::new()),
reentered: AtomicUsize::new(1)
reentered: AtomicUsize::new(1),
logger: logger,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ extern crate rand;
extern crate secp256k1;
#[cfg(test)] extern crate hex;

#[macro_use]
pub mod util;
pub mod chain;
pub mod ln;
pub mod util;
21 changes: 17 additions & 4 deletions src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,11 +23,13 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::{transaction_utils,rng};
use util::sha2::Sha256;
use util::logger::{Logger, Record};

use std;
use std::default::Default;
use std::{cmp,mem};
use std::time::Instant;
use std::sync::{Arc};

pub struct ChannelKeys {
pub funding_key: SecretKey,
Expand DownExpand Up@@ -303,6 +305,8 @@ pub struct Channel {
their_shutdown_scriptpubkey: Option<Script>,

channel_monitor: ChannelMonitor,

logger: Arc<Logger>,
}

const OUR_MAX_HTLCS: u16 = 5; //TODO
Expand DownExpand Up@@ -361,7 +365,7 @@ impl Channel {
// Constructors:

/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Channel {
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
panic!("funding value > 2^24");
}
Expand DownExpand Up@@ -429,6 +433,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
}
}

Expand All@@ -446,7 +452,7 @@ impl Channel {
/// Assumes chain_hash has already been checked and corresponds with what we expect!
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
/// that we're rejecting the new channel.
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool) -> Result<Channel, HandleError> {
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
// Check sanity of message fields:
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
Expand DownExpand Up@@ -548,6 +554,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
};

let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
Expand DownExpand Up@@ -1748,7 +1756,7 @@ impl Channel {

match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey) {
Ok(_) => {},
Err(_) => {
Err(_e) => {
// The remote end may have decided to revoke their output due to inconsistent dust
// limits, so check for that case by re-checking the signature here.
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
Expand DownExpand Up@@ -2111,6 +2119,7 @@ impl Channel {
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
Ok(res) => res,
Err(e) => {
log_error!(self, "Got bad signatures: {}!", e.err);
self.channel_monitor.unset_funding_info();
return Err(e);
}
Expand DownExpand Up@@ -2409,10 +2418,13 @@ mod tests {
use ln::chan_utils;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::test_utils;
use util::logger::Logger;
use secp256k1::{Secp256k1,Message,Signature};
use secp256k1::key::{SecretKey,PublicKey};
use crypto::sha2::Sha256;
use crypto::digest::Digest;
use std::sync::Arc;

struct TestFeeEstimator {
fee_est: u64
Expand All@@ -2433,6 +2445,7 @@ mod tests {
fn outbound_commitment_test() {
// Test vectors from BOLT 3 Appendix C:
let feeest = TestFeeEstimator{fee_est: 15000};
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let secp_ctx = Secp256k1::new();

let chan_keys = ChannelKeys {
Expand All@@ -2450,7 +2463,7 @@ mod tests {
assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).unwrap().serialize()[..],
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);

let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42); // Nothing uses their network key in this test
let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42, Arc::clone(&logger)); // Nothing uses their network key in this test
chan.their_to_self_delay = 144;
chan.our_dust_limit_satoshis = 546;

Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,12 @@ build = "build.rs"
# 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"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
max_level_warn = []
max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.13"
Expand Down
12 changes: 10 additions & 2 deletions fuzz/fuzz_targets/channel_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,17 @@ use lightning::ln::msgs::{MsgDecodable, ErrorAction};
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
use lightning::chain::transaction::OutPoint;
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

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

use std::sync::atomic::{AtomicUsize,Ordering};
use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
Expand DownExpand Up@@ -101,6 +107,8 @@ pub fn do_test(data: &[u8]) {
input: &input,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

macro_rules! get_slice {
($len: expr) => {
match input.get_slice($len as usize) {
Expand DownExpand Up@@ -191,7 +199,7 @@ pub fn do_test(data: &[u8]) {
let mut channel = if get_slice!(1)[0] != 0 {
let chan_value = slice_to_be24(get_slice!(3));

let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger));
chan.get_open_channel(Sha256dHash::from(get_slice!(32)), &fee_est).unwrap();
let accept_chan = if get_slice!(1)[0] == 0 {
decode_msg_with_len16!(msgs::AcceptChannel, 270, 1)
Expand All@@ -213,7 +221,7 @@ pub fn do_test(data: &[u8]) {
} else {
decode_msg!(msgs::OpenChannel, 2*32+6*8+4+2*2+6*33+1)
};
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0) {
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger)) {
Ok(chan) => chan,
Err(_) => return,
};
Expand Down
14 changes: 10 additions & 4 deletions fuzz/fuzz_targets/full_stack_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,11 @@ use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
use lightning::ln::router::Router;
use lightning::util::events::{EventsProvider,Event};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
Expand DownExpand Up@@ -169,18 +174,19 @@ pub fn do_test(data: &[u8]) {
Err(_) => return,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());
let monitor = Arc::new(TestChannelMonitor{});
let watch = Arc::new(ChainWatchInterfaceUtil::new());
let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
let broadcast = Arc::new(TestBroadcaster{});

let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone()).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap()));
let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger)));

let peers = RefCell::new([false; 256]);
let handler = PeerManager::new(MessageHandler {
chan_handler: channelmanager.clone(),
route_handler: router.clone(),
}, our_network_key);
}, our_network_key, Arc::clone(&logger));

let mut should_forward = false;
let mut payments_received = Vec::new();
Expand Down
11 changes: 10 additions & 1 deletion fuzz/fuzz_targets/router_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,10 +7,17 @@ use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
use lightning::ln::router::{Router, RouteHint};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

use secp256k1::key::PublicKey;
use secp256k1::Secp256k1;

mod utils;

use utils::test_logger;

use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
((v[0] as u16) << 8*1) |
Expand DownExpand Up@@ -98,8 +105,10 @@ pub fn do_test(data: &[u8]) {
}
}

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

let our_pubkey = get_pubkey!();
let router = Router::new(our_pubkey.clone());
let router = Router::new(our_pubkey.clone(), Arc::clone(&logger));

loop {
match get_slice!(1)[0] {
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/utils/mod.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
pub(crate) mod test_logger;
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/utils/test_logger.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
use lightning::util::logger::{Logger, Level, Record};

pub struct TestLogger {
level: Level,
}

impl TestLogger {
pub fn new() -> TestLogger {
TestLogger {
level: Level::Off,
}
}
pub fn enable(&mut self, level: Level) {
self.level = level;
}
}

impl Logger for TestLogger {
fn log(&self, record: &Record) {
#[cfg(any(test, not(feature = "fuzztarget")))]
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
}
}
11 changes: 7 additions & 4 deletions src/chain/chaininterface.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::util::hash::Sha256dHash;
use std::sync::{Mutex,Weak,MutexGuard};
use util::logger::Logger;
use std::sync::{Mutex,Weak,MutexGuard,Arc};
use std::sync::atomic::{AtomicUsize, Ordering};

/// An interface to request notification of certain scripts as they appear the
Expand DownExpand Up@@ -70,7 +71,8 @@ pub trait FeeEstimator: Sync + Send {
pub struct ChainWatchInterfaceUtil {
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
listeners: Mutex<Vec<Weak<ChainListener>>>,
reentered: AtomicUsize
reentered: AtomicUsize,
logger: Arc<Logger>,
}

/// Register listener
Expand DownExpand Up@@ -100,11 +102,12 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
}

impl ChainWatchInterfaceUtil {
pub fn new() -> ChainWatchInterfaceUtil {
pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
ChainWatchInterfaceUtil {
watched: Mutex::new((Vec::new(), Vec::new(), false)),
listeners: Mutex::new(Vec::new()),
reentered: AtomicUsize::new(1)
reentered: AtomicUsize::new(1),
logger: logger,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ extern crate rand;
extern crate secp256k1;
#[cfg(test)] extern crate hex;

#[macro_use]
pub mod util;
pub mod chain;
pub mod ln;
pub mod util;
21 changes: 17 additions & 4 deletions src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,11 +23,13 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::{transaction_utils,rng};
use util::sha2::Sha256;
use util::logger::{Logger, Record};

use std;
use std::default::Default;
use std::{cmp,mem};
use std::time::Instant;
use std::sync::{Arc};

pub struct ChannelKeys {
pub funding_key: SecretKey,
Expand DownExpand Up@@ -303,6 +305,8 @@ pub struct Channel {
their_shutdown_scriptpubkey: Option<Script>,

channel_monitor: ChannelMonitor,

logger: Arc<Logger>,
}

const OUR_MAX_HTLCS: u16 = 5; //TODO
Expand DownExpand Up@@ -361,7 +365,7 @@ impl Channel {
// Constructors:

/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Channel {
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
panic!("funding value > 2^24");
}
Expand DownExpand Up@@ -429,6 +433,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
}
}

Expand All@@ -446,7 +452,7 @@ impl Channel {
/// Assumes chain_hash has already been checked and corresponds with what we expect!
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
/// that we're rejecting the new channel.
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool) -> Result<Channel, HandleError> {
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
// Check sanity of message fields:
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
Expand DownExpand Up@@ -548,6 +554,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
};

let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
Expand DownExpand Up@@ -1748,7 +1756,7 @@ impl Channel {

match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey) {
Ok(_) => {},
Err(_) => {
Err(_e) => {
// The remote end may have decided to revoke their output due to inconsistent dust
// limits, so check for that case by re-checking the signature here.
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
Expand DownExpand Up@@ -2111,6 +2119,7 @@ impl Channel {
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
Ok(res) => res,
Err(e) => {
log_error!(self, "Got bad signatures: {}!", e.err);
self.channel_monitor.unset_funding_info();
return Err(e);
}
Expand DownExpand Up@@ -2409,10 +2418,13 @@ mod tests {
use ln::chan_utils;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::test_utils;
use util::logger::Logger;
use secp256k1::{Secp256k1,Message,Signature};
use secp256k1::key::{SecretKey,PublicKey};
use crypto::sha2::Sha256;
use crypto::digest::Digest;
use std::sync::Arc;

struct TestFeeEstimator {
fee_est: u64
Expand All@@ -2433,6 +2445,7 @@ mod tests {
fn outbound_commitment_test() {
// Test vectors from BOLT 3 Appendix C:
let feeest = TestFeeEstimator{fee_est: 15000};
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let secp_ctx = Secp256k1::new();

let chan_keys = ChannelKeys {
Expand All@@ -2450,7 +2463,7 @@ mod tests {
assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).unwrap().serialize()[..],
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);

let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42); // Nothing uses their network key in this test
let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42, Arc::clone(&logger)); // Nothing uses their network key in this test
chan.their_to_self_delay = 144;
chan.our_dust_limit_satoshis = 546;

Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,12 @@ build = "build.rs"
# 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"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
max_level_warn = []
max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.13"
Expand Down
12 changes: 10 additions & 2 deletions fuzz/fuzz_targets/channel_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,17 @@ use lightning::ln::msgs::{MsgDecodable, ErrorAction};
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
use lightning::chain::transaction::OutPoint;
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

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

use std::sync::atomic::{AtomicUsize,Ordering};
use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
Expand DownExpand Up@@ -101,6 +107,8 @@ pub fn do_test(data: &[u8]) {
input: &input,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

macro_rules! get_slice {
($len: expr) => {
match input.get_slice($len as usize) {
Expand DownExpand Up@@ -191,7 +199,7 @@ pub fn do_test(data: &[u8]) {
let mut channel = if get_slice!(1)[0] != 0 {
let chan_value = slice_to_be24(get_slice!(3));

let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
let mut chan = Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger));
chan.get_open_channel(Sha256dHash::from(get_slice!(32)), &fee_est).unwrap();
let accept_chan = if get_slice!(1)[0] == 0 {
decode_msg_with_len16!(msgs::AcceptChannel, 270, 1)
Expand All@@ -213,7 +221,7 @@ pub fn do_test(data: &[u8]) {
} else {
decode_msg!(msgs::OpenChannel, 2*32+6*8+4+2*2+6*33+1)
};
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0) {
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger)) {
Ok(chan) => chan,
Err(_) => return,
};
Expand Down
14 changes: 10 additions & 4 deletions fuzz/fuzz_targets/full_stack_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,11 @@ use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
use lightning::ln::router::Router;
use lightning::util::events::{EventsProvider,Event};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

mod utils;

use utils::test_logger;

use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
Expand DownExpand Up@@ -169,18 +174,19 @@ pub fn do_test(data: &[u8]) {
Err(_) => return,
};

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());
let monitor = Arc::new(TestChannelMonitor{});
let watch = Arc::new(ChainWatchInterfaceUtil::new());
let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
let broadcast = Arc::new(TestBroadcaster{});

let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone()).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap()));
let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap();
let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger)));

let peers = RefCell::new([false; 256]);
let handler = PeerManager::new(MessageHandler {
chan_handler: channelmanager.clone(),
route_handler: router.clone(),
}, our_network_key);
}, our_network_key, Arc::clone(&logger));

let mut should_forward = false;
let mut payments_received = Vec::new();
Expand Down
11 changes: 10 additions & 1 deletion fuzz/fuzz_targets/router_target.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,10 +7,17 @@ use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
use lightning::ln::router::{Router, RouteHint};
use lightning::util::reset_rng_state;
use lightning::util::logger::Logger;

use secp256k1::key::PublicKey;
use secp256k1::Secp256k1;

mod utils;

use utils::test_logger;

use std::sync::Arc;

#[inline]
pub fn slice_to_be16(v: &[u8]) -> u16 {
((v[0] as u16) << 8*1) |
Expand DownExpand Up@@ -98,8 +105,10 @@ pub fn do_test(data: &[u8]) {
}
}

let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());

let our_pubkey = get_pubkey!();
let router = Router::new(our_pubkey.clone());
let router = Router::new(our_pubkey.clone(), Arc::clone(&logger));

loop {
match get_slice!(1)[0] {
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/utils/mod.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
pub(crate) mod test_logger;
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/utils/test_logger.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
use lightning::util::logger::{Logger, Level, Record};

pub struct TestLogger {
level: Level,
}

impl TestLogger {
pub fn new() -> TestLogger {
TestLogger {
level: Level::Off,
}
}
pub fn enable(&mut self, level: Level) {
self.level = level;
}
}

impl Logger for TestLogger {
fn log(&self, record: &Record) {
#[cfg(any(test, not(feature = "fuzztarget")))]
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
}
}
11 changes: 7 additions & 4 deletions src/chain/chaininterface.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::util::hash::Sha256dHash;
use std::sync::{Mutex,Weak,MutexGuard};
use util::logger::Logger;
use std::sync::{Mutex,Weak,MutexGuard,Arc};
use std::sync::atomic::{AtomicUsize, Ordering};

/// An interface to request notification of certain scripts as they appear the
Expand DownExpand Up@@ -70,7 +71,8 @@ pub trait FeeEstimator: Sync + Send {
pub struct ChainWatchInterfaceUtil {
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
listeners: Mutex<Vec<Weak<ChainListener>>>,
reentered: AtomicUsize
reentered: AtomicUsize,
logger: Arc<Logger>,
}

/// Register listener
Expand DownExpand Up@@ -100,11 +102,12 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
}

impl ChainWatchInterfaceUtil {
pub fn new() -> ChainWatchInterfaceUtil {
pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
ChainWatchInterfaceUtil {
watched: Mutex::new((Vec::new(), Vec::new(), false)),
listeners: Mutex::new(Vec::new()),
reentered: AtomicUsize::new(1)
reentered: AtomicUsize::new(1),
logger: logger,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ extern crate rand;
extern crate secp256k1;
#[cfg(test)] extern crate hex;

#[macro_use]
pub mod util;
pub mod chain;
pub mod ln;
pub mod util;
21 changes: 17 additions & 4 deletions src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,11 +23,13 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::{transaction_utils,rng};
use util::sha2::Sha256;
use util::logger::{Logger, Record};

use std;
use std::default::Default;
use std::{cmp,mem};
use std::time::Instant;
use std::sync::{Arc};

pub struct ChannelKeys {
pub funding_key: SecretKey,
Expand DownExpand Up@@ -303,6 +305,8 @@ pub struct Channel {
their_shutdown_scriptpubkey: Option<Script>,

channel_monitor: ChannelMonitor,

logger: Arc<Logger>,
}

const OUR_MAX_HTLCS: u16 = 5; //TODO
Expand DownExpand Up@@ -361,7 +365,7 @@ impl Channel {
// Constructors:

/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Channel {
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
panic!("funding value > 2^24");
}
Expand DownExpand Up@@ -429,6 +433,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
}
}

Expand All@@ -446,7 +452,7 @@ impl Channel {
/// Assumes chain_hash has already been checked and corresponds with what we expect!
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
/// that we're rejecting the new channel.
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool) -> Result<Channel, HandleError> {
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
// Check sanity of message fields:
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
Expand DownExpand Up@@ -548,6 +554,8 @@ impl Channel {
their_shutdown_scriptpubkey: None,

channel_monitor: channel_monitor,

logger,
};

let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
Expand DownExpand Up@@ -1748,7 +1756,7 @@ impl Channel {

match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey) {
Ok(_) => {},
Err(_) => {
Err(_e) => {
// The remote end may have decided to revoke their output due to inconsistent dust
// limits, so check for that case by re-checking the signature here.
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
Expand DownExpand Up@@ -2111,6 +2119,7 @@ impl Channel {
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
Ok(res) => res,
Err(e) => {
log_error!(self, "Got bad signatures: {}!", e.err);
self.channel_monitor.unset_funding_info();
return Err(e);
}
Expand DownExpand Up@@ -2409,10 +2418,13 @@ mod tests {
use ln::chan_utils;
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
use chain::transaction::OutPoint;
use util::test_utils;
use util::logger::Logger;
use secp256k1::{Secp256k1,Message,Signature};
use secp256k1::key::{SecretKey,PublicKey};
use crypto::sha2::Sha256;
use crypto::digest::Digest;
use std::sync::Arc;

struct TestFeeEstimator {
fee_est: u64
Expand All@@ -2433,6 +2445,7 @@ mod tests {
fn outbound_commitment_test() {
// Test vectors from BOLT 3 Appendix C:
let feeest = TestFeeEstimator{fee_est: 15000};
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let secp_ctx = Secp256k1::new();

let chan_keys = ChannelKeys {
Expand All@@ -2450,7 +2463,7 @@ mod tests {
assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).unwrap().serialize()[..],
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);

let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42); // Nothing uses their network key in this test
let mut chan = Channel::new_outbound(&feeest, chan_keys, PublicKey::new(), 10000000, false, 42, Arc::clone(&logger)); // Nothing uses their network key in this test
chan.their_to_self_delay = 144;
chan.our_dust_limit_satoshis = 546;

Expand Down
Loading