Skip to content

Commit fd396ce

Browse files
authored
Merge pull request #114 from TheBlueMatt/main
0.0.118
2 parents 78310f9 + 7d60d11 commit fd396ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+4708
-2451
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
run: |
3838
git clone https://github.com/rust-bitcoin/rust-lightning
3939
cd rust-lightning
40-
git checkout 0.0.117-bindings
40+
git checkout 0.0.118-bindings
4141
# Pin memchr until we can remove it
4242
cargo update -p memchr --precise "2.5.0" --verbose
4343
- name: Fix Github Actions to not be broken
@@ -101,7 +101,7 @@ jobs:
101101
run: |
102102
git clone https://github.com/rust-bitcoin/rust-lightning
103103
cd rust-lightning
104-
git checkout 0.0.117-bindings
104+
git checkout 0.0.118-bindings
105105
- name: Fix Github Actions to not be broken
106106
run: git config --global --add safe.directory /__w/ldk-c-bindings/ldk-c-bindings
107107
- name: Fetch MacOS SDK
@@ -148,7 +148,7 @@ jobs:
148148
run: |
149149
git clone https://github.com/rust-bitcoin/rust-lightning
150150
cd rust-lightning
151-
git checkout 0.0.117-bindings
151+
git checkout 0.0.118-bindings
152152
- name: Rebuild bindings using Apple clang, and check the sample app builds + links
153153
run: ./genbindings.sh ./rust-lightning true
154154
- name: Rebuild bindings using upstream clang, and check the sample app builds + links

c-bindings-gen/src/main.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,15 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
9494
writeln!(w, ")").unwrap();
9595

