Skip to content

Commit 8e3ab05

Browse files
committed
add entrypoint for testing
1 parent 194613c commit 8e3ab05

File tree

3 files changed

+116
-9
lines changed

3 files changed

+116
-9
lines changed

bindings/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ crate-type=["staticlib", "cdylib"]
1111

1212
[dependencies]
1313
lightning = { path = "../lightning" }
14-
secp256k1 = "0.17.2"
14+
secp256k1 = "0.15"
1515
bitcoin = "0.21"
1616

1717
[build-dependencies]

bindings/src/adaptors/mod.rs

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,83 @@
11
use std::sync::{Arc};
2-
use std::time::{SystemTime, UNIX_EPOCH};
32

3+
use secp256k1::constants::{SECRET_KEY_SIZE, PUBLIC_KEY_SIZE};
44
use bitcoin::blockdata::transaction::{Transaction};
5+
use bitcoin::util::key::PrivateKey;
56

6-
use lightning::util::config::UserConfig;
77
use lightning::util::logger::{Logger, Record};
88

99
use lightning::chain::chaininterface::{BroadcasterInterface,FeeEstimator,ConfirmationTarget};
1010
use lightning::chain::keysinterface::{KeysManager, InMemoryChannelKeys};
1111
use lightning::chain::transaction::{OutPoint};
1212

13+
use lightning::ln::chan_utils::ChannelPublicKeys;
1314
use lightning::ln::channelmonitor::{ManyChannelMonitor, ChannelMonitor, HTLCUpdate, ChannelMonitorUpdateErr, ChannelMonitorUpdate};
14-
use lightning::ln::channelmanager::{ChannelManager};
1515

1616
use crate::error::FFIResult;
1717
use crate::handle::{Out, Ref, RefMut,HandleShared};
1818

19+
#[repr(transparent)]
20+
struct SecretKey([u8; SECRET_KEY_SIZE]);
21+
22+
#[repr(transparent)]
23+
struct PublicKey([u8; PUBLIC_KEY_SIZE]);
24+
25+
/*
26+
#[repr(C)]
27+
pub struct ChannelPublicKeys{
28+
/// The public key which is used to sign all commitment transactions, as it appears in the
29+
/// on-chain channel lock-in 2-of-2 multisig output.
30+
pub funding_pubkey: PublicKey,
31+
/// The base point which is used (with derive_public_revocation_key) to derive per-commitment
32+
/// revocation keys. The per-commitment revocation private key is then revealed by the owner of
33+
/// a commitment transaction so that their counterparty can claim all available funds if they
34+
/// broadcast an old state.
35+
pub revocation_basepoint: PublicKey,
36+
/// The base point which is used (with derive_public_key) to derive a per-commitment payment
37+
/// public key which receives immediately-spendable non-HTLC-encumbered funds.
38+
pub payment_basepoint: PublicKey,
39+
/// The base point which is used (with derive_public_key) to derive a per-commitment payment
40+
/// public key which receives non-HTLC-encumbered funds which are only available for spending
41+
/// after some delay (or can be claimed via the revocation path).
42+
pub delayed_payment_basepoint: PublicKey,
43+
/// The base point which is used (with derive_public_key) to derive a per-commitment public key
44+
/// which is used to encumber HTLC-in-flight outputs.
45+
pub htlc_basepoint: PublicKey,
46+
};
47+
48+
*/
49+
50+
#[repr(C)]
51+
struct FFIInMemoryChannelKeys {
52+
/// Private key of anchor tx
53+
funding_key: SecretKey,
54+
/// Local secret key for blinded revocation pubkey
55+
revocation_base_key: SecretKey,
56+
/// Local secret key used in commitment tx htlc outputs
57+
payment_base_key: SecretKey,
58+
/// Local secret key used in HTLC tx
59+
delayed_payment_base_key: SecretKey,
60+
/// Local htlc secret key used in commitment tx htlc outputs
61+
htlc_base_key: SecretKey,
62+
/// Commitment seed
63+
commitment_seed: [u8; 32],
64+
/// Local public keys and basepoints
65+
pub(crate) local_channel_pubkeys: ChannelPublicKeys,
66+
/// Remote public keys and base points
67+
pub(crate) remote_channel_pubkeys: Option<ChannelPublicKeys>,
68+
/// The total value of this channel
69+
channel_value_satoshis: u64,
70+
}
71+
72+
// #[repr(c)]
73+
// struct FFIChannelMonitor {
74+
// }
75+
#[repr(C)]
76+
struct FFIOutPoint {
77+
txid: [u8; 32],
78+
index: u16
79+
}
80+
1981
#[repr(C)]
2082
pub struct FFIManyChannelMonitor {
2183
add_monitor_ptr: extern "C" fn(&Self, funding_txo: OutPoint, monitor: ChannelMonitor<InMemoryChannelKeys>) -> i32,
@@ -100,6 +162,3 @@ impl Logger for FFILogger {
100162
(self.log_ptr)(self, record);
101163
}
102164
}
103-
104-
type FFIArcChannelManager = ChannelManager<InMemoryChannelKeys, Arc<FFIManyChannelMonitor>, Arc<FFIBroadCaster>, Arc<KeysManager>, Arc<FFIFeeEstimator>>;
105-
type FFIArcChannelManagerHandle<'a> = HandleShared<'a, FFIArcChannelManager>;

bindings/src/lib.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ mod channelmanager;
2222
mod error;
2323
mod handle;
2424

25+
use std::sync::{Arc};
26+
2527
pub use handle::*;
2628
pub use error::*;
2729

28-
2930
ffi_no_catch! {
3031
fn ffi_last_result(
3132
message_buf: Out<u8>,
@@ -70,4 +71,51 @@ ffi! {
7071
fn ffi_test_ok() -> FFIResult {
7172
FFIResult::ok()
7273
}
73-
}
74+
}
75+
76+
77+
#[cfg(debug_assertions)]
78+
#[repr(C)]
79+
pub struct FFITestInputStruct {
80+
fn_ptr: extern "C" fn(&Self),
81+
}
82+
impl FFITestInputStruct {
83+
pub fn call(&self) {
84+
(self.fn_ptr)(self)
85+
}
86+
}
87+
88+
#[cfg(debug_assertions)]
89+
pub struct FFITestOutputStruct {
90+
field: Arc<FFITestInputStruct>
91+
}
92+
93+
impl FFITestOutputStruct {
94+
fn complete(&self) {
95+
println!("finished!");
96+
}
97+
}
98+
#[cfg(debug_assertions)]
99+
type FFITestOutputStructHandle<'a> = HandleShared<'a, FFITestOutputStruct>;
100+
101+
#[cfg(debug_assertions)]
102+
ffi! {
103+
fn ffi_test_simple_struct_with_function_pointer(
104+
input: Ref<FFITestInputStruct>,
105+
output: Out<FFITestOutputStructHandle>
106+
) -> FFIResult {
107+
let input_arc = unsafe_block!(" " => input.as_arc());
108+
let result = FFITestOutputStruct{ field: input_arc };
109+
result.field.call();
110+
unsafe_block!("We know output is not null by wrapper macro. And we know `Out` is writable" => output.init(HandleShared::alloc(result)));
111+
FFIResult::ok()
112+
}
113+
114+
fn ffi_test_release(handle: FFITestOutputStructHandle) -> FFIResult {
115+
unsafe_block!("The upstream caller guarantees the handle will not be accessed after being freed" => FFITestOutputStructHandle::dealloc(handle, |mut handle| {
116+
handle.complete();
117+
118+
FFIResult::ok()
119+
}))
120+
}
121+
}

0 commit comments

Comments
 (0)