Permalink
Browse files

Proof-of-concept Ethereum RPC client(s)

  • Loading branch information...
msgilligan committed Jul 31, 2016
1 parent 9234e64 commit c0980a931d74bf5bce2d04ef03dcf4733eb01e0f
@@ -0,0 +1,9 @@
package com.msgilligan.ethereum.rpc

import com.msgilligan.bitcoinj.rpc.DynamicRPCFallback

/**
* Use `methodMissing` to implement Ethereum RPC calls dynamically
*/
class EthereumScriptingClient extends EthereumClient implements DynamicRPCFallback {
}
@@ -0,0 +1,58 @@
package com.msgilligan.ethereum.rpc;

import com.msgilligan.bitcoinj.rpc.JsonRPCStatusException;
import com.msgilligan.bitcoinj.rpc.RPCClient;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
* A partial implementation of an Ethereum RPC Client
*/
public class EthereumClient extends RPCClient {
public static URI DEFAULT_LOCALHOST;

static {
try {
DEFAULT_LOCALHOST = new URI("http://localhost:8545");
} catch (URISyntaxException e) {
DEFAULT_LOCALHOST = null;
}
}

/**
* Construct a JSON-RPC client from URI, username, and password
*
* @param server server URI should not contain username/password
* @param rpcuser username for the RPC HTTP connection
* @param rpcpassword password for the RPC HTTP connection
*/
private EthereumClient(URI server, String rpcuser, String rpcpassword) {
super(server, rpcuser, rpcpassword);
}

public EthereumClient() {
super(DEFAULT_LOCALHOST, null, null);
}

public String ethProtocolVersion() throws IOException, JsonRPCStatusException {
return this.send("eth_protocolVersion");
}

public long ethBlockNumber() throws IOException, JsonRPCStatusException {
String blockNumString = this.send("eth_blockNumber");
long blockNum = Long.decode(blockNumString);
return blockNum;
}

public boolean minerStart(int numberOfThreads) throws IOException, JsonRPCStatusException {
return this.send("miner_start", "0x" + Integer.toHexString(numberOfThreads));
}

public boolean minerStop() throws IOException, JsonRPCStatusException {
return this.send("miner_stop");
}

}
@@ -0,0 +1,63 @@
package com.msgilligan.ethereum.rpc

import spock.lang.Ignore
import spock.lang.Specification

/**
* Test the few implemented static methods
*/
@Ignore("Should be an integration test")
class EthereumClientSpec extends Specification {

def "constructor works correctly" () {
when:
def client = new EthereumClient()

then:
client.serverURI == EthereumClient.DEFAULT_LOCALHOST
}

def "can check eth version" () {
given:
def client = new EthereumClient()

when:
def version = client.ethProtocolVersion()

then:
version == "0x3f"
}

def "can check eth block number" () {
given:
def client = new EthereumClient()

when:
long blockNumber = client.ethBlockNumber()

then:
blockNumber >= 0
}

def "can start mining" () {
given:
def client = new EthereumClient()

when:
def result = client.minerStart(3)

then:
result == true
}

def "can stop mining" () {
given:
def client = new EthereumClient()

when:
def result = client.minerStop()

then:
result == true
}
}
@@ -0,0 +1,24 @@
package com.msgilligan.ethereum.rpc

import spock.lang.Ignore
import spock.lang.Specification

/**
* Test dynamic calls
*/
@Ignore("Should be an integration test")
class EthereumScriptingClientSpec extends Specification {


def "check if geth is mining" () {
given:
def client = new EthereumScriptingClient()

when:
def mining = client.eth_mining()

then:
mining == false
}

}

0 comments on commit c0980a9

Please sign in to comment.