Expose bindings versions and print on startup in debug mode
authorMatt Corallo <git@bluematt.me>
Sun, 2 May 2021 18:08:40 +0000 (11:08 -0700)
committerMatt Corallo <git@bluematt.me>
Mon, 3 May 2021 02:43:51 +0000 (02:43 +0000)
Also updates to latest upstream ldk-c-bindings include structure.

Co-Authored-By: Arik Sosman <git@arik.io>
genbindings.py
java_strings.py
typescript_strings.py

index 3d5bac974c5b563cd50d1d84dfe3d6d2b5199f59..008084d29796f47cc0370b199d09107ae6f269ad 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/env python3
-import sys, re
+import sys, re, subprocess
 
 if len(sys.argv) < 7:
     print("USAGE: /path/to/lightning.h /path/to/bindings/output /path/to/bindings/ /path/to/bindings/output.c debug lang")
@@ -33,6 +33,8 @@ else:
 
 consts = Consts(DEBUG, target=target)
 
+local_git_version = subprocess.check_output(["git", "describe", '--tag', '--dirty']).decode("utf-8").strip()
+
 from bindingstypes import *
 
 c_file = ""
@@ -709,7 +711,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
                 write_c("\treturn tuple->" + e + ";\n")
             write_c("}\n")
 
-    out_java.write(consts.bindings_header)
+    out_java.write(consts.bindings_header.replace('<git_version_ldk_garbagecollected>', local_git_version))
 
     with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "w") as out_java_struct:
         out_java_struct.write(consts.common_base)
@@ -968,7 +970,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
             out_java_struct.write("}\n")
 
 with open(sys.argv[4], "w") as out_c:
-    out_c.write(consts.c_file_pfx)
+    out_c.write(consts.c_file_pfx.replace('<git_version_ldk_garbagecollected>', local_git_version))
     out_c.write(consts.init_str())
     out_c.write(c_file)
 with open(f"{sys.argv[3]}/structs/UtilMethods{consts.file_ext}", "a") as util:
index eda4086cd92b2f4c08048bccb764a0316971ab9a..1987c10e58db6d3915cdf7e2ba7f8fe1aafb1a2f 100644 (file)
@@ -37,9 +37,21 @@ public class bindings {
                System.loadLibrary(\"lightningjni\");
                init(java.lang.Enum.class, VecOrSliceDef.class);
                init_class_cache();
+               if (!get_lib_version_string().equals(get_ldk_java_bindings_version()))
+                       throw new IllegalArgumentException("Compiled LDK library and LDK class failes do not match");
+               // Fetching the LDK versions from C also checks that the header and binaries match
+               get_ldk_c_bindings_version();
+               get_ldk_version();
        }
        static native void init(java.lang.Class c, java.lang.Class slicedef);
        static native void init_class_cache();
+       static native String get_lib_version_string();
+
+       public static String get_ldk_java_bindings_version() {
+               return "<git_version_ldk_garbagecollected>";
+       }
+       public static native String get_ldk_c_bindings_version();
+       public static native String get_ldk_version();
 
        public static native boolean deref_bool(long ptr);
        public static native long deref_long(long ptr);
@@ -76,7 +88,6 @@ class CommonBase {
 """
 
         self.c_file_pfx = """#include \"org_ldk_impl_bindings.h\"
-#include <rust_types.h>
 #include <lightning.h>
 #include <string.h>
 #include <stdatomic.h>
@@ -126,6 +137,14 @@ void __attribute__((constructor)) spawn_stderr_redirection() {
 // Assert a is true or do nothing
 #define CHECK(a) DO_ASSERT(a)
 
+void __attribute__((constructor)) debug_log_version() {
+       if (check_get_ldk_version() == NULL)
+               DEBUG_PRINT("LDK version did not match the header we built against\\n");
+       if (check_get_ldk_bindings_version() == NULL)
+               DEBUG_PRINT("LDK C Bindings version did not match the header we built against\\n");
+       DEBUG_PRINT("Loaded LDK-Java Bindings with LDK %s and LDK-C-Bindings %s\\n", check_get_ldk_version(), check_get_ldk_bindings_version());
+}
+
 // Running a leak check across all the allocations and frees of the JDK is a mess,
 // so instead we implement our own naive leak checker here, relying on the -wrap
 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
@@ -405,6 +424,16 @@ static inline LDKStr java_to_owned_str(JNIEnv *env, jstring str) {
        };
        return res;
 }
+
+JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1lib_1version_1string(JNIEnv *env, jclass _c) {
+       return str_ref_to_java(env, "<git_version_ldk_garbagecollected>", strlen("<git_version_ldk_garbagecollected>"));
+}
+JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1c_1bindings_1version(JNIEnv *env, jclass _c) {
+       return str_ref_to_java(env, check_get_ldk_bindings_version(), strlen(check_get_ldk_bindings_version()));
+}
+JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1version(JNIEnv *env, jclass _c) {
+       return str_ref_to_java(env, check_get_ldk_version(), strlen(check_get_ldk_version()));
+}
 """
 
         self.hu_struct_file_prefix = """package org.ldk.structs;
index 4300f1f80064d3bcba2fadf9cce07fef41f3e471..eb14c1554a70e2bf5d4fdcc544249438442e9686 100644 (file)
@@ -97,8 +97,7 @@ public static native long new_empty_slice_vec();
             }
 """
 
-        self.c_file_pfx = """#include <rust_types.h>
-#include "js-wasm.h"
+        self.c_file_pfx = """#include "js-wasm.h"
 #include <stdatomic.h>
 #include <lightning.h>