Skip to content

Commit 7ef5b8d

Browse files
authored
Merge pull request #105 from arik-so/2023-05-115-debugging
Add debugging lines to demo.cpp
2 parents 27a0520 + e5e6dbe commit 7ef5b8d

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

lightning-c-bindings/demo.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern "C" {
2020
#include <thread>
2121
#include <mutex>
2222
#include <vector>
23+
#include <iostream>
2324

2425
const uint8_t valid_node_announcement[] = {
2526
0x94, 0xe4, 0xf5, 0x61, 0x41, 0x24, 0x7d, 0x90, 0x23, 0xa0, 0xc8, 0x34, 0x8c, 0xc4, 0xca, 0x51,
@@ -226,13 +227,15 @@ class PeersConnection {
226227

227228
assert(!socket_connect(node1_handler, ChannelManager_get_our_node_id(&cm2), (sockaddr*)&listen_addr, sizeof(listen_addr)));
228229

230+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting initial handshake completion..." << std::endl;
229231
while (true) {
230232
// Wait for the initial handshakes to complete...
231233
LDK::CVec_C2Tuple_PublicKeyCOption_NetAddressZZZ peers_1 = PeerManager_get_peer_node_ids(&net1);
232234
LDK::CVec_C2Tuple_PublicKeyCOption_NetAddressZZZ peers_2 = PeerManager_get_peer_node_ids(&net2);
233235
if (peers_1->datalen == 1 && peers_2->datalen == 1) { break; }
234236
std::this_thread::yield();
235237
}
238+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Initial handshake complete!" << std::endl;
236239

237240
// Connect twice, which should auto-disconnect, and is a good test of our disconnect pipeline
238241
assert(!socket_connect(node1_handler, ChannelManager_get_our_node_id(&cm2), (sockaddr*)&listen_addr, sizeof(listen_addr)));
@@ -242,15 +245,18 @@ class PeersConnection {
242245
PeerManager_disconnect_by_node_id(&net1, ChannelManager_get_our_node_id(&cm2));
243246
assert(!socket_connect(node1_handler, ChannelManager_get_our_node_id(&cm2), (sockaddr*)&listen_addr, sizeof(listen_addr)));
244247

248+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting new connection handshake..." << std::endl;
245249
while (true) {
246250
// Wait for the new connection handshake...
247251
LDK::CVec_C2Tuple_PublicKeyCOption_NetAddressZZZ peers_1 = PeerManager_get_peer_node_ids(&net1);
248252
LDK::CVec_C2Tuple_PublicKeyCOption_NetAddressZZZ peers_2 = PeerManager_get_peer_node_ids(&net2);
249253
if (peers_1->datalen == 1 && peers_2->datalen == 1) { break; }
250254
std::this_thread::yield();
251255
}
256+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "New connection handshake complete!" << std::endl;
252257

253258
// Wait for all our sockets to disconnect (making sure we disconnect any new connections)...
259+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting peer disconnection..." << std::endl;
254260
while (true) {
255261
PeerManager_disconnect_by_node_id(&net1, ChannelManager_get_our_node_id(&cm2));
256262
// Wait for the peers to disconnect...
@@ -259,20 +265,23 @@ class PeersConnection {
259265
if (peers_1->datalen == 0 && peers_2->datalen == 0) { break; }
260266
std::this_thread::yield();
261267
}
268+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Peers disconnected!" << std::endl;
262269
// Note that the above is somewhat race-y, as node 2 may still think its connected.
263270
// Thus, make sure any connections are disconnected on its end as well.
264271
PeerManager_disconnect_by_node_id(&net2, ChannelManager_get_our_node_id(&cm1));
265272

266273
// Finally make an actual connection and keep it this time
267274
assert(!socket_connect(node1_handler, ChannelManager_get_our_node_id(&cm2), (sockaddr*)&listen_addr, sizeof(listen_addr)));
268275

276+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting initial handshake completion..." << std::endl;
269277
while (true) {
270278
// Wait for the initial handshakes to complete...
271279
LDK::CVec_C2Tuple_PublicKeyCOption_NetAddressZZZ peers_1 = PeerManager_get_peer_node_ids(&net1);
272280
LDK::CVec_C2Tuple_PublicKeyCOption_NetAddressZZZ peers_2 = PeerManager_get_peer_node_ids(&net2);
273281
if (peers_1->datalen == 1 && peers_2->datalen == 1) { break; }
274282
std::this_thread::yield();
275283
}
284+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Initial handshake complete!" << std::endl;
276285
}
277286
void stop() {
278287
interrupt_socket_handling(node1_handler);
@@ -354,13 +363,15 @@ class PeersConnection {
354363
auto writelen = write(pipefds_1_to_2[1], con_res->contents.result->data, con_res->contents.result->datalen);
355364
assert(writelen > 0 && uint64_t(writelen) == con_res->contents.result->datalen);
356365

366+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting initial handshake completion..." << std::endl;
357367
while (true) {
358368
// Wait for the initial handshakes to complete...
359369
LDK::CVec_C2Tuple_PublicKeyCOption_NetAddressZZZ peers_1 = PeerManager_get_peer_node_ids(&net1);
360370
LDK::CVec_C2Tuple_PublicKeyCOption_NetAddressZZZ peers_2 = PeerManager_get_peer_node_ids(&net2);
361371
if (peers_1->datalen == 1 && peers_2->datalen ==1) { break; }
362372
std::this_thread::yield();
363373
}
374+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Initial handshake complete!" << std::endl;
364375
}
365376

366377
void stop() {
@@ -644,6 +655,7 @@ int main() {
644655
LDKPublicKey chan_open_pk = ChannelCounterparty_get_node_id(&new_channels_counterparty);
645656
assert(!memcmp(chan_open_pk.compressed_form, ChannelManager_get_our_node_id(&cm2).compressed_form, 33));
646657

658+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting first channel..." << std::endl;
647659
while (true) {
648660
LDK::CVec_ChannelDetailsZ new_channels_2 = ChannelManager_list_channels(&cm2);
649661
if (new_channels_2->datalen == 1) {
@@ -655,8 +667,10 @@ int main() {
655667
}
656668
std::this_thread::yield();
657669
}
670+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "First channel listed!" << std::endl;
658671

659672
LDK::EventsProvider ev1 = ChannelManager_as_EventsProvider(&cm1);
673+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting FundingGenerationReady event..." << std::endl;
660674
while (true) {
661675
EventQueue queue;
662676
LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
@@ -677,19 +691,23 @@ int main() {
677691
}
678692
std::this_thread::yield();
679693
}
694+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received FundingGenerationReady event!" << std::endl;
680695

681696
// We observe when the funding signed messages have been exchanged by
682697
// waiting for two monitors to be registered.
683698
assert(num_txs_broadcasted == 0);
684699
PeerManager_process_events(&net1);
700+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting transaction broadcast..." << std::endl;
685701
while (num_txs_broadcasted != 1) {
686702
std::this_thread::yield();
687703
}
704+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Transaction was broadcast!" << std::endl;
688705

689706
// Note that the channel ID is the same as the channel txid reversed as the output index is 0
690707
uint8_t expected_chan_id[32];
691708
for (int i = 0; i < 32; i++) { expected_chan_id[i] = channel_open_txid[31-i]; }
692709

710+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting ChannelPending event..." << std::endl;
693711
while (true) {
694712
EventQueue queue;
695713
LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
@@ -701,8 +719,10 @@ int main() {
701719
}
702720
std::this_thread::yield();
703721
}
722+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received ChannelPending event!" << std::endl;
704723

705724
LDK::EventsProvider ev2 = ChannelManager_as_EventsProvider(&cm2);
725+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting ChannelPending event..." << std::endl;
706726
while (true) {
707727
EventQueue queue;
708728
LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
@@ -714,6 +734,7 @@ int main() {
714734
}
715735
std::this_thread::yield();
716736
}
737+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received ChannelPending event!" << std::endl;
717738

718739
LDK::Listen listener1 = ChannelManager_as_Listen(&cm1);
719740
listener1->block_connected(listener1->this_arg, LDKu8slice { .data = channel_open_block, .datalen = sizeof(channel_open_block) }, 1);
@@ -742,6 +763,7 @@ int main() {
742763
PeerManager_process_events(&net1);
743764
PeerManager_process_events(&net2);
744765

766+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting ChannelReady event..." << std::endl;
745767
while (true) {
746768
EventQueue queue;
747769
LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
@@ -753,7 +775,9 @@ int main() {
753775
}
754776
std::this_thread::yield();
755777
}
778+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received ChannelReady event!" << std::endl;
756779

780+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting ChannelReady event..." << std::endl;
757781
while (true) {
758782
EventQueue queue;
759783
LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
@@ -765,9 +789,11 @@ int main() {
765789
}
766790
std::this_thread::yield();
767791
}
792+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received ChannelReady event!" << std::endl;
768793

769794
// Now send funds from 1 to 2!
770795
uint64_t channel_scid;
796+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting usable channel..." << std::endl;
771797
while (true) {
772798
LDK::CVec_ChannelDetailsZ outbound_channels = ChannelManager_list_usable_channels(&cm1);
773799
if (outbound_channels->datalen == 1) {
@@ -793,6 +819,7 @@ int main() {
793819
}
794820
std::this_thread::yield();
795821
}
822+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Listed usable channel!" << std::endl;
796823

797824
LDKCOption_u64Z min_value = {
798825
.tag = LDKCOption_u64Z_Some,
@@ -843,11 +870,14 @@ int main() {
843870

844871
mons_updated = 0;
845872
PeerManager_process_events(&net1);
873+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting 4 updated monitors..." << std::endl;
846874
while (mons_updated != 4) {
847875
std::this_thread::yield();
848876
}
877+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "4 monitors updated!" << std::endl;
849878

850879
// Check that we received the payment!
880+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting PendingHTLCsForwardable event..." << std::endl;
851881
while (true) {
852882
EventQueue queue;
853883
LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
@@ -858,6 +888,7 @@ int main() {
858888
}
859889
std::this_thread::yield();
860890
}
891+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received PendingHTLCsForwardable event!" << std::endl;
861892
ChannelManager_process_pending_htlc_forwards(&cm2);
862893
PeerManager_process_events(&net2);
863894

@@ -886,14 +917,20 @@ int main() {
886917
}
887918
PeerManager_process_events(&net2);
888919
// Wait until we've passed through a full set of monitor updates (ie new preimage + CS/RAA messages)
920+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting 5 updated monitors..." << std::endl;
889921
while (mons_updated != 5) {
890922
std::this_thread::yield();
891923
}
924+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "5 monitors updated!" << std::endl;
892925
{
893926
EventQueue queue;
894927
LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
895-
while (queue.events.size() < 2)
928+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting PaymentSent and PaymentPathSuccessful events..." << std::endl;
929+
while (queue.events.size() < 2) {
896930
ev1.process_pending_events(handler);
931+
std::this_thread::yield();
932+
}
933+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received PaymentSent and PaymentPathSuccessful events (presumably)!" << std::endl;
897934
assert(queue.events.size() == 2);
898935
assert(queue.events[0]->tag == LDKEvent_PaymentSent);
899936
assert(!memcmp(queue.events[0]->payment_sent.payment_preimage.data, payment_preimage.data, 32));
@@ -1011,6 +1048,7 @@ int main() {
10111048

10121049
PeersConnection conn(cm1, cm2, net1, net2);
10131050

1051+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting usable channel..." << std::endl;
10141052
while (true) {
10151053
// Wait for the channels to be considered up once the reestablish messages are processed
10161054
LDK::CVec_ChannelDetailsZ outbound_channels = ChannelManager_list_usable_channels(&cm1);
@@ -1019,6 +1057,7 @@ int main() {
10191057
}
10201058
std::this_thread::yield();
10211059
}
1060+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Listed usable channel!" << std::endl;
10221061

10231062
// Send another payment, this time via the retires path
10241063
LDK::CResult_InvoiceSignOrCreationErrorZ invoice_res2 = create_invoice_from_channelmanager(&cm2,
@@ -1036,6 +1075,7 @@ int main() {
10361075
PeerManager_process_events(&net1);
10371076

10381077
// Check that we received the payment!
1078+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting PendingHTLCsForwardable event..." << std::endl;
10391079
while (true) {
10401080
EventQueue queue2;
10411081
LDKEventHandler handler2 = { .this_arg = &queue2, .handle_event = handle_event, .free = NULL };
@@ -1047,9 +1087,11 @@ int main() {
10471087
}
10481088
std::this_thread::yield();
10491089
}
1090+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received PendingHTLCsForwardable event!" << std::endl;
10501091
ChannelManager_process_pending_htlc_forwards(&cm2);
10511092
PeerManager_process_events(&net2);
10521093

1094+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting PaymentClaimable/PaymentClaimed event..." << std::endl;
10531095
while (true) {
10541096
EventQueue queue2;
10551097
LDKEventHandler handler2 = { .this_arg = &queue2, .handle_event = handle_event, .free = NULL };
@@ -1076,16 +1118,20 @@ int main() {
10761118
}
10771119
std::this_thread::yield();
10781120
}
1121+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received PaymentClaimable/PaymentClaimed event!" << std::endl;
10791122

10801123
EventQueue queue1;
10811124
LDKEventHandler handler1 = { .this_arg = &queue1, .handle_event = handle_event, .free = NULL };
1125+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting PaymentSent and PaymentPathSuccessful events..." << std::endl;
10821126
while (queue1.events.size() < 2) {
10831127
PeerManager_process_events(&net2);
10841128
PeerManager_process_events(&net1);
10851129

10861130
LDK::EventsProvider ev1 = ChannelManager_as_EventsProvider(&cm1);
10871131
ev1.process_pending_events(handler1);
1132+
std::this_thread::yield();
10881133
}
1134+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received PaymentSent and PaymentPathSuccessful events (presumably)!" << std::endl;
10891135
assert(queue1.events.size() == 2);
10901136
assert(queue1.events[0]->tag == LDKEvent_PaymentSent);
10911137
assert(queue1.events[1]->tag == LDKEvent_PaymentPathSuccessful);
@@ -1095,9 +1141,11 @@ int main() {
10951141
close_res = ChannelManager_close_channel(&cm1, &chan_id, ChannelManager_get_our_node_id(&cm2));
10961142
assert(close_res->result_ok);
10971143
PeerManager_process_events(&net1);
1144+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting 2 transaction broadcasts..." << std::endl;
10981145
while (num_txs_broadcasted != 2) {
10991146
std::this_thread::yield();
11001147
}
1148+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Broadcast 2 transactions!" << std::endl;
11011149
LDK::CVec_ChannelDetailsZ chans_after_close1 = ChannelManager_list_channels(&cm1);
11021150
assert(chans_after_close1->datalen == 0);
11031151
LDK::CVec_ChannelDetailsZ chans_after_close2 = ChannelManager_list_channels(&cm2);
@@ -1112,11 +1160,13 @@ int main() {
11121160
}, LDKBlindedPath { .inner = NULL, .is_owned = true })
11131161
.result_ok);
11141162
PeerManager_process_events(&net1);
1163+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Awaiting onion message..." << std::endl;
11151164
while (true) {
11161165
std::this_thread::yield();
11171166
std::unique_lock<std::mutex> lck(peer_2_custom_onion_messages.mtx);
11181167
if (peer_2_custom_onion_messages.msgs.size() != 0) break;
11191168
}
1169+
std::cout << __FILE__ << ":" << __LINE__ << " - " << "Received onion message!" << std::endl;
11201170

11211171
conn.stop();
11221172

0 commit comments

Comments
 (0)