From 6185a2819090bd077954244c5e2adaab5efcaa1a Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 25 Dec 2017 01:05:27 -0500 Subject: [PATCH] initial checkin --- .gitignore | 5 + Cargo.toml | 21 + README.md | 65 + src/chain/bitcoincorerpcchain.rs | 41 + src/chain/chaininterface.rs | 130 ++ src/chain/mod.rs | 3 + src/chain/rustbitcoinchain.rs | 66 + src/lib.rs | 12 + src/ln/chan_utils.rs | 234 ++++ src/ln/channel.rs | 2224 ++++++++++++++++++++++++++++++ src/ln/channelmanager.rs | 1701 +++++++++++++++++++++++ src/ln/channelmonitor.rs | 826 +++++++++++ src/ln/mod.rs | 9 + src/ln/msgs.rs | 934 +++++++++++++ src/ln/peer_channel_encryptor.rs | 774 +++++++++++ src/ln/peer_handler.rs | 604 ++++++++ src/ln/router.rs | 921 +++++++++++++ src/util/byte_utils.rs | 69 + src/util/chacha20poly1305rfc.rs | 102 ++ src/util/events.rs | 77 ++ src/util/internal_traits.rs | 7 + src/util/mod.rs | 9 + src/util/test_utils.rs | 57 + src/util/transaction_utils.rs | 19 + src/wallet/mod.rs | 1 + src/wallet/walletinterface.rs | 1 + 26 files changed, 8912 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/chain/bitcoincorerpcchain.rs create mode 100644 src/chain/chaininterface.rs create mode 100644 src/chain/mod.rs create mode 100644 src/chain/rustbitcoinchain.rs create mode 100644 src/lib.rs create mode 100644 src/ln/chan_utils.rs create mode 100644 src/ln/channel.rs create mode 100644 src/ln/channelmanager.rs create mode 100644 src/ln/channelmonitor.rs create mode 100644 src/ln/mod.rs create mode 100644 src/ln/msgs.rs create mode 100644 src/ln/peer_channel_encryptor.rs create mode 100644 src/ln/peer_handler.rs create mode 100644 src/ln/router.rs create mode 100644 src/util/byte_utils.rs create mode 100644 src/util/chacha20poly1305rfc.rs create mode 100644 src/util/events.rs create mode 100644 src/util/internal_traits.rs create mode 100644 src/util/mod.rs create mode 100644 src/util/test_utils.rs create mode 100644 src/util/transaction_utils.rs create mode 100644 src/wallet/mod.rs create mode 100644 src/wallet/walletinterface.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..c21e6c767 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/target/ +**/*.rs.bk +Cargo.lock +/target/ +**/*.rs.bk diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 000000000..8290e28ef --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "lightning" +version = "0.0.1" +authors = ["Matt Corallo"] +license = "AGPL-3.0" +repository = "https://github.com/TheBlueMatt/rust-lightning/" +description = """ +A Bitcoin Lightning implementation in Rust. +Still super-early code-dump quality and is missing large chunks. See README in git repo for suggested projects if you want to contribute. Don't have to bother telling you not to use this for anything serious, because you'd have to finish building it to even try. +""" + +[features] +# Supports tracking channels with a non-bitcoin chain hashes. Currently enables all kinds of fun DoS attacks. +non_bitcoin_chain_hash_routing = [] + +[dependencies] +bitcoin = "0.10.7" +rust-crypto = "0.2" +rand = "0.3" +secp256k1 = "0.8.1" +num = "0.1" diff --git a/README.md b/README.md new file mode 100644 index 000000000..ac512ed8d --- /dev/null +++ b/README.md @@ -0,0 +1,65 @@ +Rust-Lightning, not Rusty's Lightning! + +Currently somewhere near 5% towards usable, published to see if there is any +real interest from folks in either contributing to or using a lightning rust +library. + +The goal is to provide a full-featured but also incredibly flexible lightning +implementation, allowing the user to decide how they wish to use it. With that +in mind, everything should be exposed via simple, composable APIs. The user +should be able to decide whether they wish to use their own threading/execution +models, allowing usage inside of existing library architectures, or allow us to +handle that for them. Same goes with network connections - if the user wishes +to use their own networking stack, they should be able to do so! This all means +that we should provide simple external interfaces which allow the user to drive +all execution, while implementing sample execution drivers that create a +full-featured lightning daemon by default. + +For security reasons, do not add new dependencies. Really do not add new +non-optional/non-test/non-library dependencies. Really really do not add +dependencies with dependencies. Do convince Andrew to cut down dependency usage +in rust-bitcoin. + +Assorted random TODOs: + + * Create a general timer interface - this should be passed around in reference + form to most objects to allow them to register functions which are called on + a timer. By default we should provide an implementation of this which uses + some newfangled rusty promise-y library, but should generally ensure a + client can simply integrate this into whatever existing timer interface + they use. + + * Networking...having a simple bytes-in-bytes-out interface which does message + handling and calls our encryption layer is probably the right approach. We + should then also probably use the same promise-y library we use for timers + to do socket selection and reading/writing. + + * Figure out how to expose when-to-connect and who-to-connect-to. + + * Implement when-to-connect and who-to-connect-to based on route/node rumoring + and channelmanager state. + + * Some kind of serialization format for on-disk storage of things like + channels, channelmonitors, routing db, etc. + + * BOLT 10/network bootstrapping implementation. + + * Some kind of DoS thing including ban tracking and putting that info in + HandleError (and also rename HandleError) to be propagated up...and then + handled. + + * All the random TODOs and unimplemented!()s across the codebase. + + * BOLT 11 (invoice/address creation/generation) implementation + + * Type-ify our somewhat random usage of Uint256/[u8; 32]. Use Sha256dHash + where appropriate, create our own types for everything else. + + * Some kind of logging subsystem/API. + +Notes on coding style: + * Use tabs. If you want to align lines, use spaces. Any desired alignment + should display fine at any tab-length display setting. + +License is AGPL, but with agreement that Matt can relicense under any other +OSI-approved license at will (and likely will with sufficient motivation). diff --git a/src/chain/bitcoincorerpcchain.rs b/src/chain/bitcoincorerpcchain.rs new file mode 100644 index 000000000..f51b1d735 --- /dev/null +++ b/src/chain/bitcoincorerpcchain.rs @@ -0,0 +1,41 @@ +use bitcoin::blockdata::transaction::Transaction; +use bitcoin::blockdata::script::Script; +use bitcoin::util::hash::Sha256dHash; + +use chain::chaininterface::{ChainWatchInterface,ChainWatchInterfaceUtil,ChainListener}; + +use std::sync::Weak; + +pub struct BitcoinCoreRPCClientChain { + util: ChainWatchInterfaceUtil +} + +impl ChainWatchInterface for BitcoinCoreRPCClientChain { + fn install_watch_script(&self, spk: Script) { + self.util.install_watch_script(spk) + } + + fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32)) { + self.util.install_watch_outpoint(outpoint) + } + + fn watch_all_txn(&self) { + self.util.watch_all_txn() + } + + fn broadcast_transaction(&self, _tx: &Transaction) { + unimplemented!() + } + + fn register_listener(&self, listener: Weak) { + self.util.register_listener(listener) + } +} + +impl BitcoinCoreRPCClientChain { + pub fn new() -> BitcoinCoreRPCClientChain { + BitcoinCoreRPCClientChain { + util: ChainWatchInterfaceUtil::new(), + } + } +} diff --git a/src/chain/chaininterface.rs b/src/chain/chaininterface.rs new file mode 100644 index 000000000..ef8f9bf15 --- /dev/null +++ b/src/chain/chaininterface.rs @@ -0,0 +1,130 @@ +use bitcoin::blockdata::block::BlockHeader; +use bitcoin::blockdata::transaction::Transaction; +use bitcoin::blockdata::script::Script; +use bitcoin::util::hash::Sha256dHash; + +use std::sync::{Weak,Mutex}; + +/// An interface to request notification of certain scripts as they appear the +/// chain. +pub trait ChainWatchInterface: Sync + Send { + /// Provides a scriptPubKey which much be watched for. + fn install_watch_script(&self, script_pub_key: Script); + + /// Provides an outpoint which must be watched for, providing any transactions which spend the + /// given outpoint. + fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32)); + + /// Indicates that a listener needs to see all transactions. + fn watch_all_txn(&self); + + /// Sends a transaction out to (hopefully) be mined + fn broadcast_transaction(&self, tx: &Transaction); + + fn register_listener(&self, listener: Weak); + //TODO: unregister +} + +/// A trait indicating a desire to listen for events from the chain +pub trait ChainListener: Sync + Send { + /// Notifies a listener that a block was connected. + /// Note that if a new script/transaction is watched during a block_connected call, the block + /// *must* be re-scanned with the new script/transaction and block_connected should be called + /// again with the same header and (at least) the new transactions. + /// This also means those counting confirmations using block_connected callbacks should watch + /// for duplicate headers and not count them towards confirmations! + fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]); + /// Notifies a listener that a block was disconnected. + /// Unlike block_connected, this *must* never be called twice for the same disconnect event. + fn block_disconnected(&self, header: &BlockHeader); +} + +pub enum ConfirmationTarget { + Background, + Normal, + HighPriority, +} + +pub trait FeeEstimator: Sync + Send { + fn get_est_sat_per_vbyte(&self, ConfirmationTarget) -> u64; +} + +/// Utility to capture some common parts of ChainWatchInterface implementors. +/// Keeping a local copy of this in a ChainWatchInterface implementor is likely useful. +pub struct ChainWatchInterfaceUtil { + watched: Mutex<(Vec