2 #include "include/rust_types.h"
3 #include "include/lightning.h"
5 #include "include/lightningpp.hpp"
9 #include <sys/socket.h>
18 const uint8_t valid_node_announcement[] = {
19 0x94, 0xe4, 0xf5, 0x61, 0x41, 0x24, 0x7d, 0x90, 0x23, 0xa0, 0xc8, 0x34, 0x8c, 0xc4, 0xca, 0x51,
20 0xd8, 0x17, 0x59, 0xff, 0x7d, 0xac, 0x8c, 0x9b, 0x63, 0x29, 0x1c, 0xe6, 0x12, 0x12, 0x93, 0xbd,
21 0x66, 0x4d, 0x6b, 0x9c, 0xfb, 0x35, 0xda, 0x16, 0x06, 0x3d, 0xf0, 0x8f, 0x8a, 0x39, 0x99, 0xa2,
22 0xf2, 0x5d, 0x12, 0x0f, 0x2b, 0x42, 0x1b, 0x8b, 0x9a, 0xfe, 0x33, 0x0c, 0xeb, 0x33, 0x5e, 0x52,
23 0xee, 0x99, 0xa1, 0x07, 0x06, 0xed, 0xf8, 0x48, 0x7a, 0xc6, 0xe5, 0xf5, 0x5e, 0x01, 0x3a, 0x41,
24 0x2f, 0x18, 0x94, 0x8a, 0x3b, 0x0a, 0x52, 0x3f, 0xbf, 0x61, 0xa9, 0xc5, 0x4f, 0x70, 0xee, 0xb8,
25 0x79, 0x23, 0xbb, 0x1a, 0x44, 0x7d, 0x91, 0xe6, 0x2a, 0xbc, 0xa1, 0x07, 0xbc, 0x65, 0x3b, 0x02,
26 0xd9, 0x1d, 0xb2, 0xf2, 0x3a, 0xcb, 0x75, 0x79, 0xc6, 0x66, 0xd8, 0xc1, 0x71, 0x29, 0xdf, 0x04,
27 0x60, 0xf4, 0xbf, 0x07, 0x7b, 0xb9, 0xc2, 0x11, 0x94, 0x6a, 0x28, 0xc2, 0xdd, 0xd8, 0x7b, 0x44,
28 0x8f, 0x08, 0xe3, 0xc8, 0xd8, 0xf4, 0x81, 0xb0, 0x9f, 0x94, 0xcb, 0xc8, 0xc1, 0x3c, 0xc2, 0x6e,
29 0x31, 0x26, 0xfc, 0x33, 0x16, 0x3b, 0xe0, 0xde, 0xa1, 0x16, 0x21, 0x9f, 0x89, 0xdd, 0x97, 0xa4,
30 0x41, 0xf2, 0x9f, 0x19, 0xb1, 0xae, 0x82, 0xf7, 0x85, 0x9a, 0xb7, 0x8f, 0xb7, 0x52, 0x7a, 0x72,
31 0xf1, 0x5e, 0x89, 0xe1, 0x8a, 0xcd, 0x40, 0xb5, 0x8e, 0xc3, 0xca, 0x42, 0x76, 0xa3, 0x6e, 0x1b,
32 0xf4, 0x87, 0x35, 0x30, 0x58, 0x43, 0x04, 0xd9, 0x2c, 0x50, 0x54, 0x55, 0x47, 0x6f, 0x70, 0x9b,
33 0x42, 0x1f, 0x91, 0xfc, 0xa1, 0xdb, 0x72, 0x53, 0x96, 0xc8, 0xe5, 0xcd, 0x0e, 0xcb, 0xa0, 0xfe,
34 0x6b, 0x08, 0x77, 0x48, 0xb7, 0xad, 0x4a, 0x69, 0x7c, 0xdc, 0xd8, 0x04, 0x28, 0x35, 0x9b, 0x73,
35 0x00, 0x00, 0x43, 0x49, 0x7f, 0xd7, 0xf8, 0x26, 0x95, 0x71, 0x08, 0xf4, 0xa3, 0x0f, 0xd9, 0xce,
36 0xc3, 0xae, 0xba, 0x79, 0x97, 0x20, 0x84, 0xe9, 0x0e, 0xad, 0x01, 0xea, 0x33, 0x09, 0x00, 0x00,
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x5b, 0xe5, 0xe9, 0x47, 0x82,
38 0x09, 0x67, 0x4a, 0x96, 0xe6, 0x0f, 0x1f, 0x03, 0x7f, 0x61, 0x76, 0x54, 0x0f, 0xd0, 0x01, 0xfa,
39 0x1d, 0x64, 0x69, 0x47, 0x70, 0xc5, 0x6a, 0x77, 0x09, 0xc4, 0x2c, 0x03, 0x5c, 0x4e, 0x0d, 0xec,
40 0x72, 0x15, 0xe2, 0x68, 0x33, 0x93, 0x87, 0x30, 0xe5, 0xe5, 0x05, 0xaa, 0x62, 0x50, 0x4d, 0xa8,
41 0x5b, 0xa5, 0x71, 0x06, 0xa4, 0x6b, 0x5a, 0x24, 0x04, 0xfc, 0x9d, 0x8e, 0x02, 0xba, 0x72, 0xa6,
42 0xe8, 0xba, 0x53, 0xe8, 0xb9, 0x71, 0xad, 0x0c, 0x98, 0x23, 0x96, 0x8a, 0xef, 0x4d, 0x78, 0xce,
43 0x8a, 0xf2, 0x55, 0xab, 0x43, 0xdf, 0xf8, 0x30, 0x03, 0xc9, 0x02, 0xfb, 0x8d, 0x02, 0x16, 0x34,
44 0x5b, 0xf8, 0x31, 0x16, 0x4a, 0x03, 0x75, 0x8e, 0xae, 0xa5, 0xe8, 0xb6, 0x6f, 0xee, 0x2b, 0xe7,
45 0x71, 0x0b, 0x8f, 0x19, 0x0e, 0xe8, 0x80, 0x24, 0x90, 0x32, 0xa2, 0x9e, 0xd6, 0x6e
48 // A simple block containing only one transaction (which is the channel-open transaction for the
49 // channel we'll create). This was originally created by printing additional data in a simple
50 // rust-lightning unit test.
51 const uint8_t channel_open_block[] = {
52 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0xa2, 0x47, 0xd2, 0xf8, 0xd4, 0xe0, 0x6a, 0x3f, 0xf9, 0x7a, 0x9a, 0x34,
55 0xbb, 0xa9, 0x96, 0xde, 0x63, 0x84, 0x5a, 0xce, 0xcf, 0x98, 0xb8, 0xbb, 0x75, 0x4c, 0x4f, 0x7d,
56 0xee, 0x4c, 0xa9, 0x5f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x01, 0x40, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x20, 0x12, 0x70,
61 0x44, 0x41, 0x40, 0xaf, 0xc5, 0x72, 0x97, 0xc8, 0x69, 0xba, 0x04, 0xdb, 0x28, 0x7b, 0xd7, 0x32,
62 0x07, 0x33, 0x3a, 0x4a, 0xc2, 0xc5, 0x56, 0x06, 0x05, 0x65, 0xd7, 0xa8, 0xcf, 0x01, 0x00, 0x00,
66 // The first transaction in the block is header (80 bytes) + transaction count (1 byte) into the block data.
67 const uint8_t *channel_open_tx = channel_open_block + 81;
68 const uint8_t channel_open_txid[] = {
69 0x5f, 0xa9, 0x4c, 0xee, 0x7d, 0x4f, 0x4c, 0x75, 0xbb, 0xb8, 0x98, 0xcf, 0xce, 0x5a, 0x84, 0x63,
70 0xde, 0x96, 0xa9, 0xbb, 0x34, 0x9a, 0x7a, 0xf9, 0x3f, 0x6a, 0xe0, 0xd4, 0xf8, 0xd2, 0x47, 0xa2
73 // Two blocks built on top of channel_open_block:
74 const uint8_t block_1[] = {
75 0x01, 0x00, 0x00, 0x00, 0x65, 0x8e, 0xf1, 0x90, 0x88, 0xfa, 0x13, 0x9c, 0x6a, 0xea, 0xf7, 0xc1,
76 0x5a, 0xdd, 0x52, 0x4d, 0x3c, 0x48, 0x03, 0xb3, 0x9b, 0x25, 0x4f, 0x02, 0x79, 0x05, 0x90, 0xe0,
77 0xc4, 0x8d, 0xa0, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 const uint8_t block_2[] = {
83 0x01, 0x00, 0x00, 0x00, 0xf2, 0x08, 0x87, 0x51, 0xcb, 0xb1, 0x1a, 0x51, 0x76, 0x01, 0x6c, 0x5d,
84 0x76, 0x26, 0x54, 0x6f, 0xd9, 0xbd, 0xa6, 0xa5, 0xe9, 0x4b, 0x21, 0x6e, 0xda, 0xa3, 0x64, 0x23,
85 0xcd, 0xf1, 0xe2, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 const LDKThirtyTwoBytes payment_preimage_1 = {
92 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 }
94 const LDKThirtyTwoBytes payment_hash_1 = {
96 0xdc, 0xb1, 0xac, 0x4a, 0x5d, 0xe3, 0x70, 0xca, 0xd0, 0x91, 0xc1, 0x3f, 0x13, 0xae, 0xe2, 0xf9,
97 0x36, 0xc2, 0x78, 0xfa, 0x05, 0xd2, 0x64, 0x65, 0x3c, 0x0c, 0x13, 0x21, 0x85, 0x2a, 0x35, 0xe8
101 void print_log(const void *this_arg, const char *record) {
102 printf("%p - %s\n", this_arg, record);
105 uint32_t get_fee(const void *this_arg, LDKConfirmationTarget target) {
106 if (target == LDKConfirmationTarget_Background) {
111 // Note that we don't call _free() on target, but that's OK, its unitary
114 static int num_txs_broadcasted = 0; // Technically a race, but ints are atomic on x86
115 void broadcast_tx(const void *this_arg, LDKTransaction tx) {
116 num_txs_broadcasted += 1;
118 Transaction_free(tx);
121 struct NodeMonitors {
123 std::vector<std::pair<LDK::OutPoint, LDK::ChannelMonitor>> mons;
124 LDKChainWatchInterfaceUtil* watch_util;
127 void NodeMonitors_free(void *this_arg) {
128 NodeMonitors* arg = (NodeMonitors*) this_arg;
132 LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitor monitor_arg) {
133 // First bind the args to C++ objects so they auto-free
134 LDK::ChannelMonitor mon(std::move(monitor_arg));
135 LDK::OutPoint funding_txo(std::move(funding_txo_arg));
137 NodeMonitors* arg = (NodeMonitors*) this_arg;
138 std::unique_lock<std::mutex> l(arg->mut);
140 LDK::ChainWatchInterface watch = ChainWatchInterfaceUtil_as_ChainWatchInterface(arg->watch_util);
141 LDK::C2Tuple_OutPointScriptZ funding_info = ChannelMonitor_get_funding_txo(&mon);
142 LDKThirtyTwoBytes funding_txid;
143 memcpy(funding_txid.data, OutPoint_get_txid(funding_info->a), 32);
144 LDK::C2Tuple_Txidu32Z outpoint(C2Tuple_Txidu32Z_new(funding_txid, OutPoint_get_index(funding_info->a)));
145 watch->install_watch_outpoint(watch->this_arg, outpoint, LDKu8slice {
146 .data = funding_info->b->data,
147 .datalen = funding_info->b->datalen,
149 watch->install_watch_tx(watch->this_arg, &funding_txid.data, LDKu8slice {
150 .data = funding_info->b->data,
151 .datalen = funding_info->b->datalen,
153 arg->mons.push_back(std::make_pair(std::move(funding_txo), std::move(mon)));
154 return CResult_NoneChannelMonitorUpdateErrZ_ok();
156 static int mons_updated = 0; // Technically a race, but ints are atomic on x86.
157 LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitorUpdate monitor_arg) {
158 // First bind the args to C++ objects so they auto-free
159 LDK::ChannelMonitorUpdate update(std::move(monitor_arg));
160 LDK::OutPoint funding_txo(std::move(funding_txo_arg));
162 NodeMonitors* arg = (NodeMonitors*) this_arg;
163 std::unique_lock<std::mutex> l(arg->mut);
165 bool updated = false;
166 for (auto& mon : arg->mons) {
167 if (OutPoint_get_index(&mon.first) == OutPoint_get_index(&funding_txo) &&
168 !memcmp(OutPoint_get_txid(&mon.first), OutPoint_get_txid(&funding_txo), 32)) {
170 LDKBroadcasterInterface broadcaster = {
171 .broadcast_transaction = broadcast_tx,
173 LDK::CResult_NoneMonitorUpdateErrorZ res = ChannelMonitor_update_monitor(&mon.second, update, &broadcaster, arg->logger);
174 assert(res->result_ok);
180 return CResult_NoneChannelMonitorUpdateErrZ_ok();
182 LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
183 NodeMonitors* arg = (NodeMonitors*) this_arg;
184 std::unique_lock<std::mutex> l(arg->mut);
186 if (arg->mons.size() == 0) {
187 return LDKCVec_MonitorEventZ {
192 // We only ever actually have one channel per node, plus concatenating two
193 // Rust Vecs to each other from C++ will require a bit of effort.
194 assert(arg->mons.size() == 1);
195 return ChannelMonitor_get_and_clear_pending_monitor_events(&arg->mons[0].second);
199 uintptr_t sock_send_data(void *this_arg, LDKu8slice data, bool resume_read) {
200 return write((int)((long)this_arg), data.data, data.datalen);
202 void sock_disconnect_socket(void *this_arg) {
203 close((int)((long)this_arg));
205 bool sock_eq(const void *this_arg, const void *other_arg) {
206 return this_arg == other_arg;
208 uint64_t sock_hash(const void *this_arg) {
209 return (uint64_t)this_arg;
211 void sock_read_data_thread(int rdfd, LDKSocketDescriptor *peer_descriptor, LDKPeerManager *pm) {
212 unsigned char buf[1024];
216 while ((readlen = read(rdfd, buf, 1024)) > 0) {
217 data.datalen = readlen;
218 LDK::CResult_boolPeerHandleErrorZ res = PeerManager_read_event(&*pm, peer_descriptor, data);
219 if (!res->result_ok) {
220 peer_descriptor->disconnect_socket(peer_descriptor->this_arg);
223 PeerManager_process_events(pm);
225 PeerManager_socket_disconnected(&*pm, peer_descriptor);
229 uint8_t node_seed[32];
230 memset(&node_seed, 0, 32);
232 LDKPublicKey null_pk;
233 memset(&null_pk, 0, sizeof(null_pk));
235 LDKNetwork network = LDKNetwork_Testnet;
237 // Trait implementations:
238 LDKFeeEstimator fee_est {
240 .get_est_sat_per_1000_weight = get_fee,
244 LDKBroadcasterInterface broadcast {
246 .broadcast_transaction = broadcast_tx,
250 // Instantiate classes for node 1:
253 .this_arg = (void*)1,
258 LDK::ChainWatchInterfaceUtil chain1 = ChainWatchInterfaceUtil_new(network);
259 LDK::BlockNotifier blocks1 = BlockNotifier_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain1));
261 LDKManyChannelMonitor mon1 {
262 .this_arg = new NodeMonitors { .watch_util = &chain1, .logger = &logger1 },
263 .add_monitor = add_channel_monitor,
264 .update_monitor = update_channel_monitor,
265 .get_and_clear_pending_monitor_events = monitors_pending_monitor_events,
266 .free = NodeMonitors_free,
269 LDK::KeysManager keys1 = KeysManager_new(&node_seed, network, 0, 0);
270 LDK::KeysInterface keys_source1 = KeysManager_as_KeysInterface(&keys1);
271 LDKSecretKey node_secret1 = keys_source1->get_node_secret(keys_source1->this_arg);
273 LDK::UserConfig config1 = UserConfig_default();
275 LDK::ChannelManager cm1 = ChannelManager_new(network, fee_est, mon1, broadcast, logger1, KeysManager_as_KeysInterface(&keys1), config1, 0);
276 BlockNotifier_register_listener(&blocks1, ChannelManager_as_ChainListener(&cm1));
278 LDK::CVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm1);
279 assert(channels->datalen == 0);
281 LDK::NetGraphMsgHandler net_graph1 = NetGraphMsgHandler_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain1), logger1);
283 LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), NetGraphMsgHandler_as_RoutingMessageHandler(&net_graph1));
285 LDKThirtyTwoBytes random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg);
286 LDK::PeerManager net1 = PeerManager_new(msg_handler1, node_secret1, &random_bytes.data, logger1);
288 // Demo getting a channel key and check that its returning real pubkeys:
289 LDK::ChannelKeys chan_keys1 = keys_source1->get_channel_keys(keys_source1->this_arg, false, 42);
290 chan_keys1->set_pubkeys(&chan_keys1); // Make sure pubkeys is defined
291 LDKPublicKey payment_point = ChannelPublicKeys_get_payment_point(&chan_keys1->pubkeys);
292 assert(memcmp(&payment_point, &null_pk, sizeof(null_pk)));
294 // Instantiate classes for node 2:
297 .this_arg = (void*)2,
302 LDK::ChainWatchInterfaceUtil chain2 = ChainWatchInterfaceUtil_new(network);
303 LDK::BlockNotifier blocks2 = BlockNotifier_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain2));
305 LDKManyChannelMonitor mon2 {
306 .this_arg = new NodeMonitors { .watch_util = &chain2, .logger = &logger2 },
307 .add_monitor = add_channel_monitor,
308 .update_monitor = update_channel_monitor,
309 .get_and_clear_pending_monitor_events = monitors_pending_monitor_events,
310 .free = NodeMonitors_free,
313 memset(&node_seed, 1, 32);
314 LDK::KeysManager keys2 = KeysManager_new(&node_seed, network, 0, 0);
315 LDK::KeysInterface keys_source2 = KeysManager_as_KeysInterface(&keys2);
316 LDKSecretKey node_secret2 = keys_source2->get_node_secret(keys_source2->this_arg);
318 LDK::ChannelHandshakeConfig handshake_config2 = ChannelHandshakeConfig_default();
319 ChannelHandshakeConfig_set_minimum_depth(&handshake_config2, 2);
320 LDK::UserConfig config2 = UserConfig_default();
321 UserConfig_set_own_channel_config(&config2, handshake_config2);
323 LDK::ChannelManager cm2 = ChannelManager_new(network, fee_est, mon2, broadcast, logger2, KeysManager_as_KeysInterface(&keys2), config2, 0);
324 BlockNotifier_register_listener(&blocks2, ChannelManager_as_ChainListener(&cm2));
326 LDK::CVec_ChannelDetailsZ channels2 = ChannelManager_list_channels(&cm2);
327 assert(channels2->datalen == 0);
329 LDK::NetGraphMsgHandler net_graph2 = NetGraphMsgHandler_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain2), logger2);
330 LDK::RoutingMessageHandler net_msgs2 = NetGraphMsgHandler_as_RoutingMessageHandler(&net_graph2);
331 LDK::ChannelAnnouncement chan_ann = ChannelAnnouncement_read(LDKu8slice { .data = valid_node_announcement, .datalen = sizeof(valid_node_announcement) });
332 LDK::CResult_boolLightningErrorZ ann_res = net_msgs2->handle_channel_announcement(net_msgs2->this_arg, &chan_ann);
333 assert(ann_res->result_ok);
335 LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), net_msgs2);
337 LDKThirtyTwoBytes random_bytes2 = keys_source2->get_secure_random_bytes(keys_source2->this_arg);
338 LDK::PeerManager net2 = PeerManager_new(msg_handler2, node_secret2, &random_bytes2.data, logger2);
340 // Open a connection!
341 int pipefds_1_to_2[2];
342 int pipefds_2_to_1[2];
343 assert(!pipe(pipefds_1_to_2));
344 assert(!pipe(pipefds_2_to_1));
346 LDKSocketDescriptor sock1 {
347 .this_arg = (void*)(long)pipefds_1_to_2[1],
348 .send_data = sock_send_data,
349 .disconnect_socket = sock_disconnect_socket,
356 LDKSocketDescriptor sock2 {
357 .this_arg = (void*)(long)pipefds_2_to_1[1],
358 .send_data = sock_send_data,
359 .disconnect_socket = sock_disconnect_socket,
366 std::thread t1(&sock_read_data_thread, pipefds_2_to_1[0], &sock1, &net1);
367 std::thread t2(&sock_read_data_thread, pipefds_1_to_2[0], &sock2, &net2);
369 // Note that we have to bind the result to a C++ class to make sure it gets free'd
370 LDK::CResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&net1, ChannelManager_get_our_node_id(&cm2), sock1);
371 assert(con_res->result_ok);
372 LDK::CResult_NonePeerHandleErrorZ con_res2 = PeerManager_new_inbound_connection(&net2, sock2);
373 assert(con_res2->result_ok);
375 auto writelen = write(pipefds_1_to_2[1], con_res->contents.result->data, con_res->contents.result->datalen);
376 assert(writelen > 0 && uint64_t(writelen) == con_res->contents.result->datalen);
379 // Wait for the initial handshakes to complete...
380 LDK::CVec_PublicKeyZ peers_1 = PeerManager_get_peer_node_ids(&net1);
381 LDK::CVec_PublicKeyZ peers_2 = PeerManager_get_peer_node_ids(&net2);
382 if (peers_1->datalen == 1 && peers_2->datalen ==1) { break; }
383 std::this_thread::yield();
386 // Note that we have to bind the result to a C++ class to make sure it gets free'd
387 LDK::CResult_NoneAPIErrorZ res = ChannelManager_create_channel(&cm1, ChannelManager_get_our_node_id(&cm2), 40000, 1000, 42, config1);
388 assert(res->result_ok);
389 PeerManager_process_events(&net1);
391 LDK::CVec_ChannelDetailsZ new_channels = ChannelManager_list_channels(&cm1);
392 assert(new_channels->datalen == 1);
393 LDKPublicKey chan_open_pk = ChannelDetails_get_remote_network_id(&new_channels->data[0]);
394 assert(!memcmp(chan_open_pk.compressed_form, ChannelManager_get_our_node_id(&cm2).compressed_form, 33));
397 LDK::CVec_ChannelDetailsZ new_channels_2 = ChannelManager_list_channels(&cm2);
398 if (new_channels_2->datalen == 1) {
399 // Sample getting our counterparty's init features (which used to be hard to do without a memory leak):
400 const LDK::InitFeatures init_feats = ChannelDetails_get_counterparty_features(&new_channels_2->data[0]);
401 assert(init_feats->inner != NULL);
404 std::this_thread::yield();
407 LDKEventsProvider ev1 = ChannelManager_as_EventsProvider(&cm1);
409 LDK::CVec_EventZ events = ev1.get_and_clear_pending_events(ev1.this_arg);
410 if (events->datalen == 1) {
411 assert(events->data[0].tag == LDKEvent_FundingGenerationReady);
412 assert(events->data[0].funding_generation_ready.user_channel_id == 42);
413 assert(events->data[0].funding_generation_ready.channel_value_satoshis == 40000);
414 assert(events->data[0].funding_generation_ready.output_script.datalen == 34);
415 assert(!memcmp(events->data[0].funding_generation_ready.output_script.data, channel_open_tx + 58, 34));
416 LDKThirtyTwoBytes txid;
417 for (int i = 0; i < 32; i++) { txid.data[i] = channel_open_txid[31-i]; }
418 LDK::OutPoint outp = OutPoint_new(txid, 0);
419 ChannelManager_funding_transaction_generated(&cm1, &events->data[0].funding_generation_ready.temporary_channel_id.data, outp);
422 std::this_thread::yield();
425 // We observe when the funding signed messages have been exchanged by
426 // waiting for two monitors to be registered.
427 PeerManager_process_events(&net1);
429 LDK::CVec_EventZ events = ev1.get_and_clear_pending_events(ev1.this_arg);
430 if (events->datalen == 1) {
431 assert(events->data[0].tag == LDKEvent_FundingBroadcastSafe);
432 assert(events->data[0].funding_broadcast_safe.user_channel_id == 42);
435 std::this_thread::yield();
438 BlockNotifier_block_connected(&blocks1, LDKu8slice { .data = channel_open_block, .datalen = sizeof(channel_open_block) }, 1);
439 BlockNotifier_block_connected(&blocks2, LDKu8slice { .data = channel_open_block, .datalen = sizeof(channel_open_block) }, 1);
441 BlockNotifier_block_connected(&blocks1, LDKu8slice { .data = block_1, .datalen = sizeof(block_1) }, 2);
442 BlockNotifier_block_connected(&blocks2, LDKu8slice { .data = block_1, .datalen = sizeof(block_1) }, 2);
444 BlockNotifier_block_connected(&blocks1, LDKu8slice { .data = block_2, .datalen = sizeof(block_2) }, 3);
445 BlockNotifier_block_connected(&blocks2, LDKu8slice { .data = block_2, .datalen = sizeof(block_2) }, 3);
447 PeerManager_process_events(&net1);
448 PeerManager_process_events(&net2);
450 // Now send funds from 1 to 2!
452 LDK::CVec_ChannelDetailsZ outbound_channels = ChannelManager_list_usable_channels(&cm1);
453 if (outbound_channels->datalen == 1) {
454 const LDKChannelDetails *channel = &outbound_channels->data[0];
455 // Note that the channel ID is the same as the channel txid reversed as the output index is 0
456 uint8_t expected_chan_id[32];
457 for (int i = 0; i < 32; i++) { expected_chan_id[i] = channel_open_txid[31-i]; }
458 assert(!memcmp(ChannelDetails_get_channel_id(channel), expected_chan_id, 32));
459 assert(!memcmp(ChannelDetails_get_remote_network_id(channel).compressed_form,
460 ChannelManager_get_our_node_id(&cm2).compressed_form, 33));
461 assert(ChannelDetails_get_channel_value_satoshis(channel) == 40000);
462 // We opened the channel with 1000 push_msat:
463 assert(ChannelDetails_get_outbound_capacity_msat(channel) == 40000*1000 - 1000);
464 assert(ChannelDetails_get_inbound_capacity_msat(channel) == 1000);
465 assert(ChannelDetails_get_is_live(channel));
468 std::this_thread::yield();
471 LDK::CVec_ChannelDetailsZ outbound_channels = ChannelManager_list_usable_channels(&cm1);
472 LDKThirtyTwoBytes payment_secret;
473 memset(payment_secret.data, 0x42, 32);
475 LDK::LockedNetworkGraph graph_2_locked = NetGraphMsgHandler_read_locked_graph(&net_graph2);
476 LDK::NetworkGraph graph_2_ref = LockedNetworkGraph_graph(&graph_2_locked);
477 LDK::CResult_RouteLightningErrorZ route = get_route(ChannelManager_get_our_node_id(&cm1), &graph_2_ref, ChannelManager_get_our_node_id(&cm2), &outbound_channels, LDKCVec_RouteHintZ {
478 .data = NULL, .datalen = 0
479 }, 5000, 10, logger1);
480 assert(route->result_ok);
481 LDK::CResult_NonePaymentSendFailureZ send_res = ChannelManager_send_payment(&cm1, route->contents.result, payment_hash_1, payment_secret);
482 assert(send_res->result_ok);
486 PeerManager_process_events(&net1);
487 while (mons_updated != 4) {
488 std::this_thread::yield();
491 // Check that we received the payment!
492 LDKEventsProvider ev2 = ChannelManager_as_EventsProvider(&cm2);
494 LDK::CVec_EventZ events = ev2.get_and_clear_pending_events(ev2.this_arg);
495 if (events->datalen == 1) {
496 assert(events->data[0].tag == LDKEvent_PendingHTLCsForwardable);
499 std::this_thread::yield();
501 ChannelManager_process_pending_htlc_forwards(&cm2);
502 PeerManager_process_events(&net2);
506 LDK::CVec_EventZ events = ev2.get_and_clear_pending_events(ev2.this_arg);
507 assert(events->datalen == 1);
508 assert(events->data[0].tag == LDKEvent_PaymentReceived);
509 assert(!memcmp(events->data[0].payment_received.payment_hash.data, payment_hash_1.data, 32));
510 assert(!memcmp(events->data[0].payment_received.payment_secret.data, payment_secret.data, 32));
511 assert(events->data[0].payment_received.amt == 5000);
512 assert(ChannelManager_claim_funds(&cm2, payment_preimage_1, payment_secret, 5000));
514 PeerManager_process_events(&net2);
515 // Wait until we've passed through a full set of monitor updates (ie new preimage + CS/RAA messages)
516 while (mons_updated != 5) {
517 std::this_thread::yield();
520 LDK::CVec_EventZ events = ev1.get_and_clear_pending_events(ev1.this_arg);
521 assert(events->datalen == 1);
522 assert(events->data[0].tag == LDKEvent_PaymentSent);
523 assert(!memcmp(events->data[0].payment_sent.payment_preimage.data, payment_preimage_1.data, 32));
526 // Close the channel.
528 for (int i = 0; i < 32; i++) { chan_id[i] = channel_open_txid[31-i]; }
529 LDK::CResult_NoneAPIErrorZ close_res = ChannelManager_close_channel(&cm1, &chan_id);
530 assert(close_res->result_ok);
531 PeerManager_process_events(&net1);
532 num_txs_broadcasted = 0;
533 while (num_txs_broadcasted != 2) {
534 std::this_thread::yield();
536 LDK::CVec_ChannelDetailsZ chans_after_close1 = ChannelManager_list_channels(&cm1);
537 assert(chans_after_close1->datalen == 0);
538 LDK::CVec_ChannelDetailsZ chans_after_close2 = ChannelManager_list_channels(&cm2);
539 assert(chans_after_close2->datalen == 0);
541 close(pipefds_1_to_2[0]);
542 close(pipefds_2_to_1[0]);
543 close(pipefds_1_to_2[1]);
544 close(pipefds_2_to_1[1]);
548 // Few extra random tests:
551 LDKC2Tuple_u64u64Z kdiv_params;
552 kdiv_params.a = (uint64_t*) malloc(8);
553 kdiv_params.b = (uint64_t*) malloc(8);
556 LDK::InMemoryChannelKeys keys = InMemoryChannelKeys_new(sk, sk, sk, sk, sk, random_bytes, 42, kdiv_params);