Skip to content

Commit edfc12a

Browse files
committed
stop using util in FFIChainWatchInterface
1 parent a5dd0f3 commit edfc12a

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

bindings/src/adaptors/mod.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ pub mod chain_watch_interface_fn {
197197
pub type InstallWatchOutpointPtr = extern "cdecl" fn(outpoint: *const FFIOutPoint, out_script: *const FFIScript);
198198
pub type WatchAllTxnPtr = extern "cdecl" fn();
199199
pub type GetChainUtxoPtr = extern "cdecl" fn(genesis_hash: *const Bytes32, unspent_tx_output_identifier: u64, err: *mut FFIChainError, script_ptr: *mut u8, script_len: *mut usize, amount_satoshis: *mut u64);
200+
pub type FilterBlock = extern "cdecl" fn(block_ptr: *const u8, block_len: usize, matched_index_ptr: *mut u8, matched_inedx_len: *mut usize);
201+
pub type ReEntered = extern "cdecl" fn() -> usize;
200202
}
201203

202204
#[repr(C)]
@@ -205,14 +207,17 @@ pub struct FFIChainWatchInterface {
205207
pub install_watch_outpoint_ptr: chain_watch_interface_fn::InstallWatchOutpointPtr,
206208
pub watch_all_txn_ptr: chain_watch_interface_fn::WatchAllTxnPtr,
207209
pub get_chain_utxo_ptr: chain_watch_interface_fn::GetChainUtxoPtr,
208-
util: ChainWatchInterfaceUtil
210+
pub filter_block_ptr: chain_watch_interface_fn::FilterBlock,
211+
pub reentered_ptr: chain_watch_interface_fn::ReEntered
209212
}
210213
impl FFIChainWatchInterface {
211214
pub fn new(
212215
install_watch_tx: chain_watch_interface_fn::InstallWatchTxPtr,
213216
install_watch_outpoint: chain_watch_interface_fn::InstallWatchOutpointPtr,
214217
watch_all_txn: chain_watch_interface_fn::WatchAllTxnPtr,
215218
get_chain_utxo: chain_watch_interface_fn::GetChainUtxoPtr,
219+
filter_block: chain_watch_interface_fn::FilterBlock,
220+
reentered: chain_watch_interface_fn::ReEntered,
216221
network: Network,
217222
logger: Arc<dyn Logger>
218223
) -> FFIChainWatchInterface {
@@ -221,38 +226,30 @@ impl FFIChainWatchInterface {
221226
install_watch_outpoint_ptr: install_watch_outpoint,
222227
watch_all_txn_ptr: watch_all_txn,
223228
get_chain_utxo_ptr: get_chain_utxo,
224-
util: ChainWatchInterfaceUtil::new(network)
229+
filter_block_ptr: filter_block,
230+
reentered_ptr:reentered
225231
}
226232
}
227233
}
228234

