Update C/C++ demo bindings to latest upstream API
[ldk-c-bindings] / lightning-c-bindings / demo.cpp
index 34c4dc9364929fbfc62e58cb47b62b6781a08c50..b61bf599f5b48e20dc8d8f98361cb306546dda99 100644 (file)
@@ -54,6 +54,8 @@ const uint8_t valid_node_announcement[] = {
 // A simple block containing only one transaction (which is the channel-open transaction for the
 // channel we'll create). This was originally created by printing additional data in a simple
 // rust-lightning unit test.
+//
+// Note that the merkle root is incorrect, but it isn't ever checked by LDK, so should be fine.
 const uint8_t channel_open_block[] = {
        0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -64,16 +66,16 @@ const uint8_t channel_open_block[] = {
        0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-       0x01, 0x40, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0xd1, 0xd9, 0x13, 0xa9,
-       0x76, 0x09, 0x05, 0xa3, 0x4d, 0x13, 0x5b, 0x69, 0xaa, 0xe7, 0x79, 0x71, 0xb9, 0x75, 0xa1, 0xd0,
-       0x77, 0xcb, 0xa2, 0xf6, 0x6a, 0x25, 0x37, 0x3a, 0xaf, 0xdc, 0x11, 0x09, 0x01, 0x00, 0x00, 0x00,
+       0x01, 0x40, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0xba, 0x1e, 0x83, 0xac,
+       0xc0, 0xee, 0xc5, 0xeb, 0xd0, 0x97, 0xc8, 0x1d, 0x54, 0xbe, 0x54, 0x34, 0x53, 0x2d, 0x1b, 0x90,
+       0x50, 0x4d, 0xc9, 0x7b, 0x88, 0x5b, 0x7b, 0xee, 0x08, 0x98, 0x7b, 0x3b, 0x01, 0x00, 0x00, 0x00,
        0x00, 0x00
 };
 
 // The first transaction in the block is header (80 bytes) + transaction count (1 byte) into the block data.
 const uint8_t channel_open_txid[] = {
-       0x02, 0xe0, 0x50, 0x05, 0x33, 0xd3, 0x29, 0x66, 0x0c, 0xb2, 0xcb, 0x1e, 0x7a, 0x4a, 0xc7, 0xc7,
-       0x8b, 0x02, 0x46, 0x7e, 0x30, 0x2c, 0xe6, 0x19, 0xce, 0x43, 0x3e, 0xdf, 0x43, 0x65, 0xae, 0xf9,
+       0xe3, 0x76, 0xb9, 0xc7, 0x9e, 0xac, 0x0f, 0x28, 0xa2, 0x7f, 0xa6, 0x63, 0xa8, 0x46, 0xf3, 0xcf,
+       0xb1, 0x6a, 0x8d, 0x9a, 0x41, 0x4a, 0x8c, 0x07, 0x2d, 0xfa, 0x94, 0x72, 0x0e, 0x44, 0x3c, 0x7f
 };
 
 // Two blocks built on top of channel_open_block:
@@ -98,8 +100,10 @@ const LDKThirtyTwoBytes genesis_hash = { // We don't care particularly if this i
        .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 }
 };
 
-void print_log(const void *this_arg, const char *record) {
-       printf("%p - %s\n", this_arg, record);
+void print_log(const void *this_arg, const LDKRecord *record) {
+       LDK::Str mod = Record_get_module_path(record);
+       LDK::Str str = Record_get_args(record);
+       printf("%p - %.*s:%d - %.*s\n", this_arg, (int)mod->len, mod->chars, Record_get_line(record), (int)str->len, str->chars);
 }
 
 uint32_t get_fee(const void *this_arg, LDKConfirmationTarget target) {
@@ -137,7 +141,7 @@ struct NodeMonitors {
        }
 };
 
-LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitor monitor_arg) {
+LDKChannelMonitorUpdateStatus add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitor monitor_arg) {
        // First bind the args to C++ objects so they auto-free
        LDK::ChannelMonitor mon(std::move(monitor_arg));
        LDK::OutPoint funding_txo(std::move(funding_txo_arg));
@@ -146,10 +150,10 @@ LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg
        std::unique_lock<std::mutex> l(arg->mut);
 
        arg->mons.push_back(std::make_pair(std::move(funding_txo), std::move(mon)));
-       return CResult_NoneChannelMonitorUpdateErrZ_ok();
+       return ChannelMonitorUpdateStatus_completed();
 }
 static std::atomic_int mons_updated(0);
-LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitorUpdate monitor_arg) {
+LDKChannelMonitorUpdateStatus update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitorUpdate monitor_arg) {
        // First bind the args to C++ objects so they auto-free
        LDK::ChannelMonitorUpdate update(std::move(monitor_arg));
        LDK::OutPoint funding_txo(std::move(funding_txo_arg));
@@ -165,21 +169,21 @@ LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_
                        LDKBroadcasterInterface broadcaster = {
                                .broadcast_transaction = broadcast_tx,
                        };
-                       LDK::CResult_NoneMonitorUpdateErrorZ res = ChannelMonitor_update_monitor(&mon.second, &update, &broadcaster, &fee_est, arg->logger);
+                       LDK::CResult_NoneNoneZ res = ChannelMonitor_update_monitor(&mon.second, &update, &broadcaster, fee_est, arg->logger);
                        assert(res->result_ok);
                }
        }
        assert(updated);
 
        mons_updated += 1;
-       return CResult_NoneChannelMonitorUpdateErrZ_ok();
+       return ChannelMonitorUpdateStatus_completed();
 }
-LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
+LDKCVec_C3Tuple_OutPointCVec_MonitorEventZPublicKeyZZ monitors_pending_monitor_events(const void *this_arg) {
        NodeMonitors* arg = (NodeMonitors*) this_arg;
        std::unique_lock<std::mutex> l(arg->mut);
 
        if (arg->mons.size() == 0) {
-               return LDKCVec_MonitorEventZ {
+               return LDKCVec_C3Tuple_OutPointCVec_MonitorEventZPublicKeyZZ {
                        .data = NULL,
                        .datalen = 0,
                };
@@ -187,16 +191,26 @@ LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
                // We only ever actually have one channel per node, plus concatenating two
                // Rust Vecs to each other from C++ will require a bit of effort.
                assert(arg->mons.size() == 1);
-               return ChannelMonitor_get_and_clear_pending_monitor_events(&arg->mons[0].second);
+               LDK::CVec_MonitorEventZ events = ChannelMonitor_get_and_clear_pending_monitor_events(&arg->mons[0].second);
+               LDK::C2Tuple_OutPointScriptZ funding_info = ChannelMonitor_get_funding_txo(&arg->mons[0].second);
+               LDK::OutPoint outpoint = std::move(funding_info->a);
+               LDKPublicKey counterparty_node_id = ChannelMonitor_get_counterparty_node_id(&arg->mons[0].second);
+               LDK::C3Tuple_OutPointCVec_MonitorEventZPublicKeyZ tuple = C3Tuple_OutPointCVec_MonitorEventZPublicKeyZ_new(std::move(outpoint), std::move(events), std::move(counterparty_node_id));
+               auto vec = LDKCVec_C3Tuple_OutPointCVec_MonitorEventZPublicKeyZZ {
+                       .data = (LDKC3Tuple_OutPointCVec_MonitorEventZPublicKeyZ*)malloc(sizeof(LDKC3Tuple_OutPointCVec_MonitorEventZPublicKeyZ)),
+                       .datalen = 1,
+               };
+               vec.data[0] = std::move(tuple);
+               return vec;
        }
 }
 
 struct EventQueue {
        std::vector<LDK::Event> events;
 };
-void handle_event(const void *this_arg, const LDKEvent *event) {
+void handle_event(const void *this_arg, LDKEvent event) {
        EventQueue* arg = (EventQueue*) this_arg;
-       arg->events.push_back(Event_clone(event));
+       arg->events.push_back(std::move(event));
 }
 
 #ifdef REAL_NET
@@ -329,9 +343,9 @@ public:
                t2 = std::thread(&sock_read_data_thread, pipefds_1_to_2[0], &sock2, &net2);
 
                // Note that we have to bind the result to a C++ class to make sure it gets free'd
-               LDK::CResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&net1, ChannelManager_get_our_node_id(&cm2), sock1);
+               LDK::CResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&net1, ChannelManager_get_our_node_id(&cm2), sock1, COption_NetAddressZ_none());
                assert(con_res->result_ok);
-               LDK::CResult_NonePeerHandleErrorZ con_res2 = PeerManager_new_inbound_connection(&net2, sock2);
+               LDK::CResult_NonePeerHandleErrorZ con_res2 = PeerManager_new_inbound_connection(&net2, sock2, COption_NetAddressZ_none());
                assert(con_res2->result_ok);
 
                auto writelen = write(pipefds_1_to_2[1], con_res->contents.result->data, con_res->contents.result->datalen);
@@ -357,6 +371,47 @@ public:
 };
 #endif // !REAL_NET
 
+struct CustomOnionMsgQueue {
+       std::mutex mtx;
+       std::vector<LDK::CustomOnionMessageContents> msgs;
+};
+
+uint64_t custom_onion_msg_type_id(const void *this_arg) {
+       return 8888;
+}
+LDKCVec_u8Z custom_onion_msg_bytes(const void *this_arg) {
+       uint8_t *bytes = (uint8_t *) malloc(1024);
+       memset(bytes, 43, 1024);
+       return LDKCVec_u8Z {
+               .data = bytes, .datalen = 1024
+       };
+}
+
+void handle_custom_onion_message(const void* this_arg, struct LDKCustomOnionMessageContents msg) {
+       CustomOnionMsgQueue* arg = (CustomOnionMsgQueue*) this_arg;
+       std::unique_lock<std::mutex> lck(arg->mtx);
+       arg->msgs.push_back(std::move(msg));
+}
+
+LDKCustomOnionMessageContents build_custom_onion_message() {
+       return LDKCustomOnionMessageContents {
+               .this_arg = NULL,
+               .tlv_type = custom_onion_msg_type_id,
+               .write = custom_onion_msg_bytes,
+               .free = NULL,
+       };
+}
+
+LDKCResult_COption_CustomOnionMessageContentsZDecodeErrorZ read_custom_onion_message(const void* this_arg, uint64_t type, LDKu8slice buf) {
+       assert(type == 8888);
+       assert(buf.datalen == 1024);
+       uint8_t cmp[1024];
+       memset(cmp, 43, 1024);
+       assert(!memcmp(cmp, buf.data, 1024));
+       return CResult_COption_CustomOnionMessageContentsZDecodeErrorZ_ok(COption_CustomOnionMessageContentsZ_some(build_custom_onion_message()));
+}
+
+
 struct CustomMsgQueue {
        std::vector<LDK::Type> msgs;
 };
@@ -419,17 +474,28 @@ LDKCVec_C2Tuple_PublicKeyTypeZZ create_custom_msg(const void* this_arg) {
        return ret;
 }
 
