Skip to content

Commit 346c148

Browse files
committed
Update bindings demo apps for new code upstream
1 parent 9ac8a3a commit 346c148

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

lightning-c-bindings/demo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ int main() {
5454
.free = NULL
5555
};
5656

57-
LDKManyChannelMonitor mon = {
57+
LDKWatch mon = {
5858
.this_arg = NULL,
59-
.add_monitor = add_channel_monitor,
60-
.update_monitor = update_channel_monitor,
61-
.get_and_clear_pending_monitor_events = monitors_pending_monitor_events,
59+
.watch_channel = add_channel_monitor,
60+
.update_channel = update_channel_monitor,
61+
.release_pending_monitor_events = monitors_pending_monitor_events,
6262
.free = NULL,
6363
};
6464

lightning-c-bindings/demo.cpp

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -48,44 +48,44 @@ const uint8_t valid_node_announcement[] = {
4848
// A simple block containing only one transaction (which is the channel-open transaction for the
4949
// channel we'll create). This was originally created by printing additional data in a simple
5050
// rust-lightning unit test.
51-
const uint8_t channel_open_block[] = {
51+
const uint8_t channel_open_header[80] = {
5252
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5353
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5454
0x00, 0x00, 0x00, 0x00, 0xa2, 0x47, 0xd2, 0xf8, 0xd4, 0xe0, 0x6a, 0x3f, 0xf9, 0x7a, 0x9a, 0x34,
5555
0xbb, 0xa9, 0x96, 0xde, 0x63, 0x84, 0x5a, 0xce, 0xcf, 0x98, 0xb8, 0xbb, 0x75, 0x4c, 0x4f, 0x7d,
5656
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,
57+
};
58+
59+
const uint8_t channel_open_tx[] = {
60+
0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5861
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5962
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,
63-
0x00, 0x00, 0x00
63+
0x01, 0x40, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x20, 0x12, 0x70, 0x44,
64+
0x41, 0x40, 0xaf, 0xc5, 0x72, 0x97, 0xc8, 0x69, 0xba, 0x04, 0xdb, 0x28, 0x7b, 0xd7, 0x32, 0x07,
65+
0x33, 0x3a, 0x4a, 0xc2, 0xc5, 0x56, 0x06, 0x05, 0x65, 0xd7, 0xa8, 0xcf, 0x01, 0x00, 0x00, 0x00,
66+
0x00, 0x00
6467
};
6568

6669
// 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;
6870
const uint8_t channel_open_txid[] = {
6971
0x5f, 0xa9, 0x4c, 0xee, 0x7d, 0x4f, 0x4c, 0x75, 0xbb, 0xb8, 0x98, 0xcf, 0xce, 0x5a, 0x84, 0x63,
7072
0xde, 0x96, 0xa9, 0xbb, 0x34, 0x9a, 0x7a, 0xf9, 0x3f, 0x6a, 0xe0, 0xd4, 0xf8, 0xd2, 0x47, 0xa2
7173
};
7274

7375
// Two blocks built on top of channel_open_block:
74-
const uint8_t block_1[] = {
76+
const uint8_t header_1[80] = {
7577
0x01, 0x00, 0x00, 0x00, 0x65, 0x8e, 0xf1, 0x90, 0x88, 0xfa, 0x13, 0x9c, 0x6a, 0xea, 0xf7, 0xc1,
7678
0x5a, 0xdd, 0x52, 0x4d, 0x3c, 0x48, 0x03, 0xb3, 0x9b, 0x25, 0x4f, 0x02, 0x79, 0x05, 0x90, 0xe0,
7779
0xc4, 0x8d, 0xa0, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7880
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7981
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80-
0x00
8182
};
82-
const uint8_t block_2[] = {
83+
const uint8_t header_2[80] = {
8384
0x01, 0x00, 0x00, 0x00, 0xf2, 0x08, 0x87, 0x51, 0xcb, 0xb1, 0x1a, 0x51, 0x76, 0x01, 0x6c, 0x5d,
8485
0x76, 0x26, 0x54, 0x6f, 0xd9, 0xbd, 0xa6, 0xa5, 0xe9, 0x4b, 0x21, 0x6e, 0xda, 0xa3, 0x64, 0x23,
8586
0xcd, 0xf1, 0xe2, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8687
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8788
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88-
0x00
8989
};
9090

9191
const LDKThirtyTwoBytes payment_preimage_1 = {
@@ -121,13 +121,15 @@ void broadcast_tx(const void *this_arg, LDKTransaction tx) {
121121
struct NodeMonitors {
122122
std::mutex mut;
123123
std::vector<std::pair<LDK::OutPoint, LDK::ChannelMonitor>> mons;
124-
LDKChainWatchInterfaceUtil* watch_util;
125124
LDKLogger* logger;
125+
126+
void ConnectBlock(const uint8_t (*header)[80], uint32_t height, LDKCVec_C2Tuple_usizeTransactionZZ tx_data, LDKBroadcasterInterface broadcast, LDKFeeEstimator fee_est) {
127+
std::unique_lock<std::mutex> l(mut);
128+
for (auto& mon : mons) {
129+
LDK::CVec_C2Tuple_TxidCVec_TxOutZZZ res = ChannelMonitor_block_connected(&mon.second, &header_2, tx_data, height, broadcast, fee_est, *logger);
130+
}
131+
}
126132
};
127-
void NodeMonitors_free(void *this_arg) {
128-
NodeMonitors* arg = (NodeMonitors*) this_arg;
129-
delete arg;
130-
}
131133

132134
LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitor monitor_arg) {
133135
// First bind the args to C++ objects so they auto-free
@@ -137,19 +139,6 @@ LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg
137139
NodeMonitors* arg = (NodeMonitors*) this_arg;
138140
std::unique_lock<std::mutex> l(arg->mut);
139141

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,
148-
});
149-
watch->install_watch_tx(watch->this_arg, &funding_txid.data, LDKu8slice {
150-
.data = funding_info->b->data,
151-
.datalen = funding_info->b->datalen,
152-
});
153142
arg->mons.push_back(std::make_pair(std::move(funding_txo), std::move(mon)));
154143
return CResult_NoneChannelMonitorUpdateErrZ_ok();
155144
}
@@ -255,30 +244,27 @@ int main() {
255244
.free = NULL,
256245
};
257246

258-
LDK::ChainWatchInterfaceUtil chain1 = ChainWatchInterfaceUtil_new(network);
259-
LDK::BlockNotifier blocks1 = BlockNotifier_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain1));
260-
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,
247+
NodeMonitors mons1;
248+
mons1.logger = &logger1;
249+
LDKWatch mon1 {
250+
.this_arg = &mons1,
251+
.watch_channel = add_channel_monitor,
252+
.update_channel = update_channel_monitor,
253+
.release_pending_monitor_events = monitors_pending_monitor_events,
254+
.free = NULL,
267255
};
268256

269257
LDK::KeysManager keys1 = KeysManager_new(&node_seed, network, 0, 0);
270258
LDK::KeysInterface keys_source1 = KeysManager_as_KeysInterface(&keys1);
271259
LDKSecretKey node_secret1 = keys_source1->get_node_secret(keys_source1->this_arg);
272260

273261
LDK::UserConfig config1 = UserConfig_default();
274-
275262
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));
277263

