Skip to content

0.0.13 Bindings Updates #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 changes: 1 addition & 1 deletion background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl BackgroundProcessor {
let handle = thread::spawn(move || -> Result<(), std::io::Error> {
let mut current_time = Instant::now();
loop {
let updates_available = manager.wait_timeout(Duration::from_millis(100));
let updates_available = manager.await_persistable_update_timeout(Duration::from_millis(100));
if updates_available {
persist_manager(&*manager)?;
}
Expand Down
19 changes: 9 additions & 10 deletions c-bindings-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
writeln!(w, "\t/// the Rust equivalent takes an Option, it may be set to null to indicate None.").unwrap();
writeln!(w, "\tpub inner: *mut native{},\n\tpub is_owned: bool,\n}}\n", ident).unwrap();
writeln!(w, "impl Drop for {} {{\n\tfn drop(&mut self) {{", struct_name).unwrap();
writeln!(w, "\t\tif self.is_owned && !self.inner.is_null() {{").unwrap();
writeln!(w, "\t\tif self.is_owned && !<*mut native{}>::is_null(self.inner) {{", ident).unwrap();
writeln!(w, "\t\t\tlet _ = unsafe {{ Box::from_raw(self.inner) }};\n\t\t}}\n\t}}\n}}").unwrap();
writeln!(w, "#[no_mangle]\npub extern \"C\" fn {}_free(this_ptr: {}) {{ }}", struct_name, struct_name).unwrap();
writeln!(w, "#[allow(unused)]").unwrap();
Expand Down Expand Up @@ -808,10 +808,10 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
}
}
) );
write!(w, "\t}}\n}}\nuse {}::{} as {}TraitImport;\n", types.orig_crate, full_trait_path, trait_obj.ident).unwrap();
writeln!(w, "\t}}\n}}\n").unwrap();