-uint64_t get_chan_score(const void *this_arg, uint64_t scid, const LDKNodeId *src, const LDKNodeId *dst) { return 42; }
+uint64_t get_chan_score(const void *this_arg, uint64_t scid, const LDKNodeId *src, const LDKNodeId *dst, LDKChannelUsage usage_in) {
+       LDK::ChannelUsage usage(std::move(usage_in));
+       return 42;
+}
 
-struct CustomRouteFinderParams {
-       LDKLogger *logger;
-       LDKNetworkGraph *graph_ref;
-};
-struct LDKCResult_RouteLightningErrorZ custom_find_route(const void *this_arg, struct LDKPublicKey payer, const struct LDKRouteParameters *NONNULL_PTR route_params, struct LDKCVec_ChannelDetailsZ *first_hops, const struct LDKScore *NONNULL_PTR scorer) {
-       const struct CustomRouteFinderParams *params = (struct CustomRouteFinderParams *)this_arg;
+struct LDKCResult_RouteLightningErrorZ custom_find_route(const void *this_arg, struct LDKPublicKey payer, const struct LDKRouteParameters *NONNULL_PTR route_params, struct LDKCVec_ChannelDetailsZ *first_hops, const struct LDKInFlightHtlcs in_flights) {
+       const LDK::DefaultRouter *router = (LDK::DefaultRouter *)this_arg;
        assert(first_hops->datalen == 1);
        assert(ChannelDetails_get_is_usable(&first_hops->data[0]));
-       return find_route(payer, route_params, params->graph_ref, first_hops, *params->logger, scorer);
+       const LDK::Router router_impl = DefaultRouter_as_Router(&*router);
+       return router_impl->find_route(router_impl->this_arg, payer, route_params, first_hops, in_flights);
+}
+
+void custom_notify_payment_path_failed(const void *this_arg, struct LDKCVec_RouteHopZ path, uint64_t short_channel_id) {
+       const LDK::DefaultRouter *router = (LDK::DefaultRouter *)this_arg;
+       const LDK::Router router_impl = DefaultRouter_as_Router(&*router);
+       return router_impl->notify_payment_path_failed(router_impl->this_arg, path, short_channel_id);
+}
+void custom_notify_payment_path_successful(const void *this_arg, struct LDKCVec_RouteHopZ path) {
+       const LDK::DefaultRouter *router = (LDK::DefaultRouter *)this_arg;
+       const LDK::Router router_impl = DefaultRouter_as_Router(&*router);
+       return router_impl->notify_payment_path_successful(router_impl->this_arg, path);
 }
 
 int main() {
@@ -473,8 +539,8 @@ int main() {
                .free = NULL,
        };
 
-       LDK::NetworkGraph net_graph1 = NetworkGraph_new(genesis_hash);
-       LDK::NetGraphMsgHandler graph_msg_handler1 = NetGraphMsgHandler_new(&net_graph1, COption_AccessZ_none(), logger1);
+       LDK::NetworkGraph net_graph1 = NetworkGraph_new(genesis_hash, logger1);
+       LDK::P2PGossipSync graph_msg_handler1 = P2PGossipSync_new(&net_graph1, COption_AccessZ_none(), logger1);
        LDKSecretKey node_secret1;
 
        LDKLogger logger2 {
@@ -493,8 +559,8 @@ int main() {
                .free = NULL,
        };
 
-       LDK::NetworkGraph net_graph2 = NetworkGraph_new(genesis_hash);
-       LDK::NetGraphMsgHandler graph_msg_handler2 = NetGraphMsgHandler_new(&net_graph2, COption_AccessZ_none(), logger2);
+       LDK::NetworkGraph net_graph2 = NetworkGraph_new(genesis_hash, logger2);
+       LDK::P2PGossipSync graph_msg_handler2 = P2PGossipSync_new(&net_graph2, COption_AccessZ_none(), logger2);
        LDKSecretKey node_secret2;
 
        LDK::CVec_u8Z cm1_ser = LDKCVec_u8Z {}; // ChannelManager 1 serialization at the end of the ser-des scope
@@ -506,23 +572,30 @@ int main() {
                memset(&node_seed, 0, 32);
                LDK::KeysManager keys1 = KeysManager_new(&node_seed, 0, 0);
                LDK::KeysInterface keys_source1 = KeysManager_as_KeysInterface(&keys1);
-               node_secret1 = keys_source1->get_node_secret(keys_source1->this_arg);
+               LDK::CResult_SecretKeyNoneZ node_secret1_res = keys_source1->get_node_secret(keys_source1->this_arg, LDKRecipient_Node);
+               assert(node_secret1_res->result_ok);
+               node_secret1 = *node_secret1_res->contents.result;
 
                LDK::ChannelManager cm1 = ChannelManager_new(fee_est, mon1, broadcast, logger1, KeysManager_as_KeysInterface(&keys1), UserConfig_default(), ChainParameters_new(network, BestBlock_new(chain_tip, 0)));
 
+               LDK::IgnoringMessageHandler ignoring_handler1 = IgnoringMessageHandler_new();
+               LDK::CustomMessageHandler custom_msg_handler1 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler1);
+               LDK::CustomOnionMessageHandler custom_onion_msg_handler1 = IgnoringMessageHandler_as_CustomOnionMessageHandler(&ignoring_handler1);
+
+               LDK::OnionMessenger om1 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys1), logger1, std::move(custom_onion_msg_handler1));
+
                LDK::CVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm1);
                assert(channels->datalen == 0);
 
