forked from LimeChain/Fruzhin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement functionality to make rpc call to other nodes.
- Loading branch information
Showing
19 changed files
with
275 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.limechain.rpc; | ||
|
||
import com.limechain.polkaj.Hash256; | ||
import com.limechain.rpc.dto.ChainGetHeaderResult; | ||
import com.limechain.rpc.dto.GrandpaRoundStateResult; | ||
import com.limechain.rpc.dto.RpcMethod; | ||
import com.limechain.rpc.dto.RpcResponse; | ||
|
||
import java.util.List; | ||
|
||
public final class BlockRpcClient extends RpcClient { | ||
|
||
public static Hash256 getLastFinalizedBlockHash() { | ||
RpcResponse response = sendRpcRequest(RpcMethod.CHAIN_GET_FINALIZED_HEAD, List.of()); | ||
return Hash256.from(getResult(response, String.class)); | ||
} | ||
|
||
public static ChainGetHeaderResult getHeader(String blockHash) { | ||
RpcResponse response = sendRpcRequest(RpcMethod.CHAIN_GET_HEADER, List.of(blockHash)); | ||
return getResult(response, ChainGetHeaderResult.class); | ||
} | ||
|
||
public static GrandpaRoundStateResult getGrandpaRoundState() { | ||
RpcResponse response = sendRpcRequest(RpcMethod.GRANDPA_ROUND_STATE, List.of()); | ||
return getResult(response, GrandpaRoundStateResult.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.limechain.rpc; | ||
|
||
import com.limechain.config.HostConfig; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class LoadBalancer { | ||
|
||
private final List<String> endpoints; | ||
private final AtomicInteger index; | ||
|
||
public LoadBalancer(HostConfig hostConfig) { | ||
this.endpoints = hostConfig.getHttpsRpcEndpoints(); | ||
this.index = new AtomicInteger(0); | ||
} | ||
|
||
public String getNextEndpoint() { | ||
int currentIndex = index.getAndUpdate(i -> (i + 1) % endpoints.size()); | ||
return endpoints.get(currentIndex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.limechain.rpc; | ||
|
||
import com.limechain.config.HostConfig; | ||
import com.limechain.rpc.dto.RpcMethod; | ||
import com.limechain.rpc.dto.RpcRequest; | ||
import com.limechain.rpc.dto.RpcResponse; | ||
import com.limechain.rpc.server.AppBean; | ||
import com.limechain.teavm.HttpRequest; | ||
import com.limechain.utils.json.JsonUtil; | ||
import com.limechain.utils.json.ObjectMapper; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public sealed class RpcClient permits BlockRpcClient { | ||
|
||
private static final String POST = "POST"; | ||
|
||
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); | ||
|
||
private static String createRpcRequestJson(String method, List<Object> params) { | ||
RpcRequest request = new RpcRequest(ID_COUNTER.getAndAdd(1), method, params); | ||
return JsonUtil.stringify(request); | ||
} | ||
|
||
protected static RpcResponse sendRpcRequest(RpcMethod method, List<Object> params) { | ||
String jsonResult = HttpRequest.asyncHttpRequest(POST, LOAD_BALANCER.getNextEndpoint(), | ||
createRpcRequestJson(method.getMethod(), params)); | ||
return OBJECT_MAPPER.mapToClass(jsonResult, RpcResponse.class); | ||
} | ||
|
||
protected static <T> T getResult(RpcResponse response, Class<T> type) { | ||
if (response.getError() != null) { | ||
throw new IllegalStateException("RPC request resulted in an error with code:" + response.getError().getCode() | ||
+ " and message:" + response.getError().getMessage()); | ||
} | ||
|
||
return OBJECT_MAPPER.mapToClass(JsonUtil.stringify(response.getResult()), type); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/limechain/rpc/dto/ChainGetHeaderResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.limechain.rpc.dto; | ||
|
||
import com.limechain.teavm.annotation.Reflectable; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Reflectable | ||
public class ChainGetHeaderResult { | ||
|
||
private String parentHash; | ||
private String number; | ||
private String stateRoot; | ||
private String extrinsicsRoot; | ||
private Digest digest; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.limechain.rpc.dto; | ||
|
||
import com.limechain.teavm.annotation.Reflectable; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Reflectable | ||
public class Digest { | ||
|
||
private List<String> logs; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/limechain/rpc/dto/GrandpaRoundStateResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.limechain.rpc.dto; | ||
|
||
import com.limechain.teavm.annotation.Reflectable; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Reflectable | ||
public class GrandpaRoundStateResult { | ||
|
||
private int setId; | ||
private RoundState best; | ||
private List<RoundState> background; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.limechain.rpc.dto; | ||
|
||
import com.limechain.teavm.annotation.Reflectable; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Reflectable | ||
public class RoundState { | ||
private int round; | ||
private int totalWeight; | ||
private int thresholdWeight; | ||
private Votes prevotes; | ||
private Votes precommits; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.limechain.rpc.dto; | ||
|
||
import com.limechain.teavm.annotation.Reflectable; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Reflectable | ||
public class RpcError { | ||
|
||
private int code; | ||
private String message; | ||
private String data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.limechain.rpc.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum RpcMethod { | ||
|
||
CHAIN_GET_FINALIZED_HEAD("chain_getFinalizedHead"), | ||
CHAIN_GET_HEADER("chain_getHeader"), | ||
GRANDPA_ROUND_STATE("grandpa_roundState"); | ||
|
||
RpcMethod(String method) { | ||
this.method = method; | ||
} | ||
|
||
private final String method; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.limechain.rpc.dto; | ||
|
||
import com.limechain.teavm.annotation.Reflectable; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Reflectable | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class RpcRequest { | ||
|
||
private int id; | ||
private final String jsonrpc = "2.0"; | ||
private String method; | ||
private List<Object> params; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.limechain.rpc.dto; | ||
|
||
import com.limechain.teavm.annotation.Reflectable; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Reflectable | ||
@NoArgsConstructor | ||
public class RpcResponse { | ||
|
||
private int id; | ||
private final String jsonrpc = "2.0"; | ||
private Object result; | ||
private RpcError error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.limechain.rpc.dto; | ||
|
||
import com.limechain.teavm.annotation.Reflectable; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Reflectable | ||
public class Votes { | ||
|
||
private int currentWeight; | ||
private List<String> missing; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ | |
import lombok.extern.java.Log; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Arrays; | ||
|
||
@Getter | ||
@Log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.