Skip to content

Commit 6a576b3

Browse files
committed
Update C/C++ demos for latest upstream API
1 parent 7ca42fa commit 6a576b3

File tree

2 files changed

+93
-20
lines changed

2 files changed

+93
-20
lines changed

lightning-c-bindings/demo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ void broadcast_tx(const void *this_arg, LDKTransaction tx) {
2525
Transaction_free(tx);
2626
}
2727

28-
LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitor monitor) {
29-
return CResult_NoneChannelMonitorUpdateErrZ_ok();
28+
LDKChannelMonitorUpdateStatus add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitor monitor) {
29+
return ChannelMonitorUpdateStatus_completed();
3030
}
31-
LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitorUpdate monitor) {
32-
return CResult_NoneChannelMonitorUpdateErrZ_ok();
31+
LDKChannelMonitorUpdateStatus update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitorUpdate monitor) {
32+
return ChannelMonitorUpdateStatus_completed();
3333
}
3434
LDKCVec_C3Tuple_OutPointCVec_MonitorEventZPublicKeyZZ monitors_pending_monitor_events(const void *this_arg) {
3535
LDKCVec_C3Tuple_OutPointCVec_MonitorEventZPublicKeyZZ empty_htlc_vec = {

lightning-c-bindings/demo.cpp

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ struct NodeMonitors {
139139
}
140140
};
141141

142-
LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitor monitor_arg) {
142+
LDKChannelMonitorUpdateStatus add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitor monitor_arg) {
143143
// First bind the args to C++ objects so they auto-free
144144
LDK::ChannelMonitor mon(std::move(monitor_arg));
145145
LDK::OutPoint funding_txo(std::move(funding_txo_arg));
@@ -148,10 +148,10 @@ LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg
148148
std::unique_lock<std::mutex> l(arg->mut);
149149

150150
arg->mons.push_back(std::make_pair(std::move(funding_txo), std::move(mon)));
151-
return CResult_NoneChannelMonitorUpdateErrZ_ok();
151+
return ChannelMonitorUpdateStatus_completed();
152152
}
153153
static std::atomic_int mons_updated(0);
154-
LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitorUpdate monitor_arg) {
154+
LDKChannelMonitorUpdateStatus update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitorUpdate monitor_arg) {
155155
// First bind the args to C++ objects so they auto-free
156156
LDK::ChannelMonitorUpdate update(std::move(monitor_arg));
157157
LDK::OutPoint funding_txo(std::move(funding_txo_arg));
@@ -174,7 +174,7 @@ LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_
174174
assert(updated);
175175

176176
mons_updated += 1;
177-
return CResult_NoneChannelMonitorUpdateErrZ_ok();
177+
return ChannelMonitorUpdateStatus_completed();
178178
}
179179
LDKCVec_C3Tuple_OutPointCVec_MonitorEventZPublicKeyZZ monitors_pending_monitor_events(const void *this_arg) {
180180
NodeMonitors* arg = (NodeMonitors*) this_arg;
@@ -369,6 +369,47 @@ class PeersConnection {
369369
};
370370
#endif // !REAL_NET
371371

372+
struct CustomOnionMsgQueue {
373+
std::mutex mtx;
374+
std::vector<LDK::CustomOnionMessageContents> msgs;
375+
};
376+
377+
uint64_t custom_onion_msg_type_id(const void *this_arg) {
378+
return 8888;
379+
}
380+
LDKCVec_u8Z custom_onion_msg_bytes(const void *this_arg) {
381+
uint8_t *bytes = (uint8_t *) malloc(1024);
382+
memset(bytes, 43, 1024);
383+
return LDKCVec_u8Z {
384+
.data = bytes, .datalen = 1024
385+
};
386+
}
387+
388+
void handle_custom_onion_message(const void* this_arg, struct LDKCustomOnionMessageContents msg) {
389+
CustomOnionMsgQueue* arg = (CustomOnionMsgQueue*) this_arg;
390+
std::unique_lock<std::mutex> lck(arg->mtx);
391+
arg->msgs.push_back(std::move(msg));
392+
}
393+
394+
LDKCustomOnionMessageContents build_custom_onion_message() {
395+
return LDKCustomOnionMessageContents {
396+
.this_arg = NULL,
397+
.tlv_type = custom_onion_msg_type_id,
398+
.write = custom_onion_msg_bytes,
399+
.free = NULL,
400+
};
401+
}
402+
403+
LDKCResult_COption_CustomOnionMessageContentsZDecodeErrorZ read_custom_onion_message(const void* this_arg, uint64_t type, LDKu8slice buf) {
404+
assert(type == 8888);
405+
assert(buf.datalen == 1024);
406+
uint8_t cmp[1024];
407+
memset(cmp, 43, 1024);
408+
assert(!memcmp(cmp, buf.data, 1024));
409+
return CResult_COption_CustomOnionMessageContentsZDecodeErrorZ_ok(COption_CustomOnionMessageContentsZ_some(build_custom_onion_message()));
410+
}
411+
412+
372413
struct CustomMsgQueue {
373414
std::vector<LDK::Type> msgs;
374415
};
@@ -534,16 +575,18 @@ int main() {
534575
node_secret1 = *node_secret1_res->contents.result;
535576

536577
LDK::ChannelManager cm1 = ChannelManager_new(fee_est, mon1, broadcast, logger1, KeysManager_as_KeysInterface(&keys1), UserConfig_default(), ChainParameters_new(network, BestBlock_new(chain_tip, 0)));
537-
LDK::OnionMessenger om1 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys1), logger1);
578+
579+
LDK::IgnoringMessageHandler ignoring_handler1 = IgnoringMessageHandler_new();
580+
LDK::CustomMessageHandler custom_msg_handler1 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler1);
581+
LDK::CustomOnionMessageHandler custom_onion_msg_handler1 = IgnoringMessageHandler_as_CustomOnionMessageHandler(&ignoring_handler1);
582+
583+
LDK::OnionMessenger om1 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys1), logger1, std::move(custom_onion_msg_handler1));
538584

