Skip to content

Commit c77cd93

Browse files
committed
update
1 parent 2531071 commit c77cd93

File tree

5 files changed

+100
-12
lines changed

5 files changed

+100
-12
lines changed

bindings/src/adaptors/mod.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ impl From<ConfirmationTarget> for FFIConfirmationTarget {
9797
}
9898
}
9999

100+
pub mod FeeEstimatorFn {
101+
use super::{FFIConfirmationTarget, FFIFeeEstimator};
102+
pub type get_est_sat_per_1000_weight_ptr = extern "cdecl" fn (*const FFIFeeEstimator, FFIConfirmationTarget) -> u64;
103+
}
104+
100105
#[repr(C)]
101106
pub struct FFIFeeEstimator {
102-
get_est_sat_per_1000_weight_ptr: extern "cdecl" fn (&Self, FFIConfirmationTarget) -> u64,
107+
pub get_est_sat_per_1000_weight_ptr: FeeEstimatorFn::get_est_sat_per_1000_weight_ptr,
103108
}
104109

105110
impl FeeEstimator for FFIFeeEstimator {
@@ -191,9 +196,14 @@ impl<'a, 'b> From<&'a Record<'b>> for FFILogRecord {
191196
}
192197
}
193198

199+
pub mod FFILoggerFn {
200+
use super::{FFILogRecord, FFILogger};
201+
pub type log_extern = extern "cdecl" fn(logger: *const FFILogger, record: *const FFILogRecord);
202+
}
203+
194204
#[repr(C)]
195205
pub struct FFILogger {
196-
log_ptr: extern "cdecl" fn(*const Self, *const FFILogRecord),
206+
pub log_ptr: FFILoggerFn::log_extern,
197207
}
198208

199209
impl Logger for FFILogger {
@@ -203,7 +213,7 @@ impl Logger for FFILogger {
203213
}
204214
}
205215