278264
LDK::CVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm1);
279265
assert(channels->datalen == 0);
280266

281-
LDK::NetGraphMsgHandler net_graph1 = NetGraphMsgHandler_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain1), logger1);
267+
LDK::NetGraphMsgHandler net_graph1 = NetGraphMsgHandler_new(NULL, logger1);
282268

283269
LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), NetGraphMsgHandler_as_RoutingMessageHandler(&net_graph1));
284270

@@ -299,15 +285,14 @@ int main() {
299285
.free = NULL,
300286
};
301287

302-
LDK::ChainWatchInterfaceUtil chain2 = ChainWatchInterfaceUtil_new(network);
303-
LDK::BlockNotifier blocks2 = BlockNotifier_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain2));
304-
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,
288+
NodeMonitors mons2;
289+
mons2.logger = &logger2;
290+
LDKWatch mon2 {
291+
.this_arg = &mons2,
292+
.watch_channel = add_channel_monitor,
293+
.update_channel = update_channel_monitor,
294+
.release_pending_monitor_events = monitors_pending_monitor_events,
295+
.free = NULL,
311296
};
312297

313298
memset(&node_seed, 1, 32);
@@ -321,12 +306,11 @@ int main() {
321306
UserConfig_set_own_channel_config(&config2, handshake_config2);
322307

323308
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));
325309

326310
LDK::CVec_ChannelDetailsZ channels2 = ChannelManager_list_channels(&cm2);
327311
assert(channels2->datalen == 0);
328312

