Merge pull request #16 from TheBlueMatt/2021-03-background-manager-persister
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Wed, 31 Mar 2021 20:11:43 +0000 (20:11 +0000)
committerGitHub <noreply@github.com>
Wed, 31 Mar 2021 20:11:43 +0000 (20:11 +0000)
Adapt ChannelManagerConstructor to persist ChannelManager + handle events

12 files changed:
.github/workflows/build.yml
liblightningjni_debug.so
liblightningjni_release.so
src/main/java/org/ldk/batteries/ChannelManagerConstructor.java
src/main/java/org/ldk/impl/bindings.java
src/main/java/org/ldk/structs/ChannelManager.java
src/main/jni/bindings.c
src/main/jni/org_ldk_impl_bindings.h
src/test/java/org/ldk/HumanObjectPeerTest.java
ts/bindings.c
ts/bindings.ts
ts/structs/ChannelManager.ts

index 3b862702186fa0b98e6103fe58e04303e77cd3cd..d1385ba6330a6e0993d54b413525f686ffaba016 100644 (file)
@@ -30,7 +30,7 @@ jobs:
           cd ..
           git clone https://github.com/lightningdevkit/ldk-c-bindings
       - name: Rebuild C bindings, and check the sample app builds + links
-        run: cd ldk-c-bindings && ./genbindings.sh ../rust-lightning && cd ..
+        run: cd ldk-c-bindings && ./genbindings.sh ../rust-lightning true && cd ..
       - name: Build Java/TS Debug Bindings
         run: ./genbindings.sh ./ldk-c-bindings/ "-I/usr/lib/jvm/java-11-openjdk-amd64/include/ -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux/" true false
       - name: Run Java Tests against Debug Bindings
index 3875f1171e888ce78dc5c4f26e84efc4905ed62e..15cc64dba99660572305d0435bc98fecf3e4db52 100755 (executable)
Binary files a/liblightningjni_debug.so and b/liblightningjni_debug.so differ
index b90244bb8f05f28cee02eda9baf229b31136847e..be4d3c928028d804535576fa9f747c7047f2d47a 100755 (executable)
Binary files a/liblightningjni_release.so and b/liblightningjni_release.so differ
index 0d2055b21a860c478ee4fee2f93a150aeb0bb6b7..43bea0559b5bfeb045c66361b0916f08e35f390c 100644 (file)
@@ -34,7 +34,7 @@ public class ChannelManagerConstructor {
      */
     public final TwoTuple<ChannelMonitor, byte[]>[] channel_monitors;
 
-    private final Watch chain_watch;
+    private final ChainMonitor chain_monitor;
 
     /**
      * Deserializes a channel manager and a set of channel monitors from the given serialized copies and interface implementations
@@ -44,7 +44,7 @@ public class ChannelManagerConstructor {
      *               outputs will be loaded when chain_sync_completed is called.
      */
     public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] channel_monitors_serialized,
-                                     KeysInterface keys_interface, FeeEstimator fee_estimator, Watch chain_watch, @Nullable Filter filter,
+                                     KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor, @Nullable Filter filter,
                                      BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
         final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length];
         this.channel_monitors = new TwoTuple[monitors.length];
@@ -57,14 +57,14 @@ public class ChannelManagerConstructor {
             this.channel_monitors[i] = new TwoTuple<>(monitors[i], ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK)res).res.a);
         }
         Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ res =