-               LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), NetGraphMsgHandler_as_RoutingMessageHandler(&graph_msg_handler1));
-
-               LDK::IgnoringMessageHandler ignoring_handler1 = IgnoringMessageHandler_new();
-               LDK::CustomMessageHandler custom_msg_handler1 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler1);
+               LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), P2PGossipSync_as_RoutingMessageHandler(&graph_msg_handler1), OnionMessenger_as_OnionMessageHandler(&om1));
 
                random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg);
-               LDK::PeerManager net1 = PeerManager_new(std::move(msg_handler1), node_secret1, &random_bytes.data, logger1, std::move(custom_msg_handler1));
+               LDK::PeerManager net1 = PeerManager_new(std::move(msg_handler1), node_secret1, 0xdeadbeef, &random_bytes.data, logger1, std::move(custom_msg_handler1));
 
                // Demo getting a channel key and check that its returning real pubkeys:
-               LDK::Sign chan_signer1 = keys_source1->get_channel_signer(keys_source1->this_arg, false, 42);
+               LDKSixteenBytes user_id_1 { .data = {45, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0} };
+               LDKThirtyTwoBytes chan_signer_id1 = keys_source1->generate_channel_keys_id(keys_source1->this_arg, false, 42, U128_new(user_id_1));
+               LDK::Sign chan_signer1 = keys_source1->derive_channel_signer(keys_source1->this_arg, 42, chan_signer_id1);
                chan_signer1->BaseSign.set_pubkeys(&chan_signer1->BaseSign); // Make sure pubkeys is defined
                LDKPublicKey payment_point = ChannelPublicKeys_get_payment_point(&chan_signer1->BaseSign.pubkeys);
                assert(memcmp(&payment_point, &null_pk, sizeof(null_pk)));
@@ -531,37 +604,42 @@ int main() {
                memset(&node_seed, 1, 32);
                LDK::KeysManager keys2 = KeysManager_new(&node_seed, 0, 0);
                LDK::KeysInterface keys_source2 = KeysManager_as_KeysInterface(&keys2);
-               node_secret2 = keys_source2->get_node_secret(keys_source2->this_arg);
+               LDK::CResult_SecretKeyNoneZ node_secret2_res = keys_source2->get_node_secret(keys_source2->this_arg, LDKRecipient_Node);
+               assert(node_secret2_res->result_ok);
+               node_secret2 = *node_secret2_res->contents.result;
 
                LDK::ChannelHandshakeConfig handshake_config2 = ChannelHandshakeConfig_default();
                ChannelHandshakeConfig_set_minimum_depth(&handshake_config2, 2);
                LDK::UserConfig config2 = UserConfig_default();
-               UserConfig_set_own_channel_config(&config2, std::move(handshake_config2));
+               UserConfig_set_channel_handshake_config(&config2, std::move(handshake_config2));
 
                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)));
 
+               LDK::IgnoringMessageHandler ignoring_handler2 = IgnoringMessageHandler_new();
+               LDK::CustomMessageHandler custom_msg_handler2 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler2);
+               LDK::CustomOnionMessageHandler custom_onion_msg_handler2 = IgnoringMessageHandler_as_CustomOnionMessageHandler(&ignoring_handler2);
+
+               LDK::OnionMessenger om2 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys2), logger2, std::move(custom_onion_msg_handler2));
+
                LDK::CVec_ChannelDetailsZ channels2 = ChannelManager_list_channels(&cm2);
                assert(channels2->datalen == 0);
 
-               LDK::RoutingMessageHandler net_msgs2 = NetGraphMsgHandler_as_RoutingMessageHandler(&graph_msg_handler2);
+               LDK::RoutingMessageHandler net_msgs2 = P2PGossipSync_as_RoutingMessageHandler(&graph_msg_handler2);
                LDK::CResult_ChannelAnnouncementDecodeErrorZ chan_ann = ChannelAnnouncement_read(LDKu8slice { .data = valid_node_announcement, .datalen = sizeof(valid_node_announcement) });
                assert(chan_ann->result_ok);
                LDK::CResult_boolLightningErrorZ ann_res = net_msgs2->handle_channel_announcement(net_msgs2->this_arg, chan_ann->contents.result);
                assert(ann_res->result_ok);
 
-               LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), std::move(net_msgs2));
-
-               LDK::IgnoringMessageHandler ignoring_handler2 = IgnoringMessageHandler_new();
-               LDK::CustomMessageHandler custom_msg_handler2 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler2);
+               LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), std::move(net_msgs2), OnionMessenger_as_OnionMessageHandler(&om1));
 
                random_bytes = keys_source2->get_secure_random_bytes(keys_source2->this_arg);
-               LDK::PeerManager net2 = PeerManager_new(std::move(msg_handler2), node_secret2, &random_bytes.data, logger2, std::move(custom_msg_handler2));
+               LDK::PeerManager net2 = PeerManager_new(std::move(msg_handler2), node_secret2, 0xdeadbeef, &random_bytes.data, logger2, std::move(custom_msg_handler2));
 
                // Open a connection!
                PeersConnection conn(cm1, cm2, net1, net2);
 
                // Note that we have to bind the result to a C++ class to make sure it gets free'd
-               LDK::CResult__u832APIErrorZ res = ChannelManager_create_channel(&cm1, ChannelManager_get_our_node_id(&cm2), 40000, 1000, 42, UserConfig_default());
+               LDK::CResult__u832APIErrorZ res = ChannelManager_create_channel(&cm1, ChannelManager_get_our_node_id(&cm2), 40000, 1000, U128_new(user_id_1), UserConfig_default());
                assert(res->result_ok);
                PeerManager_process_events(&net1);
 