229235
impl ChainWatchInterface for FFIChainWatchInterface {
230236
fn install_watch_tx(&self, txid: &Txid, script_pub_key: &Script) {
231-
self.util.install_watch_tx(txid, script_pub_key);
232237
let spk_vec = bitcoin_serialize(script_pub_key);
233238
let ffi_spk = FFIScript::from(spk_vec.as_slice());
234239
let txid: Bytes32 = txid.clone().into();
235240
(self.install_watch_tx_ptr)(&txid as *const _, &ffi_spk as *const _)
236241
}
237242
fn install_watch_outpoint(&self, outpoint: (Txid, u32), out_script: &Script) {
238-
self.util.install_watch_outpoint(outpoint, out_script);
239243
let txid: Bytes32 = outpoint.0.into();
240244
let ffi_outpoint = FFIOutPoint { txid: txid, index: outpoint.1 as u16 };
241245
let out_script_vec = bitcoin_serialize(out_script);
242246
let ffi_outscript = FFIScript::from(out_script_vec.as_slice());
243247
(self.install_watch_outpoint_ptr)(&ffi_outpoint as *const _, &ffi_outscript as *const _)
244248
}
245249
fn watch_all_txn(&self) {
246-
self.util.watch_all_txn();
247250
(self.watch_all_txn_ptr)()
248251
}
249252
fn get_chain_utxo(&self, genesis_hash: BlockHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
250-
match self.util.get_chain_utxo(genesis_hash, unspent_tx_output_identifier) {
251-
Err(ChainError::NotWatched) => {
252-
return Err(ChainError::NotWatched);
253-
},
254-
_ => {},
255-
}
256253
let err = std::ptr::null_mut();
257254
// the length can be anything as long as it is enough to put the scriptPubKey.
258255
// probably this is a bit overkill but who cares.
@@ -270,11 +267,30 @@ impl ChainWatchInterface for FFIChainWatchInterface {
270267
Err(e.into())
271268
}
272269
}
270+
273271
fn filter_block<'a>(&self, block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>) {
274-
self.util.filter_block(block)
272+
let block_bytes = bitcoin_serialize(block);
273+
// the minimum weight for one tx is 440. So the max number of tx in one block is 9090.
274+
// so the max size of the buffer we have to prepare is.
275+
// `2 + (9090 * 4) = 36362`.
276+
let mut matched_tx_index = [0u8; 36864];
277+
let mut matched_tx_index_len_ptr: &mut usize = todo!();
278+
println!("coinbase tx in rl {:?}", block.txdata[0]);
279+
(self.filter_block_ptr)(block_bytes.as_ptr(), block_bytes.len(), matched_tx_index.as_mut_ptr(), matched_tx_index_len_ptr as *mut _);
280+
if (matched_tx_index_len_ptr.clone() == usize::MAX) {
281+
panic!("FFI failure. the caller must set the actual serialized length of the tx-indexes in filter_block");
282+
}
283+
let mut matched_tx_index: &[u8] = unsafe_block!("We know the caller has set the value for serialized tx index" => &matched_tx_index[..(*matched_tx_index_len_ptr)]);
284+
let matched_tx_indexes: Vec<u32> = lightning::util::ser::Readable::read(&mut std::io::Cursor::new(matched_tx_index)).unwrap();
285+
let mut matched_txs = Vec::with_capacity(matched_tx_indexes.len());
286+
for i in matched_tx_indexes.iter() {
287+
matched_txs.push(&block.txdata[i.clone() as usize]);
288+
}
289+
(matched_txs, matched_tx_indexes)
275290
}
291+
276292
fn reentered(&self) -> usize {
277-
self.util.reentered()
293+
(self.reentered_ptr)()
278294
}
279295
}
280296

