Skip to content

Commit

Permalink
refactor: Remove deprecated http rpc client api export and related co…
Browse files Browse the repository at this point in the history
…de. Remove LoadBalancer. Use only one node for HTTP rpc requests. Remove http.js. Other small cleanups.
  • Loading branch information
Zurcusa committed Sep 19, 2024
1 parent 2d65bfe commit 50cb3fb
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 194 deletions.
11 changes: 0 additions & 11 deletions src/main/java/com/limechain/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@

import com.limechain.client.HostNode;
import com.limechain.client.LightClient;
import com.limechain.rpc.RPCFunction;
import com.limechain.rpc.RpcClient;
import com.limechain.rpc.server.RpcApp;
import com.limechain.utils.DivLogger;
import org.teavm.jso.JSBody;
import org.teavm.jso.core.JSString;

import java.util.logging.Level;

public class Main {

private static final String RPC_VARIABLE_NAME = "rpc";

private static final DivLogger log = new DivLogger();

public static void main(String[] args) {
log.log("Starting LimeChain node...");
exportAPI(RpcClient::sendRpcRequest, JSString.valueOf(RPC_VARIABLE_NAME));

RpcApp rpcApp = new RpcApp();
rpcApp.start();
Expand All @@ -31,8 +24,4 @@ public static void main(String[] args) {
client.start();
log.log(Level.INFO, "\uD83D\uDE80Started light client!");
}

@JSBody(params = {"f", "apiName"}, script = "window[apiName] = f;" +
"window.fruzhin.HTTP.changeRpcExported(true);")
private static native void exportAPI(RPCFunction f, JSString apiName);
}
9 changes: 2 additions & 7 deletions src/main/java/com/limechain/config/HostConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.limechain.utils.DivLogger;
import lombok.Getter;

import java.util.List;
import java.util.logging.Level;

/**
Expand Down Expand Up @@ -38,9 +37,6 @@ public HostConfig() {

log.log(Level.INFO, "Loading rpcNodeAddress...");
switch (chain.getValue()) {
case "POLKADOT", "LOCAL":
rpcNodeAddress = RpcConstants.POLKADOT_WS_RPC;
break;
case "KUSAMA":
rpcNodeAddress = RpcConstants.KUSAMA_WS_RPC;
break;
Expand Down Expand Up @@ -68,12 +64,11 @@ public String getGenesisPath() {
};
}

public List<String> getHttpsRpcEndpoints() {
public String getHttpsRpcEndpoint() {
return switch (chain) {
case POLKADOT -> RpcConstants.POLKADOT_HTTPS_RPC;
case KUSAMA -> RpcConstants.KUSAMA_HTTPS_RPC;
case WESTEND -> RpcConstants.WESTEND_HTTPS_RPC;
case LOCAL -> List.of();
case POLKADOT, LOCAL -> RpcConstants.POLKADOT_HTTPS_RPC;
};
}
}
20 changes: 3 additions & 17 deletions src/main/java/com/limechain/constants/RpcConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,13 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.List;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class RpcConstants {
public static final String POLKADOT_WS_RPC = "wss://rpc.polkadot.io";
public static final String KUSAMA_WS_RPC = "wss://kusama-rpc.polkadot.io";
public static final String WESTEND_WS_RPC = "wss://westend-rpc.polkadot.io";

public static final List<String> POLKADOT_HTTPS_RPC = List.of(
"https://rpc.ibp.network/polkadot",
"https://polkadot-rpc.dwellir.com",
"https://rpc.polkadot.io"
);
public static final List<String> KUSAMA_HTTPS_RPC = List.of(
"https://rpc.ibp.network/kusama",
"https://kusama-rpc.dwellir.com",
"https://kusama-rpc.polkadot.io"
);
public static final List<String> WESTEND_HTTPS_RPC = List.of(
"https://rpc.ibp.network/westend",
"https://westend-rpc.dwellir.com",
"https://westend-rpc.polkadot.io"
);
public static final String POLKADOT_HTTPS_RPC = "https://rpc.polkadot.io";
public static final String KUSAMA_HTTPS_RPC = "https://kusama-rpc.polkadot.io";
public static final String WESTEND_HTTPS_RPC = "https://westend-rpc.polkadot.io";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import com.limechain.rpc.dto.ChainGetHeaderResult;
import com.limechain.rpc.server.AppBean;
import com.limechain.sync.warpsync.WarpSyncState;
import com.limechain.teavm.TeaVMScheduler;
import com.limechain.utils.RpcUtils;
import com.limechain.utils.Stopwatch;
import lombok.extern.java.Log;
import org.teavm.jso.browser.Window;

import java.util.logging.Level;

Expand Down Expand Up @@ -46,7 +46,7 @@ public void sendHandshake() {
}

private static void registerFallbackRpcScheduler(Stopwatch stopwatch) {
TeaVMScheduler.schedule(() -> {
Window.setInterval(() -> new Thread(() -> {
if (stopwatch.getElapsedTime() > FALLBACK_THRESHOLD) {
ChainGetHeaderResult rpcResult = ChainRpcClient.getHeader(null);
BlockHeader fallbackHeader = RpcUtils.toBlockHeader(rpcResult);
Expand All @@ -57,6 +57,6 @@ private static void registerFallbackRpcScheduler(Stopwatch stopwatch) {
" parentHash:" + fallbackHeader.getParentHash() +
" stateRoot:" + fallbackHeader.getStateRoot());
}
}, 6_000);
}).start(), 6_000);
}
}
7 changes: 2 additions & 5 deletions src/main/java/com/limechain/rpc/ChainRpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ public static Hash256 getLastFinalizedBlockHash() {
}

public static ChainGetHeaderResult getHeader(String blockHash) {
List<Object> params = blockHash == null
? List.of()
: List.of(blockHash);

RpcResponse response = sendRpcRequest(RpcMethod.CHAIN_GET_HEADER, params);
RpcResponse response = sendRpcRequest(RpcMethod.CHAIN_GET_HEADER,
blockHash == null ? List.of() : List.of(blockHash));
return getResult(response, ChainGetHeaderResult.class);
}
}
26 changes: 0 additions & 26 deletions src/main/java/com/limechain/rpc/LoadBalancer.java

This file was deleted.

20 changes: 4 additions & 16 deletions src/main/java/com/limechain/rpc/RpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,10 @@
public sealed class RpcClient permits ChainRpcClient, GrandpaRpcClient {

private static final String POST = "POST";
private static final String HTTP_RPC_ENDPOINT = AppBean.getBean(HostConfig.class).getHttpsRpcEndpoint();
private static final AtomicInteger ID_COUNTER = new AtomicInteger(1);
private static final LoadBalancer LOAD_BALANCER = new LoadBalancer(AppBean.getBean(HostConfig.class));
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(false);

/**
* Send an RPC request. Currently used only by the exported RPC client.
*
* @param method {@link String} representation of the RPC method name. For example "system_name".
* @param params An array of parameters for the sent RPC request.
* @return The {@link String} representation of the received RPC json result.
*/
public static String sendRpcRequest(String method, Object[] params) {
return HttpRequest.createHttpRequest(POST, LOAD_BALANCER.getNextEndpoint(),
createRpcRequestJson(method, List.of(params)));
}

/**
* Send an RPC request. Used by the specific implementations of the RpcClient.
*
Expand All @@ -45,8 +33,8 @@ public static String sendRpcRequest(String method, Object[] params) {
* @return The {@link RpcResponse} representation of the received RPC json result.
*/
protected static RpcResponse sendRpcRequest(RpcMethod method, List<Object> params) {
String jsonResult = HttpRequest.createHttpRequest(POST, LOAD_BALANCER.getNextEndpoint(),
createRpcRequestJson(method.getMethod(), params));
String jsonResult = HttpRequest.sendHttpRequest(POST, HTTP_RPC_ENDPOINT,
createRpcRequestJson(method.getMethod(), params));
return OBJECT_MAPPER.mapToClass(jsonResult, RpcResponse.class);
}

Expand All @@ -67,7 +55,7 @@ private static String createRpcRequestJson(String method, List<Object> params) {
protected static <T> T getResult(RpcResponse response, Class<T> klazz) {
if (response.getError() != null) {
throw new IllegalStateException("RPC request resulted in an error with code:" + response.getError().getCode()
+ " and message:" + response.getError().getMessage());
+ " and message:" + response.getError().getMessage());
}

return OBJECT_MAPPER.mapToClass(JsonUtil.stringify(response.getResult()), klazz);
Expand Down
31 changes: 23 additions & 8 deletions src/main/java/com/limechain/teavm/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
public class HttpRequest {

@Async
public static native String asyncHttpRequest(String method, String url, String body);
public static native String sendHttpRequest(String method, String url, String body);

private static void asyncHttpRequest(String method, String url, String body, AsyncCallback<String> callback) {
createAsyncHttpRequest(method, url, body, (error, response) -> {
private static void sendHttpRequest(String method, String url, String body, AsyncCallback<String> callback) {
sendHttpRequestNative(method, url, body, (error, response) -> {
if (error != null) {
log.log(Level.WARNING, error.getMessage());
} else {
Expand All @@ -23,11 +23,26 @@ private static void asyncHttpRequest(String method, String url, String body, Asy
});
}

@JSBody(params = {"method", "url", "body", "callback"}, script = "return window.fruzhin.HTTP.asyncHttpRequest(method, url, body, callback);")
public static native void createAsyncHttpRequest(String method, String url, String body, TeaVMCallback callback);

@JSBody(params = {"method", "url", "body"}, script = "return window.fruzhin.HTTP.httpRequestSync(method, url, body);")
public static native String createHttpRequest(String method, String url, String body);
@JSBody(params = {"method", "url", "body", "callback"}, script = "fetch(" +
"url, {" +
" method: method," +
" headers: {" +
" 'Content-Type': 'application/json'}," +
" body: method === 'POST' ? body : undefined" +
"})" +
".then(response => {" +
" if (!response.ok) {" +
" throw new Error(`Request failed with status: ${response.status}`);" +
" }" +
" return response.text();" +
"})" +
".then(result => {" +
" callback(null, result);" +
"})" +
".catch(error => {" +
" callback(new Error(`Error during sending request: ${error.message}`), null);" +
"});")
public static native void sendHttpRequestNative(String method, String url, String body, TeaVMCallback callback);
}


Expand Down
20 changes: 0 additions & 20 deletions src/main/java/com/limechain/teavm/TeaVMScheduler.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/com/limechain/utils/json/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public static String stringify(Object object) {
}

public static String readJsonFromFile(String filePath) {
return HttpRequest.asyncHttpRequest("GET", filePath, null);
return HttpRequest.sendHttpRequest("GET", filePath, null);
}
}
2 changes: 0 additions & 2 deletions src/main/webapp/js/fruzhin-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import 'https://unpkg.com/[email protected]/dist/index.min.js';
import 'https://unpkg.com/@muradsenteca/[email protected]/dist/index.min.js'
import 'https://unpkg.com/@muradsenteca/[email protected]/dist/index.min.js'

import * as HTTP from './http.js';
import * as Fruzhin from './fruzhin.js'

var startLibp2p = async (bootnodes) => {
Expand Down Expand Up @@ -45,6 +44,5 @@ var startLibp2p = async (bootnodes) => {

window.fruzhin = {
startLibp2p,
HTTP,
...Fruzhin,
}
78 changes: 0 additions & 78 deletions src/main/webapp/js/http.js

This file was deleted.

0 comments on commit 50cb3fb

Please sign in to comment.