@@ -583,20 +661,22 @@ int main() {
                        std::this_thread::yield();
                }
 
-               LDKEventsProvider ev1 = ChannelManager_as_EventsProvider(&cm1);
+               LDK::EventsProvider ev1 = ChannelManager_as_EventsProvider(&cm1);
                while (true) {
                        EventQueue queue;
                        LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
-                       ev1.process_pending_events(ev1.this_arg, handler);
+                       ev1.process_pending_events(handler);
                        if (queue.events.size() == 1) {
                                assert(queue.events[0]->tag == LDKEvent_FundingGenerationReady);
-                               assert(queue.events[0]->funding_generation_ready.user_channel_id == 42);
+                               LDKSixteenBytes event_id = U128_le_bytes(queue.events[0]->funding_generation_ready.user_channel_id);
+                               assert(!memcmp(&event_id, &user_id_1, 16));
                                assert(queue.events[0]->funding_generation_ready.channel_value_satoshis == 40000);
                                assert(queue.events[0]->funding_generation_ready.output_script.datalen == 34);
+
                                assert(!memcmp(queue.events[0]->funding_generation_ready.output_script.data, channel_open_block + 58 + 81, 34));
                                LDKTransaction funding_transaction { .data = const_cast<uint8_t*>(channel_open_block + 81), .datalen = sizeof(channel_open_block) - 81, .data_is_owned = false };
 
-                               LDK::CResult_NoneAPIErrorZ fund_res = ChannelManager_funding_transaction_generated(&cm1, &queue.events[0]->funding_generation_ready.temporary_channel_id.data, funding_transaction);
+                               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);
                                assert(fund_res->result_ok);
                                break;
                        }