macro_rules! impl_meth {
($m: expr, $trait: expr, $indent: expr) => {
($m: expr, $trait_path: expr, $trait: expr, $indent: expr) => {
let trait_method = $trait.items.iter().filter_map(|item| {
if let syn::TraitItem::Method(t_m) = item { Some(t_m) } else { None }
}).find(|trait_meth| trait_meth.sig.ident == $m.sig.ident).unwrap();
Expand Down Expand Up @@ -842,9 +842,9 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
t_gen_args += "_"
}
if takes_self {
write!(w, "<native{} as {}TraitImport<{}>>::{}(unsafe {{ &mut *(this_arg as *mut native{}) }}, ", ident, $trait.ident, t_gen_args, $m.sig.ident, ident).unwrap();
write!(w, "<native{} as {}::{}<{}>>::{}(unsafe {{ &mut *(this_arg as *mut native{}) }}, ", ident, types.orig_crate, $trait_path, t_gen_args, $m.sig.ident, ident).unwrap();
} else {
write!(w, "<native{} as {}TraitImport<{}>>::{}(", ident, $trait.ident, t_gen_args, $m.sig.ident).unwrap();
write!(w, "<native{} as {}::{}<{}>>::{}(", ident, types.orig_crate, $trait_path, t_gen_args, $m.sig.ident).unwrap();
}

let mut real_type = "".to_string();
Expand Down Expand Up @@ -881,20 +881,19 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
for item in i.items.iter() {
match item {
syn::ImplItem::Method(m) => {
impl_meth!(m, trait_obj, "");
impl_meth!(m, full_trait_path, trait_obj, "");
},
syn::ImplItem::Type(_) => {},
_ => unimplemented!(),
}
}
walk_supertraits!(trait_obj, Some(&types), (
(s, t) => {
(s, _) => {
if let Some(supertrait_obj) = types.crate_types.traits.get(s).cloned() {
writeln!(w, "use {}::{} as native{}Trait;", types.orig_crate, s, t).unwrap();
for item in supertrait_obj.items.iter() {
match item {
syn::TraitItem::Method(m) => {
impl_meth!(m, supertrait_obj, "\t");
impl_meth!(m, s, supertrait_obj, "\t");
},
_ => {},
}
Expand All @@ -914,7 +913,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
writeln!(w, "impl Clone for {} {{", ident).unwrap();
writeln!(w, "\tfn clone(&self) -> Self {{").unwrap();
writeln!(w, "\t\tSelf {{").unwrap();
writeln!(w, "\t\t\tinner: if self.inner.is_null() {{ std::ptr::null_mut() }} else {{").unwrap();
writeln!(w, "\t\t\tinner: if <*mut native{}>::is_null(self.inner) {{ std::ptr::null_mut() }} else {{", ident).unwrap();
writeln!(w, "\t\t\t\tBox::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())) }},").unwrap();
writeln!(w, "\t\t\tis_owned: true,").unwrap();
writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();
Expand Down
8 changes: 3 additions & 5 deletions c-bindings-gen/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
"bitcoin::blockdata::transaction::Transaction" => Some("crate::c_types::Transaction::from_vec(local_"),
"bitcoin::blockdata::transaction::OutPoint" => Some("crate::c_types::bitcoin_to_C_outpoint("),
"bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut::from_rust("),
"bitcoin::network::constants::Network" => Some("crate::bitcoin::network::Network::from_bitcoin("),
"bitcoin::blockdata::block::BlockHeader" if is_ref => Some("&local_"),
"bitcoin::blockdata::block::Block" if is_ref => Some("crate::c_types::u8slice::from_slice(&local_"),

Expand Down Expand Up @@ -899,6 +900,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
"bitcoin::blockdata::transaction::Transaction" => Some(")"),
"bitcoin::blockdata::transaction::OutPoint" => Some(")"),
"bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(")"),
"bitcoin::network::constants::Network" => Some(")"),
"bitcoin::blockdata::block::BlockHeader" if is_ref => Some(""),
"bitcoin::blockdata::block::Block" if is_ref => Some(")"),

Expand Down Expand Up @@ -1944,11 +1946,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
if !self.write_c_path_intern(w, &$p_arg.path, generics, true, true, true) { return false; }
}
} else {
if $p_arg.path.segments.len() == 1 {
write!(w, "{}", $p_arg.path.segments.iter().next().unwrap().ident).unwrap();
} else {
return false;
}
write!(w, "{}", $p_arg.path.segments.last().unwrap().ident).unwrap();
}
} else if self.is_known_container(&subtype, is_ref) || self.is_transparent_container(&subtype, is_ref) {
if !self.write_c_mangled_container_path_intern(w, Self::path_to_generic_args(&$p_arg.path), generics,
Expand Down
12 changes: 10 additions & 2 deletions genbindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,16 @@ else
echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
fi

cargo rustc -v --target=wasm32-wasi -- -C embed-bitcode=yes || echo "WARNING: Failed to generate WASM LLVM-bitcode-embedded library"
CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target=wasm32-wasi -- -C opt-level=s -C linker-plugin-lto -C lto || echo "WARNING: Failed to generate WASM LLVM-bitcode-embedded optimized library"
if [ "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
# Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1)
echo "int main() {}" > genbindings_wasm_test_file.c
clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1 &&
# And if it does, build a WASM binary without capturing errors
cargo rustc -v --target=wasm32-wasi -- -C embed-bitcode=yes &&
CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target=wasm32-wasi -- -C opt-level=s -C linker-plugin-lto -C lto ||
echo "Cannot build WASM lib as clang does not seem to support the wasm32-wasi target"
rm genbindings_wasm_test_file.c
fi

# Now build with LTO on on both C++ and rust, but without cross-language LTO:
CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
Expand Down
6 changes: 4 additions & 2 deletions lightning-c-bindings/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ int main() {
LDKKeysInterface keys_source = KeysManager_as_KeysInterface(&keys);

LDKUserConfig config = UserConfig_default();

LDKChannelManager cm = ChannelManager_new(net, fee_est, mon, broadcast, logger, keys_source, config, 0);
LDKThirtyTwoBytes chain_tip;
memset(&chain_tip, 0, 32);
LDKChainParameters chain = ChainParameters_new(net, chain_tip, 0);
LDKChannelManager cm = ChannelManager_new(fee_est, mon, broadcast, logger, keys_source, config, chain);

LDKCVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm);
assert((unsigned long)channels.data < 4096); // There's an offset, but it should still be an offset against null in the 0 page
Expand Down
6 changes: 4 additions & 2 deletions lightning-c-bindings/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ int main() {
memset(&null_pk, 0, sizeof(null_pk));

LDKThirtyTwoBytes random_bytes;
LDKThirtyTwoBytes chain_tip;
memset(&chain_tip, 0, sizeof(chain_tip)); // channel_open_header's prev_blockhash is all-0s

LDKNetwork network = LDKNetwork_Testnet;

Expand Down Expand Up @@ -352,7 +354,7 @@ int main() {
LDK::KeysInterface keys_source1 = KeysManager_as_KeysInterface(&keys1);
node_secret1 = keys_source1->get_node_secret(keys_source1->this_arg);

LDK::ChannelManager cm1 = ChannelManager_new(network, fee_est, mon1, broadcast, logger1, KeysManager_as_KeysInterface(&keys1), UserConfig_default(), 0);
LDK::ChannelManager cm1 = ChannelManager_new(fee_est, mon1, broadcast, logger1, KeysManager_as_KeysInterface(&keys1), UserConfig_default(), ChainParameters_new(network, chain_tip, 0));

LDK::CVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm1);
assert(channels->datalen == 0);
Expand All @@ -379,7 +381,7 @@ int main() {
LDK::UserConfig config2 = UserConfig_default();
UserConfig_set_own_channel_config(&config2, std::move(handshake_config2));

LDK::ChannelManager cm2 = ChannelManager_new(network, fee_est, mon2, broadcast, logger2, KeysManager_as_KeysInterface(&keys2), std::move(config2), 0);
LDK::ChannelManager cm2 = ChannelManager_new(fee_est, mon2, broadcast, logger2, KeysManager_as_KeysInterface(&keys2), std::move(config2), ChainParameters_new(network, chain_tip, 0));

LDK::CVec_ChannelDetailsZ channels2 = ChannelManager_list_channels(&cm2);
assert(channels2->datalen == 0);
Expand Down
Loading