Skip to content

Commit 78ae376

Browse files
committed
Generate UniFFI scaffolding
We generate the scaffolding from an UDL file and include it in `lib.rs`. Furthermore, we add a bindings generation shell script for convenience.
1 parent 407e957 commit 78ae376

File tree

7 files changed

+249
-39
lines changed

7 files changed

+249
-39
lines changed

Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ edition = "2018"
77

88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

10+
[lib]
11+
crate-type = ["staticlib", "cdylib"]
12+
name = "ldk_lite"
13+
1014
[dependencies]
1115
#lightning = { version = "0.0.112", features = ["max_level_trace", "std"] }
1216
#lightning-invoice = { version = "0.20" }
@@ -46,13 +50,18 @@ chrono = "0.4"
4650
futures = "0.3"
4751
serde_json = { version = "1.0" }
4852
tokio = { version = "1", features = [ "full" ] }
53+
uniffi = { version = "0.21.0", features = ["builtin-bindgen"] }
54+
uniffi_macros = { version = "0.21.0", features = ["builtin-bindgen"] }
4955

5056
[dev-dependencies]
5157
electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] }
5258
electrum-client = "0.12.0"
5359
lazy_static = "1.4.0"
5460
once_cell = "1.16.0"
5561

62+
[build-dependencies]
63+
uniffi_build = "0.21.0"
64+
5665
[profile.release]
5766
panic = "abort"
5867

build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
uniffi_build::generate_scaffolding("uniffi/ldk_lite.udl").unwrap();
3+
}