@@ -638,6 +718,35 @@ int main() {
                PeerManager_process_events(&net1);
                PeerManager_process_events(&net2);
 
+               // Note that the channel ID is the same as the channel txid reversed as the output index is 0
+               uint8_t expected_chan_id[32];
+               for (int i = 0; i < 32; i++) { expected_chan_id[i] = channel_open_txid[31-i]; }
+
+               LDK::EventsProvider ev2 = ChannelManager_as_EventsProvider(&cm2);
+               while (true) {
+                       EventQueue queue;
+                       LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
+                       ev2.process_pending_events(handler);
+                       if (queue.events.size() == 1) {
+                               assert(queue.events[0]->tag == LDKEvent_ChannelReady);
+                               assert(!memcmp(queue.events[0]->channel_ready.channel_id.data, expected_chan_id, 32));
+                               break;
+                       }
+                       std::this_thread::yield();
+               }
+
+               while (true) {
+                       EventQueue queue;
+                       LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
+                       ev1.process_pending_events(handler);
+                       if (queue.events.size() == 1) {
+                               assert(queue.events[0]->tag == LDKEvent_ChannelReady);
+                               assert(!memcmp(queue.events[0]->channel_ready.channel_id.data, expected_chan_id, 32));
+                               break;
+                       }
+                       std::this_thread::yield();
+               }
+
                // Now send funds from 1 to 2!
                uint64_t channel_scid;
                while (true) {
@@ -645,9 +754,7 @@ int main() {
                        if (outbound_channels->datalen == 1) {
                                const LDKChannelDetails *channel = &outbound_channels->data[0];
                                LDK::ChannelCounterparty counterparty = ChannelDetails_get_counterparty(channel);
-                               // Note that the channel ID is the same as the channel txid reversed as the output index is 0
-                               uint8_t expected_chan_id[32];
-                               for (int i = 0; i < 32; i++) { expected_chan_id[i] = channel_open_txid[31-i]; }
+
                                assert(!memcmp(ChannelDetails_get_channel_id(channel), expected_chan_id, 32));
                                assert(!memcmp(
                                        ChannelCounterparty_get_node_id(&counterparty).compressed_form,
@@ -673,13 +780,13 @@ int main() {
                        .some = 5000,
                };
                LDK::CResult_InvoiceSignOrCreationErrorZ invoice = create_invoice_from_channelmanager(&cm2,
-                       KeysManager_as_KeysInterface(&keys2),
+                       KeysManager_as_KeysInterface(&keys2), logger2,
                        LDKCurrency_Bitcoin, min_value,
                        LDKStr {
                                .chars = (const uint8_t *)"Invoice Description",
                                .len =             strlen("Invoice Description"),
                                .chars_is_owned = false
-                       });
+                       }, 3600);
                assert(invoice->result_ok);
                LDKThirtyTwoBytes payment_hash;
                memcpy(payment_hash.data, Invoice_payment_hash(invoice->contents.result), 32);
@@ -689,12 +796,16 @@ int main() {
                        LDK::Score chan_scorer = LDKScore {
                                .this_arg = NULL, .channel_penalty_msat = get_chan_score, .free = NULL
                        };
-                       LDK::RouteParameters route_params = RouteParameters_new(Payee_new(
+                       LDK::RouteParameters route_params = RouteParameters_new(PaymentParameters_new(
                                        ChannelManager_get_our_node_id(&cm2), LDKInvoiceFeatures {
                                                .inner = NULL, .is_owned = false
-                                       }, Invoice_route_hints(invoice->contents.result), COption_u64Z_none()),
+                                       }, Invoice_route_hints(invoice->contents.result), COption_u64Z_none(), 0xffffffff,
+                                       1, 2, LDKCVec_u64Z { .data = NULL, .datalen = 0 }),
                                5000, Invoice_min_final_cltv_expiry(invoice->contents.result));
-                       LDK::CResult_RouteLightningErrorZ route = find_route(ChannelManager_get_our_node_id(&cm1), &route_params, &net_graph2, &outbound_channels, logger1, &chan_scorer);
+                       random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg);
+
+                       LDK::CResult_RouteLightningErrorZ route = find_route(ChannelManager_get_our_node_id(&cm1), &route_params, &net_graph2, &outbound_channels, logger1, &chan_scorer, &random_bytes.data);
+
                        assert(route->result_ok);
                        LDK::CVec_CVec_RouteHopZZ paths = Route_get_paths(route->contents.result);
                        assert(paths->datalen == 1);
@@ -704,7 +815,7 @@ int main() {
                        assert(RouteHop_get_short_channel_id(&paths->data[0].data[0]) == channel_scid);
                        LDKThirtyTwoBytes payment_secret;
                        memcpy(payment_secret.data, Invoice_payment_secret(invoice->contents.result), 32);
-                       LDK::CResult_PaymentIdPaymentSendFailureZ send_res = ChannelManager_send_payment(&cm1, route->contents.result, payment_hash, payment_secret);
+                       LDK::CResult_NonePaymentSendFailureZ send_res = ChannelManager_send_payment(&cm1, route->contents.result, payment_hash, payment_secret, payment_hash);
                        assert(send_res->result_ok);
                }
 
@@ -715,7 +826,6 @@ int main() {
                }
 
                // Check that we received the payment!
-               LDK::EventsProvider ev2 = ChannelManager_as_EventsProvider(&cm2);
                while (true) {
                        EventQueue queue;
                        LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
@@ -736,14 +846,21 @@ int main() {
                        LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
                        ev2.process_pending_events(handler);
                        assert(queue.events.size() == 1);
-                       assert(queue.events[0]->tag == LDKEvent_PaymentReceived);
-                       assert(!memcmp(queue.events[0]->payment_received.payment_hash.data, payment_hash.data, 32));
-                       assert(queue.events[0]->payment_received.purpose.tag == LDKPaymentPurpose_InvoicePayment);
-                       assert(!memcmp(queue.events[0]->payment_received.purpose.invoice_payment.payment_secret.data,
+                       assert(queue.events[0]->tag == LDKEvent_PaymentClaimable);
+                       assert(!memcmp(queue.events[0]->payment_claimable.payment_hash.data, payment_hash.data, 32));
+                       assert(queue.events[0]->payment_claimable.purpose.tag == LDKPaymentPurpose_InvoicePayment);
+                       assert(!memcmp(queue.events[0]->payment_claimable.purpose.invoice_payment.payment_secret.data,
                                        Invoice_payment_secret(invoice->contents.result), 32));
-                       assert(queue.events[0]->payment_received.amt == 5000);
-                       memcpy(payment_preimage.data, queue.events[0]->payment_received.purpose.invoice_payment.payment_preimage.data, 32);
-                       assert(ChannelManager_claim_funds(&cm2, payment_preimage));
+                       assert(queue.events[0]->payment_claimable.amount_msat == 5000);
+                       memcpy(payment_preimage.data, queue.events[0]->payment_claimable.purpose.invoice_payment.payment_preimage.data, 32);
+                       ChannelManager_claim_funds(&cm2, payment_preimage);
+
+                       queue.events.clear();
+                       ev2.process_pending_events(handler);
+                       assert(queue.events.size() == 1);
+                       assert(queue.events[0]->tag == LDKEvent_PaymentClaimed);
+                       assert(!memcmp(queue.events[0]->payment_claimed.payment_hash.data, payment_hash.data, 32));
+                       assert(queue.events[0]->payment_claimed.purpose.tag == LDKPaymentPurpose_InvoicePayment);
                }
                PeerManager_process_events(&net2);
                // Wait until we've passed through a full set of monitor updates (ie new preimage + CS/RAA messages)
@@ -753,10 +870,13 @@ int main() {
                {
                        EventQueue queue;
                        LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
-                       ev1.process_pending_events(ev1.this_arg, handler);
-                       assert(queue.events.size() == 1);
+                       while (queue.events.size() < 2)
+                               ev1.process_pending_events(handler);
+                       assert(queue.events.size() == 2);
                        assert(queue.events[0]->tag == LDKEvent_PaymentSent);
                        assert(!memcmp(queue.events[0]->payment_sent.payment_preimage.data, payment_preimage.data, 32));
+                       assert(queue.events[1]->tag == LDKEvent_PaymentPathSuccessful);
+                       assert(!memcmp(queue.events[1]->payment_path_successful.payment_hash.data, payment_hash.data, 32));
                }
 
                conn.stop();
@@ -780,6 +900,14 @@ int main() {
        assert(cm1_read->result_ok);
        LDK::ChannelManager cm1(std::move(cm1_read->contents.result->b));
 
+       LDKCustomOnionMessageHandler custom_onion_msg_handler1 = {
+               .this_arg = NULL,
+               .handle_custom_message = NULL, // We only create custom messages, not handle them
+               .read_custom_message = NULL, // We only create custom messages, not handle them
+               .free = NULL,
+       };
+       LDK::OnionMessenger om1 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys1), logger1, custom_onion_msg_handler1);
+
        LDK::CVec_ChannelMonitorZ mons_list2 = LDKCVec_ChannelMonitorZ { .data = (LDKChannelMonitor*)malloc(sizeof(LDKChannelMonitor)), .datalen = 1 };
        assert(mons2.mons.size() == 1);
        mons_list2->data[0] = *& std::get<1>(mons2.mons[0]); // Note that we need a reference, thus need a raw clone here, which *& does.
@@ -793,14 +921,23 @@ int main() {
        assert(cm2_read->result_ok);
        LDK::ChannelManager cm2(std::move(cm2_read->contents.result->b));
 
+       CustomOnionMsgQueue peer_2_custom_onion_messages;
+       LDKCustomOnionMessageHandler custom_onion_msg_handler2 = {
+               .this_arg = &peer_2_custom_onion_messages,
+               .handle_custom_message = handle_custom_onion_message,
+               .read_custom_message = read_custom_onion_message,
+               .free = NULL,
+       };
+       LDK::OnionMessenger om2 = OnionMessenger_new(KeysManager_as_KeysInterface(&keys2), logger2, custom_onion_msg_handler2);
+
        // Attempt to close the channel...
        uint8_t chan_id[32];
        for (int i = 0; i < 32; i++) { chan_id[i] = channel_open_txid[31-i]; }
-       LDK::CResult_NoneAPIErrorZ close_res = ChannelManager_close_channel(&cm1, &chan_id);
+       LDK::CResult_NoneAPIErrorZ close_res = ChannelManager_close_channel(&cm1, &chan_id, ChannelManager_get_our_node_id(&cm2));
        assert(!close_res->result_ok); // Note that we can't close while disconnected!
 
        // Open a connection!
-       LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), NetGraphMsgHandler_as_RoutingMessageHandler(&graph_msg_handler1));
+       LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), P2PGossipSync_as_RoutingMessageHandler(&graph_msg_handler1), OnionMessenger_as_OnionMessageHandler(&om1));
        random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg);
 
        LDKPublicKey chan_2_node_id = ChannelManager_get_our_node_id(&cm2);
@@ -815,9 +952,9 @@ int main() {
                },
                .free = NULL,
        };
-       LDK::PeerManager net1 = PeerManager_new(std::move(msg_handler1), node_secret1, &random_bytes.data, logger1, std::move(custom_msg_handler1));
+       LDK::PeerManager net1 = PeerManager_new(std::move(msg_handler1), node_secret1, 0xdeadbeef, &random_bytes.data, logger1, std::move(custom_msg_handler1));
 
-       LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), NetGraphMsgHandler_as_RoutingMessageHandler(&graph_msg_handler2));
+       LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), P2PGossipSync_as_RoutingMessageHandler(&graph_msg_handler2), OnionMessenger_as_OnionMessageHandler(&om2));
        CustomMsgQueue peer_2_custom_messages;
        LDKCustomMessageHandler custom_msg_handler2 = {
                .this_arg = &peer_2_custom_messages,
@@ -831,7 +968,7 @@ int main() {
                .free = NULL,
        };
        random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg);
