Skip to content

Commit 45ad332

Browse files
authored
Merge pull request #81 from TheBlueMatt/main
0.0.108 Bindings Updates
2 parents c014ce6 + e6b0c57 commit 45ad332

Some content is hidden

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

49 files changed

+13890
-11376
lines changed

.github/workflows/build.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ jobs:
2727
touch src/version.rs
2828
RUSTFLAGS="--cfg=c_bindings" cargo check --features std
2929
- name: Install cbindgen
30-
run: cargo install --version 0.20.0 cbindgen
30+
run: |
31+
git clone https://github.com/eqrion/cbindgen
32+
cd cbindgen/
33+
git checkout v0.20.0
34+
cargo update -p indexmap --precise "1.6.2" --verbose
35+
cargo install --locked --path .
3136
- name: Checkout Rust-Lightning git
3237
run: |
3338
git clone https://github.com/rust-bitcoin/rust-lightning
3439
cd rust-lightning
35-
git checkout 0.0.106-bindings
40+
git checkout 0.0.108-bindings
3641
- name: Rebuild bindings without std, and check the sample app builds + links
3742
run: ./genbindings.sh ./rust-lightning false
3843
- name: Rebuild bindings, and check the sample app builds + links
@@ -78,12 +83,12 @@ jobs:
7883
with:
7984
fetch-depth: 0
8085
- name: Install cbindgen
81-
run: cargo install --version 0.20.0 cbindgen
86+
run: cargo install cbindgen
8287
- name: Checkout Rust-Lightning git
8388
run: |
8489
git clone https://github.com/rust-bitcoin/rust-lightning
8590
cd rust-lightning
86-
git checkout 0.0.106-bindings
91+
git checkout 0.0.108-bindings
8792
- name: Rebuild bindings using Apple clang, and check the sample app builds + links
8893
run: ./genbindings.sh ./rust-lightning true
8994
- name: Rebuild bindings using upstream clang, and check the sample app builds + links

c-bindings-gen/src/blocks.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ fn writeln_docs_impl<'a, W: std::io::Write, I>(w: &mut W, attrs: &[syn::Attribut
460460
},
461461
}
462462
}
463-
if let Some((types, generics, inp, outp, field)) = method_args_ret {
463+
if let Some((types, generics, inp, outp, field_ty)) = method_args_ret {
464464
let mut nullable_found = false;
465465
for (name, inp) in inp {
466466
if types.skip_arg(inp, generics) { continue; }
@@ -491,8 +491,13 @@ fn writeln_docs_impl<'a, W: std::io::Write, I>(w: &mut W, attrs: &[syn::Attribut
491491
nullable_found = true;
492492
writeln!(w, "{}/// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None", prefix).unwrap();
493493
}
494+
let field = field_ty.map(|ty| generics.resolve_type(ty));
494495
if if let Some(syn::Type::Reference(syn::TypeReference { elem, .. })) = field {
495496
if let syn::Type::Path(syn::TypePath { ref path, .. }) = &**elem {
497+
let resolved_path = types.resolve_path(path, generics);
498+
if types.crate_types.opaques.get(&resolved_path).is_some() {
499+
writeln!(w, "{}/// Note that this field is expected to be a reference.", prefix).unwrap();
500+
}
496501
types.is_path_transparent_container(path, generics, true)
497502
} else { false }
498503
} else if let Some(syn::Type::Path(syn::TypePath { ref path, .. })) = field {
@@ -571,7 +576,7 @@ pub fn write_method_params<W: std::io::Write>(w: &mut W, sig: &syn::Signature, t
571576
},
572577
_ => unimplemented!(),
573578
}
574-
w.write(&c_type).unwrap();
579+
w.write_all(&c_type).unwrap();
575580
}
576581
}
577582
}

c-bindings-gen/src/main.rs

Lines changed: 339 additions & 172 deletions
Large diffs are not rendered by default.

c-bindings-gen/src/types.rs

Lines changed: 153 additions & 95 deletions
Large diffs are not rendered by default.

genbindings.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,13 @@ if [ "$2" = "true" ]; then
182182
add_crate "lightning-persister" "lightning_persister"
183183
add_crate "lightning-background-processor" "lightning_background_processor"
184184
add_crate "lightning-invoice" "lightning_invoice" --features=std
185+
add_crate "lightning-rapid-gossip-sync" "lightning_rapid_gossip_sync"
185186
CARGO_BUILD_ARGS="--features=std"
186187
else
187188
add_crate lightning lightning --features=no-std
188189
drop_crate "lightning-persister"
189190
drop_crate "lightning-background-processor"
191+
drop_crate "lightning-rapid-gossip-sync"
190192
add_crate "lightning-invoice" "lightning_invoice" --features=no-std
191193
CARGO_BUILD_ARGS="--features=no-std"
192194
fi

