Skip to content

Commit

Permalink
Merge pull request #46 from XDagger/feature/rpc
Browse files Browse the repository at this point in the history
Feature/rpc #13
  • Loading branch information
punk8 authored May 31, 2021
2 parents 9b50cb1 + 8d17f31 commit 3237c83
Show file tree
Hide file tree
Showing 95 changed files with 5,595 additions and 96 deletions.
39 changes: 39 additions & 0 deletions docs/XdagJ_RPC_Service_end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# XDAGJ RPC Service Tutorial

This section describes XDAGJ RPC service.

- [System environment](#system-environment)
- [Usage](#usage)

## System environment

```yaml
JDK : v15
script : xdag.sh
jar : xdagj-0.x.y-shaded.jar
```
Please make sure that the above environment is already available in your operating system, and the JDK version must be 15
## Usage
If you want to open the rpc service,you need to set "isRPCEnabled = true" in your configuration file.
The configuration file is located in `src/main/resources/xdag-XXX.config`, if you do not modify it, the default configuration is disabled.

- Show XDAG synchronization status

request
```js
curl http://localhost:4444/ -s -X POST -H "Content-Type: application/json" --data "{\"jsonrpc\":\"2.0\",\"method\":\"xdag_syncing\",\"params\":[],\"id\":1}"
```
response
```json
{"jsonrpc":"2.0","id":1,"result":{"currentBlock":"0x30331","highestBlock":"0x30331"}}
```





27 changes: 27 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,33 @@
</exclusions>
</dependency>

<!-- https://mvnrepository.com/artifact/com.github.briandilley.jsonrpc4j/jsonrpc4j -->
<dependency>
<groupId>com.github.briandilley.jsonrpc4j</groupId>
<artifactId>jsonrpc4j</artifactId>
<version>1.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-ri</artifactId>
<version>2.3.0</version>
<type>pom</type>
</dependency>

<!--config-->
<!-- https://mvnrepository.com/artifact/com.typesafe/config -->
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.4.1</version>
</dependency>

</dependencies>

<profiles>
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/io/xdag/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,26 @@
import io.xdag.net.message.NetDB;
import io.xdag.net.node.NodeManager;
import io.xdag.randomx.RandomX;
import io.xdag.rpc.Web3;
import io.xdag.rpc.Web3Impl;
import io.xdag.rpc.cors.CorsConfiguration;
import io.xdag.rpc.modules.web3.Web3XdagModule;
import io.xdag.rpc.modules.web3.Web3XdagModuleImpl;
import io.xdag.rpc.modules.xdag.XdagModule;
import io.xdag.rpc.modules.xdag.XdagModuleTransactionDisabled;
import io.xdag.rpc.modules.xdag.XdagModuleTransactionEnabled;
import io.xdag.rpc.modules.xdag.XdagModuleWalletDisabled;
import io.xdag.rpc.netty.*;
import io.xdag.rpc.serialize.JacksonBasedRpcSerializer;
import io.xdag.rpc.serialize.JsonRpcSerializer;
import io.xdag.utils.XdagTime;
import io.xdag.wallet.Wallet;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -113,6 +127,14 @@ public class Kernel {
protected long startEpoch;


// rpc
protected JsonRpcWeb3ServerHandler jsonRpcWeb3ServerHandler;
protected Web3 web3;
protected Web3WebSocketServer web3WebSocketServer;
protected Web3HttpServer web3HttpServer;
protected JsonRpcWeb3FilterHandler jsonRpcWeb3FilterHandler;
protected JacksonBasedRpcSerializer jacksonBasedRpcSerializer;

public Kernel(Config config, Wallet wallet) {
this.config = config;
this.wallet = wallet;
Expand Down Expand Up @@ -278,6 +300,14 @@ public synchronized void testStart() throws Exception {
xdagState = XdagState.WTST;
}

// ====================================
// rpc start
// ====================================
if (config.getRPCSpec().isRPCEnabled()) {
getWeb3HttpServer().start();
getWeb3WebSocketServer().start();
}

// ====================================
// telnet server
// ====================================
Expand All @@ -286,6 +316,80 @@ public synchronized void testStart() throws Exception {
Launcher.registerShutdownHook("kernel", this::testStop);
}

private Web3 getWeb3() {
if (web3 == null) {
web3 = buildWeb3();
}

return web3;
}

private Web3 buildWeb3() {
Web3XdagModule web3XdagModule = new Web3XdagModuleImpl(new XdagModule((byte) 0x1,new XdagModuleWalletDisabled(),new XdagModuleTransactionEnabled(this.getBlockchain())),this);
return new Web3Impl(web3XdagModule);
}

private JsonRpcWeb3ServerHandler getJsonRpcWeb3ServerHandler() {
if (jsonRpcWeb3ServerHandler == null) {
jsonRpcWeb3ServerHandler = new JsonRpcWeb3ServerHandler(
getWeb3(),
config.getRPCSpec().getRpcModules()
);
}

return jsonRpcWeb3ServerHandler;
}

private Web3WebSocketServer getWeb3WebSocketServer() throws UnknownHostException {
if (web3WebSocketServer == null) {
JsonRpcSerializer jsonRpcSerializer = getJsonRpcSerializer();
XdagJsonRpcHandler jsonRpcHandler = new XdagJsonRpcHandler(jsonRpcSerializer);
web3WebSocketServer = new Web3WebSocketServer(
InetAddress.getByName(config.getRPCSpec().getRPCHost()),
config.getRPCSpec().getRPCPortByWebSocket(),
jsonRpcHandler,
getJsonRpcWeb3ServerHandler()
);
}

return web3WebSocketServer;
}

private Web3HttpServer getWeb3HttpServer() throws UnknownHostException {
if (web3HttpServer == null) {
web3HttpServer = new Web3HttpServer(
InetAddress.getByName(config.getRPCSpec().getRPCHost()),
config.getRPCSpec().getRPCPortByHttp(),
123,
true,
new CorsConfiguration("*"),
getJsonRpcWeb3FilterHandler(),
getJsonRpcWeb3ServerHandler()
);
}

return web3HttpServer;
}

private JsonRpcWeb3FilterHandler getJsonRpcWeb3FilterHandler() throws UnknownHostException {
if (jsonRpcWeb3FilterHandler == null) {
jsonRpcWeb3FilterHandler = new JsonRpcWeb3FilterHandler(
"*",
InetAddress.getByName(config.getRPCSpec().getRPCHost()),
null
);
}

return jsonRpcWeb3FilterHandler;
}

private JsonRpcSerializer getJsonRpcSerializer() {
if (jacksonBasedRpcSerializer == null) {
jacksonBasedRpcSerializer = new JacksonBasedRpcSerializer();
}

return jacksonBasedRpcSerializer;
}
/** Stops the kernel. */
public synchronized void testStop() {

Expand All @@ -295,6 +399,14 @@ public synchronized void testStop() {

isRunning.set(false);

//
if (web3HttpServer != null) {
web3HttpServer.stop();
}
if (web3WebSocketServer != null) {
web3WebSocketServer.stop();
}

// 1. 工作层关闭
// stop consensus
sync.stop();
Expand Down
96 changes: 95 additions & 1 deletion src/main/java/io/xdag/config/AbstractConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
package io.xdag.config;

import cn.hutool.setting.Setting;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigObject;
import io.xdag.config.spec.*;
import io.xdag.core.XdagField;
import io.xdag.crypto.DnetKeys;
import io.xdag.crypto.jni.Native;
import io.xdag.rpc.modules.ModuleDescription;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -42,7 +45,7 @@
@Slf4j
@Getter
@Setter
public class AbstractConfig implements Config, AdminSpec, PoolSpec, NodeSpec, WalletSpec {
public class AbstractConfig implements Config, AdminSpec, PoolSpec, NodeSpec, WalletSpec, RPCSpec {
protected String configName;

// =========================
Expand Down Expand Up @@ -127,6 +130,15 @@ public class AbstractConfig implements Config, AdminSpec, PoolSpec, NodeSpec, Wa
protected long apolloForkAmount;


// =========================
// Xdag RPC modules
// =========================
protected List<ModuleDescription> moduleDescriptions;
protected boolean rpcEnabled = false;
protected String rpcHost;
protected int rpcPortHttp;
protected int rpcPortWs;

public void setDir() {
storeDir = getRootDir() + "/rocksdb/xdagdb";
storeBackupDir = getRootDir() + "/rocksdb/xdagdb/backupdata";
Expand Down Expand Up @@ -167,6 +179,11 @@ public void initKeys() throws Exception {
}
}

@Override
public RPCSpec getRPCSpec() {
return this;
}

@Override
public PoolSpec getPoolSpec() {
return this;
Expand Down Expand Up @@ -226,6 +243,13 @@ public void getSetting() {
if (bootnodelist != null) {
bootnodes.addAll(Arrays.asList(bootnodelist));
}
// rpc
rpcEnabled = setting.getBool("isRPCEnabled") != null && setting.getBool("isRPCEnabled");
if (rpcEnabled) {
rpcHost = setting.getStr("rpcHost");
rpcPortHttp = setting.getInt("rpcPort_http");
rpcPortWs = setting.getInt("rpcPort_ws");
}
}

@Override
Expand Down Expand Up @@ -310,4 +334,74 @@ public void setDnetKeyBytes(byte[] dnetKeyBytes) {
public boolean enableRefresh() {
return this.enableRefresh;
}

@Override
public List<ModuleDescription> getRpcModules() {

if (!rpcEnabled) {
return null;
}

if (this.moduleDescriptions != null) {
return this.moduleDescriptions;
}

List<ModuleDescription> modules = new ArrayList<>();

com.typesafe.config.Config configFromFiles = ConfigFactory.load("rpc_modules");
List<? extends ConfigObject> list = configFromFiles.getObjectList("rpc.modules");

for (ConfigObject configObject : list) {
com.typesafe.config.Config configElement = configObject.toConfig();
String name = configElement.getString("name");
String version = configElement.getString("version");
boolean enabled = configElement.getBoolean("enabled");
List<String> enabledMethods = null;
List<String> disabledMethods = null;

if (configElement.hasPath("methods.enabled")) {
enabledMethods = configElement.getStringList("methods.enabled");
}

if (configElement.hasPath("methods.disabled")) {
disabledMethods = configElement.getStringList("methods.disabled");
}

modules.add(new ModuleDescription(name, version, enabled, enabledMethods, disabledMethods));
}

this.moduleDescriptions = modules;

// TODO: get modules from config
// String name = "xdag";
// String version = "1.0";
// boolean enabled = true;
// List<String> enabledMethods = null;
// List<String> disabledMethods = null;
//
// modules.add(new ModuleDescription(name, version, enabled, enabledMethods, disabledMethods));
// this.moduleDescriptions = modules;

return modules;
}

@Override
public boolean isRPCEnabled() {
return rpcEnabled;
}

@Override
public String getRPCHost() {
return rpcHost;
}

@Override
public int getRPCPortByHttp() {
return rpcPortHttp;
}

@Override
public int getRPCPortByWebSocket() {
return rpcPortWs;
}
}
12 changes: 8 additions & 4 deletions src/main/java/io/xdag/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
package io.xdag.config;

import io.xdag.config.spec.AdminSpec;
import io.xdag.config.spec.PoolSpec;
import io.xdag.config.spec.NodeSpec;
import io.xdag.config.spec.WalletSpec;
import io.xdag.config.spec.*;
import io.xdag.core.XdagField;
import io.xdag.rpc.modules.ModuleDescription;

import java.util.List;

/**
* The Xdag blockchain configurations.
Expand Down Expand Up @@ -78,4 +78,8 @@ public interface Config {
void setDir();
void initKeys() throws Exception;

// rpc
// List<ModuleDescription> getRpcModules();
// boolean isRPCEnabled();
RPCSpec getRPCSpec();
}
Loading

0 comments on commit 3237c83

Please sign in to comment.