Skip to content

Commit 3cc1b63

Browse files
committed
Return TransactionData from register_output
Electrum clients primarily operate by subscribing to notifications of transactions by script pubkeys. Therefore, they will send filtered transaction data without including dependent transactions. Outputs for such transactions must be explicitly registered with these clients. Therefore, upon block_connected, provide a mechanism for an Electrum- backed chain::Filter to return new transaction data to scan.
1 parent 33071a5 commit 3cc1b63

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

lightning/src/chain/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bitcoin::hash_types::{BlockHash, Txid};
1616

1717
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent};
1818
use chain::keysinterface::Sign;
19-
use chain::transaction::OutPoint;
19+
use chain::transaction::{OutPoint, TransactionData};
2020

2121
pub mod chaininterface;
2222
pub mod chainmonitor;
@@ -134,7 +134,10 @@ pub trait Filter: Send + Sync {
134134

135135
/// Registers interest in spends of a transaction output identified by `outpoint` having
136136
/// `script_pubkey` as the spending condition.
137-
fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);
137+
///
138+
/// Optionally, returns any transactions dependent on the output. This is useful for Electrum
139+
/// clients to facilitate registering any in-block descendants.
140+
fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script) -> Option<TransactionData>;
138141
}
139142

140143
impl<T: Listen> Listen for std::ops::Deref<Target = T> {

lightning/src/chain/transaction.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,37 @@ use bitcoin::hash_types::Txid;
1313
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
1414
use bitcoin::blockdata::transaction::Transaction;
1515

16+
/// Transaction data where each item consists of a transaction paired with the index of the
17+
/// transaction within a block.
18+
///
19+
/// Useful for returning enumerated transactions from a block, possibly filtered, in order to retain
20+
/// the transaction index.
21+
///
22+
/// ```
23+
/// extern crate bitcoin;
24+
/// extern crate lightning;
25+
///
26+
/// use bitcoin::blockdata::block::Block;
27+
/// use bitcoin::blockdata::constants::genesis_block;
28+
/// use bitcoin::network::constants::Network;
29+
/// use lightning::chain::transaction::TransactionData;
30+
///
31+
/// let block = genesis_block(Network::Bitcoin);
32+
/// let coinbase = block.txdata[0].clone();
33+
///
34+
/// let mut txdata = enumerate_txdata(block);
35+
/// assert_eq!(txdata.len(), 1);
36+
///
37+
/// let (index, tx) = txdata.pop().unwrap();
38+
/// assert_eq!(index, 0);
39+
/// assert_eq!(tx, coinbase);
40+
///
41+
/// fn enumerate_txdata(mut block: Block) -> TransactionData {
42+
/// block.txdata.drain(..).enumerate().collect()
43+
/// }
44+
/// ```
45+
pub type TransactionData = Vec<(usize, Transaction)>;
46+
1647
/// Transaction data where each item consists of a transaction reference paired with the index of
1748
/// the transaction within a block.
1849
///

lightning/src/util/test_utils.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use chain::chaininterface::ConfirmationTarget;
1313
use chain::chainmonitor;
1414
use chain::channelmonitor;
1515
use chain::channelmonitor::MonitorEvent;
16-
use chain::transaction::OutPoint;
16+
use chain::transaction::{OutPoint, TransactionData};
1717
use chain::keysinterface;
1818
use ln::features::{ChannelFeatures, InitFeatures};
1919
use ln::msgs;
@@ -545,7 +545,8 @@ impl chain::Filter for TestChainSource {
545545
self.watched_txn.lock().unwrap().insert((*txid, script_pubkey.clone()));
546546
}
547547

548-
fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script) {
548+
fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script) -> Option<TransactionData> {
549549
self.watched_outputs.lock().unwrap().insert((*outpoint, script_pubkey.clone()));
550+
None
550551
}
551552
}

0 commit comments

Comments
 (0)