Skip to content

Commit

Permalink
Merge pull request #131 from /issues/130
Browse files Browse the repository at this point in the history
Issues/130 -  Instrument state update fails with new OkHttp method calls
  • Loading branch information
cnorburn authored Sep 15, 2022
2 parents ac8a252 + 1021017 commit 3d9a035
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 68 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/com/casper/sdk/service/http/rpc/HttpMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -31,24 +33,28 @@ public HttpMethods(final JsonConversionService jsonConversionService, final Stri

Optional<String> 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());
}
}
Expand Down
72 changes: 20 additions & 52 deletions src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
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.service.serialization.util.ByteUtils;
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;
Expand Down Expand Up @@ -213,10 +211,18 @@ void getBlockTransfers() throws JsonProcessingException {
assertThat(blockTransfersByHash, hasJsonPath("$.transfers"));
}

/**
* Tests that the Instrument state update call does not give error:
* <pre>
* [org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is
* java.lang.NoSuchMethodError: okhttp3.RequestBody.create([BLokhttp3/MediaType;)Lokhttp3/RequestBody;]
* </pre>
* <p>
* DOES NOT WORK NEED TO GET INPUT ON HOW TO CONFIGURE TEST
*/
@Test
@Disabled
void testIssue2() throws IOException {

void testIssue130() throws IOException {

final InputStream erc20wasmIn = getWasmIn("/com/casper/sdk/how_to/erc20.wasm");
final String chainName = "casper-net-1";
Expand All @@ -229,6 +235,7 @@ void testIssue2() throws IOException {
// 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(
Expand Down Expand Up @@ -257,57 +264,19 @@ void testIssue2() throws IOException {

contractHash = new ContractHash("6b6f1b4a38d94956154d20089842ca69f891ea44322df9d20921015ce711dc34");

System.out.println("ContractHash: " + contractHash);

byte[] k1Bytes = ByteUtils.decodeHex("e07cA98F1b5C15bC9ce75e8adB8a3b4D334A1B1Fa14DD16CfD3320bf77Cc3aAb");
final CLValue key1 = CLValueBuilder.byteArray(k1Bytes);



byte[] k2Bytes = ByteUtils.decodeHex("e3D394334Ce46C6043BCd33E4686D2B7a369C606BfCce4C26ca14d2C73Fac824");
final CLValue key2 = CLValueBuilder.byteArray(k2Bytes);
final CLValue value = CLValueBuilder.u256(0.4e6);


String accountHash = casperSdk.getAccountHash(operatorKeyPair.getPublic());

final KeyPair platformKeyPair = getNodeKeyPair(1);

CLMap map1 = CLValueBuilder.map(CollectionUtils.Map.of(key1, value));
CLMap map2 = CLValueBuilder.map(CollectionUtils.Map.of(key2, value));

byte[] map1Bytes = map1.getBytes();
byte[] map2Byte = map2.getBytes();

final DeployNamedArg assetHolders = new DeployNamedArg("asset_holders", map1);
final DeployNamedArg liabilityHolders = new DeployNamedArg("liability_holders", map2);

// Test the bytes from both CLMap named args
byte[] assertHoldersBytes = serializerFactory.getByteSerializer(assetHolders).toBytes(assetHolders);

// Assert assertHoldersBytes match expected
// TODO

byte[] liabilityHoldersBytes = serializerFactory.getByteSerializer(liabilityHolders).toBytes(liabilityHolders);
System.out.println("ContractHash: " + contractHash);

// Assert liabilityHoldersBytes match expected
// TODO
final KeyPair platformKeyPair = nodeKeyPair;

byte[] key = casperSdk.getPublicKeyBytes(nodeKeyPair.getPublic());
final List<DeployNamedArg> namedArgs = new DeployNamedArgBuilder()
.add("token_id", CLValueBuilder.string("token-id"))
.add("instrument_id", CLValueBuilder.string("c9536033-386a-4bed-9b57-fd67c3d49dc1"))
.add("asset_decimals", CLValueBuilder.u256(1))
.add("asset_units", CLValueBuilder.u256(50000))
.add(assetHolders)
.add("liability_decimals", CLValueBuilder.u256(1))
.add("liability_units", CLValueBuilder.u256(40000))
.add(liabilityHolders)
// TODO Where do these values come from?
.add("instrument", CLValueBuilder.string("c9536033-386a-4bed-9b57-fd67c3d49dc1"))
.add("instrument_state_hash", CLValueBuilder.byteArray("52eb6c9d9d5e1c63a2320b6e964ec08a241fa49fee23350f170713f24462a474"))
.build();

byte[] namedArgsBytes = serializerFactory.getByteSerializer(namedArgs).toBytes(namedArgs);

// Assert namedArgsBytes match expected
// TODO


final Deploy deploy = casperSdk.makeDeploy(
new DeployParams(
Expand All @@ -325,14 +294,13 @@ void testIssue2() throws IOException {
);

assertThat(deploy, is(notNullValue()));

casperSdk.signDeploy(deploy, operatorKeyPair);
casperSdk.signDeploy(deploy, nodeKeyPair);

Digest digest = casperSdk.putDeploy(deploy);
assertThat(digest, is(notNullValue()));

// Assert hash matches expected
byte [] expectedIssue2Hash = {};
byte[] expectedIssue2Hash = {};
assertThat(digest.getHash(), is(expectedIssue2Hash));
}

Expand Down

0 comments on commit 3d9a035

Please sign in to comment.