Skip to content

Commit 24971cb

Browse files
authored
Merge pull request #15 from TheBlueMatt/2021-03-fix-clone
Fix clone-required detection and handling for complex enums in vecs
2 parents c45b91b + 77df45c commit 24971cb

File tree

7 files changed

+131
-111
lines changed

7 files changed

+131
-111
lines changed

gen_type_mapping.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,12 @@ def map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, holds_re
296296
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);",
297297
to_hu_conv_name = "ret_hu_conv",
298298
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")"))
299-
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
299+
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
300300
if needs_full_clone:
301-
if not ty_info.var_name.startswith("ret") or "res" in ty_info.var_name: # XXX: This is a stupid hack
301+
if "res" in ty_info.var_name: # XXX: This is a stupid hack
302302
needs_full_clone = False
303303
if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
304-
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 + ");"
304+
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));"
305305
elif needs_full_clone:
306306
base_conv = base_conv + "\n// Warning: we may need a move here but no clone is available for " + ty_info.rust_obj
307307
if not needs_full_clone and ty_info.rust_obj != "LDKu8slice":

liblightningjni_debug.so

296 Bytes
Binary file not shown.

liblightningjni_release.so

976 Bytes
Binary file not shown.

pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@
2929
<dependency>
3030
<groupId>org.jetbrains</groupId>
3131
<artifactId>annotations</artifactId>
32-
<version>13.0</version>
33-
<scope>compile</scope>
34-
</dependency>
35-
<dependency>
36-
<groupId>org.jetbrains</groupId>
37-
<artifactId>annotations</artifactId>
38-
<version>13.0</version>
32+
<version>RELEASE</version>
3933
<scope>compile</scope>
4034
</dependency>
4135
</dependencies>

src/main/jni/bindings.c

Lines changed: 49 additions & 49 deletions
Large diffs are not rendered by default.

src/test/java/org/ldk/HumanObjectPeerTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public MonitorEvent[] release_pending_monitor_events() {
162162
final Logger logger;
163163
final FeeEstimator fee_estimator;
164164
final BroadcasterInterface tx_broadcaster;
165+
final KeysManager explicit_keys_manager;
165166
final KeysInterface keys_interface;
166167
final ChainMonitor chain_monitor;
167168
final NetGraphMsgHandler router;
@@ -251,8 +252,10 @@ public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id,
251252
KeysManager keys = KeysManager.constructor_new(key_seed, System.currentTimeMillis() / 1000, (int) (System.currentTimeMillis() * 1000));
252253
if (use_km_wrapper) {
253254
this.keys_interface = manual_keysif(keys.as_KeysInterface());
255+
this.explicit_keys_manager = null;
254256
} else {
255257
this.keys_interface = keys.as_KeysInterface();
258+
this.explicit_keys_manager = keys;
256259
}
257260
this.router = NetGraphMsgHandler.constructor_new(new byte[32], null, logger);
258261
}
@@ -349,7 +352,7 @@ TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] connect_block(Block b, int height
349352
TwoTuple<Long, byte[]>[] txn;
350353
if (b.hasTransactions()) {
351354
assert b.getTransactions().size() == 1;
352-
TwoTuple<Long, byte[]> txp = new TwoTuple<>((long) 1, b.getTransactions().get(0).bitcoinSerialize());
355+
TwoTuple<Long, byte[]> txp = new TwoTuple<>((long) 0, b.getTransactions().get(0).bitcoinSerialize());
353356
txn = new TwoTuple[]{txp};
354357
} else
355358
txn = new TwoTuple[0];
@@ -387,7 +390,7 @@ Route get_route(byte[] dest_node, ChannelDetails[] our_chans) {
387390
try (LockedNetworkGraph netgraph = this.router.read_locked_graph()) {
388391
NetworkGraph graph = netgraph.graph();
389392
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()},
390-
new long[0], 1000, 42, this.logger._test_only_get_ptr());
393+
new long[0], 1000000, 42, this.logger._test_only_get_ptr());
391394
assert bindings.LDKCResult_RouteLightningErrorZ_result_ok(res);
392395
byte[] serialized_route = bindings.Route_write(bindings.LDKCResult_RouteLightningErrorZ_get_ok(res));
393396
must_free_objs.add(new WeakReference<>(serialized_route));
@@ -665,12 +668,35 @@ void do_test_message_handler_b(TestState state) {
665668
if (watch_outputs != null) { // We only process watch_outputs manually when we use a manually-build Watch impl
666669
assert watch_outputs.length == 1;
667670
assert Arrays.equals(watch_outputs[0].a, tx.getTxId().getReversedBytes());
668-
assert watch_outputs[0].b.length == 1;
671+
assert watch_outputs[0].b.length == 2;
672+
assert watch_outputs[0].b[0].a == 0;
673+
assert watch_outputs[0].b[1].a == 1;
669674
}
670675

676+
for (int i = 11; i < 21; i++) {
677+
b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, new ArrayList<>());
678+
state.peer2.connect_block(b, i, 0);
679+
}
680+
681+
Event[] broadcastable_event = null;
682+
if (state.peer2.chain_monitor != null) {
683+
broadcastable_event = state.peer2.chain_monitor.as_EventsProvider().get_and_clear_pending_events();
684+
}
671685
// This used to be buggy and double-free, so go ahead and fetch them!
672686
for (ChannelMonitor mon : state.peer2.monitors.values()) {
673687
byte[][] txn = mon.get_latest_holder_commitment_txn(state.peer2.logger);
688+
if (state.peer2.chain_monitor == null) {
689+
broadcastable_event = mon.get_and_clear_pending_events();
690+
}
691+
}
692+
assert broadcastable_event.length == 1;
693+
assert broadcastable_event[0] instanceof Event.SpendableOutputs;
694+
if (state.peer2.explicit_keys_manager != null) {
695+
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);
696+
assert tx_res instanceof Result_TransactionNoneZ.Result_TransactionNoneZ_OK;
697+
Transaction built_tx = new Transaction(bitcoinj_net, ((Result_TransactionNoneZ.Result_TransactionNoneZ_OK) tx_res).res);
698+
assert built_tx.getOutputs().size() == 1;
699+
assert Arrays.equals(built_tx.getOutput(0).getScriptBytes(), new byte[]{0x00});
674700
}
675701
}
676702

0 commit comments

Comments
 (0)