-                UtilMethods.constructor_BlockHashChannelManagerZ_read(channel_manager_serialized, keys_interface, fee_estimator, chain_watch, tx_broadcaster,
+                UtilMethods.constructor_BlockHashChannelManagerZ_read(channel_manager_serialized, keys_interface, fee_estimator, chain_monitor.as_Watch(), tx_broadcaster,
                         logger, UserConfig.constructor_default(), monitors);
         if (res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_Err) {
             throw new InvalidSerializedDataException();
         }
         this.channel_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.b;
         this.channel_manager_latest_block_hash = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.a;
-        this.chain_watch = chain_watch;
+        this.chain_monitor = chain_monitor;
         if (filter != null) {
             for (ChannelMonitor monitor : monitors) {
                 monitor.load_outputs_to_watch(filter);
@@ -76,21 +76,74 @@ public class ChannelManagerConstructor {
      * Constructs a channel manager from the given interface implementations
      */
     public ChannelManagerConstructor(LDKNetwork network, UserConfig config, byte[] current_blockchain_tip_hash, int current_blockchain_tip_height,
-                                     KeysInterface keys_interface, FeeEstimator fee_estimator, Watch chain_watch,
+                                     KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor,
                                      BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
         channel_monitors = new TwoTuple[0];
         channel_manager_latest_block_hash = null;
-        this.chain_watch = chain_watch;
-        channel_manager = ChannelManager.constructor_new(fee_estimator, chain_watch, tx_broadcaster, logger, keys_interface, config, network, current_blockchain_tip_hash, current_blockchain_tip_height);
+        this.chain_monitor = chain_monitor;
+        channel_manager = ChannelManager.constructor_new(fee_estimator, chain_monitor.as_Watch(), tx_broadcaster, logger, keys_interface, config, network, current_blockchain_tip_hash, current_blockchain_tip_height);
     }
 
+    /**
+     * Abstract interface which should handle Events and persist the ChannelManager. When you call chain_sync_completed
+     * a background thread is started which will automatically call these methods for you when events occur.
+     */
+    public interface ChannelManagerPersister {
+        void handle_events(Event[] events);
+        void persist_manager(byte[] channel_manager_bytes);
+    }
+
+    Thread persister_thread = null;
+    volatile boolean shutdown = false;
+
     /**
      * Utility which adds all of the deserialized ChannelMonitors to the chain watch so that further updates from the
      * ChannelManager are processed as normal.
+     *
+     * This also spawns a background thread which will call the appropriate methods on the provided
+     * ChannelManagerPersister as required.
      */
-    public void chain_sync_completed() {
+    public void chain_sync_completed(ChannelManagerPersister persister) {
+        if (persister_thread != null) { return; }
         for (TwoTuple<ChannelMonitor, byte[]> monitor: channel_monitors) {
-            this.chain_watch.watch_channel(monitor.a.get_funding_txo().a, monitor.a);
+            this.chain_monitor.as_Watch().watch_channel(monitor.a.get_funding_txo().a, monitor.a);
         }
+        persister_thread = new Thread(() -> {
+            long lastTimerTick = System.currentTimeMillis();
+            while (true) {
+                boolean need_persist = this.channel_manager.await_persistable_update_timeout(1);
+                Event[] events = this.channel_manager.as_EventsProvider().get_and_clear_pending_events();
+                if (events.length != 0) {
+                    persister.handle_events(events);
+                    need_persist = true;
+                }
+                events = this.chain_monitor.as_EventsProvider().get_and_clear_pending_events();
+                if (events.length != 0) {
+                    persister.handle_events(events);
+                    need_persist = true;
+                }
+                if (need_persist) {
+                    persister.persist_manager(this.channel_manager.write());
+                }
+                if (shutdown) {
+                    return;
+                }
+                if (lastTimerTick < System.currentTimeMillis() - 60 * 1000) {
+                    this.channel_manager.timer_chan_freshness_every_min();
+                    lastTimerTick = System.currentTimeMillis();
+                }
+            }
+        }, "NioPeerHandler NIO Thread");
+        persister_thread.start();
+    }
+
+    /**
+     * Interrupt the background thread, stopping the background handling of
+     */
+    public void interrupt() {
+        shutdown = true;
+        try {
+            persister_thread.join();
+        } catch (InterruptedException ignored) { }
     }
 }
index 47752cfb8c392e3b95cc2448721288412847af15..5a4dc21e7f2580d8fef147b95df73f398714757b 100644 (file)
@@ -2163,6 +2163,8 @@ public class bindings {
        public static native void ChannelManager_block_connected(long this_arg, byte[] header, long[] txdata, int height);
        // void ChannelManager_block_disconnected(const struct LDKChannelManager *NONNULL_PTR this_arg, const uint8_t (*header)[80]);
        public static native void ChannelManager_block_disconnected(long this_arg, byte[] header);
+       // MUST_USE_RES bool ChannelManager_await_persistable_update_timeout(const struct LDKChannelManager *NONNULL_PTR this_arg, uint64_t max_wait);
+       public static native boolean ChannelManager_await_persistable_update_timeout(long this_arg, long max_wait);
        // void ChannelManager_await_persistable_update(const struct LDKChannelManager *NONNULL_PTR this_arg);
        public static native void ChannelManager_await_persistable_update(long this_arg);
        // struct LDKChannelMessageHandler ChannelManager_as_ChannelMessageHandler(const struct LDKChannelManager *NONNULL_PTR this_arg);
index c5afb1964e181236eeaf01bd593594c63ff5f053..d0de49a291672f50455df9016f68909808a12c49 100644 (file)
@@ -394,6 +394,18 @@ public class ChannelManager extends CommonBase {
                bindings.ChannelManager_block_disconnected(this.ptr, header);
        }
 
+       /**
+        * Blocks until ChannelManager needs to be persisted or a timeout is reached. It returns a bool
+        * indicating whether persistence is necessary. Only one listener on
+        * `await_persistable_update` or `await_persistable_update_timeout` is guaranteed to be woken
+        * up.
+        * Note that the feature `allow_wallclock_use` must be enabled to use this function.
+        */
+       public boolean await_persistable_update_timeout(long max_wait) {
+               boolean ret = bindings.ChannelManager_await_persistable_update_timeout(this.ptr, max_wait);
+               return ret;
+       }
+
        /**
         * Blocks until ChannelManager needs to be persisted. Only one listener on
         * `await_persistable_update` or `await_persistable_update_timeout` is guaranteed to be woken
index 156170495d3dfd9f9ffb356ce059f9cae5411b86..221801aaa5e2ba622175f3c022d25a51e5ca8cc8 100644 (file)
@@ -12722,6 +12722,14 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManager_1block_1disconn
        ChannelManager_block_disconnected(&this_arg_conv, header_ref);
 }
 
+JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_ChannelManager_1await_1persistable_1update_1timeout(JNIEnv *env, jclass clz, int64_t this_arg, int64_t max_wait) {
+       LDKChannelManager this_arg_conv;
+       this_arg_conv.inner = (void*)(this_arg & (~1));
+       this_arg_conv.is_owned = false;
+       jboolean ret_val = ChannelManager_await_persistable_update_timeout(&this_arg_conv, max_wait);
+       return ret_val;
+}
+
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManager_1await_1persistable_1update(JNIEnv *env, jclass clz, int64_t this_arg) {
        LDKChannelManager this_arg_conv;
        this_arg_conv.inner = (void*)(this_arg & (~1));
index 7ae607bada4dac7346ea1fffcda5e3da19743db4..b97895f7dcfaae4cf51fe7382515080d43d87762 100644 (file)
@@ -8287,6 +8287,14 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManager_1block_1connect
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManager_1block_1disconnected
   (JNIEnv *, jclass, jlong, jbyteArray);
 
+/*
+ * Class:     org_ldk_impl_bindings
+ * Method:    ChannelManager_await_persistable_update_timeout
+ * Signature: (JJ)Z
+ */
+JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_ChannelManager_1await_1persistable_1update_1timeout
+  (JNIEnv *, jclass, jlong, jlong);
+
 /*
  * Class:     org_ldk_impl_bindings
  * Method:    ChannelManager_await_persistable_update
index acdf4576bca439dd787638addc5f1ceffd57103c..0bde005b66ba08b5c043df58944a486e8e48e095 100644 (file)
@@ -1,10 +1,8 @@
 package org.ldk;
 
 import org.bitcoinj.core.*;
-import org.bitcoinj.core.Transaction;
 import org.bitcoinj.script.Script;
 import org.junit.jupiter.api.Test;
-import org.ldk.batteries.ChannelManagerConstructor;
 import org.ldk.batteries.NioPeerHandler;
 import org.ldk.enums.LDKNetwork;
 import org.ldk.impl.bindings;
@@ -287,38 +285,26 @@ class HumanObjectPeerTestInstance {
         Object ptr_to;
         Peer(Peer orig) {
             this(null, orig.seed);
-            if (!break_cross_peer_refs) {
-                ChannelMonitor[] monitors = new ChannelMonitor[1];
-                synchronized (monitors) {
-                    assert orig.monitors.size() == 1;
+            // TODO: Optionally test ChannelManagerConstructor
+            ChannelMonitor[] monitors = new ChannelMonitor[1];
+            synchronized (monitors) {
+                assert orig.monitors.size() == 1;
+                if (!break_cross_peer_refs) {
                     monitors[0] = orig.monitors.values().stream().iterator().next();
-                }
-                byte[] serialized = orig.chan_manager.write();
-                Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res =
-                        UtilMethods.constructor_BlockHashChannelManagerZ_read(serialized, this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger, UserConfig.constructor_default(), monitors);
-                assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK;
-                this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b;
-                this.chain_watch.watch_channel(monitors[0].get_funding_txo().a, monitors[0]);
-            } else {
-                final ArrayList<byte[]> channel_monitors = new ArrayList();
-                synchronized (monitors) {
-                    assert orig.monitors.size() == 1;
-                    channel_monitors.add(orig.monitors.values().stream().iterator().next().write());
-                }
-                byte[] serialized = orig.chan_manager.write();
-                try {
-                    ChannelManagerConstructor constructed = new ChannelManagerConstructor(serialized, channel_monitors.toArray(new byte[1][]), this.keys_interface, this.fee_estimator, this.chain_watch, this.filter, this.tx_broadcaster, this.logger);
-                    this.chan_manager = constructed.channel_manager;
-                    constructed.chain_sync_completed();
-                    if (use_filter && !use_manual_watch) {
-                        // With a manual watch we don't actually use the filter object at all.
-                        assert this.filter_additions.containsAll(orig.filter_additions) &&
-                                orig.filter_additions.containsAll(this.filter_additions);
-                    }
-                } catch (ChannelManagerConstructor.InvalidSerializedDataException e) {
-                    assert false;
+                } else {
+                    byte[] serialized = orig.monitors.values().stream().iterator().next().write();
+                    Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res =
+                            UtilMethods.constructor_BlockHashChannelMonitorZ_read(serialized, this.keys_interface);
+                    assert res instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
+                    monitors[0] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.b;
                 }
             }
+            byte[] serialized = orig.chan_manager.write();
+            Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res =
+                    UtilMethods.constructor_BlockHashChannelManagerZ_read(serialized, this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger, UserConfig.constructor_default(), monitors);
+            assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK;
+            this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b;
+            this.chain_watch.watch_channel(monitors[0].get_funding_txo().a, monitors[0]);
             if (!break_cross_peer_refs && (use_manual_watch || use_km_wrapper)) {
                 // When we pass monitors[0] into chain_watch.watch_channel we create a reference from the new Peer to a
                 // field in the old peer, preventing freeing of the original Peer until the new Peer is freed. Thus, we
index 0655425c8f11948c8cf187a94f296f39feb115fc..5b6b077430876006d1ff0fc7ece6dd898d27316d 100644 (file)
@@ -11492,6 +11492,14 @@ void  __attribute__((visibility("default"))) TS_ChannelManager_block_disconnecte
        ChannelManager_block_disconnected(&this_arg_conv, header_ref);
 }
 
+jboolean  __attribute__((visibility("default"))) TS_ChannelManager_await_persistable_update_timeout(uint32_t this_arg, int64_t max_wait) {
+       LDKChannelManager this_arg_conv;
+       this_arg_conv.inner = (void*)(this_arg & (~1));
+       this_arg_conv.is_owned = false;
+       jboolean ret_val = ChannelManager_await_persistable_update_timeout(&this_arg_conv, max_wait);
+       return ret_val;
+}
+
 void  __attribute__((visibility("default"))) TS_ChannelManager_await_persistable_update(uint32_t this_arg) {
        LDKChannelManager this_arg_conv;
        this_arg_conv.inner = (void*)(this_arg & (~1));
index 78842cbafb806723c61803d724a4345b512516c4..3a8bfbe90632e3ad68be620b0462efab08df1827 100644 (file)
@@ -6634,6 +6634,14 @@ public static native long new_empty_slice_vec();
                const nativeResponseValue = wasm.ChannelManager_block_disconnected(this_arg, encodeArray(header));
                // debug statements here
        }
+       // MUST_USE_RES bool ChannelManager_await_persistable_update_timeout(const struct LDKChannelManager *NONNULL_PTR this_arg, uint64_t max_wait);
+       export function ChannelManager_await_persistable_update_timeout(this_arg: number, max_wait: number): boolean {
+               if(!isWasmInitialized) {
+                       throw new Error("initializeWasm() must be awaited first!");
+               }
+               const nativeResponseValue = wasm.ChannelManager_await_persistable_update_timeout(this_arg, max_wait);
+               return nativeResponseValue;
+       }
        // void ChannelManager_await_persistable_update(const struct LDKChannelManager *NONNULL_PTR this_arg);
        export function ChannelManager_await_persistable_update(this_arg: number): void {
                if(!isWasmInitialized) {
index 72337ed604cdd530e377c95eee8f5d3b675b19ff..003e029fc0be6def4fa966c684f1a7979638b8bb 100644 (file)
@@ -153,6 +153,11 @@ import * as bindings from '../bindings' // TODO: figure out location
                bindings.ChannelManager_block_disconnected(this.ptr, header);
        }
 
+       public boolean await_persistable_update_timeout(number max_wait) {
+               boolean ret = bindings.ChannelManager_await_persistable_update_timeout(this.ptr, max_wait);
+               return ret;
+       }
+
        public void await_persistable_update() {
                bindings.ChannelManager_await_persistable_update(this.ptr);
        }