9696
writeln!(w, "}}").unwrap();
97+
98+
writeln!(w, "#[allow(unused)]").unwrap();
99+
writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap();
97100
if has_inner {
98-
writeln!(w, "#[no_mangle]").unwrap();
99-
writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap();
100101
writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap();
101-
writeln!(w, "}}").unwrap();
102+
} else {
103+
writeln!(w, "\t{}_write(unsafe {{ &*(obj as *const {}) }})", for_obj, for_obj).unwrap();
102104
}
105+
writeln!(w, "}}").unwrap();
103106
},
104107
"lightning::util::ser::Readable"|"lightning::util::ser::ReadableArgs"|"lightning::util::ser::MaybeReadable" => {
105108
// Create the Result<Object, DecodeError> syn::Type
@@ -554,6 +557,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
554557
writeln!(w, "unsafe impl Send for {} {{}}", trait_name).unwrap();
555558
writeln!(w, "unsafe impl Sync for {} {{}}", trait_name).unwrap();
556559

560+
writeln!(w, "#[allow(unused)]").unwrap();
557561
writeln!(w, "pub(crate) fn {}_clone_fields(orig: &{}) -> {} {{", trait_name, trait_name, trait_name).unwrap();
558562
writeln!(w, "\t{} {{", trait_name).unwrap();
559563
writeln!(w, "\t\tthis_arg: orig.this_arg,").unwrap();
@@ -1023,10 +1027,14 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
10231027
if is_type_unconstructable(&resolved_path) {
10241028
writeln!(w, "\t\tunreachable!();").unwrap();
10251029
} else {
1026-
writeln!(w, "\t\tlet mut rust_obj = {} {{ inner: ObjOps::heap_alloc(obj), is_owned: true }};", ident).unwrap();
1027-
writeln!(w, "\t\tlet mut ret = {}_as_{}(&rust_obj);", ident, trait_obj.ident).unwrap();
1028-
writeln!(w, "\t\t// We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn").unwrap();
1029-
writeln!(w, "\t\trust_obj.inner = core::ptr::null_mut();").unwrap();
1030+
types.write_to_c_conversion_new_var(w, &format_ident!("obj"), &*i.self_ty, Some(&gen_types), false);
1031+
write!(w, "\t\tlet rust_obj = ").unwrap();
1032+
types.write_to_c_conversion_inline_prefix(w, &*i.self_ty, Some(&gen_types), false);
1033+
write!(w, "obj").unwrap();
1034+
types.write_to_c_conversion_inline_suffix(w, &*i.self_ty, Some(&gen_types), false);
1035+
writeln!(w, ";\n\t\tlet mut ret = {}_as_{}(&rust_obj);", ident, trait_obj.ident).unwrap();
1036+
writeln!(w, "\t\t// We want to free rust_obj when ret gets drop()'d, not rust_obj, so forget it and set ret's free() fn").unwrap();
1037+
writeln!(w, "\t\tcore::mem::forget(rust_obj);").unwrap();
10301038
writeln!(w, "\t\tret.free = Some({}_free_void);", ident).unwrap();
10311039
writeln!(w, "\t\tret").unwrap();
10321040
}
@@ -1041,7 +1049,11 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
10411049
writeln!(w, "/// This copies the `inner` pointer in this_arg and thus the returned {} must be freed before this_arg is", trait_obj.ident).unwrap();
10421050
write!(w, "#[no_mangle]\npub extern \"C\" fn {}_as_{}(this_arg: &{}) -> crate::{} {{\n", ident, trait_obj.ident, ident, full_trait_path).unwrap();
10431051
writeln!(w, "\tcrate::{} {{", full_trait_path).unwrap();
1044-
writeln!(w, "\t\tthis_arg: unsafe {{ ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }},").unwrap();
1052+
if types.c_type_has_inner_from_path(&resolved_path) {
1053+
writeln!(w, "\t\tthis_arg: unsafe {{ ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }},").unwrap();
1054+
} else {
1055+
writeln!(w, "\t\tthis_arg: unsafe {{ ObjOps::untweak_ptr(this_arg as *const {} as *mut {}) as *mut c_void }},", ident, ident).unwrap();
1056+
}
10451057
writeln!(w, "\t\tfree: None,").unwrap();
10461058

10471059
macro_rules! write_meth {
@@ -1345,7 +1357,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
13451357
writeln!(w, "#[allow(unused)]").unwrap();
13461358
writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap();
13471359
writeln!(w, "pub(crate) extern \"C\" fn {}_clone_void(this_ptr: *const c_void) -> *mut c_void {{", ident).unwrap();
1348-
writeln!(w, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *mut native{})).clone() }})) as *mut c_void", ident).unwrap();
1360+
writeln!(w, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *const native{})).clone() }})) as *mut c_void", ident).unwrap();
13491361
writeln!(w, "}}").unwrap();
13501362
writeln!(w, "#[no_mangle]").unwrap();
13511363
writeln!(w, "/// Creates a copy of the {}", ident).unwrap();
@@ -1936,7 +1948,18 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
19361948
writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", e.ident, e.ident, e.ident).unwrap();
19371949
writeln!(w, "\torig.clone()").unwrap();
19381950
writeln!(w, "}}").unwrap();
1951+
writeln!(w, "#[allow(unused)]").unwrap();
1952+
writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap();
1953+
writeln!(w, "pub(crate) extern \"C\" fn {}_clone_void(this_ptr: *const c_void) -> *mut c_void {{", e.ident).unwrap();
1954+
writeln!(w, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *const {})).clone() }})) as *mut c_void", e.ident).unwrap();
1955+
writeln!(w, "}}").unwrap();
19391956
}
1957+
1958+
writeln!(w, "#[allow(unused)]").unwrap();
1959+
writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap();
1960+
writeln!(w, "pub(crate) extern \"C\" fn {}_free_void(this_ptr: *mut c_void) {{", e.ident).unwrap();
1961+
writeln!(w, "\tlet _ = unsafe {{ Box::from_raw(this_ptr as *mut {}) }};\n}}", e.ident).unwrap();
1962+
19401963
w.write_all(&constr).unwrap();
19411964
write_cpp_wrapper(cpp_headers, &format!("{}", e.ident), needs_free, None);
19421965
}

c-bindings-gen/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
15341534
if is_ref => Some(".as_inner()"),
15351535
"bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin_hashes::sha256::Hash"
15361536
if !is_ref => Some(".into_inner() }"),
1537-
"bitcoin::blockdata::constants::ChainHash" if is_ref => Some(".as_bytes() }"),
1537+
"bitcoin::blockdata::constants::ChainHash" if is_ref => Some(".as_bytes()"),
15381538
"bitcoin::blockdata::constants::ChainHash" if !is_ref => Some(".to_bytes() }"),
15391539
"bitcoin::secp256k1::Message" if !is_ref => Some(".as_ref().clone() }"),
15401540
"lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"

