Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new methods to SDK (Query Interface) #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ task casperJar(type: Jar) {
with jar
}

task casperFatJar(type: Jar) {
archiveBaseName = 'casper-java-sdk'
archiveVersion = '0.3.0'
manifest.from jar.manifest
archiveClassifier = 'all'
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}


test {
useJUnitPlatform()
}
5 changes: 5 additions & 0 deletions src/main/java/com/casper/sdk/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class Properties {
public static String STATE_GET_AUCTION_INFO = "state_get_auction_info";
public static String INFO_GET_PEERS = "info_get_peers";
public static String INFO_GET_STATUS = "info_get_status";
public static String INFO_GET_DEPLOY = "info_get_deploy";
public static String CHAIN_GET_BLOCK = "chain_get_block";
public static String CHAIN_GET_BLOCK_TRANSFERS = "chain_get_block_transfers";
public static String CHAIN_GET_ERA_INFO_BY_SWITCH_BLOCK = "chain_get_era_info_by_switch_block";
public static String RPC_DISCOVER = "rpc.discover";

public static Map<String, String> properties = new HashMap<>();

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/casper/sdk/controller/CasperSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,24 @@ public String getNodePeers() throws Throwable {
return queryService.getNodePeers();
}

public String getBlock() throws Throwable {
return queryService.getBlock();
}

public String getBlockTransfers() throws Throwable {
return queryService.getBlockTransfers();
}

public String getDeployInfo(final String deployHash) throws Throwable {
return queryService.getDeployInfo(deployHash);
}

public String getEraInfoBySwitchBlock() throws Throwable {
return queryService.getEraInfoBySwitchBlock();
}

public String getRpcSchema() throws Throwable {
return queryService.getRpcSchema();
}

}
51 changes: 51 additions & 0 deletions src/main/java/com/casper/sdk/service/MethodEnums.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,57 @@ public String getValue(final String result) throws ValueNotFoundException {
throw new ValueNotFoundException("result not found");
}
}
},
CHAIN_GET_BLOCK{
@Override
public String getValue(final String result) throws ValueNotFoundException {
try{
final JsonNode node = new ObjectMapper().readTree(result);
return node.get("result").toPrettyString();
} catch (Exception e){
throw new ValueNotFoundException("result not found");
}
}
},CHAIN_GET_BLOCK_TRANSFERS{
@Override
public String getValue(final String result) throws ValueNotFoundException {
try{
final JsonNode node = new ObjectMapper().readTree(result);
return node.get("result").toPrettyString();
} catch (Exception e){
throw new ValueNotFoundException("result not found");
}
}
},INFO_GET_DEPLOY{
@Override
public String getValue(final String result) throws ValueNotFoundException {
try{
final JsonNode node = new ObjectMapper().readTree(result);
return node.get("result").toPrettyString();
} catch (Exception e){
throw new ValueNotFoundException("result not found");
}
}
},CHAIN_GET_ERA_INFO_BY_SWITCH_BLOCK{
@Override
public String getValue(final String result) throws ValueNotFoundException {
try{
final JsonNode node = new ObjectMapper().readTree(result);
return node.get("result").toPrettyString();
} catch (Exception e){
throw new ValueNotFoundException("result not found");
}
}
},RPC_DISCOVER {
@Override
public String getValue(final String result) throws ValueNotFoundException {
try {
final JsonNode node = new ObjectMapper().readTree(result);
return node.get("result").toPrettyString();
} catch (Exception e) {
throw new ValueNotFoundException("result not found");
}
}
};

private static final List<String> MAP = new ArrayList<>();
Expand Down
59 changes: 56 additions & 3 deletions src/main/java/com/casper/sdk/service/QueryService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.casper.sdk.service;

import static com.casper.sdk.Properties.*;

import com.casper.sdk.service.http.rpc.HttpMethods;
import com.casper.sdk.service.http.rpc.Method;

import java.util.Collections;
import java.util.HashMap;
import java.util.Optional;

import static com.casper.sdk.Properties.*;