lightning-c-bindings/Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ no-std = ["bitcoin/no-std", "lightning/no-std", "lightning-invoice/no-std", "cor
1919
std = ["bitcoin/std", "lightning/std", "lightning-invoice/std"]
2020

2121
[dependencies]
22-
bitcoin = { version = "0.27", default-features = false }
23-
secp256k1 = { version = "0.20.3", features = ["global-context-less-secure"] }
22+
bitcoin = { version = "0.28", default-features = false }
23+
secp256k1 = { version = "0.22", 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.106-bindings", default-features = false }
26-
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.106-bindings", default-features = false }
27-
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.106-bindings", default-features = false }
28-
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.106-bindings", default-features = false }
25+
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.108-bindings", default-features = false }
26+
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.108-bindings", default-features = false }
27+
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.108-bindings", default-features = false }
28+
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.108-bindings", default-features = false }
29+
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.108-bindings", default-features = false }
2930

3031
core2 = { version = "0.3.0", optional = true, default-features = false }
3132

lightning-c-bindings/demo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg
3131
LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitorUpdate monitor) {
3232
return CResult_NoneChannelMonitorUpdateErrZ_ok();
3333
}
34-
LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
35-
LDKCVec_MonitorEventZ empty_htlc_vec = {
34+
LDKCVec_C2Tuple_OutPointCVec_MonitorEventZZZ monitors_pending_monitor_events(const void *this_arg) {
35+
LDKCVec_C2Tuple_OutPointCVec_MonitorEventZZZ empty_htlc_vec = {
3636
.data = NULL,
3737
.datalen = 0,
3838
};

lightning-c-bindings/demo.cpp

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,29 @@ LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_
176176
mons_updated += 1;
177177
return CResult_NoneChannelMonitorUpdateErrZ_ok();
178178
}
179-
LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
179+
LDKCVec_C2Tuple_OutPointCVec_MonitorEventZZZ monitors_pending_monitor_events(const void *this_arg) {
180180
NodeMonitors* arg = (NodeMonitors*) this_arg;
181181
std::unique_lock<std::mutex> l(arg->mut);
182182

183183
if (arg->mons.size() == 0) {
184-
return LDKCVec_MonitorEventZ {
184+
return LDKCVec_C2Tuple_OutPointCVec_MonitorEventZZZ {
185185
.data = NULL,
186186
.datalen = 0,
187187
};
188188
} else {
189189
// We only ever actually have one channel per node, plus concatenating two
190190
// Rust Vecs to each other from C++ will require a bit of effort.
191191
assert(arg->mons.size() == 1);
192-
return ChannelMonitor_get_and_clear_pending_monitor_events(&arg->mons[0].second);
192+
LDK::CVec_MonitorEventZ events = ChannelMonitor_get_and_clear_pending_monitor_events(&arg->mons[0].second);
193+
LDK::C2Tuple_OutPointScriptZ funding_info = ChannelMonitor_get_funding_txo(&arg->mons[0].second);
194+
LDK::OutPoint outpoint = std::move(funding_info->a);
195+
LDK::C2Tuple_OutPointCVec_MonitorEventZZ pair = C2Tuple_OutPointCVec_MonitorEventZZ_new(std::move(outpoint), std::move(events));
196+
auto vec = LDKCVec_C2Tuple_OutPointCVec_MonitorEventZZZ {
197+
.data = (LDKC2Tuple_OutPointCVec_MonitorEventZZ*)malloc(sizeof(LDKC2Tuple_OutPointCVec_MonitorEventZZ)),
198+
.datalen = 1,
199+
};
200+
vec.data[0] = std::move(pair);
201+
return vec;
193202
}
194203
}
195204

@@ -421,7 +430,8 @@ LDKCVec_C2Tuple_PublicKeyTypeZZ create_custom_msg(const void* this_arg) {
421430
return ret;
422431
}
423432

424-
uint64_t get_chan_score(const void *this_arg, uint64_t scid, uint64_t htlc_amt, uint64_t chan_capacity, const LDKNodeId *src, const LDKNodeId *dst) {
433+
uint64_t get_chan_score(const void *this_arg, uint64_t scid, const LDKNodeId *src, const LDKNodeId *dst, LDKChannelUsage usage_in) {
434+
LDK::ChannelUsage usage(std::move(usage_in));
425435
return 42;
426436
}
427437

@@ -434,7 +444,8 @@ struct LDKCResult_RouteLightningErrorZ custom_find_route(const void *this_arg, s
434444
const struct CustomRouteFinderParams *params = (struct CustomRouteFinderParams *)this_arg;
435445
assert(first_hops->datalen == 1);
436446
assert(ChannelDetails_get_is_usable(&first_hops->data[0]));
437-
return find_route(payer, route_params, params->graph_ref, first_hops, *params->logger, scorer, &params->random_seed_bytes.data);
447+
LDK::ReadOnlyNetworkGraph graph_lock = NetworkGraph_read_only(params->graph_ref);
448+
return find_route(payer, route_params, &graph_lock, first_hops, *params->logger, scorer, &params->random_seed_bytes.data);
438449
}
439450

440451
int main() {
@@ -478,8 +489,8 @@ int main() {
478489
.free = NULL,
479490
};
480491

481-
LDK::NetworkGraph net_graph1 = NetworkGraph_new(genesis_hash);
482-
LDK::NetGraphMsgHandler graph_msg_handler1 = NetGraphMsgHandler_new(&net_graph1, COption_AccessZ_none(), logger1);
492+
LDK::NetworkGraph net_graph1 = NetworkGraph_new(genesis_hash, logger1);
493+
LDK::P2PGossipSync graph_msg_handler1 = P2PGossipSync_new(&net_graph1, COption_AccessZ_none(), logger1);
483494
LDKSecretKey node_secret1;
484495

485496
LDKLogger logger2 {
@@ -498,8 +509,8 @@ int main() {
498509
.free = NULL,
499510
};
500511

501-
LDK::NetworkGraph net_graph2 = NetworkGraph_new(genesis_hash);
502-
LDK::NetGraphMsgHandler graph_msg_handler2 = NetGraphMsgHandler_new(&net_graph2, COption_AccessZ_none(), logger2);
512+
LDK::NetworkGraph net_graph2 = NetworkGraph_new(genesis_hash, logger2);
513+
LDK::P2PGossipSync graph_msg_handler2 = P2PGossipSync_new(&net_graph2, COption_AccessZ_none(), logger2);
503514
LDKSecretKey node_secret2;
504515

505516
LDK::CVec_u8Z cm1_ser = LDKCVec_u8Z {}; // ChannelManager 1 serialization at the end of the ser-des scope
@@ -520,7 +531,7 @@ int main() {
520531
LDK::CVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm1);
521532
assert(channels->datalen == 0);
522533

523-
LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), NetGraphMsgHandler_as_RoutingMessageHandler(&graph_msg_handler1));
534+
LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), P2PGossipSync_as_RoutingMessageHandler(&graph_msg_handler1));
524535

525536
LDK::IgnoringMessageHandler ignoring_handler1 = IgnoringMessageHandler_new();
526537
LDK::CustomMessageHandler custom_msg_handler1 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler1);
@@ -552,7 +563,7 @@ int main() {
552563
LDK::CVec_ChannelDetailsZ channels2 = ChannelManager_list_channels(&cm2);
553564
assert(channels2->datalen == 0);
554565

555-
LDK::RoutingMessageHandler net_msgs2 = NetGraphMsgHandler_as_RoutingMessageHandler(&graph_msg_handler2);
566+
LDK::RoutingMessageHandler net_msgs2 = P2PGossipSync_as_RoutingMessageHandler(&graph_msg_handler2);
556567
LDK::CResult_ChannelAnnouncementDecodeErrorZ chan_ann = ChannelAnnouncement_read(LDKu8slice { .data = valid_node_announcement, .datalen = sizeof(valid_node_announcement) });
557568
assert(chan_ann->result_ok);
558569
LDK::CResult_boolLightningErrorZ ann_res = net_msgs2->handle_channel_announcement(net_msgs2->this_arg, chan_ann->contents.result);
@@ -605,7 +616,7 @@ int main() {
605616
assert(!memcmp(queue.events[0]->funding_generation_ready.output_script.data, channel_open_block + 58 + 81, 34));
606617
LDKTransaction funding_transaction { .data = const_cast<uint8_t*>(channel_open_block + 81), .datalen = sizeof(channel_open_block) - 81, .data_is_owned = false };
607618

608-
LDK::CResult_NoneAPIErrorZ fund_res = ChannelManager_funding_transaction_generated(&cm1, &queue.events[0]->funding_generation_ready.temporary_channel_id.data, funding_transaction);
619+
LDK::CResult_NoneAPIErrorZ fund_res = ChannelManager_funding_transaction_generated(&cm1, &queue.events[0]->funding_generation_ready.temporary_channel_id.data, queue.events[0]->funding_generation_ready.counterparty_node_id, funding_transaction);
609620
assert(fund_res->result_ok);
610621
break;
611622
}
@@ -688,7 +699,7 @@ int main() {
688699
.chars = (const uint8_t *)"Invoice Description",
689700
.len = strlen("Invoice Description"),
690701
.chars_is_owned = false
691-
});
702+
}, 3600);
692703
assert(invoice->result_ok);
693704
LDKThirtyTwoBytes payment_hash;
694705
memcpy(payment_hash.data, Invoice_payment_hash(invoice->contents.result), 32);
@@ -704,7 +715,10 @@ int main() {
704715
}, Invoice_route_hints(invoice->contents.result), COption_u64Z_none(), 0xffffffff),
705716
5000, Invoice_min_final_cltv_expiry(invoice->contents.result));
706717
random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg);
707-
LDK::CResult_RouteLightningErrorZ route = find_route(ChannelManager_get_our_node_id(&cm1), &route_params, &net_graph2, &outbound_channels, logger1, &chan_scorer, &random_bytes.data);
718+
LDK::ReadOnlyNetworkGraph graph_lock = NetworkGraph_read_only(&net_graph2);
719+
720+
LDK::CResult_RouteLightningErrorZ route = find_route(ChannelManager_get_our_node_id(&cm1), &route_params, &graph_lock, &outbound_channels, logger1, &chan_scorer, &random_bytes.data);
721+
708722
assert(route->result_ok);
709723
LDK::CVec_CVec_RouteHopZZ paths = Route_get_paths(route->contents.result);
710724
assert(paths->datalen == 1);
@@ -751,9 +765,16 @@ int main() {
751765
assert(queue.events[0]->payment_received.purpose.tag == LDKPaymentPurpose_InvoicePayment);
752766
assert(!memcmp(queue.events[0]->payment_received.purpose.invoice_payment.payment_secret.data,
753767
Invoice_payment_secret(invoice->contents.result), 32));
754-
assert(queue.events[0]->payment_received.amt == 5000);
768+
assert(queue.events[0]->payment_received.amount_msat == 5000);
755769
memcpy(payment_preimage.data, queue.events[0]->payment_received.purpose.invoice_payment.payment_preimage.data, 32);
756-
assert(ChannelManager_claim_funds(&cm2, payment_preimage));
770+
ChannelManager_claim_funds(&cm2, payment_preimage);
771+
772+
queue.events.clear();
773+
ev2.process_pending_events(handler);
774+
assert(queue.events.size() == 1);
775+
assert(queue.events[0]->tag == LDKEvent_PaymentClaimed);
776+
assert(!memcmp(queue.events[0]->payment_claimed.payment_hash.data, payment_hash.data, 32));
777+
assert(queue.events[0]->payment_claimed.purpose.tag == LDKPaymentPurpose_InvoicePayment);
757778
}
758779
PeerManager_process_events(&net2);
759780
// Wait until we've passed through a full set of monitor updates (ie new preimage + CS/RAA messages)
@@ -809,11 +830,11 @@ int main() {
809830
// Attempt to close the channel...
810831
uint8_t chan_id[32];
811832
for (int i = 0; i < 32; i++) { chan_id[i] = channel_open_txid[31-i]; }
812-
LDK::CResult_NoneAPIErrorZ close_res = ChannelManager_close_channel(&cm1, &chan_id);
833+
LDK::CResult_NoneAPIErrorZ close_res = ChannelManager_close_channel(&cm1, &chan_id, ChannelManager_get_our_node_id(&cm2));
813834
assert(!close_res->result_ok); // Note that we can't close while disconnected!
814835

815836
// Open a connection!
816-
LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), NetGraphMsgHandler_as_RoutingMessageHandler(&graph_msg_handler1));
837+
LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), P2PGossipSync_as_RoutingMessageHandler(&graph_msg_handler1));
817838
random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg);
818839

