Skip to content

Commit

Permalink
chore: return string instead of array
Browse files Browse the repository at this point in the history
  • Loading branch information
ablax committed Aug 26, 2024
1 parent 7a21b86 commit 25164df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ teavm.js {
targetFileName = "fruzhin.js"
}


//TODO: Debug only. Remove when doing release build
teavm {
js {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,21 @@ public WarpSyncResponse send(WarpSyncRequest req, String protocolId) {
System.out.println("Request: " + req.getBlockHash());
final var lock = new Object();

JSPromise<JSArray<Byte>> objectJSPromise = sendRequest(StringUtils.toHex(req.getBlockHash().getBytes()), protocolId);
JSPromise<String> objectJSPromise =
sendRequest(StringUtils.toHex(req.getBlockHash().getBytes()), protocolId);

objectJSPromise.then((ttt) -> {
System.out.println(ttt);
System.out.println(ttt.get(0));
System.out.println(ttt.get(ttt.getLength() - 1));
byte[] bytes = new byte[ttt.getLength()];
for (int i = 0; i < ttt.getLength(); i++) {
//bytes[i] = (byte) ((int) ttt.get(i)); //fails here
}
// byte[] bytes = StringUtils.fromHex(ttt);
System.out.println("Received response: " + " " + bytes);
System.out.println("Received response: " + ttt);
System.out.println("Received response len: " + ttt.length());
byte[] bytes = StringUtils.fromHex(ttt);
System.out.println("Received response: " + bytes);
System.out.println("Received response len: " + bytes.length);

synchronized (lock) {
synchronized (lock) {
// System.out.println("Received response: " + bytes.length + " " + bytes);
ScaleCodecReader scaleCodecReader = new ScaleCodecReader(bytes);
WarpSyncResponse responseaa = new WarpSyncResponseScaleReader().read(scaleCodecReader);
System.out.println(responseaa);
System.out.println(responseaa);
// response.set(result);
lock.notify();
}
Expand All @@ -95,11 +91,13 @@ public WarpSyncResponse send(WarpSyncRequest req, String protocolId) {
}

@JSBody(params = {"blockHash", "protocolId"}, script = "return (async () => {" +
"let peer = libp.getConnections()[0].remotePeer;" +
"let stream = await ItPbStream.pbStream(await libp.dialProtocol(peer, protocolId));" +
"stream.writeLP(new Uint8Array([...blockHash.matchAll(/../g)].map(m => parseInt(m[0], 16))));" +
"return Array.from((await stream.readLP()).subarray());})()")
private static native JSPromise<JSArray<Byte>> sendRequest(String blockHash, String protocolId);
" let peer = libp.getConnections()[0].remotePeer;" +
" let stream = await ItPbStream.pbStream(await libp.dialProtocol(peer, protocolId));" +
" stream.writeLP(new Uint8Array([...blockHash.matchAll(/../g)].map(m => parseInt(m[0], 16))));" +
" let bytes = (await stream.readLP()).subarray();" +
" return [...bytes].map(n => n.toString(16)).join('');" +
"})()")
private static native JSPromise<String> sendRequest(String blockHash, String protocolId);

}
}

0 comments on commit 25164df

Please sign in to comment.