Skip to content

Commit 47cbd09

Browse files
authored
Merge pull request #104 from TheBlueMatt/main
Fix TS u64 call semantics (and test it)
2 parents 47341be + 0635e0e commit 47cbd09

File tree

9 files changed

+1211
-687
lines changed

9 files changed

+1211
-687
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ jobs:
9595
kill $SERVER_PID
9696
- name: Check latest TS files are in git
9797
run: |
98+
git checkout ts/package.json
9899
git diff --exit-code
99100
100101
java_bindings:

genbindings.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ else
189189
./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript browser
190190
fi
191191
rm -f ts/bindings.c
192+
sed -i 's/^ "version": .*/ "version": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'",/g' ts/package.json
192193
if [ "$3" = "true" ]; then
193194
echo "#define LDK_DEBUG_BUILD" > ts/bindings.c
194195
elif [ "$3" = "leaks" ]; then

ts/bindings.c

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

ts/bindings.c.body

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

ts/bindings.mts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async function finishInitializeWasm(wasmInstance: WebAssembly.Instance) {
7171
}
7272

7373
if (decodeString(wasm.TS_get_lib_version_string()) !== version.get_ldk_java_bindings_version())
74-
throw new Error("Compiled LDK library and LDK class failes do not match");
74+
throw new Error("Compiled LDK library and LDK class files do not match");
7575
// Fetching the LDK versions from C also checks that the header and binaries match
7676
const c_bindings_ver: number = wasm.TS_get_ldk_c_bindings_version();
7777
const ldk_ver: number = wasm.TS_get_ldk_version();
@@ -88,15 +88,17 @@ async function finishInitializeWasm(wasmInstance: WebAssembly.Instance) {
8888

8989
/* @internal */
9090
export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) {
91-
imports.env["js_invoke_function"] = js_invoke;
91+
imports.env["js_invoke_function_u"] = js_invoke;
92+
imports.env["js_invoke_function_b"] = js_invoke;
9293
const { instance: wasmInstance } = await WebAssembly.instantiate(wasmBinary, imports);
9394
await finishInitializeWasm(wasmInstance);
9495
}
9596

9697
/* @internal */
9798
export async function initializeWasmFetch(uri: string) {
9899
const stream = fetch(uri);
99-
imports.env["js_invoke_function"] = js_invoke;
100+
imports.env["js_invoke_function_u"] = js_invoke;
101+
imports.env["js_invoke_function_b"] = js_invoke;
100102
const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
101103
await finishInitializeWasm(wasmInstance);
102104
}
@@ -32624,7 +32626,7 @@ export function SiPrefix_to_str(o: number): number {
3262432626
}
3262532627

3262632628