206-
mod ChainWatchInterfaceFn {
216+
pub mod ChainWatchInterfaceFn {
207217
use super::*;
208218
pub type install_watch_tx_ptr = extern "cdecl" fn(*const FFIChainWatchInterface, *const FFISha256dHash, script_pub_key: *const FFIScript);
209219
pub type install_watch_outpoint_ptr = extern "cdecl" fn(*const FFIChainWatchInterface, outpoint: *const FFIOutPoint, out_script: *const FFIScript);
@@ -214,11 +224,11 @@ mod ChainWatchInterfaceFn {
214224

215225
#[repr(C)]
216226
pub struct FFIChainWatchInterface {
217-
install_watch_tx_ptr: ChainWatchInterfaceFn::install_watch_tx_ptr,
218-
install_watch_outpoint_ptr: ChainWatchInterfaceFn::install_watch_tx_ptr,
219-
watch_all_txn_ptr: ChainWatchInterfaceFn::watch_all_txn_ptr,
220-
get_chain_utxo_ptr: ChainWatchInterfaceFn::get_chain_utxo_ptr,
221-
filter_block_ptr: ChainWatchInterfaceFn::filter_block_ptr,
227+
pub install_watch_tx_ptr: ChainWatchInterfaceFn::install_watch_tx_ptr,
228+
pub install_watch_outpoint_ptr: ChainWatchInterfaceFn::install_watch_outpoint_ptr,
229+
pub watch_all_txn_ptr: ChainWatchInterfaceFn::watch_all_txn_ptr,
230+
pub get_chain_utxo_ptr: ChainWatchInterfaceFn::get_chain_utxo_ptr,
231+
pub filter_block_ptr: ChainWatchInterfaceFn::filter_block_ptr,
222232
}
223233

224234
impl ChainWatchInterface for FFIChainWatchInterface {

bindings/src/chainwatcher.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
11
use crate::adaptors::*;
22
use crate::error::FFIResult;
3-
use crate::handle::{Ref};
3+
use crate::handle::{Ref, Out,HandleShared};
4+
5+
type ChainWatchInterfaceHandle<'a> = HandleShared<'a, FFIChainWatchInterface>;
46

57
ffi! {
6-
fn ffi_test_chainwatch_interface(chainwatcher_ptr: Ref<FFIChainWatchInterface>) -> FFIResult {
7-
let chainwatcher = unsafe_block!("" => chainwatcher_ptr.as_ref());
8-
// chainwatcher.install_watch_tx();
8+
fn create_chain_watch_interface(
9+
install_watch_tx_ref: Ref<ChainWatchInterfaceFn::install_watch_tx_ptr>,
10+
install_watch_outpoint_ref: Ref<ChainWatchInterfaceFn::install_watch_outpoint_ptr>,
11+
watch_all_txn_ref: Ref<ChainWatchInterfaceFn::watch_all_txn_ptr>,
12+
get_chain_utxo_ref: Ref<ChainWatchInterfaceFn::get_chain_utxo_ptr>,
13+
filter_block_ref: Ref<ChainWatchInterfaceFn::filter_block_ptr>,
14+
handle: Out<ChainWatchInterfaceHandle>
15+
) -> FFIResult {
16+
let install_watch_tx =
17+
unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => *install_watch_tx_ref.as_ref());
18+
let install_watch_outpoint =
19+
unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => *install_watch_outpoint_ref.as_ref());
20+
let watch_all_txn =
21+
unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => *watch_all_txn_ref.as_ref());
22+
let get_chain_utxo =
23+
unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => *get_chain_utxo_ref.as_ref());
24+
let filter_block =
25+
unsafe_block!("function pointer lives as long as ChainWatchInterface and it points to valid data" => *filter_block_ref.as_ref());
26+
let chain_watch_interface =
27+
FFIChainWatchInterface {
28+
install_watch_tx_ptr: install_watch_tx,
29+
install_watch_outpoint_ptr: install_watch_outpoint,
30+
watch_all_txn_ptr: watch_all_txn,
31+
get_chain_utxo_ptr: get_chain_utxo,
32+
filter_block_ptr: filter_block
33+
};
34+
unsafe_block!("We know handle is not null by wrapper macro. And we know `Out` is writable" => handle.init(ChainWatchInterfaceHandle::alloc(chain_watch_interface)));
935
FFIResult::ok()
1036
}
37+
38+
fn release_chain_watch_interface(handle: ChainWatchInterfaceHandle) -> FFIResult {
39+
unsafe_block!("The upstream caller guarantees the handle will not be accessed after being freed" => ChainWatchInterfaceHandle::dealloc(handle, |mut handle| {
40+
FFIResult::ok()
41+
}))
42+
}
1143
}

bindings/src/fee_estimator.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::adaptors::*;
2+
use crate::handle::{Ref, Out, HandleShared};
3+
use crate::error::FFIResult;
4+
5+
type FeeEstimatorHandle<'a> = HandleShared<'a, FFIFeeEstimator>;
6+
7+
ffi! {
8+
fn create_fee_estimator(fn_ref: Ref<FeeEstimatorFn::get_est_sat_per_1000_weight_ptr>, out: Out<FeeEstimatorHandle>) -> FFIResult {
9+
let func = unsafe_block!("" => *fn_ref.as_ref());
10+
let fee_estimator = FFIFeeEstimator {get_est_sat_per_1000_weight_ptr: func};
11+
unsafe_block!("We know fee_estimator handle is not null by wrapper macro. And we know `Out` is writable" =>
12+
out.init(FeeEstimatorHandle::alloc(fee_estimator))
13+
);
14+
FFIResult::ok()
15+
}
16+
17+
fn release_fee_estimator(handle: FeeEstimatorHandle) -> FFIResult {
18+
unsafe_block!("The upstream caller guarantees the handle will not be accessed after being freed" => FeeEstimatorHandle::dealloc(handle, |mut handle| {
19+
FFIResult::ok()
20+
}))
21+
}
22+
}

bindings/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ pub(crate) mod lazy_static;
2323
pub(crate) mod is_null;
2424

2525
mod adaptors;
26+
27+
mod logger;
2628
mod broadcaster;
29+
mod fee_estimator;
30+
mod chainwatcher;
31+
2732
mod channelmonitor;
2833
mod channelmanager;
2934
mod error;

bindings/src/logger.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::adaptors::*;
2+
use crate::handle::{Ref, Out, HandleShared};
3+
use crate::error::FFIResult;
4+
5+
type FFILoggerHandle<'a> = HandleShared<'a, FFILogger>;
6+
7+
ffi! {
8+
fn create_logger(log_ref: Ref<FFILoggerFn::log_extern>, out: Out<FFILoggerHandle>) -> FFIResult {
9+
let log = unsafe_block!("" => *log_ref.as_ref());
10+
unsafe_block!("We know logger handle is not null by wrapper macro. And we know `Out` is writable" => out.init(FFILoggerHandle::alloc( FFILogger { log_ptr: log })));
11+
FFIResult::ok()
12+
}
13+
14+
fn release_logger(handle: FFILoggerHandle) -> FFIResult {
15+
unsafe_block!("The upstream caller guarantees the handle will not be accessed after being freed" => FFILoggerHandle::dealloc(handle, |mut handle| {
16+
FFIResult::ok()
17+
}))
18+
}
19+
}

0 commit comments

Comments
 (0)