src/error.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ pub enum Error {
1111
FundingTxCreationFailed,
1212
/// A network connection has been closed.
1313
ConnectionFailed,
14+
/// The given address is invalid.
15+
AddressInvalid,
16+
/// The given public key is invalid.
17+
PublicKeyInvalid,
18+
/// The given payment hash is invalid.
19+
PaymentHashInvalid,
1420
/// Payment of the given invoice has already been intiated.
1521
NonUniquePaymentHash,
1622
/// The given invoice is invalid.
1723
InvoiceInvalid,
1824
/// Invoice creation failed.
1925
InvoiceCreationFailed,
26+
/// The given channel ID is invalid.
27+
ChannelIdInvalid,
2028
/// No route for the given target could be found.
2129
RoutingFailed,
2230
/// A given peer info could not be parsed.
@@ -40,13 +48,15 @@ impl fmt::Display for Error {
4048
match *self {
4149
Self::AlreadyRunning => write!(f, "LDKLite is already running."),
4250
Self::NotRunning => write!(f, "LDKLite is not running."),
43-
Self::FundingTxCreationFailed => {
44-
write!(f, "Funding transaction could not be created.")
45-
}
51+
Self::FundingTxCreationFailed => write!(f, "Funding transaction could not be created."),
4652
Self::ConnectionFailed => write!(f, "Network connection closed."),
53+
Self::AddressInvalid => write!(f, "The given address is invalid."),
54+
Self::PublicKeyInvalid => write!(f, "The given public key is invalid."),
55+
Self::PaymentHashInvalid => write!(f, "The given payment hash is invalid."),
4756
Self::NonUniquePaymentHash => write!(f, "An invoice must not get payed twice."),
4857
Self::InvoiceInvalid => write!(f, "The given invoice is invalid."),
4958
Self::InvoiceCreationFailed => write!(f, "Failed to create invoice."),
59+
Self::ChannelIdInvalid => write!(f, "The given channel ID is invalid."),
5060
Self::RoutingFailed => write!(f, "Failed to find route."),
5161
Self::PeerInfoParseFailed => write!(f, "Failed to parse the given peer information."),
5262
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),

src/event.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
hex_utils, ChannelManager, Config, Error, NetworkGraph, PaymentInfo, PaymentInfoStorage,
3-
PaymentStatus, Wallet,
2+
hex_utils, ChannelId, ChannelManager, Config, Error, NetworkGraph, PaymentInfo, PaymentInfoStorage,
3+
PaymentStatus, Wallet, UserChannelId,
44
};
55

66
use crate::logger::{log_error, log_given_level, log_info, log_internal, Logger};
@@ -50,16 +50,16 @@ pub enum Event {
5050
/// A channel is ready to be used.
5151
ChannelReady {
5252
/// The `channel_id` of the channel.
53-
channel_id: [u8; 32],
53+
channel_id: ChannelId,
5454
/// The `user_channel_id` of the channel.
55-
user_channel_id: u128,
55+
user_channel_id: UserChannelId,
5656
},
5757
/// A channel has been closed.
5858
ChannelClosed {
5959
/// The `channel_id` of the channel.
60-
channel_id: [u8; 32],
60+
channel_id: ChannelId,
6161
/// The `user_channel_id` of the channel.
62-
user_channel_id: u128,
62+
user_channel_id: UserChannelId,
6363
},
6464
}
6565

@@ -83,13 +83,13 @@ impl Readable for Event {
8383
Ok(Self::PaymentReceived { payment_hash, amount_msat })
8484
}
8585
3u8 => {
86-
let channel_id: [u8; 32] = Readable::read(reader)?;
87-
let user_channel_id: u128 = Readable::read(reader)?;
86+
let channel_id = ChannelId(Readable::read(reader)?);
87+
let user_channel_id = UserChannelId(Readable::read(reader)?);
8888
Ok(Self::ChannelReady { channel_id, user_channel_id })
8989
}
9090
4u8 => {
91-
let channel_id: [u8; 32] = Readable::read(reader)?;
92-
let user_channel_id: u128 = Readable::read(reader)?;
91+
let channel_id = ChannelId(Readable::read(reader)?);
92+
let user_channel_id = UserChannelId(Readable::read(reader)?);
9393
Ok(Self::ChannelClosed { channel_id, user_channel_id })
9494
}
9595
_ => Err(lightning::ln::msgs::DecodeError::InvalidValue),
@@ -118,14 +118,14 @@ impl Writeable for Event {
118118
}
119119
Self::ChannelReady { channel_id, user_channel_id } => {
120120
3u8.write(writer)?;
121-
channel_id.write(writer)?;
122-
user_channel_id.write(writer)?;
121+
channel_id.0.write(writer)?;
122+
user_channel_id.0.write(writer)?;
123123
Ok(())
124124
}
125125
Self::ChannelClosed { channel_id, user_channel_id } => {
126126
4u8.write(writer)?;
127-
channel_id.write(writer)?;
128-
user_channel_id.write(writer)?;
127+
channel_id.0.write(writer)?;
128+
user_channel_id.0.write(writer)?;
129129
Ok(())
130130
}
131131
}
@@ -136,7 +136,7 @@ pub(crate) struct EventQueue<K: Deref>
136136
where
137137
K::Target: KVStorePersister,
138138
{
139-
queue: Mutex<VecDeque<Arc<Event>>>,
139+
queue: Mutex<VecDeque<Event>>,
140140
notifier: Condvar,
141141
persister: K,
142142
}
@@ -146,26 +146,26 @@ where
146146
K::Target: KVStorePersister,
147147
{
148148
pub(crate) fn new(persister: K) -> Self {
149-
let queue: Mutex<VecDeque<Arc<Event>>> = Mutex::new(VecDeque::new());
149+
let queue: Mutex<VecDeque<Event>> = Mutex::new(VecDeque::new());
150150
let notifier = Condvar::new();
151151
Self { queue, notifier, persister }
152152
}
153153

154154
pub(crate) fn add_event(&self, event: Event) -> Result<(), Error> {
155155
{
156156
let mut locked_queue = self.queue.lock().unwrap();
157-
locked_queue.push_back(Arc::new(event));
157+
locked_queue.push_back(event);
158158
self.persist_queue(&*locked_queue)?;
159159
}
160160

161161
self.notifier.notify_one();
162162
Ok(())
163163
}
164164

165-
pub(crate) fn next_event(&self) -> Arc<Event> {
165+
pub(crate) fn next_event(&self) -> Event {
166166
let locked_queue =
167167
self.notifier.wait_while(self.queue.lock().unwrap(), |queue| queue.is_empty()).unwrap();
168-
Arc::clone(&locked_queue.front().unwrap())
168+
locked_queue.front().unwrap().clone()
169169
}
170170

171171
pub(crate) fn event_handled(&self) -> Result<(), Error> {
@@ -178,7 +178,7 @@ where
178178
Ok(())
179179
}
180180

181-
fn persist_queue(&self, locked_queue: &VecDeque<Arc<Event>>) -> Result<(), Error> {
181+
fn persist_queue(&self, locked_queue: &VecDeque<Event>) -> Result<(), Error> {
182182
self.persister
183183
.persist(EVENTS_PERSISTENCE_KEY, &EventQueueSerWrapper(locked_queue))
184184
.map_err(|_| Error::PersistenceFailed)?;
@@ -195,13 +195,13 @@ where
195195
reader: &mut R, persister: K,
196196
) -> Result<Self, lightning::ln::msgs::DecodeError> {
197197
let read_queue: EventQueueDeserWrapper = Readable::read(reader)?;
198-
let queue: Mutex<VecDeque<Arc<Event>>> = Mutex::new(read_queue.0);
198+
let queue: Mutex<VecDeque<Event>> = Mutex::new(read_queue.0);
199199
let notifier = Condvar::new();
200200
Ok(Self { queue, notifier, persister })
201201
}
202202
}
203203

204-
struct EventQueueDeserWrapper(VecDeque<Arc<Event>>);
204+
struct EventQueueDeserWrapper(VecDeque<Event>);
205205

206206
impl Readable for EventQueueDeserWrapper {
207207
fn read<R: lightning::io::Read>(
@@ -210,13 +210,13 @@ impl Readable for EventQueueDeserWrapper {
210210
let len: u16 = Readable::read(reader)?;
211211
let mut queue = VecDeque::with_capacity(len as usize);
212212
for _ in 0..len {
213-
queue.push_back(Arc::new(Readable::read(reader)?));
213+
queue.push_back(Readable::read(reader)?);
214214
}
215215
Ok(Self(queue))
216216
}
217217
}
218218

219-
struct EventQueueSerWrapper<'a>(&'a VecDeque<Arc<Event>>);
219+
struct EventQueueSerWrapper<'a>(&'a VecDeque<Event>);
220220

221221
impl Writeable for EventQueueSerWrapper<'_> {
222222
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
@@ -559,7 +559,7 @@ where
559559
counterparty_node_id,
560560
);
561561
self.event_queue
562-
.add_event(Event::ChannelReady { channel_id, user_channel_id })
562+
.add_event(Event::ChannelReady { channel_id: ChannelId(channel_id), user_channel_id: UserChannelId(user_channel_id) })
563563
.expect("Failed to push to event queue");
564564
}
565565
LdkEvent::ChannelClosed { channel_id, reason, user_channel_id } => {
@@ -570,7 +570,7 @@ where
570570
reason
571571
);
572572
self.event_queue
573-
.add_event(Event::ChannelClosed { channel_id, user_channel_id })
573+
.add_event(Event::ChannelClosed { channel_id: ChannelId(channel_id), user_channel_id: UserChannelId(user_channel_id) })
574574
.expect("Failed to push to event queue");
575575
}
576576
LdkEvent::DiscardFunding { .. } => {}

0 commit comments

Comments
 (0)