819840
LDKPublicKey chan_2_node_id = ChannelManager_get_our_node_id(&cm2);
@@ -830,7 +851,7 @@ int main() {
830851
};
831852
LDK::PeerManager net1 = PeerManager_new(std::move(msg_handler1), node_secret1, &random_bytes.data, logger1, std::move(custom_msg_handler1));
832853

833-
LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), NetGraphMsgHandler_as_RoutingMessageHandler(&graph_msg_handler2));
854+
LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), P2PGossipSync_as_RoutingMessageHandler(&graph_msg_handler2));
834855
CustomMsgQueue peer_2_custom_messages;
835856
LDKCustomMessageHandler custom_msg_handler2 = {
836857
.this_arg = &peer_2_custom_messages,
@@ -867,11 +888,11 @@ int main() {
867888
.find_route = custom_find_route,
868889
.free = NULL,
869890
};
870-
LDK::ProbabilisticScorer scorer = ProbabilisticScorer_new(ProbabilisticScoringParameters_default(), &net_graph1);
891+
LDK::ProbabilisticScorer scorer = ProbabilisticScorer_new(ProbabilisticScoringParameters_default(), &net_graph1, logger1);
871892
LDK::MultiThreadedLockableScore scorer_mtx = MultiThreadedLockableScore_new(ProbabilisticScorer_as_Score(&scorer));
872893
EventQueue queue1;
873894
LDKEventHandler handler1 = { .this_arg = &queue1, .handle_event = handle_event, .free = NULL };
874-
LDK::InvoicePayer payer = InvoicePayer_new(ChannelManager_as_Payer(&cm1), sending_router, &scorer_mtx, logger1, handler1, RetryAttempts_new(0));
895+
LDK::InvoicePayer payer = InvoicePayer_new(ChannelManager_as_Payer(&cm1), sending_router, &scorer_mtx, logger1, handler1, Retry_attempts(0));
875896

876897
LDK::CResult_InvoiceSignOrCreationErrorZ invoice_res2 = create_invoice_from_channelmanager(&cm2,
877898
KeysManager_as_KeysInterface(&keys2),
@@ -880,7 +901,7 @@ int main() {
880901
.chars = (const uint8_t *)"Invoice 2 Description",
881902
.len = strlen("Invoice 2 Description"),
882903
.chars_is_owned = false
883-
});
904+
}, 3600);
884905
assert(invoice_res2->result_ok);
885906
const LDKInvoice *invoice2 = invoice_res2->contents.result;
886907
LDK::CResult_PaymentIdPaymentErrorZ invoice_pay_res = InvoicePayer_pay_invoice(&payer, invoice2);
@@ -914,8 +935,16 @@ int main() {
914935
assert(event_data->purpose.tag == LDKPaymentPurpose_InvoicePayment);
915936
assert(!memcmp(event_data->purpose.invoice_payment.payment_secret.data,
916937
Invoice_payment_secret(invoice2), 32));
917-
assert(event_data->amt == 10000);
918-
assert(ChannelManager_claim_funds(&cm2, event_data->purpose.invoice_payment.payment_preimage));
938+
assert(event_data->amount_msat == 10000);
939+
ChannelManager_claim_funds(&cm2, event_data->purpose.invoice_payment.payment_preimage);
940+
941+
queue2.events.clear();
942+
ev2.process_pending_events(handler2);
943+
assert(queue2.events.size() == 1);
944+
assert(queue2.events[0]->tag == LDKEvent_PaymentClaimed);
945+
assert(!memcmp(queue2.events[0]->payment_claimed.payment_hash.data, Invoice_payment_hash(invoice2), 32));
946+
assert(queue2.events[0]->payment_claimed.purpose.tag == LDKPaymentPurpose_InvoicePayment);
947+
919948
break;
920949
}
921950
std::this_thread::yield();
@@ -935,7 +964,7 @@ int main() {
935964

936965
// Actually close the channel
937966
num_txs_broadcasted = 0;
938-
close_res = ChannelManager_close_channel(&cm1, &chan_id);
967+
close_res = ChannelManager_close_channel(&cm1, &chan_id, ChannelManager_get_our_node_id(&cm2));
939968
assert(close_res->result_ok);
940969
PeerManager_process_events(&net1);
941970
while (num_txs_broadcasted != 2) {

0 commit comments

Comments
 (0)