Skip to content

Commit 2d8afec

Browse files
committed
Rewrite channelmonitor framework and implement a bunch of it
1 parent 7400b3b commit 2d8afec

File tree

6 files changed

+1157
-447
lines changed

6 files changed

+1157
-447
lines changed

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crypto::sha2::Sha256;
1515
use crypto::digest::Digest;
1616

1717
use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
18-
use lightning::ln::{channelmonitor,msgs};
18+
use lightning::ln::channelmonitor;
1919
use lightning::ln::channelmanager::ChannelManager;
2020
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
2121
use lightning::ln::router::Router;
@@ -93,7 +93,7 @@ impl FeeEstimator for FuzzEstimator {
9393

9494
struct TestChannelMonitor {}
9595
impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
96-
fn add_update_monitor(&self, _funding_txo: (Sha256dHash, u16), _monitor: channelmonitor::ChannelMonitor) -> Result<(), msgs::HandleError> {
96+
fn add_update_monitor(&self, _funding_txo: (Sha256dHash, u16), _monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
9797
//TODO!
9898
Ok(())
9999
}

src/ln/chan_utils.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use bitcoin::blockdata::script::{Script,Builder};
22
use bitcoin::blockdata::opcodes;
3-
use bitcoin::util::hash::Hash160;
3+
use bitcoin::blockdata::transaction::{TxIn,TxOut,Transaction};
4+
use bitcoin::util::hash::{Hash160,Sha256dHash};
45

56
use secp256k1::key::{PublicKey,SecretKey};
67
use secp256k1::Secp256k1;
@@ -11,6 +12,9 @@ use crypto::ripemd160::Ripemd160;
1112

1213
use util::sha2::Sha256;
1314

15+
pub const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
16+
pub const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
17+
1418
// Various functions for key derivation and transaction creation for use within channels. Primarily
1519
// used in Channel and ChannelMonitor.
1620

@@ -233,3 +237,33 @@ pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a
233237
pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Script {
234238
get_htlc_redeemscript_with_explicit_keys(htlc, &keys.a_htlc_key, &keys.b_htlc_key, &keys.revocation_key)
235239
}
240+
241+
pub fn build_htlc_transaction(prev_hash: &Sha256dHash, feerate_per_kw: u64, to_self_delay: u16, htlc: &HTLCOutputInCommitment, a_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
242+
let mut txins: Vec<TxIn> = Vec::new();
243+
txins.push(TxIn {
244+
prev_hash: prev_hash.clone(),
245+
prev_index: htlc.transaction_output_index,
246+
script_sig: Script::new(),
247+
sequence: 0,
248+
witness: Vec::new(),
249+
});
250+
251+
let total_fee = if htlc.offered {
252+
feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000
253+
} else {
254+
feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000
255+
};
256+
257+
let mut txouts: Vec<TxOut> = Vec::new();
258+
txouts.push(TxOut {
259+
script_pubkey: get_revokeable_redeemscript(revocation_key, to_self_delay, a_delayed_payment_key).to_v0_p2wsh(),
260+
value: htlc.amount_msat / 1000 - total_fee //TODO: BOLT 3 does not specify if we should add amount_msat before dividing or if we should divide by 1000 before subtracting (as we do here)
261+
});
262+
263+
Transaction {
264+
version: 2,
265+
lock_time: if htlc.offered { htlc.cltv_expiry } else { 0 },
266+
input: txins,
267+
output: txouts,
268+
}
269+
}

0 commit comments

Comments
 (0)