Skip to content

Commit 76b135f

Browse files
committed
separate adaptor types into adaptors folder
1 parent 4e9c962 commit 76b135f

File tree

3 files changed

+109
-95
lines changed

3 files changed

+109
-95
lines changed

bindings/src/adaptors/mod.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use std::sync::{Arc};
2+
use std::time::{SystemTime, UNIX_EPOCH};
3+
4+
use bitcoin::blockdata::transaction::{Transaction};
5+
6+
use lightning::util::config::UserConfig;
7+
use lightning::util::logger::{Logger, Record};
8+
9+
use lightning::chain::chaininterface::{BroadcasterInterface,FeeEstimator,ConfirmationTarget};
10+
use lightning::chain::keysinterface::{KeysManager, InMemoryChannelKeys};
11+
use lightning::chain::transaction::{OutPoint};
12+
13+
use lightning::ln::channelmonitor::{ManyChannelMonitor, ChannelMonitor, HTLCUpdate, ChannelMonitorUpdateErr, ChannelMonitorUpdate};
14+
use lightning::ln::channelmanager::{ChannelManager};
15+
16+
use crate::error::FFIResult;
17+
use crate::handle::{Out, Ref, RefMut,HandleShared};
18+
19+
#[repr(C)]
20+
pub struct FFIManyChannelMonitor {
21+
add_monitor_ptr: extern "C" fn(&Self, funding_txo: OutPoint, monitor: ChannelMonitor<InMemoryChannelKeys>) -> i32,
22+
update_monitor_ptr: extern "C" fn(&Self, funding_txo: OutPoint, monitor: ChannelMonitorUpdate) -> i32,
23+
get_and_clear_pending_htlcs_updated_ptr: extern "C" fn(&Self) -> Vec<HTLCUpdate>,
24+
}
25+
impl FFIManyChannelMonitor {
26+
fn errorcode_to_result(errorcode: i32) -> Result<(), ChannelMonitorUpdateErr> {
27+
if errorcode == 0 {
28+
return Ok(());
29+
}
30+
if errorcode == 1 {
31+
return Err(ChannelMonitorUpdateErr::PermanentFailure);
32+
}
33+
if errorcode == 2 {
34+
return Err(ChannelMonitorUpdateErr::TemporaryFailure);
35+
}
36+
unreachable!("Unknown error code from ffi {}", errorcode);
37+
}
38+
}
39+
40+
impl ManyChannelMonitor<InMemoryChannelKeys> for FFIManyChannelMonitor {
41+
fn add_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor<InMemoryChannelKeys>) -> Result<(), ChannelMonitorUpdateErr> {
42+
let errorcode = (self.add_monitor_ptr)(self, funding_txo, monitor);
43+
FFIManyChannelMonitor::errorcode_to_result(errorcode)
44+
}
45+
fn update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr> {
46+
let errorcode = (self.update_monitor_ptr)(self, funding_txo, monitor);
47+
FFIManyChannelMonitor::errorcode_to_result(errorcode)
48+
}
49+
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
50+
(self.get_and_clear_pending_htlcs_updated_ptr)(self)
51+
}
52+
}
53+
54+
#[repr(C)]
55+
pub struct FFIBroadCaster {
56+
broadcast_transaction_ptr: extern "C" fn(&Self, tx: &Transaction),
57+
}
58+
59+
impl BroadcasterInterface for FFIBroadCaster {
60+
fn broadcast_transaction(&self, tx: &Transaction) {
61+
(self.broadcast_transaction_ptr)(self, tx)
62+
}
63+
}
64+
65+
#[repr(C)]
66+
pub struct FFIFeeEstimator {
67+
get_est_sat_per_1000_weight_ptr: extern "C" fn (&Self, ConfirmationTarget) -> u64,
68+
}
69+
70+
impl FeeEstimator for FFIFeeEstimator {
71+
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64 {
72+
(self.get_est_sat_per_1000_weight_ptr)(self, confirmation_target)
73+
}
74+
}
75+
76+
#[repr(C)]
77+
pub enum FFINetwork {
78+
MainNet = 0,
79+
TestNet = 1,
80+
RegTest = 2,
81+
}
82+
83+
impl FFINetwork {
84+
pub fn to_network(&self) -> bitcoin::network::constants::Network {
85+
match self {
86+
FFINetwork::MainNet => { bitcoin::network::constants::Network::Bitcoin },
87+
FFINetwork::TestNet => { bitcoin::network::constants::Network::Testnet },
88+
FFINetwork::RegTest => { bitcoin::network::constants::Network::Regtest },
89+
}
90+
}
91+
}
92+
93+
#[repr(C)]
94+
pub struct FFILogger {
95+
log_ptr: fn(&Self, &Record),
96+
}
97+
98+
impl Logger for FFILogger {
99+
fn log(&self, record: &Record) {
100+
(self.log_ptr)(self, record);
101+
}
102+
}
103+
104+
type FFIArcChannelManager = ChannelManager<InMemoryChannelKeys, Arc<FFIManyChannelMonitor>, Arc<FFIBroadCaster>, Arc<KeysManager>, Arc<FFIFeeEstimator>>;
105+
type FFIArcChannelManagerHandle<'a> = HandleShared<'a, FFIArcChannelManager>;

