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

sdk/java: add CoreConfig class #1055

Merged
merged 9 commits into from
Apr 27, 2017
Merged
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
2 changes: 1 addition & 1 deletion docker/ci/bin/setup-core
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ waitForLeader() {(
)}

PATH=$(go env GOPATH)/bin:$PATH:$CHAIN/bin
go install -tags 'plain_http loopback_auth' chain/cmd/{cored,corectl}
go install -tags 'plain_http loopback_auth reset' chain/cmd/{cored,corectl}
initlog=`mktemp`
cored 2>&1 | tee $initlog &
waitForLeader
Expand Down
2 changes: 1 addition & 1 deletion generated/rev/RevId.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

public final class RevId {
public final String Id = "main/rev3032";
public final String Id = "main/rev3033";
}
2 changes: 1 addition & 1 deletion generated/rev/revid.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package rev

const ID string = "main/rev3032"
const ID string = "main/rev3033"
2 changes: 1 addition & 1 deletion generated/rev/revid.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

export const rev_id = "main/rev3032"
export const rev_id = "main/rev3033"
2 changes: 1 addition & 1 deletion generated/rev/revid.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

module Chain::Rev
ID = "main/rev3032".freeze
ID = "main/rev3033".freeze
end
141 changes: 141 additions & 0 deletions sdk/java/src/main/java/com/chain/api/CoreConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.chain.api;

import com.chain.http.*;
import com.chain.exception.*;
import com.google.gson.annotations.SerializedName;

import java.util.*;

public class CoreConfig {

public static class Info {
public String state;

@SerializedName("is_configured")
public boolean isConfigured;

@SerializedName("configured_at")
public Date configuredAt;

@SerializedName("is_signer")
public boolean isSigner;

@SerializedName("is_generator")
public boolean isGenerator;

@SerializedName("generator_url")
public String generatorUrl;

@SerializedName("generator_access_token")
public String generatorAccessToken;

@SerializedName("blockchain_id")
public String blockchainId;

@SerializedName("block_height")
public long blockHeight;

@SerializedName("generator_block_height")
public long generatorBlockHeight;

@SerializedName("generator_block_height_fetched_at")
public Date generatorBlockHeightFetchedAt;

@SerializedName("network_rpc_version")
public int networkRpcVersion;

@SerializedName("core_id")
public String coreId;

public String version;

@SerializedName("build_commit")
public String buildCommit;

@SerializedName("build_date")
public String buildDate;

@SerializedName("build_config")
public BuildConfig buildConfig;

public Map<String, Object> health;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any more schema to this that we can provide? it's a little unclear how to use this as-is. If I understand the cored code right, this map would look something like:

{
  "errors": {
    "generator": "error generating new block"
  }
}

Maybe instead of a Map<String,Object> this should be a separate class? Even if we only expose a method that returns this map, it'll give us the flexibility to introduce more convenience methods down the road.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this also might be the most useful of the fields exposed here, so I don't feel like it's over-engineering to ensure we can make it easy to access.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Let's land this PR and follow up with something. How confident are you in the stability of the structure of the health object?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to ask @kr, I'm not really sure.


public Snapshot snapshot;

public static class BuildConfig {
@SerializedName("is_loopback_auth")
public boolean isLoopbackAuth;

@SerializedName("is_mockhsm")
public boolean isMockHsm;

@SerializedName("is_reset")
public boolean isReset;
}

public static class Snapshot {
public int attempt;

public long height;

public long size;

public long downloaded;

@SerializedName("in_progress")
public boolean inProgress;
}
}

public static Info getInfo(Client client) throws ChainException {
return client.request("info", null, Info.class);
}

public static void reset(Client client) throws ChainException {
client.request("reset", null, SuccessMessage.class);
}

public static void resetEverything(Client client) throws ChainException {
Map<String, Object> params = new HashMap<>();
params.put("everything", true);
client.request("reset", params, SuccessMessage.class);
}

public static class Builder {
@SerializedName("is_generator")
private boolean isGenerator;

@SerializedName("generator_url")
private String generatorUrl;

@SerializedName("generator_access_token")
private String generatorAccessToken;

@SerializedName("blockchain_id")
private String blockchainId;

public Builder setIsGenerator(boolean isGenerator) {
this.isGenerator = isGenerator;
return this;
}

public Builder setGeneratorUrl(String url) {
generatorUrl = url;
return this;
}

public Builder setGeneratorAccessToken(String token) {
generatorAccessToken = token;
return this;
}

public Builder setBlockchainId(String blockchainId) {
this.blockchainId = blockchainId;
return this;
}

public void configure(Client client) throws ChainException {
client.request("configure", this);
}
}
}
18 changes: 18 additions & 0 deletions sdk/java/src/main/java/com/chain/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ public T create(Response response, Gson deserializer) throws IOException {
return post(action, body, rc);
}

/**
* Perform a single HTTP POST request against the API for a specific action,
* ignoring the body of the response.
*
* @param action The requested API action
* @param body Body payload sent to the API as JSON
* @throws ChainException
*/
public void request(String action, Object body) throws ChainException {
ResponseCreator<Object> rc =
new ResponseCreator<Object>() {
public Object create(Response response, Gson deserializer) throws IOException {
return null;
}
};
post(action, body, rc);
}

/**
* Perform a single HTTP POST request against the API for a specific action.
* Use this method if you want batch semantics, i.e., the endpoint response
Expand Down
25 changes: 25 additions & 0 deletions sdk/java/src/test/java/com/chain/integration/CoreConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.chain.integration;

import com.chain.api.*;
import com.chain.http.*;

import org.junit.Test;
import static org.junit.Assert.*;

public class CoreConfigTest {
@Test
public void run() throws Exception {
Client c = new Client();

CoreConfig.Info info = CoreConfig.getInfo(c);
assertTrue(info.isConfigured);

CoreConfig.resetEverything(c);
info = CoreConfig.getInfo(c);
assertFalse(info.isConfigured);

new CoreConfig.Builder().setIsGenerator(true).configure(c);
info = CoreConfig.getInfo(c);
assertTrue(info.isConfigured);
}
}