]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Update C and C++ demos to latest API and test custom messages
authorMatt Corallo <git@bluematt.me>
Thu, 23 Sep 2021 02:57:30 +0000 (02:57 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 24 Sep 2021 18:42:40 +0000 (18:42 +0000)
lightning-c-bindings/demo.c
lightning-c-bindings/demo.cpp

index a9556303fa3d1a0e826fbcff69625a38f8124b03..e8a0997363b9291cfce627339c6dba1116950b5a 100644 (file)
@@ -35,7 +35,7 @@ LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
        return empty_htlc_vec;
 }
 
-void never_handle_event(const void *this_arg, struct LDKEvent event) {
+void never_handle_event(const void *this_arg, const struct LDKEvent* event) {
        // Note that we never actually generate any events to handle in the code below.
        assert(false);
 }
index 1412f2b11b42441b44e279c11c244a830ed62c3f..e7b596b0199a6e72a36f50c37ddcf9b45192c25d 100644 (file)
@@ -194,9 +194,9 @@ LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
 struct EventQueue {
        std::vector<LDK::Event> events;
 };
-void handle_event(const void *this_arg, LDKEvent event) {
+void handle_event(const void *this_arg, const LDKEvent *event) {
        EventQueue* arg = (EventQueue*) this_arg;
-       arg->events.push_back(std::move(event));
+       arg->events.push_back(Event_clone(event));
 }
 
 #ifdef REAL_NET
@@ -354,6 +354,68 @@ public:
 };
 #endif // !REAL_NET
 
+struct CustomMsgQueue {
+       std::vector<LDK::Type> msgs;
+};
+
+uint16_t custom_msg_type_id(const void *this_arg) {
+       return 8888;
+}
+LDKCVec_u8Z custom_msg_bytes(const void *this_arg) {
+       uint8_t *bytes = (uint8_t *) malloc(1024);
+       memset(bytes, 42, 1024);
+       return LDKCVec_u8Z {
+               .data = bytes, .datalen = 1024
+       };
+}
+LDKStr custom_msg_debug(const void *this_arg) {
+       return LDKStr {
+               .chars = NULL, .len = 0, .chars_is_owned = false
+       };
+}
+
+LDKCResult_COption_TypeZDecodeErrorZ read_custom_message(const void* this_arg, uint16_t type_id, LDKu8slice buf) {
+       assert(type_id == 8888);
+       assert(buf.datalen == 1024);
+       uint8_t cmp[1024];
+       memset(cmp, 42, 1024);
+       assert(!memcmp(cmp, buf.data, 1024));
+       return CResult_COption_TypeZDecodeErrorZ_ok(COption_TypeZ_some(LDKType {
+               .this_arg = NULL,
+               .type_id = custom_msg_type_id,
+               .debug_str = custom_msg_debug,
+               .free = NULL,
+       }));
+}
+
+LDKCResult_NoneLightningErrorZ handle_custom_message(const void* this_arg, struct LDKType msg, struct LDKPublicKey _sender_node_id) {
+       CustomMsgQueue* arg = (CustomMsgQueue*) this_arg;
+       arg->msgs.push_back(std::move(msg));
+       return CResult_NoneLightningErrorZ_ok();
+}
+LDKCVec_C2Tuple_PublicKeyTypeZZ never_send_custom_msgs(const void* this_arg) {
+       return LDKCVec_C2Tuple_PublicKeyTypeZZ {
+               .data = NULL, .datalen = 0
+       };
+}
+
+LDKCVec_C2Tuple_PublicKeyTypeZZ create_custom_msg(const void* this_arg) {
+       const LDKPublicKey *counterparty_node_id = (const LDKPublicKey *)this_arg;
+       LDKCVec_C2Tuple_PublicKeyTypeZZ ret = {
+               .data = ((LDKC2Tuple_PublicKeyTypeZ*)malloc(sizeof(LDKC2Tuple_PublicKeyTypeZ))),
+               .datalen = 1
+       };
+       ret.data[0].a = *counterparty_node_id;
+       ret.data[0].b = LDKType {
+               .this_arg = NULL,
+               .type_id = custom_msg_type_id,
+               .debug_str = custom_msg_debug,
+               .write = custom_msg_bytes,
+               .free = NULL,
+       };
+       return ret;
+}
+
 int main() {
        uint8_t channel_open_header[80];
        uint8_t header_1[80];
@@ -395,7 +457,7 @@ int main() {
                .free = NULL,
        };
 
-       LDK::NetGraphMsgHandler net_graph1 = NetGraphMsgHandler_new(genesis_hash, NULL, logger1);
+       LDK::NetGraphMsgHandler net_graph1 = NetGraphMsgHandler_new(NetworkGraph_new(genesis_hash), COption_AccessZ_none(), logger1);
        LDKSecretKey node_secret1;
 
        LDKLogger logger2 {
@@ -414,7 +476,7 @@ int main() {
                .free = NULL,
        };
 
-       LDK::NetGraphMsgHandler net_graph2 = NetGraphMsgHandler_new(genesis_hash, NULL, logger2);
+       LDK::NetGraphMsgHandler net_graph2 = NetGraphMsgHandler_new(NetworkGraph_new(genesis_hash), 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
@@ -435,8 +497,11 @@ int main() {
 
                LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), NetGraphMsgHandler_as_RoutingMessageHandler(&net_graph1));
 
+               LDK::IgnoringMessageHandler ignoring_handler1 = IgnoringMessageHandler_new();
+               LDK::CustomMessageHandler custom_msg_handler1 = IgnoringMessageHandler_as_CustomMessageHandler(&ignoring_handler1);
+
                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);
+               LDK::PeerManager net1 = PeerManager_new(std::move(msg_handler1), node_secret1, &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);
@@ -468,8 +533,11 @@ int main() {
 
                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);
+
                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);
+               LDK::PeerManager net2 = PeerManager_new(std::move(msg_handler2), node_secret2, &random_bytes.data, logger2, std::move(custom_msg_handler2));
 
                // Open a connection!
                PeersConnection conn(cm1, cm2, net1, net2);
@@ -600,8 +668,7 @@ int main() {
 
                {
                        LDK::CVec_ChannelDetailsZ outbound_channels = ChannelManager_list_usable_channels(&cm1);
-                       LDK::LockedNetworkGraph graph_2_locked = NetGraphMsgHandler_read_locked_graph(&net_graph2);
-                       LDK::NetworkGraph graph_2_ref = LockedNetworkGraph_graph(&graph_2_locked);
+                       LDK::NetworkGraph graph_2_ref = NetGraphMsgHandler_get_network_graph(&net_graph2);
                        LDK::CResult_RouteLightningErrorZ route = get_route(ChannelManager_get_our_node_id(&cm1), &graph_2_ref, ChannelManager_get_our_node_id(&cm2), LDKInvoiceFeatures {
                                        .inner = NULL, .is_owned = false
                                }, &outbound_channels, Invoice_route_hints(invoice->contents.result),
@@ -711,11 +778,36 @@ int main() {
        // Open a connection!
        LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), NetGraphMsgHandler_as_RoutingMessageHandler(&net_graph1));
        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);
+
+       LDKPublicKey chan_2_node_id = ChannelManager_get_our_node_id(&cm2);
+       LDKCustomMessageHandler custom_msg_handler1 = {
+               .this_arg = &chan_2_node_id,
+               .handle_custom_message = NULL, // We only create custom messages, not handle them
+               .get_and_clear_pending_msg = create_custom_msg,
+               .CustomMessageReader = LDKCustomMessageReader {
+                       .this_arg = NULL,
+                       .read = read_custom_message,
+                       .free = NULL,
+               },
+               .free = NULL,
+       };
+       LDK::PeerManager net1 = PeerManager_new(std::move(msg_handler1), node_secret1, &random_bytes.data, logger1, std::move(custom_msg_handler1));
 
        LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), NetGraphMsgHandler_as_RoutingMessageHandler(&net_graph2));
+       CustomMsgQueue peer_2_custom_messages;
+       LDKCustomMessageHandler custom_msg_handler2 = {
+               .this_arg = &peer_2_custom_messages,
+               .handle_custom_message = handle_custom_message,
+               .get_and_clear_pending_msg = never_send_custom_msgs,
+               .CustomMessageReader = LDKCustomMessageReader {
+                       .this_arg = NULL,
+                       .read = read_custom_message,
+                       .free = NULL,
+               },
+               .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);
+       LDK::PeerManager net2 = PeerManager_new(std::move(msg_handler2), node_secret2, &random_bytes.data, logger2, std::move(custom_msg_handler2));
 
        PeersConnection conn(cm1, cm2, net1, net2);
 
@@ -742,6 +834,8 @@ int main() {
 
        conn.stop();
 
+       assert(peer_2_custom_messages.msgs.size() != 0);
+
        // Few extra random tests:
        LDKSecretKey sk;
        memset(&sk, 42, 32);