Skip to content

Commit 7a4799b

Browse files
committed
update
1 parent 5bb67f3 commit 7a4799b

File tree

5 files changed

+70
-37
lines changed

5 files changed

+70
-37
lines changed

bindings/src/adaptors/mod.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,41 +126,20 @@ impl From<Level> for FFILogLevel {
126126
}
127127

128128
#[repr(C)]
129+
#[derive(Debug)]
129130
pub struct FFILogRecord {
130131
/// The verbosity level of the message.
131132
pub level: FFILogLevel,
132133
/// The message body.
133-
pub args: CString,
134+
pub args: *const i8,
134135
/// The module path of the message.
135-
pub module_path: CString,
136+
pub module_path: *const i8,
136137
/// The source file containing the message.
137-
pub file: CString,
138+
pub file: *const i8,
138139
/// The line containing the message.
139140
pub line: u32,
140141
}
141142

142-
143-
impl<'a> From<Record<'a>> for FFILogRecord {
144-
fn from(rec: Record<'a>) -> Self {
145-
let args = CString::new(std::fmt::format(rec.args)).unwrap_or(CString::new("Record.args contains null char in the middle").unwrap());
146-
let module_path = CString::new(rec.module_path).unwrap_or(CString::new("Record.module_path contains null char in the middle").unwrap());
147-
let file = CString::new(rec.file).unwrap_or(CString::new("Record.file contains null char in the middle").unwrap());
148-
FFILogRecord {
149-
level: rec.level.into(),
150-
args,
151-
module_path,
152-
file,
153-
line: rec.line,
154-
}
155-
}
156-
}
157-
158-
impl<'a, 'b> From<&'a Record<'b>> for FFILogRecord {
159-
fn from(rec: &'a Record) -> Self {
160-
rec.into()
161-
}
162-
}
163-
164143
pub mod ffilogger_fn {
165144
use super::{FFILogRecord, FFILogger};
166145
pub type LogExtern = extern "cdecl" fn(record: *const FFILogRecord);
@@ -172,9 +151,21 @@ pub struct FFILogger {
172151
}
173152

174153
impl Logger for FFILogger {
175-
fn log(&self, record: &Record) {
176-
let ffi_record: FFILogRecord = record.into();
154+
fn log(&self, rec: &Record) {
155+
let args = CString::new(std::fmt::format(rec.args)).unwrap_or(CString::new("Record.args contains null char in the middle").unwrap());
156+
let module_path = CString::new(rec.module_path).unwrap_or(CString::new("Record.module_path contains null char in the middle").unwrap());
157+
let file = CString::new(rec.file).unwrap_or(CString::new("Record.file contains null char in the middle").unwrap());
158+
let ffi_record =
159+
FFILogRecord {
160+
level: rec.level.into(),
161+
args: args.as_ptr(),
162+
module_path: module_path.as_ptr(),
163+
file: file.as_ptr(),
164+
line: rec.line,
165+
};
166+
println!("Going to call log callback with {:?}", ffi_record);
177167
(self.log_ptr)(&ffi_record as *const _);
168+
println!("rust: finished calling logger");
178169
}
179170
}
180171

@@ -183,7 +174,7 @@ pub mod chain_watch_interface_fn {
183174
pub type InstallWatchTxPtr = extern "cdecl" fn(*const FFISha256dHash, script_pub_key: *const FFIScript);
184175
pub type InstallWatchOutpointPtr = extern "cdecl" fn(outpoint: *const FFIOutPoint, out_script: *const FFIScript);
185176
pub type WatchAllTxnPtr = extern "cdecl" fn();
186-
pub type GetChainUtxoPtr = extern "cdecl" fn(genesis_hash: *const FFISha256dHash, unspent_tx_output_identifier: u64, err: *mut ChainError, script: *mut FFITxOut);
177+
pub type GetChainUtxoPtr = extern "cdecl" fn(genesis_hash: *const FFISha256dHash, unspent_tx_output_identifier: u64, err: *mut FFIChainError, script: *mut FFITxOut);
187178
pub type FilterBlockPtr = extern "cdecl" fn(*const FFIBlock);
188179
}
189180

@@ -210,10 +201,20 @@ impl ChainWatchInterface for FFIChainWatchInterface {
210201
(self.install_watch_outpoint_ptr)(&ffi_outpoint as *const _, &ffi_outscript as *const _)
211202
}
212203
fn watch_all_txn(&self) {
213-
unimplemented!()
204+
(self.watch_all_txn_ptr)()
214205
}
215206
fn get_chain_utxo(&self, genesis_hash: Sha256dHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
216-
unimplemented!()
207+
let err = std::ptr::null_mut();
208+
let tx_out = std::ptr::null_mut();
209+
(self.get_chain_utxo_ptr)(&genesis_hash.into(), unspent_tx_output_identifier, err, tx_out);
210+
if err.is_null() {
211+
let tx_out: FFITxOut = unsafe_block!("We know the caller has set the value into the tx_out" => (*tx_out).clone());
212+
Ok((tx_out.script_pubkey.to_script(), tx_out.value))
213+
}
214+
else {
215+
let e = unsafe_block!("we know the error is not a null pointer" => (*err).clone());
216+
Err(e.into())
217+
}
217218
}
218219
fn filter_block<'a>(&self, block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>) {
219220
unimplemented!()

bindings/src/adaptors/primitives.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ use bitcoin::blockdata::script::Script;
33
use bitcoin::blockdata::transaction::{Transaction};
44
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
55
use crate::FFIResult;
6+
use lightning::chain::chaininterface::ChainError;
67

78
macro_rules! array_struct{
89
(
910
$(#[$meta:meta])*
1011
$name:ident, $ty:ty) => {
1112
$(#[$meta])*
13+
#[derive(Clone)]
1214
#[repr(C)]
1315
pub struct $name<'a> {
1416
ptr: *const $ty,
@@ -61,18 +63,47 @@ impl<'a> From<Sha256dHash> for FFISha256dHash<'a> {
6163

6264

6365
array_struct!(FFIScript, u8);
66+
impl<'a> FFIScript<'a> {
67+
pub fn to_script(&self) -> Script {
68+
unimplemented!()
69+
}
70+
}
6471

72+
#[derive(Clone)]
6573
#[repr(C)]
6674
pub struct FFIOutPoint<'a> {
6775
pub txid: FFISha256dHash<'a>,
6876
pub index: u32,
6977
}
7078

79+
#[derive(Clone)]
7180
#[repr(C)]
7281
pub struct FFITxOut<'a> {
73-
value: u64,
74-
script_pubkey: FFIScript<'a>,
82+
pub value: u64,
83+
pub script_pubkey: FFIScript<'a>,
84+
}
85+
86+
#[derive(Clone, Copy, Debug)]
87+
#[repr(C)]
88+
pub enum FFIChainError {
89+
/// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)
90+
NotSupported,
91+
/// Chain isn't the one watched
92+
NotWatched,
93+
/// Tx doesn't exist or is unconfirmed
94+
UnknownTx,
7595
}
7696

97+
impl From<FFIChainError> for ChainError {
98+
fn from(e: FFIChainError) -> Self {
99+
match e {
100+
FFIChainError::NotSupported => ChainError::NotSupported,
101+
FFIChainError::NotWatched => ChainError::NotWatched,
102+
FFIChainError::UnknownTx => ChainError::UnknownTx,
103+
}
104+
}
105+
}
106+
107+
77108
array_struct!(FFITransaction, u8);
78109
array_struct!(FFIBlock, u8);

bindings/src/block_notirifer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
use crate::handle::HandleShared;
33

4-
pub type FFIBlockNotifierHandle = HandleShared<'a, >;
4+
pub type FFIBlockNotifierHandle = HandleShared<'a, FFIBlockNotifier>;

bindings/src/logger.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub type FFILoggerHandle<'a> = HandleShared<'a, FFILogger>;
77

88
ffi! {
99
fn create_logger(log_ref: Ref<ffilogger_fn::LogExtern>, out: Out<FFILoggerHandle>) -> FFIResult {
10-
let log = unsafe_block!("" => *log_ref.as_ref());
11-
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 })));
10+
let log = unsafe_block!("" => log_ref.as_ref());
11+
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 })));
1212
FFIResult::ok()
1313
}
1414

@@ -26,7 +26,9 @@ use lightning::util::logger::{Record, Level};
2626
ffi! {
2727
fn test_logger(handle: FFILoggerHandle) -> FFIResult {
2828
let logger: &FFILogger = unsafe_block!("" => handle.as_ref());
29-
logger.log(&Record::new(Level::Warn, std::format_args!("{}", "warn message for ffi."), "module_path", "logger.rs", 29));
29+
let r: Record = Record::new(Level::Warn, std::format_args!("{}", "warn message for ffi."), "module_path", "logger.rs", 29);
30+
logger.log(&r);
31+
println!("rust: finished calling test_logger");
3032
FFIResult::ok()
3133
}
3234
}

lightning/src/chain/chaininterface.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use std::panic::{UnwindSafe, RefUnwindSafe};
2323
use std::ptr;
2424

2525
/// Used to give chain error details upstream
26-
#[repr(C)]
2726
pub enum ChainError {
2827
/// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)
2928
NotSupported,

0 commit comments

Comments
 (0)