lightning-c-bindings/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ std = ["bitcoin/std", "lightning/std", "lightning-invoice/std", "lightning-backg
2222
bitcoin = { version = "0.29", default-features = false }
2323
secp256k1 = { version = "0.24", features = ["global-context", "recovery"] }
2424
# Note that the following line is matched by genbindings to update the path
25-
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.117-bindings", default-features = false }
26-
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.117-bindings", default-features = false }
27-
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.117-bindings", default-features = false }
28-
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.117-bindings", default-features = false }
29-
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.117-bindings", default-features = false }
25+
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.118-bindings", default-features = false }
26+
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.118-bindings", default-features = false }
27+
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.118-bindings", default-features = false }
28+
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.118-bindings", default-features = false }
29+
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.118-bindings", default-features = false }
3030

3131
core2 = { version = "0.3.0", optional = true, default-features = false }
3232

lightning-c-bindings/demo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void print_log(const void *this_arg, const LDKRecord *record) {
1313
}
1414

1515
uint32_t get_fee(const void *this_arg, LDKConfirmationTarget target) {
16-
if (target == LDKConfirmationTarget_Background) {
16+
if (target == LDKConfirmationTarget_AnchorChannelFee || target == LDKConfirmationTarget_MinAllowedAnchorChannelRemoteFee) {
1717
return 253;
1818
} else {
1919
return 507;

lightning-c-bindings/demo.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void print_log(const void *this_arg, const LDKRecord *record) {
104104
}
105105

106106
uint32_t get_fee(const void *this_arg, LDKConfirmationTarget target) {
107-
if (target == LDKConfirmationTarget_Background) {
107+
if (target == LDKConfirmationTarget_AnchorChannelFee || target == LDKConfirmationTarget_MinAllowedAnchorChannelRemoteFee) {
108108
return 253;
109109
} else {
110110
return 507;
@@ -386,7 +386,7 @@ class PeersConnection {
386386

387387
struct CustomOnionMsgQueue {
388388
std::mutex mtx;
389-
std::vector<LDK::CustomOnionMessageContents> msgs;
389+
std::vector<LDK::OnionMessageContents> msgs;
390390
};
391391

392392
uint64_t custom_onion_msg_type_id(const void *this_arg) {
@@ -400,31 +400,35 @@ LDKCVec_u8Z custom_onion_msg_bytes(const void *this_arg) {
400400
};
401401
}
402402

403-
LDKCOption_CustomOnionMessageContentsZ handle_custom_onion_message(const void* this_arg, struct LDKCustomOnionMessageContents msg) {
403+
LDKCOption_OnionMessageContentsZ handle_custom_onion_message(const void* this_arg, struct LDKOnionMessageContents msg) {
404404
CustomOnionMsgQueue* arg = (CustomOnionMsgQueue*) this_arg;
405405
std::unique_lock<std::mutex> lck(arg->mtx);
406406
arg->msgs.push_back(std::move(msg));
407-
return COption_CustomOnionMessageContentsZ_none();
407+
return COption_OnionMessageContentsZ_none();
408408
}
409409

410-
LDKCustomOnionMessageContents build_custom_onion_message() {
411-
return LDKCustomOnionMessageContents {
410+
LDKOnionMessageContents build_custom_onion_message() {
411+
return LDKOnionMessageContents {
412412
.this_arg = NULL,
413413
.tlv_type = custom_onion_msg_type_id,
414414
.write = custom_onion_msg_bytes,
415415
.free = NULL,
416416
};
417417
}
418418

419-
LDKCResult_COption_CustomOnionMessageContentsZDecodeErrorZ read_custom_onion_message(const void* this_arg, uint64_t type, LDKu8slice buf) {
419+
LDKCResult_COption_OnionMessageContentsZDecodeErrorZ read_custom_onion_message(const void* this_arg, uint64_t type, LDKu8slice buf) {
420420
assert(type == 8888);
421421
assert(buf.datalen == 1024);
422422
uint8_t cmp[1024];
423423
memset(cmp, 43, 1024);
424424
assert(!memcmp(cmp, buf.data, 1024));
425-
return CResult_COption_CustomOnionMessageContentsZDecodeErrorZ_ok(COption_CustomOnionMessageContentsZ_some(build_custom_onion_message()));
425+
return CResult_COption_OnionMessageContentsZDecodeErrorZ_ok(COption_OnionMessageContentsZ_some(build_custom_onion_message()));
426426
}
427427

428+
LDKCVec_C3Tuple_OnionMessageContentsDestinationBlindedPathZZ release_no_messages(const void* this_arg) {
429+
return LDKCVec_C3Tuple_OnionMessageContentsDestinationBlindedPathZZ {
430+
.data = NULL, .datalen = 0 };
431+
}
428432

429433
struct CustomMsgQueue {
430434
std::vector<LDK::Type> msgs;
@@ -991,6 +995,7 @@ int main() {
991995
.this_arg = NULL,
992996
.handle_custom_message = NULL, // We only create custom messages, not handle them
993997
.read_custom_message = NULL, // We only create custom messages, not handle them
998+
.release_pending_custom_messages = release_no_messages,
994999
.free = NULL,
9951000
};
9961001
LDK::DefaultMessageRouter mr1 = DefaultMessageRouter_new();
@@ -1018,6 +1023,7 @@ int main() {
10181023
.this_arg = &peer_2_custom_onion_messages,
10191024
.handle_custom_message = handle_custom_onion_message,
10201025
.read_custom_message = read_custom_onion_message,
1026+
.release_pending_custom_messages = release_no_messages,
10211027
.free = NULL,
10221028
};
10231029
LDK::DefaultMessageRouter mr2 = DefaultMessageRouter_new();
@@ -1178,10 +1184,8 @@ int main() {
11781184
LDKCVec_PublicKeyZ { .data = NULL, .datalen = 0, },
11791185
Destination_node(ChannelManager_get_our_node_id(&cm2))
11801186
),
1181-
LDKOnionMessageContents {
1182-
.tag = LDKOnionMessageContents_Custom,
1183-
.custom = build_custom_onion_message()
1184-
}, LDKBlindedPath { .inner = NULL, .is_owned = true })
1187+
build_custom_onion_message(),
1188+
LDKBlindedPath { .inner = NULL, .is_owned = true })
11851189
.result_ok);
11861190
PeerManager_process_events(&net1);
11871191
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting onion message..." << std::endl;

lightning-c-bindings/include/ldk_rust_types.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ struct nativeDelayedPaymentOutputDescriptorOpaque;
2424
typedef struct nativeDelayedPaymentOutputDescriptorOpaque LDKnativeDelayedPaymentOutputDescriptor;
2525
struct nativeStaticPaymentOutputDescriptorOpaque;
2626
typedef struct nativeStaticPaymentOutputDescriptorOpaque LDKnativeStaticPaymentOutputDescriptor;
27+
struct nativeChannelDerivationParametersOpaque;
28+
typedef struct nativeChannelDerivationParametersOpaque LDKnativeChannelDerivationParameters;
29+
struct nativeHTLCDescriptorOpaque;
30+
typedef struct nativeHTLCDescriptorOpaque LDKnativeHTLCDescriptor;
2731
struct LDKChannelSigner;
2832
struct nativeInMemorySignerOpaque;
2933
typedef struct nativeInMemorySignerOpaque LDKnativeInMemorySigner;
@@ -121,12 +125,8 @@ struct nativeNodeAliasOpaque;
121125
typedef struct nativeNodeAliasOpaque LDKnativeNodeAlias;
122126
struct nativeNodeInfoOpaque;
123127
typedef struct nativeNodeInfoOpaque LDKnativeNodeInfo;
124-
struct nativeChannelDerivationParametersOpaque;
125-
typedef struct nativeChannelDerivationParametersOpaque LDKnativeChannelDerivationParameters;
126128
struct nativeAnchorDescriptorOpaque;
127129
typedef struct nativeAnchorDescriptorOpaque LDKnativeAnchorDescriptor;
128-
struct nativeHTLCDescriptorOpaque;
129-
typedef struct nativeHTLCDescriptorOpaque LDKnativeHTLCDescriptor;
130130
struct nativeInputOpaque;
131131
typedef struct nativeInputOpaque LDKnativeInput;
132132
struct nativeUtxoOpaque;

0 commit comments

Comments
 (0)