-       LDK::PeerManager net2 = PeerManager_new(std::move(msg_handler2), node_secret2, &random_bytes.data, logger2, std::move(custom_msg_handler2));
+       LDK::PeerManager net2 = PeerManager_new(std::move(msg_handler2), node_secret2, 0xdeadbeef, &random_bytes.data, logger2, std::move(custom_msg_handler2));
 
        PeersConnection conn(cm1, cm2, net1, net2);
 
@@ -844,29 +981,33 @@ int main() {
        }
 
        // Send another payment, this time via the InvoicePayer
-       struct CustomRouteFinderParams router_params = {
-               .logger = &logger1,
-               .graph_ref = &net_graph1,
-       };
+       LDK::ProbabilisticScorer scorer = ProbabilisticScorer_new(ProbabilisticScoringParameters_default(), &net_graph1, logger1);
+       LDK::Score scorer_trait = ProbabilisticScorer_as_Score(&scorer);
+       LDK::MultiThreadedLockableScore scorer_mtx = MultiThreadedLockableScore_new(std::move(scorer_trait));
+       LDK::LockableScore scorer_mtx_trait = MultiThreadedLockableScore_as_LockableScore(&scorer_mtx);
+       const LDK::DefaultRouter router = DefaultRouter_new(&net_graph1, logger1, keys_source1->get_secure_random_bytes(keys_source1->this_arg), std::move(scorer_mtx_trait));
        LDKRouter sending_router = {
-               .this_arg = &router_params,
+               .this_arg = (void*)&router,
                .find_route = custom_find_route,
+               .notify_payment_path_failed = custom_notify_payment_path_failed,
+               .notify_payment_path_successful = custom_notify_payment_path_successful,
+               // We don't probe, so we opt to crash if the probe functions are called.
+               .notify_payment_probe_successful = NULL,
+               .notify_payment_probe_failed = NULL,
                .free = NULL,
        };
-       LDK::Scorer scorer = Scorer_default();
-       LDK::LockableScore scorer_mtx = LockableScore_new(Scorer_as_Score(&scorer));
        EventQueue queue1;
        LDKEventHandler handler1 = { .this_arg = &queue1, .handle_event = handle_event, .free = NULL };