329-
LDK::NetGraphMsgHandler net_graph2 = NetGraphMsgHandler_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain2), logger2);
313+
LDK::NetGraphMsgHandler net_graph2 = NetGraphMsgHandler_new(NULL, logger2);
330314
LDK::RoutingMessageHandler net_msgs2 = NetGraphMsgHandler_as_RoutingMessageHandler(&net_graph2);
331315
LDK::ChannelAnnouncement chan_ann = ChannelAnnouncement_read(LDKu8slice { .data = valid_node_announcement, .datalen = sizeof(valid_node_announcement) });
332316
LDK::CResult_boolLightningErrorZ ann_res = net_msgs2->handle_channel_announcement(net_msgs2->this_arg, &chan_ann);
@@ -435,14 +419,31 @@ int main() {
435419
std::this_thread::yield();
436420
}
437421

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);
422+
LDKCVec_C2Tuple_usizeTransactionZZ txdata { .data = (LDKC2TupleTempl_usize__Transaction*)malloc(sizeof(LDKC2Tuple_usizeTransactionZ)), .datalen = 1 };
423+
*txdata.data = C2Tuple_usizeTransactionZ_new(0, LDKTransaction { .data = channel_open_tx, .datalen = sizeof(channel_open_tx), .data_is_owned = false });
424+
ChannelManager_block_connected(&cm1, &channel_open_header, txdata, 1);
425+
426+
txdata = LDKCVec_C2Tuple_usizeTransactionZZ { .data = (LDKC2TupleTempl_usize__Transaction*)malloc(sizeof(LDKC2Tuple_usizeTransactionZ)), .datalen = 1 };
427+
*txdata.data = C2Tuple_usizeTransactionZ_new(0, LDKTransaction { .data = channel_open_tx, .datalen = sizeof(channel_open_tx), .data_is_owned = false });
428+
ChannelManager_block_connected(&cm2, &channel_open_header, txdata, 1);
429+
430+
txdata = LDKCVec_C2Tuple_usizeTransactionZZ { .data = (LDKC2TupleTempl_usize__Transaction*)malloc(sizeof(LDKC2Tuple_usizeTransactionZ)), .datalen = 1 };
431+
*txdata.data = C2Tuple_usizeTransactionZ_new(0, LDKTransaction { .data = channel_open_tx, .datalen = sizeof(channel_open_tx), .data_is_owned = false });
432+
mons1.ConnectBlock(&channel_open_header, 1, txdata, broadcast, fee_est);
433+
434+
txdata = LDKCVec_C2Tuple_usizeTransactionZZ { .data = (LDKC2TupleTempl_usize__Transaction*)malloc(sizeof(LDKC2Tuple_usizeTransactionZ)), .datalen = 1 };
435+
*txdata.data = C2Tuple_usizeTransactionZ_new(0, LDKTransaction { .data = channel_open_tx, .datalen = sizeof(channel_open_tx), .data_is_owned = false });
436+
mons2.ConnectBlock(&channel_open_header, 1, txdata, broadcast, fee_est);
440437

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);
438+
ChannelManager_block_connected(&cm1, &header_1, LDKCVec_C2Tuple_usizeTransactionZZ { .data = NULL, .datalen = 0 }, 2);
439+
ChannelManager_block_connected(&cm2, &header_1, LDKCVec_C2Tuple_usizeTransactionZZ { .data = NULL, .datalen = 0 }, 2);
440+
mons1.ConnectBlock(&header_1, 2, LDKCVec_C2Tuple_usizeTransactionZZ { .data = NULL, .datalen = 0 }, broadcast, fee_est);
441+
mons2.ConnectBlock(&header_1, 2, LDKCVec_C2Tuple_usizeTransactionZZ { .data = NULL, .datalen = 0 }, broadcast, fee_est);
443442

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);
443+
ChannelManager_block_connected(&cm1, &header_2, LDKCVec_C2Tuple_usizeTransactionZZ { .data = NULL, .datalen = 0 }, 3);
444+
ChannelManager_block_connected(&cm2, &header_2, LDKCVec_C2Tuple_usizeTransactionZZ { .data = NULL, .datalen = 0 }, 3);
445+
mons1.ConnectBlock(&header_2, 3, LDKCVec_C2Tuple_usizeTransactionZZ { .data = NULL, .datalen = 0 }, broadcast, fee_est);
446+
mons2.ConnectBlock(&header_2, 3, LDKCVec_C2Tuple_usizeTransactionZZ { .data = NULL, .datalen = 0 }, broadcast, fee_est);
446447

447448
PeerManager_process_events(&net1);
448449
PeerManager_process_events(&net2);

0 commit comments

Comments
 (0)