32627-
js_invoke = function(obj_ptr: number, fn_id: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number, arg7: number, arg8: number, arg9: number, arg10: number) {
32629+
js_invoke = function(obj_ptr: number, fn_id: number, arg1: bigint|number, arg2: bigint|number, arg3: bigint|number, arg4: bigint|number, arg5: bigint|number, arg6: bigint|number, arg7: bigint|number, arg8: bigint|number, arg9: bigint|number, arg10: bigint|number) {
3262832630
const weak: WeakRef<object> = js_objs[obj_ptr];
3262932631
if (weak == null || weak == undefined) {
3263032632
console.error("Got function call on unknown/free'd JS object!");
@@ -32743,5 +32745,7 @@ js_invoke = function(obj_ptr: number, fn_id: number, arg1: number, arg2: number,
3274332745
console.error("Got function call on incorrect JS object!");
3274432746
throw new Error("Got function call on incorrect JS object!");
3274532747
}
32746-
return fn.value.bind(obj)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
32748+
const ret = fn.value.bind(obj)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
32749+
if (ret === undefined || ret === null) return BigInt(0);
32750+
return BigInt(ret);
3274732751
}

ts/js-wasm.h

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,46 @@
44

55
extern size_t strlen(const char *s);
66

7+
// We only support two call types - u32 and u64. u32 is mapped to a JavaScript
8+
// "number" whereas u64 is mapped to a JavaScript "bigint". Ultimately it all
9+
// calls through to the same function, but at the FFI boundary itself we have
10+
// to use proper types.
11+
712
typedef uint32_t JSValue;
8-
extern JSValue js_invoke_function(JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue) __attribute__((import_name("js_invoke_function")));
13+
extern uint64_t js_invoke_function_u(JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue) __attribute__((import_name("js_invoke_function_u")));
14+
extern uint64_t js_invoke_function_b(JSValue,JSValue,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t) __attribute__((import_name("js_invoke_function_b")));
915

10-
static inline JSValue js_invoke_function_0(JSValue obj, JSValue fn){
11-
return js_invoke_function(obj,fn,0,0,0,0,0,0,0,0,0,0);
12-
}
13-
static inline JSValue js_invoke_function_1(JSValue obj, JSValue fn, JSValue a){
14-
return js_invoke_function(obj,fn,a,0,0,0,0,0,0,0,0,0);
16+
static inline JSValue js_invoke_function_u_(JSValue obj, JSValue fn){
17+
return js_invoke_function_u(obj,fn,0,0,0,0,0,0,0,0,0,0);
1518
}
16-
static inline JSValue js_invoke_function_2(JSValue obj, JSValue fn, JSValue a, JSValue b){
17-
return js_invoke_function(obj,fn,a,b,0,0,0,0,0,0,0,0);
19+
static inline JSValue js_invoke_function_u_u(JSValue obj, JSValue fn, JSValue a){
20+
return js_invoke_function_u(obj,fn,a,0,0,0,0,0,0,0,0,0);
1821
}
19-
static inline JSValue js_invoke_function_3(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c){
20-
return js_invoke_function(obj,fn,a,b,c,0,0,0,0,0,0,0);
22+
static inline JSValue js_invoke_function_u_uu(JSValue obj, JSValue fn, JSValue a, JSValue b){
23+
return js_invoke_function_u(obj,fn,a,b,0,0,0,0,0,0,0,0);
2124
}
22-
static inline JSValue js_invoke_function_4(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d){
23-
return js_invoke_function(obj,fn,a,b,c,d,0,0,0,0,0,0);
25+
static inline JSValue js_invoke_function_u_uuu(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c){
26+
return js_invoke_function_u(obj,fn,a,b,c,0,0,0,0,0,0,0);
2427
}
25-
static inline JSValue js_invoke_function_5(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e){
26-
return js_invoke_function(obj,fn,a,b,c,d,e,0,0,0,0,0);
28+
static inline JSValue js_invoke_function_u_uuuu(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d){
29+
return js_invoke_function_u(obj,fn,a,b,c,d,0,0,0,0,0,0);
2730
}
28-
static inline JSValue js_invoke_function_6(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f){
29-
return js_invoke_function(obj,fn,a,b,c,d,e,f,0,0,0,0);
31+
static inline JSValue js_invoke_function_u_uuuuu(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e){
32+
return js_invoke_function_u(obj,fn,a,b,c,d,e,0,0,0,0,0);
3033
}
31-
static inline JSValue js_invoke_function_7(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f, JSValue g){
32-
return js_invoke_function(obj,fn,a,b,c,d,e,f,g,0,0,0);
34+
35+
static inline uint64_t js_invoke_function_b_(JSValue obj, JSValue fn){
36+
return js_invoke_function_u(obj,fn,0,0,0,0,0,0,0,0,0,0);
3337
}
34-
static inline JSValue js_invoke_function_8(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f, JSValue g, JSValue h){
35-
return js_invoke_function(obj,fn,a,b,c,d,e,f,g,h,0,0);
38+
39+
static inline uint64_t js_invoke_function_b_uuuu(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d){
40+
return js_invoke_function_u(obj,fn,a,b,c,d,0,0,0,0,0,0);
3641
}
37-
static inline JSValue js_invoke_function_9(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f, JSValue g, JSValue h, JSValue i){
38-
return js_invoke_function(obj,fn,a,b,c,d,e,f,g,h,i,0);
42+
static inline JSValue js_invoke_function_u_b(JSValue obj, JSValue fn, uint64_t a){
43+
return js_invoke_function_b(obj,fn,a,0,0,0,0,0,0,0,0,0);
3944
}
40-
static inline JSValue js_invoke_function_10(JSValue obj, JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f, JSValue g, JSValue h, JSValue i, JSValue j){
41-
return js_invoke_function(obj,fn,a,b,c,d,e,f,g,h,i,j);
45+
static inline JSValue js_invoke_function_u_bb(JSValue obj, JSValue fn, uint64_t a, uint64_t b){
46+
return js_invoke_function_b(obj,fn,a,b,0,0,0,0,0,0,0,0);
4247
}
48+
4349
#endif

ts/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lightningdevkit",
3-
"version": "0.0.106.0beta1",
3+
"version": "Set in genbindings.sh automagically",
44
"description": "Lightning Development Kit",
55
"main": "index.mjs",
66
"types": "index.d.mts",
@@ -15,6 +15,8 @@
1515
"enums/*.d.mts",
1616
"bindings.mjs",
1717
"bindings.d.mts",
18+
"index.mjs",
19+
"index.d.mts",
1820
"version.mjs",
1921
"liblightningjs.wasm",
2022
"tsconfig.json",

ts/test/tests.mts

Lines changed: 106 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ tests.push(async () => {
5555
});
5656

5757
var seed_counter = 0;
58-
function get_chanman() {
58+
class Node {
59+
constructor(public chan_man: ldk.ChannelManager, public tx_broadcasted: Promise<Uint8Array>, public logger: ldk.Logger,
60+
public node_id: Uint8Array, public node_secret: Uint8Array) {}
61+
}
62+
function get_chanman(): Node {
5963
const fee_est = ldk.FeeEstimator.new_impl({
6064
get_est_sat_per_1000_weight(confirmation_target: ldk.ConfirmationTarget): number {
6165
return 253;
@@ -93,7 +97,11 @@ function get_chanman() {
9397
const config = ldk.UserConfig.constructor_default();
9498
const params = ldk.ChainParameters.constructor_new(ldk.Network.LDKNetwork_Testnet, ldk.BestBlock.constructor_from_genesis(ldk.Network.LDKNetwork_Testnet));
9599

96-
return [ldk.ChannelManager.constructor_new(fee_est, chain_watch, tx_broadcaster, logger, keys_interface, config, params), tx_broadcasted];
100+
const chan_man = ldk.ChannelManager.constructor_new(fee_est, chain_watch, tx_broadcaster, logger, keys_interface, config, params);
101+
return new Node(
102+
chan_man, tx_broadcasted, logger, chan_man.get_our_node_id(),
103+
(keys_interface.get_node_secret(ldk.Recipient.LDKRecipient_Node) as ldk.Result_SecretKeyNoneZ_OK).res
104+
);
97105
}
98106

99107
function exchange_messages(a: ldk.ChannelManager, b: ldk.ChannelManager) {
@@ -139,39 +147,41 @@ function assign_u64(arr: Uint8Array, offset: number, value: bigint) {
139147
arr[offset + 7] = Number((value >> BigInt(8 * 7)) & BigInt(0xff));
140148
}
141149

150+
function get_event(chan_man: ldk.ChannelManager): ldk.Event {
151+
const events: Array<ldk.Event> = [];
152+
const event_handler = ldk.EventHandler.new_impl({
153+
handle_event(event: ldk.Event): void {
154+
events.push(event);
155+
}
156+
} as ldk.EventHandlerInterface);
157+
158+
chan_man.as_EventsProvider().process_pending_events(event_handler);
159+
console.assert(events.length == 1);
160+
return events[0];
161+
}
162+
142163
tests.push(async () => {
143-
const peer_a = get_chanman();
144-
const peer_b = get_chanman();
145-
const chan_man_a: ldk.ChannelManager = peer_a[0] as ldk.ChannelManager;
146-
const chan_man_b: ldk.ChannelManager = peer_b[0] as ldk.ChannelManager;
164+
const a = get_chanman();
165+
const b = get_chanman();
147166

148-
chan_man_a.as_ChannelMessageHandler().peer_connected(chan_man_b.get_our_node_id(), ldk.Init.constructor_new(ldk.InitFeatures.constructor_known(), ldk.Option_NetAddressZ.constructor_none()));
149-
chan_man_b.as_ChannelMessageHandler().peer_connected(chan_man_a.get_our_node_id(), ldk.Init.constructor_new(ldk.InitFeatures.constructor_known(), ldk.Option_NetAddressZ.constructor_none()));
167+
a.chan_man.as_ChannelMessageHandler().peer_connected(b.chan_man.get_our_node_id(), ldk.Init.constructor_new(ldk.InitFeatures.constructor_known(), ldk.Option_NetAddressZ.constructor_none()));
168+
b.chan_man.as_ChannelMessageHandler().peer_connected(a.chan_man.get_our_node_id(), ldk.Init.constructor_new(ldk.InitFeatures.constructor_known(), ldk.Option_NetAddressZ.constructor_none()));
150169

151-
const chan_create_err = chan_man_a.create_channel(chan_man_b.get_our_node_id(), BigInt(0), BigInt(400), BigInt(0), ldk.UserConfig.constructor_default());
170+
const chan_create_err = a.chan_man.create_channel(b.chan_man.get_our_node_id(), BigInt(0), BigInt(400), BigInt(0), ldk.UserConfig.constructor_default());
152171
if (chan_create_err.is_ok()) return false;
153172
if (!(chan_create_err instanceof ldk.Result__u832APIErrorZ_Err)) return false;
154173
if (!(chan_create_err.err instanceof ldk.APIError_APIMisuseError)) return false;
155174
if (chan_create_err.err.err != "Channel value must be at least 1000 satoshis. It was 0") return false;
156175

157-
const chan_create_res = chan_man_a.create_channel(chan_man_b.get_our_node_id(), BigInt(1000000), BigInt(400), BigInt(0), ldk.UserConfig.constructor_default());
176+
const chan_create_res = a.chan_man.create_channel(b.chan_man.get_our_node_id(), BigInt(1000000), BigInt(400), BigInt(0), ldk.UserConfig.constructor_default());
158177
if (!chan_create_res.is_ok()) return false;
159178

160-
if (!exchange_messages(chan_man_a, chan_man_b)) return false;
179+
if (!exchange_messages(a.chan_man, b.chan_man)) return false;
161180

162-
const events: Array<ldk.Event> = [];
163-
const event_handler = ldk.EventHandler.new_impl({
164-
handle_event(event: ldk.Event): void {
165-
events.push(event);
166-
}
167-
} as ldk.EventHandlerInterface);
168-
169-
chan_man_a.as_EventsProvider().process_pending_events(event_handler);
170-
if (events.length != 1) return false;
171-
if (!(events[0] instanceof ldk.Event_FundingGenerationReady)) return false;
181+
const event = get_event(a.chan_man) as ldk.Event_FundingGenerationReady;
172182

173183
// (very) manually create a funding transaction
174-
const witness_pos = events[0].output_script.length + 58;
184+
const witness_pos = event.output_script.length + 58;
175185
const funding_tx = new Uint8Array(witness_pos + 7);
176186
funding_tx[0] = 2; // 4-byte tx version 2
177187
funding_tx[4] = 0; funding_tx[5] = 1; // segwit magic bytes
@@ -180,23 +190,91 @@ tests.push(async () => {
180190
funding_tx[43] = 0; // 1-byte input script length 0
181191
funding_tx[44] = 0xff; funding_tx[45] = 0xff; funding_tx[46] = 0xff; funding_tx[47] = 0xff; // 4-byte nSequence
182192
funding_tx[48] = 1; // one output
183-
assign_u64(funding_tx, 49, events[0].channel_value_satoshis);
184-
funding_tx[57] = events[0].output_script.length; // 1-byte output script length
185-
funding_tx.set(events[0].output_script, 58);
193+
assign_u64(funding_tx, 49, event.channel_value_satoshis);
194+
funding_tx[57] = event.output_script.length; // 1-byte output script length
195+
funding_tx.set(event.output_script, 58);
186196
funding_tx[witness_pos] = 1; funding_tx[witness_pos + 1] = 1; funding_tx[witness_pos + 2] = 0xff; // one witness element of size 1 with contents 0xff
187197
funding_tx[witness_pos + 3] = 0; funding_tx[witness_pos + 4] = 0; funding_tx[witness_pos + 5] = 0; funding_tx[witness_pos + 6] = 0; // lock time 0
188198

189-
const funding_res = chan_man_a.funding_transaction_generated(events[0].temporary_channel_id, events[0].counterparty_node_id, funding_tx);
199+
const funding_res = a.chan_man.funding_transaction_generated(event.temporary_channel_id, event.counterparty_node_id, funding_tx);
190200
if (!(funding_res instanceof ldk.Result_NoneAPIErrorZ_OK)) return false;
191201

192-
if (!exchange_messages(chan_man_a, chan_man_b)) return false;
202+
if (!exchange_messages(a.chan_man, b.chan_man)) return false;
193203

194-
const tx_broadcasted: Uint8Array = (await peer_a[1]) as Uint8Array;
204+
const tx_broadcasted: Uint8Array = (await a.tx_broadcasted);
195205
if (!array_eq(tx_broadcasted, funding_tx)) return false;
196206

197207
return true;
198208
});
199209

210+
tests.push(async () => {
211+
const a = get_chanman();
212+
const b = get_chanman();
213+
214+
const ignorer = ldk.IgnoringMessageHandler.constructor_new();
215+
const pm_a = ldk.PeerManager.constructor_new(a.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), a.node_secret, a.node_secret, a.logger, ignorer.as_CustomMessageHandler());
216+
const pm_b = ldk.PeerManager.constructor_new(b.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), b.node_secret, b.node_secret, b.logger, ignorer.as_CustomMessageHandler());
217+
218+
var sock_b: ldk.SocketDescriptor;
219+
const sock_a = ldk.SocketDescriptor.new_impl({
220+
send_data(data: Uint8Array, resume_read: boolean): number {
221+
console.assert(pm_b.read_event(sock_b, data) instanceof ldk.Result_boolPeerHandleErrorZ_OK);
222+
return data.length;
223+
},
224+
disconnect_socket(): void {
225+
console.assert(false);
226+
},
227+
eq(other: ldk.SocketDescriptor): boolean {
228+
return other.hash() == this.hash();
229+
},
230+
hash(): bigint {
231+
return BigInt(1);
232+
}
233+
} as ldk.SocketDescriptorInterface);
234+
sock_b = ldk.SocketDescriptor.new_impl({
235+
send_data(data: Uint8Array, resume_read: boolean): number {
236+
console.assert(pm_a.read_event(sock_a, data) instanceof ldk.Result_boolPeerHandleErrorZ_OK);
237+
return data.length;
238+
},
239+
disconnect_socket(): void {
240+
console.assert(false);
241+
},
242+
eq(other: ldk.SocketDescriptor): boolean {
243+
return other.hash() == this.hash();
244+
},
245+
hash(): bigint {
246+
return BigInt(2);
247+
}
248+
} as ldk.SocketDescriptorInterface);
249+
250+
const v4_netaddr = ldk.NetAddress.constructor_ipv4(Uint8Array.from([42,0,42,1]), 9735);
251+
console.assert(pm_b.new_inbound_connection(sock_b, ldk.Option_NetAddressZ.constructor_some(v4_netaddr)) instanceof ldk.Result_NonePeerHandleErrorZ_OK);
252+
const init_bytes = pm_a.new_outbound_connection(b.node_id, sock_a, ldk.Option_NetAddressZ.constructor_none());
253+
if (!(init_bytes instanceof ldk.Result_CVec_u8ZPeerHandleErrorZ_OK)) return false;
254+
console.assert(pm_b.read_event(sock_b, init_bytes.res) instanceof ldk.Result_boolPeerHandleErrorZ_OK);
255+
256+
console.assert(pm_a.get_peer_node_ids().length == 0);
257+
console.assert(pm_b.get_peer_node_ids().length == 0);
258+
259+
pm_b.process_events();
260+
pm_a.process_events();
261+
pm_b.process_events();
262+
263+
console.assert(pm_a.get_peer_node_ids().length == 1);
264+
console.assert(pm_b.get_peer_node_ids().length == 1);
265+
266+
const chan_create_res = a.chan_man.create_channel(b.node_id, BigInt(1000000), BigInt(400), BigInt(0), ldk.UserConfig.constructor_default());
267+
if (!chan_create_res.is_ok()) return false;
268+
269+
pm_a.process_events();
270+
pm_b.process_events();
271+
272+
const event = get_event(a.chan_man);
273+
if (!(event instanceof ldk.Event_FundingGenerationReady)) return false;
274+
275+
return true;
276+
});
277+
200278
async function run_tests(check_leaks: boolean) {
201279
var test_runs = [];
202280
for (const test of tests) {

0 commit comments

Comments
 (0)