Skip to content

Commit

Permalink
Merge pull request #15 from TheBlueMatt/2021-03-fix-clone
Browse files Browse the repository at this point in the history
Fix clone-required detection and handling for complex enums in vecs
  • Loading branch information
TheBlueMatt authored Mar 30, 2021
2 parents c45b91b + 77df45c commit 24971cb
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 111 deletions.
6 changes: 3 additions & 3 deletions gen_type_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,12 @@ def map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, holds_re
to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");\nret_hu_conv.ptrs_to.add(this);",
to_hu_conv_name = "ret_hu_conv",
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")"))
needs_full_clone = not is_free and (not ty_info.is_ptr and not holds_ref or ty_info.requires_clone == True) and ty_info.requires_clone != False
needs_full_clone = not is_free and ((not ty_info.is_ptr and not holds_ref) or ty_info.requires_clone == True) and ty_info.requires_clone != False
if needs_full_clone:
if not ty_info.var_name.startswith("ret") or "res" in ty_info.var_name: # XXX: This is a stupid hack
if "res" in ty_info.var_name: # XXX: This is a stupid hack
needs_full_clone = False
if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
base_conv = base_conv + "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone((" + ty_info.rust_obj + "*)" + ty_info.var_name + ");"
base_conv = base_conv + "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone((" + ty_info.rust_obj + "*)(((uint64_t)" + ty_info.var_name + ") & ~1));"
elif needs_full_clone:
base_conv = base_conv + "\n// Warning: we may need a move here but no clone is available for " + ty_info.rust_obj
if not needs_full_clone and ty_info.rust_obj != "LDKu8slice":
Expand Down
Binary file modified liblightningjni_debug.so
Binary file not shown.
Binary file modified liblightningjni_release.so
Binary file not shown.
8 changes: 1 addition & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
98 changes: 49 additions & 49 deletions src/main/jni/bindings.c

Large diffs are not rendered by default.

32 changes: 29 additions & 3 deletions src/test/java/org/ldk/HumanObjectPeerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public MonitorEvent[] release_pending_monitor_events() {
final Logger logger;
final FeeEstimator fee_estimator;
final BroadcasterInterface tx_broadcaster;
final KeysManager explicit_keys_manager;
final KeysInterface keys_interface;
final ChainMonitor chain_monitor;
final NetGraphMsgHandler router;
Expand Down Expand Up @@ -251,8 +252,10 @@ public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id,
KeysManager keys = KeysManager.constructor_new(key_seed, System.currentTimeMillis() / 1000, (int) (System.currentTimeMillis() * 1000));
if (use_km_wrapper) {
this.keys_interface = manual_keysif(keys.as_KeysInterface());
this.explicit_keys_manager = null;
} else {
this.keys_interface = keys.as_KeysInterface();
this.explicit_keys_manager = keys;
}
this.router = NetGraphMsgHandler.constructor_new(new byte[32], null, logger);
}
Expand Down Expand Up @@ -349,7 +352,7 @@ TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] connect_block(Block b, int height
TwoTuple<Long, byte[]>[] txn;
if (b.hasTransactions()) {
assert b.getTransactions().size() == 1;
TwoTuple<Long, byte[]> txp = new TwoTuple<>((long) 1, b.getTransactions().get(0).bitcoinSerialize());
TwoTuple<Long, byte[]> txp = new TwoTuple<>((long) 0, b.getTransactions().get(0).bitcoinSerialize());
txn = new TwoTuple[]{txp};
} else
txn = new TwoTuple[0];
Expand Down Expand Up @@ -387,7 +390,7 @@ Route get_route(byte[] dest_node, ChannelDetails[] our_chans) {
try (LockedNetworkGraph netgraph = this.router.read_locked_graph()) {
NetworkGraph graph = netgraph.graph();
long res = bindings.get_route(this.node_id, graph._test_only_get_ptr(), dest_node, 0L, new long[]{our_chans[0]._test_only_get_ptr()},
new long[0], 1000, 42, this.logger._test_only_get_ptr());
new long[0], 1000000, 42, this.logger._test_only_get_ptr());
assert bindings.LDKCResult_RouteLightningErrorZ_result_ok(res);
byte[] serialized_route = bindings.Route_write(bindings.LDKCResult_RouteLightningErrorZ_get_ok(res));
must_free_objs.add(new WeakReference<>(serialized_route));
Expand Down Expand Up @@ -665,12 +668,35 @@ void do_test_message_handler_b(TestState state) {
if (watch_outputs != null) { // We only process watch_outputs manually when we use a manually-build Watch impl
assert watch_outputs.length == 1;
assert Arrays.equals(watch_outputs[0].a, tx.getTxId().getReversedBytes());
assert watch_outputs[0].b.length == 1;
assert watch_outputs[0].b.length == 2;
assert watch_outputs[0].b[0].a == 0;
assert watch_outputs[0].b[1].a == 1;
}

for (int i = 11; i < 21; i++) {
b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, new ArrayList<>());
state.peer2.connect_block(b, i, 0);
}

Event[] broadcastable_event = null;
if (state.peer2.chain_monitor != null) {
broadcastable_event = state.peer2.chain_monitor.as_EventsProvider().get_and_clear_pending_events();
}
// This used to be buggy and double-free, so go ahead and fetch them!
for (ChannelMonitor mon : state.peer2.monitors.values()) {
byte[][] txn = mon.get_latest_holder_commitment_txn(state.peer2.logger);
if (state.peer2.chain_monitor == null) {
broadcastable_event = mon.get_and_clear_pending_events();
}
}
assert broadcastable_event.length == 1;
assert broadcastable_event[0] instanceof Event.SpendableOutputs;
if (state.peer2.explicit_keys_manager != null) {
Result_TransactionNoneZ tx_res = state.peer2.explicit_keys_manager.spend_spendable_outputs(((Event.SpendableOutputs) broadcastable_event[0]).outputs, new TxOut[0], new byte[] {0x00}, 253);
assert tx_res instanceof Result_TransactionNoneZ.Result_TransactionNoneZ_OK;
Transaction built_tx = new Transaction(bitcoinj_net, ((Result_TransactionNoneZ.Result_TransactionNoneZ_OK) tx_res).res);
assert built_tx.getOutputs().size() == 1;
assert Arrays.equals(built_tx.getOutput(0).getScriptBytes(), new byte[]{0x00});
}
}

Expand Down
Loading

0 comments on commit 24971cb

Please sign in to comment.