Skip to content

Commit

Permalink
Merge pull request #246 from /issues/245
Browse files Browse the repository at this point in the history
issues/245 - All protocol selection via use or URL connection parameter
  • Loading branch information
cnorburn authored Feb 14, 2024
2 parents 2b4e737 + 2017a2e commit bf55cfa
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/main/java/com/casper/sdk/service/CasperService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.casper.sdk.model.uref.URef;
import com.casper.sdk.model.validator.ValidatorChangeData;
import com.googlecode.jsonrpc4j.*;
import org.apache.cxf.common.util.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -42,6 +43,31 @@
*/
public interface CasperService {

/**
* Builds a CasperService for the node url.
*
* @param url the peer url to connect to
* @param additionalHeaders additional headers to be added to the request
* @return A Dynamic Proxy to CasperService
* @throws MalformedURLException is thrown if ip/port are not compliant
*/
static CasperService usingPeer(URL url, final Map<String, String> additionalHeaders) throws MalformedURLException {
if (StringUtils.isEmpty(url.getPath())) {
url = new URL(url.getProtocol(), url.getHost(), url.getPort(), "/rpc");
}

final Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
if (additionalHeaders != null) {
headers.putAll(additionalHeaders);
}

final JsonRpcHttpClient client = new JsonRpcHttpClient(new CasperObjectMapper(), url, headers);
client.setExceptionResolver(new CasperClientExceptionResolver());

return ProxyUtil.createClientProxy(CasperService.class.getClassLoader(), CasperService.class, client);
}

/**
* Builds a CasperService for the node ip/port pair
*
Expand All @@ -51,16 +77,7 @@ public interface CasperService {
* @throws MalformedURLException is thrown if ip/port are not compliant
*/
static CasperService usingPeer(String ip, int port) throws MalformedURLException {
CasperObjectMapper objectMapper = new CasperObjectMapper();
Map<String, String> newHeaders = new HashMap<>();
newHeaders.put("Content-Type", "application/json");
JsonRpcHttpClient client = new JsonRpcHttpClient(objectMapper, new URL("http", ip, port, "/rpc"),
newHeaders);

ExceptionResolver exceptionResolver = new CasperClientExceptionResolver();
client.setExceptionResolver(exceptionResolver);

return ProxyUtil.createClientProxy(CasperService.class.getClassLoader(), CasperService.class, client);
return usingPeer(new URL("http", ip, port, "/rpc"), null);
}

//region INFORMATIONAL METHODS
Expand Down Expand Up @@ -213,13 +230,13 @@ DictionaryData getStateDictionaryItem(@JsonRpcParam("state_root_hash") String st
*/
@JsonRpcMethod("speculative_exec")
SpeculativeDeployData speculativeExec(@JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier,
@JsonRpcParam("deploy") Deploy deploy);
@JsonRpcParam("deploy") Deploy deploy);

/**
* The speculative_exec endpoint provides a method to execute a Deploy
* without committing it to global state
*
* @param deploy the deploy object to send to the network
* @param deploy the deploy object to send to the network
* @return Object holding the api version and the deploy hash
*/
@JsonRpcMethod("speculative_exec")
Expand Down Expand Up @@ -287,6 +304,7 @@ QueryBalanceData queryBalance(@JsonRpcParam("state_identifier") GlobalStateIdent
//endregion

//region DEPRECATED METHODS

/**
* Returns a stored value from the network. This RPC is deprecated, use `query_global_state` instead"
*
Expand Down

0 comments on commit bf55cfa

Please sign in to comment.