/**
* Service to query the chain
* Methods call the HTTP methods with an instantiated method object
Expand Down Expand Up @@ -62,7 +62,8 @@ public String getAccountBalance(final String accountKey) throws Throwable {
public String getAccountMainPurseURef(final String accountKey) throws Throwable {

final Optional<String> result = httpMethods.rpcCallMethod(new Method(STATE_GET_ITEM,
new HashMap<>() { {
new HashMap<>() {
{
put("state_root_hash", getStateRootHash());
put("key", "account-hash-" + HashService.getAccountHash(accountKey));
put("path", Collections.emptyList());
Expand Down Expand Up @@ -104,5 +105,57 @@ public String getNodeStatus() throws Throwable {

}

public String getBlock() throws Throwable {

final Optional<String> result = httpMethods.rpcCallMethod(new Method(CHAIN_GET_BLOCK,
new HashMap<>()));

return (result.isEmpty()) ? null
: MethodEnums.CHAIN_GET_BLOCK.getValue(result.get());

}

public String getBlockTransfers() throws Throwable {

final Optional<String> result = httpMethods.rpcCallMethod(new Method(CHAIN_GET_BLOCK_TRANSFERS,
new HashMap<>()));

return (result.isEmpty()) ? null
: MethodEnums.CHAIN_GET_BLOCK_TRANSFERS.getValue(result.get());

}

public String getDeployInfo(String deployHash) throws Throwable {

final Optional<String> result = httpMethods.rpcCallMethod(new Method(INFO_GET_DEPLOY,
new HashMap<>() {
{
put("deploy_hash", deployHash);
}
}));

return (result.isEmpty()) ? null
: MethodEnums.INFO_GET_DEPLOY.getValue(result.get());

}

public String getEraInfoBySwitchBlock() throws Throwable {

final Optional<String> result = httpMethods.rpcCallMethod(new Method(CHAIN_GET_ERA_INFO_BY_SWITCH_BLOCK,
new HashMap<>()));

return (result.isEmpty()) ? null
: MethodEnums.CHAIN_GET_ERA_INFO_BY_SWITCH_BLOCK.getValue(result.get());

}

public String getRpcSchema() throws Throwable {

final Optional<String> result = httpMethods.rpcCallMethod(new Method(RPC_DISCOVER,
new HashMap<>()));

return (result.isEmpty()) ? null
: MethodEnums.RPC_DISCOVER.getValue(result.get());

}
}
83 changes: 83 additions & 0 deletions src/test/java/com/casper/sdk/TestQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import com.casper.sdk.service.QueryService;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -136,6 +137,78 @@ public void testGetNodeStatus() throws Throwable {

}

@Test
public void testGetBlock() throws Throwable {

Properties.properties.put("node-url", url);
Properties.properties.put("node-port", String.valueOf(mockBackEnd.getPort()));

mockBackEnd.setDispatcher(
new MethodDispatcher());

String block = query.getBlock();
assertNotNull(block);

}

@Test
public void testGetBlockTransfers() throws Throwable {

Properties.properties.put("node-url", url);
Properties.properties.put("node-port", String.valueOf(mockBackEnd.getPort()));

mockBackEnd.setDispatcher(
new MethodDispatcher());

String blockTransfers = query.getBlockTransfers();
assertNotNull(blockTransfers);

}

@Test
public void testGetEraInfoBySwitchBlock() throws Throwable {

Properties.properties.put("node-url", url);
Properties.properties.put("node-port", String.valueOf(mockBackEnd.getPort()));

mockBackEnd.setDispatcher(
new MethodDispatcher());

String eraInfoBySwitchBlock = query.getEraInfoBySwitchBlock();
assertNotNull(eraInfoBySwitchBlock);

}

@Test
public void testGetDeployInfo() throws Throwable {

Properties.properties.put("node-url", url);
Properties.properties.put("node-port", String.valueOf(mockBackEnd.getPort()));

String deployHash = "6c4048f8ebd40a160e9df47e73680eda8ae8430309a9566655bb357a5967276b";

mockBackEnd.setDispatcher(
new MethodDispatcher());

String deployInfo = query.getDeployInfo(deployHash);
assertNotNull(deployInfo);

}

@Test
public void testGetRpcSchema() throws Throwable {

Properties.properties.put("node-url", url);
Properties.properties.put("node-port", String.valueOf(mockBackEnd.getPort()));

mockBackEnd.setDispatcher(
new MethodDispatcher());

String rpcSchema = query.getRpcSchema();
assertNotNull(rpcSchema);

}


private static final class MethodDispatcher extends Dispatcher{

Expand All @@ -159,6 +232,16 @@ public MockResponse dispatch(RecordedRequest request) {
responseBodyFile = "method-json/info_get_peers.json";
} else if (request.getBody().toString().contains("info_get_status")){
responseBodyFile = "method-json/info_get_status.json";
} else if (request.getBody().toString().contains("chain_get_block")){
responseBodyFile = "method-json/chain_get_block.json";
} else if (request.getBody().toString().contains("chain_get_block_transfers")){
responseBodyFile = "method-json/chain_get_block_transfers.json";
} else if (request.getBody().toString().contains("chain_get_era_info_by_switch_b")){
responseBodyFile = "method-json/chain_get_era_info_by_switch_block.json";
} else if (request.getBody().toString().contains("info_get_deploy")){
responseBodyFile = "method-json/info_get_deploy.json";
} else if (request.getBody().toString().contains("rpc.discover")){
responseBodyFile = "method-json/rpc_discover.json";
}

return new MockResponse().setResponseCode(200)
Expand Down
Loading