bindings/src/channelmanager.rs

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,13 @@
1-
use std::sync::{Arc};
1+
use std::sync::Arc;
22
use std::time::{SystemTime, UNIX_EPOCH};
33

4-
use bitcoin::blockdata::transaction::{Transaction};
5-
64
use lightning::util::config::UserConfig;
7-
use lightning::util::logger::{Logger, Record};
8-
9-
use lightning::chain::chaininterface::{BroadcasterInterface,FeeEstimator,ConfirmationTarget};
105
use lightning::chain::keysinterface::{KeysManager, InMemoryChannelKeys};
11-
use lightning::chain::transaction::{OutPoint};
12-
13-
use lightning::ln::channelmonitor::{ManyChannelMonitor, ChannelMonitor, HTLCUpdate, ChannelMonitorUpdateErr, ChannelMonitorUpdate};
146
use lightning::ln::channelmanager::{ChannelManager};
157

16-
use crate::error::FFIResult;
178
use crate::handle::{Out, Ref, RefMut,HandleShared};
18-
19-
#[repr(C)]
20-
pub struct FFIManyChannelMonitor {
21-
add_monitor_ptr: extern "C" fn(&Self, funding_txo: OutPoint, monitor: ChannelMonitor<InMemoryChannelKeys>) -> i32,
22-
update_monitor_ptr: extern "C" fn(&Self, funding_txo: OutPoint, monitor: ChannelMonitorUpdate) -> i32,
23-
get_and_clear_pending_htlcs_updated_ptr: extern "C" fn(&Self) -> Vec<HTLCUpdate>,
24-
}
25-
impl FFIManyChannelMonitor {
26-
fn errorcode_to_result(errorcode: i32) -> Result<(), ChannelMonitorUpdateErr> {
27-
if errorcode == 0 {
28-
return Ok(());
29-
}
30-
if errorcode == 1 {
31-
return Err(ChannelMonitorUpdateErr::PermanentFailure);
32-
}
33-
if errorcode == 2 {
34-
return Err(ChannelMonitorUpdateErr::TemporaryFailure);
35-
}
36-
unreachable!("Unknown error code from ffi {}", errorcode);
37-
}
38-
}
39-
40-
impl ManyChannelMonitor<InMemoryChannelKeys> for FFIManyChannelMonitor {
41-
fn add_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor<InMemoryChannelKeys>) -> Result<(), ChannelMonitorUpdateErr> {
42-
let errorcode = (self.add_monitor_ptr)(self, funding_txo, monitor);
43-
FFIManyChannelMonitor::errorcode_to_result(errorcode)
44-
}
45-
fn update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr> {
46-
let errorcode = (self.update_monitor_ptr)(self, funding_txo, monitor);
47-
FFIManyChannelMonitor::errorcode_to_result(errorcode)
48-
}
49-
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
50-
(self.get_and_clear_pending_htlcs_updated_ptr)(self)
51-
}
52-
}
53-
54-
#[repr(C)]
55-
pub struct FFIBroadCaster {
56-
broadcast_transaction_ptr: extern "C" fn(&Self, tx: &Transaction),
57-
}
58-
59-
impl BroadcasterInterface for FFIBroadCaster {
60-
fn broadcast_transaction(&self, tx: &Transaction) {
61-
(self.broadcast_transaction_ptr)(self, tx)
62-
}
63-
}
64-
65-
#[repr(C)]
66-
pub struct FFIFeeEstimator {
67-
get_est_sat_per_1000_weight_ptr: extern "C" fn (&Self, ConfirmationTarget) -> u64,
68-
}
69-
70-
impl FeeEstimator for FFIFeeEstimator {
71-
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64 {
72-
(self.get_est_sat_per_1000_weight_ptr)(self, confirmation_target)
73-
}
74-
}
75-
76-
#[repr(C)]
77-
pub enum FFINetwork {
78-
MainNet = 0,
79-
TestNet = 1,
80-
RegTest = 2,
81-
}
82-
83-
impl FFINetwork {
84-
pub fn to_network(&self) -> bitcoin::network::constants::Network {
85-
match self {
86-
FFINetwork::MainNet => { bitcoin::network::constants::Network::Bitcoin },
87-
FFINetwork::TestNet => { bitcoin::network::constants::Network::Testnet },
88-
FFINetwork::RegTest => { bitcoin::network::constants::Network::Regtest },
89-
}
90-
}
91-
}
92-
93-
#[repr(C)]
94-
pub struct FFILogger {
95-
log_ptr: fn(&Self, &Record),
96-
}
97-
98-
impl Logger for FFILogger {
99-
fn log(&self, record: &Record) {
100-
(self.log_ptr)(self, record);
101-
}
102-
}
9+
use crate::error::FFIResult;
10+
use crate::adaptors::*;
10311

10412
type FFIArcChannelManager = ChannelManager<InMemoryChannelKeys, Arc<FFIManyChannelMonitor>, Arc<FFIBroadCaster>, Arc<KeysManager>, Arc<FFIFeeEstimator>>;
10513
type FFIArcChannelManagerHandle<'a> = HandleShared<'a, FFIArcChannelManager>;

bindings/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub(crate) mod lazy_static;
1717

1818
pub(crate) mod is_null;
1919

20+
mod adaptors;
2021
mod channelmanager;
2122
mod error;
2223
mod handle;

0 commit comments

Comments
 (0)