From 798362a31590a4c5626db086566c083088ef0861 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 2 May 2021 11:08:40 -0700 Subject: [PATCH] Expose bindings versions and print on startup in debug mode Also updates to latest upstream ldk-c-bindings include structure. Co-Authored-By: Arik Sosman --- genbindings.py | 8 +++++--- java_strings.py | 31 ++++++++++++++++++++++++++++++- typescript_strings.py | 3 +-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/genbindings.py b/genbindings.py index 3d5bac97..008084d2 100755 --- a/genbindings.py +++ b/genbindings.py @@ -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('', 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('', 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: diff --git a/java_strings.py b/java_strings.py index eda4086c..1987c10e 100644 --- a/java_strings.py +++ b/java_strings.py @@ -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 ""; + } + 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 #include #include #include @@ -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, "", strlen("")); +} +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; diff --git a/typescript_strings.py b/typescript_strings.py index 4300f1f8..eb14c155 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -97,8 +97,7 @@ public static native long new_empty_slice_vec(); } """ - self.c_file_pfx = """#include -#include "js-wasm.h" + self.c_file_pfx = """#include "js-wasm.h" #include #include -- 2.39.5