539585
LDK::CVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm1);
540586
assert(channels->datalen == 0);
541587

542588
LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), P2PGossipSync_as_RoutingMessageHandler(&graph_msg_handler1), OnionMessenger_as_OnionMessageHandler(&om1));
543589

544-
LDK::IgnoringMessageHandler ignoring_handler1 = IgnoringMessageHandler_new();
545-
LDK::CustomMessageHandler custom_msg_handler1 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler1);
546-
547590
random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg);
548591
LDK::PeerManager net1 = PeerManager_new(std::move(msg_handler1), node_secret1, 0xdeadbeef, &random_bytes.data, logger1, std::move(custom_msg_handler1));
549592

@@ -567,7 +610,12 @@ int main() {
567610
UserConfig_set_channel_handshake_config(&config2, std::move(handshake_config2));
568611

569612
LDK::ChannelManager cm2 = ChannelManager_new(fee_est, mon2, broadcast, logger2, KeysManager_as_KeysInterface(&keys2), std::move(config2), ChainParameters_new(network, BestBlock_new(chain_tip, 0)));
570-
LDK::OnionMessenger om2 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys2), logger2);
613+
614+
LDK::IgnoringMessageHandler ignoring_handler2 = IgnoringMessageHandler_new();
615+
LDK::CustomMessageHandler custom_msg_handler2 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler2);
616+
LDK::CustomOnionMessageHandler custom_onion_msg_handler2 = IgnoringMessageHandler_as_CustomOnionMessageHandler(&ignoring_handler2);
617+
618+
LDK::OnionMessenger om2 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys2), logger2, std::move(custom_onion_msg_handler2));
571619

572620
LDK::CVec_ChannelDetailsZ channels2 = ChannelManager_list_channels(&cm2);
573621
assert(channels2->datalen == 0);
@@ -580,9 +628,6 @@ int main() {
580628

581629
LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), std::move(net_msgs2), OnionMessenger_as_OnionMessageHandler(&om1));
582630

583-
LDK::IgnoringMessageHandler ignoring_handler2 = IgnoringMessageHandler_new();
584-
LDK::CustomMessageHandler custom_msg_handler2 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler2);
585-
586631
random_bytes = keys_source2->get_secure_random_bytes(keys_source2->this_arg);
587632
LDK::PeerManager net2 = PeerManager_new(std::move(msg_handler2), node_secret2, 0xdeadbeef, &random_bytes.data, logger2, std::move(custom_msg_handler2));
588633

