1 package org.ldk.batteries;
3 import org.jetbrains.annotations.Nullable;
4 import org.ldk.enums.LDKNetwork;
5 import org.ldk.structs.*;
6 import org.ldk.util.TwoTuple;
10 * A simple utility class which assists in constructing a fresh or deserializing from disk a ChannelManager and one or
11 * more ChannelMonitors.
13 public class ChannelManagerConstructor {
15 * An Exception that indicates the serialized data is invalid and has been corrupted on disk. You should attempt to
16 * restore from a backup if there is one which is known to be current. Otherwise, funds may have been lost.
18 public static class InvalidSerializedDataException extends Exception {}
21 * The ChannelManager either deserialized or newly-constructed.
23 public final ChannelManager channel_manager;
25 * The latest block has the channel manager saw. If this is non-null it is a 32-byte block hash.
26 * You should sync the blockchain starting with the block that builds on this block.
28 public final byte[] channel_manager_latest_block_hash;
30 * A list of ChannelMonitors and the last block they each saw. You should sync the blockchain on each individually
31 * starting with the block that builds on the hash given.
32 * After doing so (and syncing the blockchain on the channel manager as well), you should call chain_sync_completed()
33 * and then continue to normal application operation.
35 public final TwoTuple<ChannelMonitor, byte[]>[] channel_monitors;
37 private final Watch chain_watch;
40 * Deserializes a channel manager and a set of channel monitors from the given serialized copies and interface implementations
42 * @param filter If provided, the outputs which were previously registered to be monitored for will be loaded into the filter.
44 public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] channel_monitors_serialized,
45 KeysInterface keys_interface, FeeEstimator fee_estimator, Watch chain_watch, @Nullable Filter filter,
46 BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
47 final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length];
48 this.channel_monitors = new TwoTuple[monitors.length];
49 for (int i = 0; i < monitors.length; i++) {
50 Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res = UtilMethods.constructor_BlockHashChannelMonitorZ_read(channel_monitors_serialized[i], keys_interface);
51 if (res instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_Err) {
52 throw new InvalidSerializedDataException();
54 monitors[i] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.b;
55 this.channel_monitors[i] = new TwoTuple<>(monitors[i], ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK)res).res.a);
57 Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ res =
58 UtilMethods.constructor_BlockHashChannelManagerZ_read(channel_manager_serialized, keys_interface, fee_estimator, chain_watch, tx_broadcaster,
59 logger, UserConfig.constructor_default(), monitors);
60 if (res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_Err) {
61 throw new InvalidSerializedDataException();
63 this.channel_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.b;
64 this.channel_manager_latest_block_hash = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.a;
65 this.chain_watch = chain_watch;
67 for (ChannelMonitor monitor : monitors) {
68 monitor.load_outputs_to_watch(filter);
74 * Constructs a channel manager from the given interface implementations
76 public ChannelManagerConstructor(LDKNetwork network, UserConfig config, byte[] current_blockchain_tip_hash, int current_blockchain_tip_height,
77 KeysInterface keys_interface, FeeEstimator fee_estimator, Watch chain_watch,
78 BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
79 channel_monitors = new TwoTuple[0];
80 channel_manager_latest_block_hash = null;
81 this.chain_watch = chain_watch;
82 channel_manager = ChannelManager.constructor_new(fee_estimator, chain_watch, tx_broadcaster, logger, keys_interface, config, network, current_blockchain_tip_hash, current_blockchain_tip_height);
86 * Utility which adds all of the deserialized ChannelMonitors to the chain watch so that further updates from the
87 * ChannelManager are processed as normal.
89 public void chain_sync_completed() {
90 for (TwoTuple<ChannelMonitor, byte[]> monitor: channel_monitors) {
91 this.chain_watch.watch_channel(monitor.a.get_funding_txo().a, monitor.a);