From 1c1510b305ccfbdc704a070637c5a8c296100f06 Mon Sep 17 00:00:00 2001 From: meywood Date: Tue, 6 Sep 2022 15:32:54 +0100 Subject: [PATCH 1/3] issues/130 - rollback of changes to OkHttp client call to resolve issue reported since refactor --- .../sdk/service/http/rpc/HttpMethods.java | 36 +++++++++++-------- .../casper/sdk/CasperSdkIntegrationTest.java | 5 +-- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/casper/sdk/service/http/rpc/HttpMethods.java b/src/main/java/com/casper/sdk/service/http/rpc/HttpMethods.java index 0a574315f..768f61a53 100644 --- a/src/main/java/com/casper/sdk/service/http/rpc/HttpMethods.java +++ b/src/main/java/com/casper/sdk/service/http/rpc/HttpMethods.java @@ -2,8 +2,10 @@ import com.casper.sdk.exceptions.HttpException; import com.casper.sdk.service.json.JsonConversionService; +import com.fasterxml.jackson.core.JsonProcessingException; import okhttp3.*; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Optional; @@ -31,24 +33,28 @@ public HttpMethods(final JsonConversionService jsonConversionService, final Stri Optional rpcCallMethod(final Method method) throws HttpException { - try { - - final String content = jsonConversionService.writeValueAsString(method); - final byte[] bytes = content.getBytes(StandardCharsets.UTF_8); - final RequestBody body = RequestBody.create(bytes, JSON); - - final Request request = new Request.Builder() - .url(buildRpcUrl()) - .header(ACCEPT, APPLICATION_JSON) - .header(CONTENT_TYPE, APPLICATION_JSON) - .post(body) - .build(); - final Response response = client.newCall(request).execute(); + final String content; + try { + content = jsonConversionService.writeValueAsString(method); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + final byte[] bytes = content.getBytes(StandardCharsets.UTF_8); + //noinspection deprecation + final RequestBody body = RequestBody.create(JSON, bytes); + + final Request request = new Request.Builder() + .url(buildRpcUrl()) + .header(ACCEPT, APPLICATION_JSON) + .header(CONTENT_TYPE, APPLICATION_JSON) + .post(body) + .build(); + + try (final Response response = client.newCall(request).execute()) { //noinspection ConstantConditions return Optional.ofNullable(response.body().string()); - - } catch (Exception e) { + } catch (IOException e) { throw new HttpException(e.getMessage()); } } diff --git a/src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java b/src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java index 14982e59a..9fe0cd65a 100644 --- a/src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java +++ b/src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java @@ -2,8 +2,6 @@ import com.casper.sdk.how_to.HowToUtils; import com.casper.sdk.service.hash.HashService; -import com.casper.sdk.service.serialization.cltypes.CLValueBuilder; -import com.casper.sdk.service.serialization.util.CollectionUtils; import com.casper.sdk.types.*; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -17,7 +15,6 @@ import java.io.IOException; import java.math.BigInteger; import java.security.KeyPair; -import java.security.PublicKey; import java.time.Instant; import static com.casper.sdk.how_to.HowToUtils.getUserKeyPairStreams; @@ -28,7 +25,7 @@ /** * Casper SDK integration tests. The NCTL test nodes must be running for these tests to execute. - * + *

* Path the nctl folder can be overridden with -Dnctl.home=some-path */ @Disabled // Remove this comment to test against a network From 79238ec84fcc4709024c29560c3d41719cc82720 Mon Sep 17 00:00:00 2001 From: meywood Date: Tue, 6 Sep 2022 15:51:06 +0100 Subject: [PATCH 2/3] issues/130 - increment version number to 0.3.13 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index acf1eca5d..8de215585 100644 --- a/build.gradle +++ b/build.gradle @@ -74,7 +74,7 @@ jar { task casperJar(type: Jar) { archiveBaseName = 'casper-java-sdk' - archiveVersion = '0.3.12' + archiveVersion = '0.3.13' manifest { attributes 'Main-Class': 'com.casper.sdk.CasperSdk' } From 39d4acda234da0a435abf375fa972e7bbb278bbe Mon Sep 17 00:00:00 2001 From: meywood Date: Thu, 15 Sep 2022 13:43:42 +0200 Subject: [PATCH 3/3] issues/130 - Added test case for issue, need to revisit once have confirmation of how to execute --- .../casper/sdk/CasperSdkIntegrationTest.java | 101 +++++++++++++++++- .../com/casper/sdk/how_to/HowToUtils.java | 2 +- 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java b/src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java index 9fe0cd65a..98293ff25 100644 --- a/src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java +++ b/src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java @@ -2,6 +2,8 @@ import com.casper.sdk.how_to.HowToUtils; import com.casper.sdk.service.hash.HashService; +import com.casper.sdk.service.serialization.cltypes.CLValueBuilder; +import com.casper.sdk.service.serialization.types.ByteSerializerFactory; import com.casper.sdk.types.*; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -13,11 +15,13 @@ import org.slf4j.LoggerFactory; import java.io.IOException; +import java.io.InputStream; import java.math.BigInteger; import java.security.KeyPair; import java.time.Instant; +import java.util.List; -import static com.casper.sdk.how_to.HowToUtils.getUserKeyPairStreams; +import static com.casper.sdk.how_to.HowToUtils.*; import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; @@ -59,6 +63,8 @@ class CasperSdkIntegrationTest { */ private CasperSdk casperSdk; + private final ByteSerializerFactory serializerFactory = new ByteSerializerFactory(); + @BeforeEach void setUp() { casperSdk = new CasperSdk("http://localhost", 11101); @@ -205,6 +211,99 @@ void getBlockTransfers() throws JsonProcessingException { assertThat(blockTransfersByHash, hasJsonPath("$.transfers")); } + /** + * Tests that the Instrument state update call does not give error: + *

+     * [org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is
+     * java.lang.NoSuchMethodError: okhttp3.RequestBody.create([BLokhttp3/MediaType;)Lokhttp3/RequestBody;]
+     * 
+ *

+ * DOES NOT WORK NEED TO GET INPUT ON HOW TO CONFIGURE TEST + */ + @Test + @Disabled + void testIssue130() throws IOException { + + final InputStream erc20wasmIn = getWasmIn("/com/casper/sdk/how_to/erc20.wasm"); + final String chainName = "casper-net-1"; + final Number payment = 50e9; + final int tokenDecimals = 11; + final String tokenName = "Acme Token"; + final Number tokenTotalSupply = 1e15; + final String tokenSymbol = "ACME"; + + // Get contract operator. + final KeyPairStreams faucetKeyPair = getFaucetKeyPair(); + final KeyPair operatorKeyPair = casperSdk.loadKeyPair(faucetKeyPair.getPublicKeyIn(), faucetKeyPair.getPrivateKeyIn()); + final KeyPair nodeKeyPair = getNodeKeyPair(1); + + // Set deploy. + final Deploy installContractDeploy = casperSdk.makeInstallContract( + new DeployParams( + operatorKeyPair.getPublic(), + chainName, + null, + null, + null, + null + ), + payment, + erc20wasmIn, + tokenDecimals, + tokenName, + tokenSymbol, + tokenTotalSupply + ); + + // Approve deploy. + casperSdk.signDeploy(installContractDeploy, operatorKeyPair); + + // Dispatch deploy to a node. + Digest contractHash = casperSdk.putDeploy(installContractDeploy); + assertThat(contractHash, is(notNullValue())); + + contractHash = new ContractHash("6b6f1b4a38d94956154d20089842ca69f891ea44322df9d20921015ce711dc34"); + + String accountHash = casperSdk.getAccountHash(operatorKeyPair.getPublic()); + + System.out.println("ContractHash: " + contractHash); + + final KeyPair platformKeyPair = nodeKeyPair; + + byte[] key = casperSdk.getPublicKeyBytes(nodeKeyPair.getPublic()); + final List namedArgs = new DeployNamedArgBuilder() + // TODO Where do these values come from? + .add("instrument", CLValueBuilder.string("c9536033-386a-4bed-9b57-fd67c3d49dc1")) + .add("instrument_state_hash", CLValueBuilder.byteArray("52eb6c9d9d5e1c63a2320b6e964ec08a241fa49fee23350f170713f24462a474")) + .build(); + + + final Deploy deploy = casperSdk.makeDeploy( + new DeployParams( + platformKeyPair.getPublic(), "casper-net-1", + 1, + Instant.now().toEpochMilli(), + DeployParams.DEFAULT_TTL, + null + ), + new StoredContractByHash( + new ContractHash(contractHash.getHash()), + "set_state", + namedArgs), + casperSdk.standardPayment(new BigInteger("10000000000")) + ); + + assertThat(deploy, is(notNullValue())); + casperSdk.signDeploy(deploy, nodeKeyPair); + + Digest digest = casperSdk.putDeploy(deploy); + assertThat(digest, is(notNullValue())); + + // Assert hash matches expected + byte[] expectedIssue2Hash = {}; + assertThat(digest.getHash(), is(expectedIssue2Hash)); + } + private KeyPair geUserKeyPair(int userNumber) throws IOException { final KeyPairStreams streams = getUserKeyPairStreams(userNumber); return casperSdk.loadKeyPair(streams.getPublicKeyIn(), streams.getPrivateKeyIn()); diff --git a/src/test/java/com/casper/sdk/how_to/HowToUtils.java b/src/test/java/com/casper/sdk/how_to/HowToUtils.java index e1c89df56..bf27416c7 100644 --- a/src/test/java/com/casper/sdk/how_to/HowToUtils.java +++ b/src/test/java/com/casper/sdk/how_to/HowToUtils.java @@ -39,7 +39,7 @@ public static KeyPairStreams getNodeKeyPairStreams(final int nodeNumber) throws ); } - static KeyPairStreams getFaucetKeyPair() throws IOException { + public static KeyPairStreams getFaucetKeyPair() throws IOException { return new KeyPairStreams( Files.newInputStream(new File(getNctlHome() + "/assets/net-1/faucet", "public_key.pem").toPath()), Files.newInputStream(new File(getNctlHome() + "/assets/net-1/faucet", "secret_key.pem").toPath())