Skip to content

Commit e6173a7

Browse files
authored
Merge pull request #49 from TheBlueMatt/main
v0.0.101.3
2 parents 8c0814b + 3c0e7b4 commit e6173a7

File tree

268 files changed

+964
-910
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+964
-910
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,15 @@ jobs:
257257
./genbindings.sh ./ldk-c-bindings/ "-I$JAVA_HOME/include/ -I$JAVA_HOME/include/darwin -isysroot$(xcrun --show-sdk-path)" false false || echo
258258
cat src/main/resources/liblightningjni_MacOSX-aarch64.nativelib > /dev/null
259259
fi
260-
- name: Fetch Maven 3.8.1
260+
- name: Fetch Maven 3.8.3
261261
run: |
262-
wget -O apache-maven-3.8.1-bin.tar.gz https://apache.osuosl.org/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
263-
if [ "$(shasum -a 256 apache-maven-3.8.1-bin.tar.gz | awk '{ print $1 }')" != "b98a1905eb554d07427b2e5509ff09bd53e2f1dd7a0afa38384968b113abef02" ]; then
262+
wget -O apache-maven-3.8.3-bin.tar.gz https://apache.osuosl.org/maven/maven-3/3.8.3/binaries/apache-maven-3.8.3-bin.tar.gz
263+
if [ "$(shasum -a 256 apache-maven-3.8.3-bin.tar.gz | awk '{ print $1 }')" != "0f1597d11085b8fe93d84652a18c6deea71ece9fabba45a02cf6600c7758fd5b" ]; then
264264
echo "Bad hash"
265265
exit 1
266266
fi
267-
tar xvvf apache-maven-3.8.1-bin.tar.gz
268-
export PATH=apache-maven-3.8.1/bin:$PATH
267+
tar xvvf apache-maven-3.8.3-bin.tar.gz
268+
export PATH=apache-maven-3.8.3/bin:$PATH
269269
- name: Run Java Tests against built jar
270270
run: |
271271
mvn -DskipTests=true package

android-build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ LDK_C_BINDINGS="$(realpath $2)"
2626
RUST_LIGHTNING="$(realpath $1)"
2727
pushd "$2"
2828
export LDK_C_BINDINGS_EXTRA_TARGETS="x86_64-linux-android i686-linux-android armv7-linux-androideabi aarch64-linux-android"
29-
export LDK_C_BINDINGS_EXTRA_TARGET_CCS="x86_64-linux-android21-clang i686-linux-android21-clang armv7a-linux-androideabi21-clang aarch64-linux-android21-clang"
29+
export LDK_C_BINDINGS_EXTRA_TARGET_CCS="x86_64-linux-android24-clang i686-linux-android24-clang armv7a-linux-androideabi24-clang aarch64-linux-android24-clang"
3030
./genbindings.sh "$RUST_LIGHTNING" true
3131
popd
3232