bindings/src/adaptors/primitives.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ pub struct FFIEvents {
280280

281281
impl Writeable for FFIEvents {
282282
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
283-
(self.events.len() as u16).write(writer);
283+
(self.events.len() as u16).write(writer)?;
284284
for e in &self.events {
285285
match e {
286286
Event::FundingGenerationReady {ref temporary_channel_id, ref channel_value_satoshis, ref output_script, ref user_channel_id} => {

bindings/src/blocknotifier.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ ffi! {
1919
fn create_block_notifier(
2020
network_ref: FFINetwork,
2121
log_ptr: Ref<ffilogger_fn::LogExtern>,
22+
2223
install_watch_tx_ptr: Ref<chain_watch_interface_fn::InstallWatchTxPtr>,
2324
install_watch_outpoint_ptr: Ref<chain_watch_interface_fn::InstallWatchOutpointPtr>,
2425
watch_all_txn_ptr: Ref<chain_watch_interface_fn::WatchAllTxnPtr>,
2526
get_chain_utxo_ptr: Ref<chain_watch_interface_fn::GetChainUtxoPtr>,
27+
filter_block_ptr: Ref<chain_watch_interface_fn::FilterBlock>,
28+
reentered_ptr: Ref<chain_watch_interface_fn::ReEntered>,
29+
2630
handle: Out<FFIBlockNotifierHandle>
2731
) -> FFIResult {
2832
let network = network_ref.to_network();
@@ -33,12 +37,17 @@ ffi! {
3337
let install_watch_outpoint_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => install_watch_outpoint_ptr.as_ref());
3438
let watch_all_txn_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => watch_all_txn_ptr.as_ref());
3539
let get_chain_utxo_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => get_chain_utxo_ptr.as_ref());
40+
let filter_block_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => filter_block_ptr.as_ref());
41+
let reentered_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => reentered_ptr.as_ref());
42+
3643
let chain_watch_interface_arc =
3744
Arc::new(FFIChainWatchInterface::new(
3845
*install_watch_tx_ref,
3946
*install_watch_outpoint_ref,
4047
*watch_all_txn_ref,
4148
*get_chain_utxo_ref,
49+
*filter_block_ref,
50+
*reentered_ref,
4251
network,
4352
logger_arc.clone()
4453
));

bindings/src/channelmanager.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ pub(crate) fn construct_channel_manager(
8383
install_watch_outpoint: &chain_watch_interface_fn::InstallWatchOutpointPtr,
8484
watch_all_txn: &chain_watch_interface_fn::WatchAllTxnPtr,
8585
get_chain_utxo: &chain_watch_interface_fn::GetChainUtxoPtr,
86+
filter_block: &chain_watch_interface_fn::FilterBlock,
87+
reentered: &chain_watch_interface_fn::ReEntered,
8688

8789
broadcast_transaction_ptr: Ref<broadcaster_fn::BroadcastTransactionPtr>,
8890
log_ref: &ffilogger_fn::LogExtern,
@@ -98,11 +100,13 @@ pub(crate) fn construct_channel_manager(
98100

99101
let chain_watch_interface_arc =
100102
Arc::new(FFIChainWatchInterface::new(
101-
*install_watch_tx,
103+
*install_watch_tx,
102104
*install_watch_outpoint,
103105
*watch_all_txn,
104106
*get_chain_utxo,
105-
network,
107+
*filter_block,
108+
*reentered,
109+
network,
106110
logger_arc.clone()
107111
));
108112

@@ -141,6 +145,8 @@ ffi! {
141145
install_watch_outpoint_ptr: Ref<chain_watch_interface_fn::InstallWatchOutpointPtr>,
142146
watch_all_txn_ptr: Ref<chain_watch_interface_fn::WatchAllTxnPtr>,
143147
get_chain_utxo_ptr: Ref<chain_watch_interface_fn::GetChainUtxoPtr>,
148+
filter_block_ptr: Ref<chain_watch_interface_fn::FilterBlock>,
149+
reentered_ptr: Ref<chain_watch_interface_fn::ReEntered>,
144150

145151
broadcast_transaction_ptr: Ref<broadcaster_fn::BroadcastTransactionPtr>,
146152
log_ptr: Ref<ffilogger_fn::LogExtern>,
@@ -154,15 +160,21 @@ ffi! {
154160
let install_watch_outpoint_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => install_watch_outpoint_ptr.as_ref());
155161
let watch_all_txn_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => watch_all_txn_ptr.as_ref());
156162
let get_chain_utxo_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => get_chain_utxo_ptr.as_ref());
163+
let filter_block_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => filter_block_ptr.as_ref());
164+
let reentered_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => reentered_ptr.as_ref());
165+
157166
let chan_man_raw =
158167
construct_channel_manager(
159168
seed,
160169
network,
161170
cfg,
171+
162172
install_watch_tx_ref,
163173
install_watch_outpoint_ref,
164174
watch_all_txn_ref,
165175
get_chain_utxo_ref,
176+
filter_block_ref,
177+
reentered_ref,
166178

167179
broadcast_transaction_ptr,
168180
log_ref,

bindings/src/peermanager.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ ffi! {
5252
install_watch_outpoint_ptr: Ref<chain_watch_interface_fn::InstallWatchOutpointPtr>,
5353
watch_all_txn_ptr: Ref<chain_watch_interface_fn::WatchAllTxnPtr>,
5454
get_chain_utxo_ptr: Ref<chain_watch_interface_fn::GetChainUtxoPtr>,
55+
filter_block_ptr: Ref<chain_watch_interface_fn::FilterBlock>,
56+
reentered_ptr: Ref<chain_watch_interface_fn::ReEntered>,
57+
5558
log_ptr: Ref<ffilogger_fn::LogExtern>,
5659

5760
our_node_secret_ptr: Ref<Bytes32>,
@@ -74,13 +77,18 @@ ffi! {
7477
let install_watch_outpoint_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => install_watch_outpoint_ptr.as_ref());
7578
let watch_all_txn_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => watch_all_txn_ptr.as_ref());
7679
let get_chain_utxo_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => get_chain_utxo_ptr.as_ref());
80+
let filter_block_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => filter_block_ptr.as_ref());
81+
let reentered_ref = unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => reentered_ptr.as_ref());
82+
7783
let logger_arc = Arc::new(FFILogger { log_ptr: *log_ref });
7884
let chain_watch_interface_arc =
7985
Arc::new(FFIChainWatchInterface::new(
8086
*install_watch_tx_ref,
8187
*install_watch_outpoint_ref,
8288
*watch_all_txn_ref,
8389
*get_chain_utxo_ref,
90+
*filter_block_ref,
91+
*reentered_ref,
8492
network.to_network(),
8593
logger_arc.clone()
8694
));

0 commit comments

Comments
 (0)