-       LDK::InvoicePayer payer = InvoicePayer_new(ChannelManager_as_Payer(&cm1), sending_router, &scorer_mtx, logger1, handler1, RetryAttempts_new(0));
+       LDK::InvoicePayer payer = InvoicePayer_new(ChannelManager_as_Payer(&cm1), sending_router, logger1, handler1, Retry_attempts(0));
 
        LDK::CResult_InvoiceSignOrCreationErrorZ invoice_res2 = create_invoice_from_channelmanager(&cm2,
-               KeysManager_as_KeysInterface(&keys2),
+               KeysManager_as_KeysInterface(&keys2), logger1,
                LDKCurrency_Bitcoin, COption_u64Z_some(10000),
                LDKStr {
                        .chars = (const uint8_t *)"Invoice 2 Description",
                        .len =             strlen("Invoice 2 Description"),
                        .chars_is_owned = false
-               });
+               }, 3600);
        assert(invoice_res2->result_ok);
        const LDKInvoice *invoice2 = invoice_res2->contents.result;
        LDK::CResult_PaymentIdPaymentErrorZ invoice_pay_res = InvoicePayer_pay_invoice(&payer, invoice2);
@@ -894,20 +1035,28 @@ int main() {
                LDK::EventsProvider ev2 = ChannelManager_as_EventsProvider(&cm2);
                ev2.process_pending_events(handler2);
                if (queue2.events.size() == 1) {
-                       assert(queue2.events[0]->tag == LDKEvent_PaymentReceived);
-                       const struct LDKEvent_LDKPaymentReceived_Body *event_data = &queue2.events[0]->payment_received;
+                       assert(queue2.events[0]->tag == LDKEvent_PaymentClaimable);
+                       const struct LDKEvent_LDKPaymentClaimable_Body *event_data = &queue2.events[0]->payment_claimable;
                        assert(!memcmp(event_data->payment_hash.data, Invoice_payment_hash(invoice2), 32));
                        assert(event_data->purpose.tag == LDKPaymentPurpose_InvoicePayment);
                        assert(!memcmp(event_data->purpose.invoice_payment.payment_secret.data,
                                        Invoice_payment_secret(invoice2), 32));
-                       assert(event_data->amt == 10000);
-                       assert(ChannelManager_claim_funds(&cm2, event_data->purpose.invoice_payment.payment_preimage));
+                       assert(event_data->amount_msat == 10000);
+                       ChannelManager_claim_funds(&cm2, event_data->purpose.invoice_payment.payment_preimage);
+
+                       queue2.events.clear();
+                       ev2.process_pending_events(handler2);
+                       assert(queue2.events.size() == 1);
+                       assert(queue2.events[0]->tag == LDKEvent_PaymentClaimed);
+                       assert(!memcmp(queue2.events[0]->payment_claimed.payment_hash.data, Invoice_payment_hash(invoice2), 32));
+                       assert(queue2.events[0]->payment_claimed.purpose.tag == LDKPaymentPurpose_InvoicePayment);
+
                        break;
                }
                std::this_thread::yield();
        }
 
-       while (queue1.events.size() == 0) {
+       while (queue1.events.size() < 2) {
                PeerManager_process_events(&net2);
                PeerManager_process_events(&net1);
 
@@ -915,12 +1064,13 @@ int main() {
                LDK::EventHandler evh1 = InvoicePayer_as_EventHandler(&payer);
                ev1.process_pending_events(std::move(evh1));
        }
-       assert(queue1.events.size() == 1);
+       assert(queue1.events.size() == 2);
        assert(queue1.events[0]->tag == LDKEvent_PaymentSent);
+       assert(queue1.events[1]->tag == LDKEvent_PaymentPathSuccessful);
 
        // Actually close the channel
        num_txs_broadcasted = 0;
-       close_res = ChannelManager_close_channel(&cm1, &chan_id);
+       close_res = ChannelManager_close_channel(&cm1, &chan_id, ChannelManager_get_our_node_id(&cm2));
        assert(close_res->result_ok);
        PeerManager_process_events(&net1);
        while (num_txs_broadcasted != 2) {
@@ -931,8 +1081,26 @@ int main() {
        LDK::CVec_ChannelDetailsZ chans_after_close2 = ChannelManager_list_channels(&cm2);
        assert(chans_after_close2->datalen == 0);
 
+       assert(OnionMessenger_send_onion_message(&om1,
+                       LDKCVec_PublicKeyZ { .data = NULL, .datalen = 0, },
+                       Destination_node(ChannelManager_get_our_node_id(&cm2)),
+                       LDKOnionMessageContents {
+                               .tag = LDKOnionMessageContents_Custom,
+                               .custom = build_custom_onion_message()
+                       }, LDKBlindedPath { .inner = NULL, .is_owned = true })
+               .result_ok);
+       PeerManager_process_events(&net1);
+       while (true) {
+               std::this_thread::yield();
+               std::unique_lock<std::mutex> lck(peer_2_custom_onion_messages.mtx);
+               if (peer_2_custom_onion_messages.msgs.size() != 0) break;
+       }
+
        conn.stop();
 
+       std::unique_lock<std::mutex> lck(peer_2_custom_onion_messages.mtx);
+       assert(peer_2_custom_onion_messages.msgs.size() == 1);
+       assert(peer_2_custom_onion_messages.msgs[0].tlv_type() == 8888);
        assert(peer_2_custom_messages.msgs.size() != 0);
 
        // Few extra random tests:
@@ -940,5 +1108,5 @@ int main() {
        memset(&sk, 42, 32);
        LDKThirtyTwoBytes kdiv_params;
        memset(&kdiv_params, 43, 32);
-       LDK::InMemorySigner signer = InMemorySigner_new(sk, sk, sk, sk, sk, random_bytes, 42, kdiv_params);
+       LDK::InMemorySigner signer = InMemorySigner_new(sk, sk, sk, sk, sk, sk, random_bytes, 42, kdiv_params);
 }