Skip to content

Commit 832fc4f

Browse files
authored
Merge pull request #269 from TheBlueMatt/2018-12-198-review
Detect HTLC-resolving on-chain actions and pass them to ChannelManager
2 parents 302f131 + d56b479 commit 832fc4f

File tree

10 files changed

+1531
-261
lines changed

10 files changed

+1531
-261
lines changed

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 12 additions & 10 deletions
Large diffs are not rendered by default.

src/ln/chan_utils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use bitcoin::blockdata::opcodes;
33
use bitcoin::blockdata::transaction::{TxIn,TxOut,OutPoint,Transaction};
44
use bitcoin::util::hash::{Hash160,Sha256dHash};
55

6+
use ln::channelmanager::PaymentHash;
7+
68
use secp256k1::key::{PublicKey,SecretKey};
79
use secp256k1::Secp256k1;
810
use secp256k1;
@@ -156,15 +158,15 @@ pub struct HTLCOutputInCommitment {
156158
pub offered: bool,
157159
pub amount_msat: u64,
158160
pub cltv_expiry: u32,
159-
pub payment_hash: [u8; 32],
161+
pub payment_hash: PaymentHash,
160162
pub transaction_output_index: u32,
161163
}
162164

163165
#[inline]
164166
pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a_htlc_key: &PublicKey, b_htlc_key: &PublicKey, revocation_key: &PublicKey) -> Script {
165167
let payment_hash160 = {
166168
let mut ripemd = Ripemd160::new();
167-
ripemd.input(&htlc.payment_hash);
169+
ripemd.input(&htlc.payment_hash.0[..]);
168170
let mut res = [0; 20];
169171
ripemd.result(&mut res);
170172
res

src/ln/channel.rs

Lines changed: 142 additions & 95 deletions
Large diffs are not rendered by default.

src/ln/channelmanager.rs

Lines changed: 924 additions & 72 deletions
Large diffs are not rendered by default.

src/ln/channelmonitor.rs

Lines changed: 401 additions & 74 deletions
Large diffs are not rendered by default.

src/ln/msgs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use std::result::Result;
2929
use util::{byte_utils, events};
3030
use util::ser::{Readable, Writeable, Writer};
3131

32+
use ln::channelmanager::{PaymentPreimage, PaymentHash};
33+
3234
/// An error in decoding a message or struct.
3335
#[derive(Debug)]
3436
pub enum DecodeError {
@@ -256,7 +258,7 @@ pub struct UpdateAddHTLC {
256258
pub(crate) channel_id: [u8; 32],
257259
pub(crate) htlc_id: u64,
258260
pub(crate) amount_msat: u64,
259-
pub(crate) payment_hash: [u8; 32],
261+
pub(crate) payment_hash: PaymentHash,
260262
pub(crate) cltv_expiry: u32,
261263
pub(crate) onion_routing_packet: OnionPacket,
262264
}
@@ -266,7 +268,7 @@ pub struct UpdateAddHTLC {
266268
pub struct UpdateFulfillHTLC {
267269
pub(crate) channel_id: [u8; 32],
268270
pub(crate) htlc_id: u64,
269-
pub(crate) payment_preimage: [u8; 32],
271+
pub(crate) payment_preimage: PaymentPreimage,
270272
}
271273

272274
/// An update_fail_htlc message to be sent or received from a peer

src/ln/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::collections::btree_map::Entry as BtreeEntry;
2525
use std;
2626

2727
/// A hop in a route
28-
#[derive(Clone)]
28+
#[derive(Clone, PartialEq)]
2929
pub struct RouteHop {
3030
/// The node_id of the node at this hop.
3131
pub pubkey: PublicKey,
@@ -39,7 +39,7 @@ pub struct RouteHop {
3939
}
4040

4141
/// A route from us through the network to a destination
42-
#[derive(Clone)]
42+
#[derive(Clone, PartialEq)]
4343
pub struct Route {
4444
/// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
4545
/// must always be at least length one. By protocol rules, this may not currently exceed 20 in

src/util/events.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//TODO: We need better separation of event types ^
1414

1515
use ln::msgs;
16+
use ln::channelmanager::{PaymentPreimage, PaymentHash};
1617
use chain::transaction::OutPoint;
1718
use chain::keysinterface::SpendableOutputDescriptor;
1819

@@ -59,7 +60,7 @@ pub enum Event {
5960
/// the amount expected.
6061
PaymentReceived {
6162
/// The hash for which the preimage should be handed to the ChannelManager.
62-
payment_hash: [u8; 32],
63+
payment_hash: PaymentHash,
6364
/// The value, in thousandths of a satoshi, that this payment is for.
6465
amt: u64,
6566
},
@@ -71,15 +72,15 @@ pub enum Event {
7172
/// The preimage to the hash given to ChannelManager::send_payment.
7273
/// Note that this serves as a payment receipt, if you wish to have such a thing, you must
7374
/// store it somehow!
74-
payment_preimage: [u8; 32],
75+
payment_preimage: PaymentPreimage,
7576
},
7677
/// Indicates an outbound payment we made failed. Probably some intermediary node dropped
7778
/// something. You may wish to retry with a different route.
7879
/// Note that duplicative PaymentFailed Events may be generated - it is your responsibility to
7980
/// deduplicate them by payment_hash (which MUST be unique)!
8081
PaymentFailed {
8182
/// The hash which was given to ChannelManager::send_payment.
82-
payment_hash: [u8; 32],
83+
payment_hash: PaymentHash,
8384
/// Indicates the payment was rejected for some reason by the recipient. This implies that
8485
/// the payment has failed, not just the route in question. If this is not set, you may
8586
/// retry the payment via a different route.

src/util/ser.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use bitcoin::util::hash::Sha256dHash;
1212
use bitcoin::blockdata::script::Script;
1313
use std::marker::Sized;
1414
use ln::msgs::DecodeError;
15+
use ln::channelmanager::{PaymentPreimage, PaymentHash};
1516
use util::byte_utils;
1617

1718
use util::byte_utils::{be64_to_array, be48_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be48, slice_to_be64};
@@ -386,3 +387,29 @@ impl<R: Read> Readable<R> for Signature {
386387
}
387388
}
388389
}
390+
391+
impl Writeable for PaymentPreimage {
392+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
393+
self.0.write(w)
394+
}
395+
}
396+
397+
impl<R: Read> Readable<R> for PaymentPreimage {
398+
fn read(r: &mut R) -> Result<Self, DecodeError> {
399+
let buf: [u8; 32] = Readable::read(r)?;
400+
Ok(PaymentPreimage(buf))
401+
}
402+
}
403+
404+
impl Writeable for PaymentHash {
405+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
406+
self.0.write(w)
407+
}
408+
}
409+
410+
impl<R: Read> Readable<R> for PaymentHash {
411+
fn read(r: &mut R) -> Result<Self, DecodeError> {
412+
let buf: [u8; 32] = Readable::read(r)?;
413+
Ok(PaymentHash(buf))
414+
}
415+
}

src/util/test_utils.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use chain::transaction::OutPoint;
44
use ln::channelmonitor;
55
use ln::msgs;
66
use ln::msgs::{HandleError};
7+
use ln::channelmonitor::HTLCUpdate;
78
use util::events;
89
use util::logger::{Logger, Level, Record};
910
use util::ser::{ReadableArgs, Writer};
@@ -64,6 +65,10 @@ impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
6465
assert!(self.simple_monitor.add_update_monitor(funding_txo, monitor).is_ok());
6566
self.update_ret.lock().unwrap().clone()
6667
}
68+
69+
fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate> {
70+
return self.simple_monitor.fetch_pending_htlc_updated();
71+
}
6772
}
6873

6974
pub struct TestBroadcaster {
@@ -178,12 +183,17 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
178183

179184
pub struct TestLogger {
180185
level: Level,
186+
id: String,
181187
}
182188

183189
impl TestLogger {
184190
pub fn new() -> TestLogger {
191+
Self::with_id("".to_owned())
192+
}
193+
pub fn with_id(id: String) -> TestLogger {
185194
TestLogger {
186195
level: Level::Trace,
196+
id,
187197
}
188198
}
189199
pub fn enable(&mut self, level: Level) {
@@ -194,7 +204,7 @@ impl TestLogger {
194204
impl Logger for TestLogger {
195205
fn log(&self, record: &Record) {
196206
if self.level >= record.level {
197-
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
207+
println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
198208
}
199209
}
200210
}

0 commit comments

Comments
 (0)