Skip to content

Commit 32687d9

Browse files
committed
Update
* Add check for genesis_hash in `get_chain_utxo` . This is done automatically by `ChainWatchInterfaceUtil` * Remove now-useless primitive conversion function. * Change the type signature of get_chain_utxo for ffi fucntion.
1 parent 1b97457 commit 32687d9

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

bindings/src/adaptors/mod.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub mod chain_watch_interface_fn {
196196
pub type InstallWatchTxPtr = extern "cdecl" fn(*const Bytes32, script_pub_key: *const FFIScript);
197197
pub type InstallWatchOutpointPtr = extern "cdecl" fn(outpoint: *const FFIOutPoint, out_script: *const FFIScript);
198198
pub type WatchAllTxnPtr = extern "cdecl" fn();
199-
pub type GetChainUtxoPtr = extern "cdecl" fn(genesis_hash: *const Bytes32, unspent_tx_output_identifier: u64, err: *mut FFIChainError, script: *mut FFITxOut);
199+
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);
200200
}
201201

202202
#[repr(C)]
@@ -247,14 +247,25 @@ impl ChainWatchInterface for FFIChainWatchInterface {
247247
(self.watch_all_txn_ptr)()
248248
}
249249
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+
}
250256
let err = std::ptr::null_mut();
251-
let tx_out = std::ptr::null_mut();
252-
(self.get_chain_utxo_ptr)(&genesis_hash.into(), unspent_tx_output_identifier, err, tx_out);
257+
// the length can be anything as long as it is enough to put the scriptPubKey.
258+
// probably this is a bit overkill but who cares.
259+
let mut script = [0u8; 128];
260+
let script_len = std::ptr::null_mut();
261+
let amount_satoshis = std::ptr::null_mut();
262+
(self.get_chain_utxo_ptr)(&genesis_hash.into(), unspent_tx_output_identifier, err, script.as_mut_ptr(), script_len, amount_satoshis);
253263
if err.is_null() {
254-
let tx_out: FFITxOut = unsafe_block!("We know the caller has set the value into the tx_out" => (*tx_out).clone());
255-
Ok((tx_out.script_pubkey.to_script(), tx_out.value))
256-
}
257-
else {
264+
let script_bytes: &[u8] = unsafe_block!("We know the caller has set the value into the script_ptr, script_len" => &script[..(*script_len)]);
265+
let amount: u64 = unsafe_block!("We know the caller has set the value into the amount_satoshis" => *amount_satoshis);
266+
let s = bitcoin::consensus::deserialize(script_bytes).expect("Failed to parse scriptpubkey");
267+
Ok((s, amount))
268+
} else {
258269
let e = unsafe_block!("we know the error is not a null pointer" => (*err).clone());
259270
Err(e.into())
260271
}

bindings/src/channelmanager.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ use crate::{
3939
utils::into_fixed_buffer,
4040
};
4141

42-
#[inline]
43-
pub fn slice_to_be32(v: &[u8]) -> u32 {
44-
((v[0] as u32) << 8*3) |
45-
((v[1] as u32) << 8*2) |
46-
((v[2] as u32) << 8*1) |
47-
((v[3] as u32) << 8*0)
48-
}
49-
5042
pub type FFIManyChannelMonitor = SimpleManyChannelMonitor<OutPoint, InMemoryChannelKeys, Arc<FFIBroadCaster>, Arc<FFIFeeEstimator>, Arc<FFILogger>, Arc<FFIChainWatchInterface>>;
5143
pub type FFIArcChannelManager = ChannelManager<InMemoryChannelKeys, Arc<FFIManyChannelMonitor>, Arc<FFIBroadCaster>, Arc<KeysManager>, Arc<FFIFeeEstimator>, Arc<FFILogger>>;
5244
pub type FFIArcChannelManagerHandle<'a> = HandleShared<'a, FFIArcChannelManager>;

0 commit comments

Comments
 (0)