java_strings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __init__(self, DEBUG: bool, target: Target, **kwargs):
115115
class CommonBase {
116116
long ptr;
117117
LinkedList<Object> ptrs_to = new LinkedList();
118-
protected CommonBase(long ptr) { assert ptr > 1024; this.ptr = ptr; }
118+
protected CommonBase(long ptr) { assert ptr < 0 || ptr > 1024; this.ptr = ptr; }
119119
}
120120
"""
121121

@@ -1258,7 +1258,7 @@ def map_function(self, argument_types, c_call_string, method_name, meth_n, retur
12581258
out_java_struct += (info.arg_name)
12591259
out_java_struct += (");\n")
12601260
if return_type_info.java_ty == "long" and return_type_info.java_hu_ty != "long":
1261-
out_java_struct += "\t\tif (ret < 1024) { return null; }\n"
1261+
out_java_struct += "\t\tif (ret >= 0 && ret < 1024) { return null; }\n"
12621262

12631263
if return_type_info.to_hu_conv is not None:
12641264
if not takes_self:

src/main/java/org/ldk/batteries/NioPeerHandler.java

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.ldk.batteries;
22

3+
import org.ldk.impl.bindings;
34
import org.ldk.structs.*;
45

56
import java.io.IOException;
7+
import java.lang.reflect.Field;
68
import java.util.LinkedList;
79
import java.net.SocketAddress;
810
import java.net.StandardSocketOptions;
@@ -17,6 +19,7 @@
1719
public class NioPeerHandler {
1820
private static class Peer {
1921
SocketDescriptor descriptor;
22+
long descriptor_raw_pointer;
2023
SelectionKey key;
2124
}
2225

@@ -45,6 +48,19 @@ private void do_selector_action(SelectorCall meth) throws IOException {
4548
}
4649
}
4750

51+
static private Field CommonBasePointer;
52+
static {
53+
try {
54+
Class c = PeerManager.class.getSuperclass();
55+
CommonBasePointer = c.getDeclaredField("ptr");
56+
CommonBasePointer.setAccessible(true);
57+
long _dummy_check = CommonBasePointer.getLong(Ping.of((short)0, (short)0));
58+
} catch (NoSuchFieldException | IllegalAccessException e) {
59+
throw new IllegalArgumentException(
60+
"We currently use reflection to access protected fields as Java has no reasonable access controls", e);
61+
}
62+
}
63+
4864
private Peer setup_socket(SocketChannel chan) throws IOException {
4965
chan.configureBlocking(false);
5066
// Lightning tends to send a number of small messages back and forth between peers quickly, which Nagle is
@@ -61,13 +77,14 @@ private Peer setup_socket(SocketChannel chan) throws IOException {
6177
@Override
6278
public long send_data(byte[] data, boolean resume_read) {
6379
try {
64-
if (resume_read) {
65-
do_selector_action(() -> peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_READ));
66-
}
6780
long written = chan.write(ByteBuffer.wrap(data));
6881
if (written != data.length) {
69-
do_selector_action(() -> peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_WRITE));
70-
}
82+
do_selector_action(() -> peer.key.interestOps(
83+
(peer.key.interestOps() | SelectionKey.OP_WRITE) & (~SelectionKey.OP_READ)));
84+
} else if (resume_read) {
85+
do_selector_action(() -> peer.key.interestOps(
86+
(peer.key.interestOps() | SelectionKey.OP_READ) & (~SelectionKey.OP_WRITE)));
87+
}
7188
return written;
7289
} catch (IOException e) {
7390
// Most likely the socket is disconnected, let the background thread handle it.
@@ -88,6 +105,12 @@ public void disconnect_socket() {
88105
@Override public long hash() { return our_id; }
89106
});
90107
peer.descriptor = descriptor;
108+
try {
109+
peer.descriptor_raw_pointer = CommonBasePointer.getLong(descriptor);
110+
} catch (IllegalAccessException e) {
111+
throw new IllegalArgumentException(
112+
"We currently use reflection to access protected fields as Java has no reasonable access controls", e);
113+
}
91114
return peer;
92115
}
93116

@@ -107,7 +130,16 @@ public NioPeerHandler(PeerManager manager) throws IOException {
107130
this.peer_manager = manager;
108131
this.selector = Selector.open();
109132
io_thread = new Thread(() -> {
110-
ByteBuffer buf = ByteBuffer.allocate(8192);
133+
int BUF_SZ = 16 * 1024;
134+
byte[] max_buf_byte_object = new byte[BUF_SZ];
135+
ByteBuffer buf = ByteBuffer.allocate(BUF_SZ);
136+
137+
long peer_manager_raw_pointer;
138+
try {
139+
peer_manager_raw_pointer = CommonBasePointer.getLong(this.peer_manager);
140+
} catch (IllegalAccessException e) {
141+
throw new RuntimeException(e);
142+
}
111143
while (true) {
112144
try {
113145
if (IS_ANDROID) {
@@ -167,17 +199,32 @@ public NioPeerHandler(PeerManager manager) throws IOException {
167199
key.cancel();
168200
} else if (read > 0) {
169201
((Buffer)buf).flip();
170-
byte[] read_bytes = new byte[read];
202+
// This code is quite hot during initial network graph sync, so we go a ways out of
203+
// our way to avoid object allocations that'll make the GC sweat later -
204+
// * when we're hot, we'll likely often be reading the full buffer, so we keep
205+
// around a full-buffer-sized byte array to reuse across reads,
206+
// * We use the manual memory management call logic directly in bindings instead of
207+
// the nice "human-readable" wrappers. This puts us at risk of memory issues,
208+
// so we indirectly ensure compile fails if the types change by writing the
209+
// "human-readable" form of the same code in the dummy function below.
210+
byte[] read_bytes;
211+
if (read == BUF_SZ) {
212+
read_bytes = max_buf_byte_object;
213+
} else {
214+
read_bytes = new byte[read];
215+
}
171216
buf.get(read_bytes, 0, read);
172-
Result_boolPeerHandleErrorZ res = this.peer_manager.read_event(peer.descriptor, read_bytes);
173-
if (res instanceof Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) {
174-
if (((Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) res).res) {
217+
long read_result_pointer = bindings.PeerManager_read_event(
218+
peer_manager_raw_pointer, peer.descriptor_raw_pointer, read_bytes);
219+
if (bindings.LDKCResult_boolPeerHandleErrorZ_result_ok(read_result_pointer)) {
220+
if (bindings.LDKCResult_boolPeerHandleErrorZ_get_ok(read_result_pointer)) {
175221
key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
176222
}
177223
} else {
178224
key.channel().close();
179225
key.cancel();
180226
}
227+
bindings.CResult_boolPeerHandleErrorZ_free(read_result_pointer);
181228
}
182229
}
183230
} catch (IOException ignored) {
@@ -197,6 +244,13 @@ public NioPeerHandler(PeerManager manager) throws IOException {
197244
io_thread.start();
198245
}
199246

247+
// Ensure the types used in the above manual code match what they were when the code was written.
248+
// Ensure the above manual bindings.* code changes if this fails to compile.
249+
private void dummy_check_return_type_matches_manual_memory_code_above(Peer peer) {
250+
byte[] read_bytes = new byte[32];
251+
Result_boolPeerHandleErrorZ res = this.peer_manager.read_event(peer.descriptor, read_bytes);
252+
}
253+
200254
/**
201255
* Connect to a peer given their node id and socket address. Blocks until a connection is established (or returns
202256
* IOException) and then the connection handling runs in the background.

src/main/java/org/ldk/structs/APIError.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private IncompatibleShutdownScript(long ptr, bindings.LDKAPIError.IncompatibleSh
110110
*/
111111
public APIError clone() {
112112
long ret = bindings.APIError_clone(this.ptr);
113-
if (ret < 1024) { return null; }
113+
if (ret >= 0 && ret < 1024) { return null; }
114114
APIError ret_hu_conv = APIError.constr_from_ptr(ret);
115115
ret_hu_conv.ptrs_to.add(this);
116116
return ret_hu_conv;
@@ -121,7 +121,7 @@ public APIError clone() {
121121
*/
122122
public static APIError apimisuse_error(java.lang.String err) {
123123
long ret = bindings.APIError_apimisuse_error(err);
124-
if (ret < 1024) { return null; }
124+
if (ret >= 0 && ret < 1024) { return null; }
125125
APIError ret_hu_conv = APIError.constr_from_ptr(ret);
126126
ret_hu_conv.ptrs_to.add(ret_hu_conv);
127127
return ret_hu_conv;
@@ -132,7 +132,7 @@ public static APIError apimisuse_error(java.lang.String err) {
132132
*/
133133
public static APIError fee_rate_too_high(java.lang.String err, int feerate) {
134134
long ret = bindings.APIError_fee_rate_too_high(err, feerate);
135-
if (ret < 1024) { return null; }
135+
if (ret >= 0 && ret < 1024) { return null; }
136136
APIError ret_hu_conv = APIError.constr_from_ptr(ret);
137137
ret_hu_conv.ptrs_to.add(ret_hu_conv);
138138
return ret_hu_conv;
@@ -143,7 +143,7 @@ public static APIError fee_rate_too_high(java.lang.String err, int feerate) {
143143
*/
144144
public static APIError route_error(java.lang.String err) {
145145
long ret = bindings.APIError_route_error(err);
146-
if (ret < 1024) { return null; }
146+
if (ret >= 0 && ret < 1024) { return null; }
147147
APIError ret_hu_conv = APIError.constr_from_ptr(ret);
148148
ret_hu_conv.ptrs_to.add(ret_hu_conv);
149149
return ret_hu_conv;
@@ -154,7 +154,7 @@ public static APIError route_error(java.lang.String err) {
154154
*/
155155
public static APIError channel_unavailable(java.lang.String err) {
156156
long ret = bindings.APIError_channel_unavailable(err);
157-
if (ret < 1024) { return null; }
157+
if (ret >= 0 && ret < 1024) { return null; }
158158
APIError ret_hu_conv = APIError.constr_from_ptr(ret);
159159
ret_hu_conv.ptrs_to.add(ret_hu_conv);
160160
return ret_hu_conv;
@@ -165,7 +165,7 @@ public static APIError channel_unavailable(java.lang.String err) {
165165
*/
166166
public static APIError monitor_update_failed() {
167167
long ret = bindings.APIError_monitor_update_failed();
168-
if (ret < 1024) { return null; }
168+
if (ret >= 0 && ret < 1024) { return null; }
169169
APIError ret_hu_conv = APIError.constr_from_ptr(ret);
170170
ret_hu_conv.ptrs_to.add(ret_hu_conv);
171171
return ret_hu_conv;
@@ -176,7 +176,7 @@ public static APIError monitor_update_failed() {
176176
*/
177177
public static APIError incompatible_shutdown_script(ShutdownScript script) {
178178
long ret = bindings.APIError_incompatible_shutdown_script(script == null ? 0 : script.ptr & ~1);
179-
if (ret < 1024) { return null; }
179+
if (ret >= 0 && ret < 1024) { return null; }
180180
APIError ret_hu_conv = APIError.constr_from_ptr(ret);
181181
ret_hu_conv.ptrs_to.add(ret_hu_conv);
182182
return ret_hu_conv;

src/main/java/org/ldk/structs/AcceptChannel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public void set_first_per_commitment_point(byte[] val) {
234234
*/
235235
public AcceptChannel clone() {
236236
long ret = bindings.AcceptChannel_clone(this.ptr);
237-
if (ret < 1024) { return null; }
237+
if (ret >= 0 && ret < 1024) { return null; }
238238
AcceptChannel ret_hu_conv = new AcceptChannel(null, ret);
239239
ret_hu_conv.ptrs_to.add(this);
240240
return ret_hu_conv;
@@ -253,7 +253,7 @@ public byte[] write() {
253253
*/
254254
public static Result_AcceptChannelDecodeErrorZ read(byte[] ser) {
255255
long ret = bindings.AcceptChannel_read(ser);
256-
if (ret < 1024) { return null; }
256+
if (ret >= 0 && ret < 1024) { return null; }
257257
Result_AcceptChannelDecodeErrorZ ret_hu_conv = Result_AcceptChannelDecodeErrorZ.constr_from_ptr(ret);
258258
return ret_hu_conv;
259259
}

src/main/java/org/ldk/structs/Access.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static Access new_impl(AccessInterface arg) {
5555
*/
5656
public Result_TxOutAccessErrorZ get_utxo(byte[] genesis_hash, long short_channel_id) {
5757
long ret = bindings.Access_get_utxo(this.ptr, genesis_hash, short_channel_id);
58-
if (ret < 1024) { return null; }
58+
if (ret >= 0 && ret < 1024) { return null; }
5959
Result_TxOutAccessErrorZ ret_hu_conv = Result_TxOutAccessErrorZ.constr_from_ptr(ret);
6060
return ret_hu_conv;
6161
}

src/main/java/org/ldk/structs/AnnouncementSignatures.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void set_bitcoin_signature(byte[] val) {
8484
*/
8585
public static AnnouncementSignatures of(byte[] channel_id_arg, long short_channel_id_arg, byte[] node_signature_arg, byte[] bitcoin_signature_arg) {
8686
long ret = bindings.AnnouncementSignatures_new(channel_id_arg, short_channel_id_arg, node_signature_arg, bitcoin_signature_arg);
87-
if (ret < 1024) { return null; }
87+
if (ret >= 0 && ret < 1024) { return null; }
8888
AnnouncementSignatures ret_hu_conv = new AnnouncementSignatures(null, ret);
8989
ret_hu_conv.ptrs_to.add(ret_hu_conv);
9090
return ret_hu_conv;
@@ -95,7 +95,7 @@ public static AnnouncementSignatures of(byte[] channel_id_arg, long short_channe
9595
*/
9696
public AnnouncementSignatures clone() {
9797
long ret = bindings.AnnouncementSignatures_clone(this.ptr);
98-
if (ret < 1024) { return null; }
98+
if (ret >= 0 && ret < 1024) { return null; }
9999
AnnouncementSignatures ret_hu_conv = new AnnouncementSignatures(null, ret);
100100
ret_hu_conv.ptrs_to.add(this);
101101
return ret_hu_conv;
@@ -114,7 +114,7 @@ public byte[] write() {
114114
*/
115115
public static Result_AnnouncementSignaturesDecodeErrorZ read(byte[] ser) {
116116
long ret = bindings.AnnouncementSignatures_read(ser);
117-
if (ret < 1024) { return null; }
117+
if (ret >= 0 && ret < 1024) { return null; }
118118
Result_AnnouncementSignaturesDecodeErrorZ ret_hu_conv = Result_AnnouncementSignaturesDecodeErrorZ.constr_from_ptr(ret);
119119
return ret_hu_conv;
120120
}

src/main/java/org/ldk/structs/BackgroundProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected void finalize() throws Throwable {
7979
*/
8080
public static BackgroundProcessor start(ChannelManagerPersister persister, EventHandler event_handler, ChainMonitor chain_monitor, ChannelManager channel_manager, NetGraphMsgHandler net_graph_msg_handler, PeerManager peer_manager, Logger logger) {
8181
long ret = bindings.BackgroundProcessor_start(persister == null ? 0 : persister.ptr, event_handler == null ? 0 : event_handler.ptr, chain_monitor == null ? 0 : chain_monitor.ptr & ~1, channel_manager == null ? 0 : channel_manager.ptr & ~1, net_graph_msg_handler == null ? 0 : net_graph_msg_handler.ptr & ~1, peer_manager == null ? 0 : peer_manager.ptr & ~1, logger == null ? 0 : logger.ptr);
82-
if (ret < 1024) { return null; }
82+
if (ret >= 0 && ret < 1024) { return null; }
8383
BackgroundProcessor ret_hu_conv = new BackgroundProcessor(null, ret);
8484
ret_hu_conv.ptrs_to.add(ret_hu_conv);
8585
ret_hu_conv.ptrs_to.add(persister);
@@ -105,7 +105,7 @@ public static BackgroundProcessor start(ChannelManagerPersister persister, Event
105105
*/
106106
public Result_NoneErrorZ join() {
107107
long ret = bindings.BackgroundProcessor_join(this.ptr);
108-
if (ret < 1024) { return null; }
108+
if (ret >= 0 && ret < 1024) { return null; }
109109
Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
110110
this.ptrs_to.add(this);
111111
// Due to rust's strict-ownership memory model, in some cases we need to "move"
@@ -132,7 +132,7 @@ public Result_NoneErrorZ join() {
132132
*/
133133
public Result_NoneErrorZ stop() {
134134
long ret = bindings.BackgroundProcessor_stop(this.ptr);
135-
if (ret < 1024) { return null; }
135+
if (ret >= 0 && ret < 1024) { return null; }
136136
Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
137137
this.ptrs_to.add(this);
138138
// Due to rust's strict-ownership memory model, in some cases we need to "move"

src/main/java/org/ldk/structs/Balance.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private MaybeClaimableHTLCAwaitingTimeout(long ptr, bindings.LDKBalance.MaybeCla
105105
*/
106106
public Balance clone() {
107107
long ret = bindings.Balance_clone(this.ptr);
108-
if (ret < 1024) { return null; }
108+
if (ret >= 0 && ret < 1024) { return null; }
109109
Balance ret_hu_conv = Balance.constr_from_ptr(ret);
110110
ret_hu_conv.ptrs_to.add(this);
111111
return ret_hu_conv;
@@ -116,7 +116,7 @@ public Balance clone() {
116116
*/
117117
public static Balance claimable_on_channel_close(long claimable_amount_satoshis) {
118118
long ret = bindings.Balance_claimable_on_channel_close(claimable_amount_satoshis);
119-
if (ret < 1024) { return null; }
119+
if (ret >= 0 && ret < 1024) { return null; }
120120
Balance ret_hu_conv = Balance.constr_from_ptr(ret);
121121
ret_hu_conv.ptrs_to.add(ret_hu_conv);
122122
return ret_hu_conv;
@@ -127,7 +127,7 @@ public static Balance claimable_on_channel_close(long claimable_amount_satoshis)
127127
*/
128128
public static Balance claimable_awaiting_confirmations(long claimable_amount_satoshis, int confirmation_height) {
129129
long ret = bindings.Balance_claimable_awaiting_confirmations(claimable_amount_satoshis, confirmation_height);
130-
if (ret < 1024) { return null; }
130+
if (ret >= 0 && ret < 1024) { return null; }
131131
Balance ret_hu_conv = Balance.constr_from_ptr(ret);
132132
ret_hu_conv.ptrs_to.add(ret_hu_conv);
133133
return ret_hu_conv;
@@ -138,7 +138,7 @@ public static Balance claimable_awaiting_confirmations(long claimable_amount_sat
138138
*/
139139
public static Balance contentious_claimable(long claimable_amount_satoshis, int timeout_height) {
140140
long ret = bindings.Balance_contentious_claimable(claimable_amount_satoshis, timeout_height);
141-
if (ret < 1024) { return null; }
141+
if (ret >= 0 && ret < 1024) { return null; }
142142
Balance ret_hu_conv = Balance.constr_from_ptr(ret);
143143
ret_hu_conv.ptrs_to.add(ret_hu_conv);
144144
return ret_hu_conv;
@@ -149,7 +149,7 @@ public static Balance contentious_claimable(long claimable_amount_satoshis, int
149149
*/
150150
public static Balance maybe_claimable_htlcawaiting_timeout(long claimable_amount_satoshis, int claimable_height) {
151151
long ret = bindings.Balance_maybe_claimable_htlcawaiting_timeout(claimable_amount_satoshis, claimable_height);
152-
if (ret < 1024) { return null; }
152+
if (ret >= 0 && ret < 1024) { return null; }
153153
Balance ret_hu_conv = Balance.constr_from_ptr(ret);
154154
ret_hu_conv.ptrs_to.add(ret_hu_conv);
155155
return ret_hu_conv;

0 commit comments

Comments
 (0)