@@ -702,7 +747,7 @@ int main() {
702747
.some = 5000,
703748
};
704749
LDK::CResult_InvoiceSignOrCreationErrorZ invoice = create_invoice_from_channelmanager(&cm2,
705-
KeysManager_as_KeysInterface(&keys2),
750+
KeysManager_as_KeysInterface(&keys2), logger2,
706751
LDKCurrency_Bitcoin, min_value,
707752
LDKStr {
708753
.chars = (const uint8_t *)"Invoice Description",
@@ -823,7 +868,13 @@ int main() {
823868
assert(cm1_read->result_ok);
824869
LDK::ChannelManager cm1(std::move(cm1_read->contents.result->b));
825870

826-
LDK::OnionMessenger om1 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys1), logger1);
871+
LDKCustomOnionMessageHandler custom_onion_msg_handler1 = {
872+
.this_arg = NULL,
873+
.handle_custom_message = NULL, // We only create custom messages, not handle them
874+
.read_custom_message = NULL, // We only create custom messages, not handle them
875+
.free = NULL,
876+
};
877+
LDK::OnionMessenger om1 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys1), logger1, custom_onion_msg_handler1);
827878

828879
LDK::CVec_ChannelMonitorZ mons_list2 = LDKCVec_ChannelMonitorZ { .data = (LDKChannelMonitor*)malloc(sizeof(LDKChannelMonitor)), .datalen = 1 };
829880
assert(mons2.mons.size() == 1);
@@ -838,7 +889,14 @@ int main() {
838889
assert(cm2_read->result_ok);
839890
LDK::ChannelManager cm2(std::move(cm2_read->contents.result->b));
840891

841-
LDK::OnionMessenger om2 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys2), logger2);
892+
CustomOnionMsgQueue peer_2_custom_onion_messages;
893+
LDKCustomOnionMessageHandler custom_onion_msg_handler2 = {
894+
.this_arg = &peer_2_custom_onion_messages,
895+
.handle_custom_message = handle_custom_onion_message,
896+
.read_custom_message = read_custom_onion_message,
897+
.free = NULL,
898+
};
899+
LDK::OnionMessenger om2 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys2), logger2, custom_onion_msg_handler2);
842900

843901
// Attempt to close the channel...
844902
uint8_t chan_id[32];
@@ -911,7 +969,7 @@ int main() {
911969
LDK::InvoicePayer payer = InvoicePayer_new(ChannelManager_as_Payer(&cm1), sending_router, logger1, handler1, Retry_attempts(0));
912970

913971
LDK::CResult_InvoiceSignOrCreationErrorZ invoice_res2 = create_invoice_from_channelmanager(&cm2,
914-
KeysManager_as_KeysInterface(&keys2),
972+
KeysManager_as_KeysInterface(&keys2), logger1,
915973
LDKCurrency_Bitcoin, COption_u64Z_some(10000),
916974
LDKStr {
917975
.chars = (const uint8_t *)"Invoice 2 Description",
@@ -991,8 +1049,23 @@ int main() {
9911049
LDK::CVec_ChannelDetailsZ chans_after_close2 = ChannelManager_list_channels(&cm2);
9921050
assert(chans_after_close2->datalen == 0);
9931051

1052+
assert(OnionMessenger_send_custom_onion_message(&om1,
1053+
LDKCVec_PublicKeyZ { .data = NULL, .datalen = 0, },
1054+
Destination_node(ChannelManager_get_our_node_id(&cm2)),
1055+
build_custom_onion_message(), LDKBlindedRoute { .inner = NULL, .is_owned = true })
1056+
.result_ok);
1057+
PeerManager_process_events(&net1);
1058+
while (true) {
1059+
std::this_thread::yield();
1060+
std::unique_lock<std::mutex> lck(peer_2_custom_onion_messages.mtx);
1061+
if (peer_2_custom_onion_messages.msgs.size() != 0) break;
1062+
}
1063+
9941064
conn.stop();
9951065

1066+
std::unique_lock<std::mutex> lck(peer_2_custom_onion_messages.mtx);
1067+
assert(peer_2_custom_onion_messages.msgs.size() == 1);
1068+
assert(peer_2_custom_onion_messages.msgs[0].tlv_type() == 8888);
9961069
assert(peer_2_custom_messages.msgs.size() != 0);
9971070

9981071
// Few extra random tests:

0 commit comments

Comments
 (0)