Skip to content

Commit

Permalink
Merge pull request #95 from /issues/94
Browse files Browse the repository at this point in the history
Issues/94
  • Loading branch information
cnorburn authored Feb 22, 2022
2 parents e77540a + 17a66cb commit 6c86361
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/casper/sdk/CasperSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ public Deploy makeInstallContract(final DeployParams deployParams,
.add(TOKEN_DECIMALS, CLValueBuilder.u8(tokenDecimals))
.add(TOKEN_NAME, CLValueBuilder.string(tokenName))
.add(TOKEN_SYMBOL, CLValueBuilder.string(tokenSymbol))
.add(CREATE_DEPLOY_ARG, CLValueBuilder.u256(tokenTotalSupply))
.add(TOKEN_TOTAL_SUPPLYL, CLValueBuilder.u256(tokenTotalSupply))
.build()
),
standardPayment(payment)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/casper/sdk/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class Constants {
public static final String TOKEN_DECIMALS = "token_decimals";
public static final String TOKEN_NAME = "token_name";
public static final String TOKEN_SYMBOL = "token_symbol";
public static final String TOKEN_TOTAL_SUPPLYL = "token_total_supply";
public static final String VALIDATOR = "validator";
public static final String UNBOND_PURSE = "unbond_purse";
}
21 changes: 18 additions & 3 deletions src/main/java/com/casper/sdk/service/http/rpc/MethodEnums.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ public String getValue(final String result) throws ValueNotFoundException {
ACCOUNT_PUT_DEPLOY {
@Override
public String getValue(final String result) throws ValueNotFoundException {
JsonNode resultNode = null;
try {
resultNode = getResultNode(result);
final JsonNode resultNode = getResultNode(result);
return resultNode.get(Constants.DEPLOY_HASH).textValue();
} catch (Exception e) {
throw new ValueNotFoundException("deploy_hash not found " + buildErrorMessage(resultNode));
throw new ValueNotFoundException("deploy_hash not found " + buildErrorMessage(result));
}
}
},
Expand Down Expand Up @@ -169,6 +168,22 @@ JsonNode getResultNode(final String result) throws JsonProcessingException {
return node.get(RESULT);
}

String buildErrorMessage(final String result) {

try {
final JsonNode node = new ObjectMapper().readTree(result);
final JsonNode error = node != null ? node.get("error") : null;
if (error != null) {
return error.toString();
} else {
return Constants.EMPTY_STRING;
}
} catch (JsonProcessingException e) {
return Constants.EMPTY_STRING;
}

}

String buildErrorMessage(final JsonNode node) {
final JsonNode error = node != null ? node.get("error") : null;
if (error != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public TypesFactory() {
instances.put(CLType.PUBLIC_KEY, new PublicKeySerializer());
instances.put(CLType.MAP, new MapSerializer(this));
instances.put(CLType.STRING, new StringSerializer(this));
instances.put(CLType.U8, new U8Serializer());
instances.put(CLType.U32, new U32Serializer());
instances.put(CLType.U64, new U64Serializer());
instances.put(CLType.U128, new U128Serializer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
class DeployExecutableByteSerializer implements ByteSerializer<DeployExecutable> {

private final ByteSerializerFactory factory;
private final TypesSerializer u32Serializer;
private final TypesSerializer byteArraySerializer;
private final TypesSerializer stringSerializer;
private final TypesSerializer u32Serializer;

public DeployExecutableByteSerializer(final ByteSerializerFactory factory, final TypesFactory typesFactory) {
this.factory = factory;
u32Serializer = typesFactory.getInstance(CLType.U32);
byteArraySerializer = typesFactory.getInstance(CLType.BYTE_ARRAY);
stringSerializer = typesFactory.getInstance(CLType.STRING);
u32Serializer = typesFactory.getInstance(CLType.U32);
}

@Override
public Class<DeployExecutable> getType() {
return DeployExecutable.class;
}

@Override
Expand All @@ -36,7 +43,7 @@ public byte[] toBytes(final DeployExecutable deployExecutable) {
builder.append(((StoredContractByHash) deployExecutable).getHash().getHash());
builder.append(stringSerializer.serialize(((StoredContractByHash) deployExecutable).getEntryPoint()));
} else if (deployExecutable instanceof ModuleBytes) {
builder.append(u32Serializer.serialize(deployExecutable.getModuleBytes()));
builder.append(byteArraySerializer.serialize(deployExecutable.getModuleBytes()));
}

// Append any args if present
Expand All @@ -45,11 +52,6 @@ public byte[] toBytes(final DeployExecutable deployExecutable) {
return builder.toByteArray();
}

@Override
public Class<DeployExecutable> getType() {
return DeployExecutable.class;
}

private byte[] toBytes(final List<DeployNamedArg> args) {

final ByteArrayBuilder builder = new ByteArrayBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.casper.sdk.service.serialization.util;

import java.math.BigDecimal;
import java.math.BigInteger;

public class NumberUtils {
Expand All @@ -17,7 +18,11 @@ public static BigInteger toBigInteger(final Object source) {
if (source instanceof BigInteger) {
bigInt = (BigInteger) source;
} else if (source instanceof Number) {
bigInt = new BigInteger(source.toString());
if (source instanceof Double) {
bigInt = BigDecimal.valueOf((Double) source).toBigInteger();
} else {
bigInt = new BigInteger(source.toString());
}
} else if (source instanceof String && ((String) source).length() > 0) {
bigInt = new BigInteger(String.valueOf(source));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Integration tests for installing a contract
*/
@Disabled // Remove this to run locally
//@Disabled // Remove this to run locally
public class HowToInstallAContract {

/**
Expand All @@ -27,7 +27,7 @@ void howToInstallAContract() throws Throwable {
// Step 1: Set casper node client.
final CasperSdk casperSdk = new CasperSdk("http://localhost", 11101);

final InputStream erc20wasmIn = getWasmIn(getNctlHome() + "/assets/net-1/bin/eco/erc20.wasm");
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;
Expand All @@ -42,7 +42,7 @@ void howToInstallAContract() throws Throwable {
// Set deploy.
final Deploy deploy = casperSdk.makeInstallContract(
new DeployParams(
operatorKeyPair.getPrivate(),
operatorKeyPair.getPublic(),
chainName,
null,
null,
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/casper/sdk/how_to/HowToInvokeAContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Integration tests for invoking a contract
*/
@Disabled // Remove this to run locally
//@Disabled // Remove this to run locally
public class HowToInvokeAContract {

/**
Expand All @@ -46,7 +46,7 @@ void howToInvokeAContract() throws Throwable {
// Make deploy
Deploy deploy = casperSdk.makeInvokeContract(
new DeployParams(
operatorKeyPair.getPrivate(),
operatorKeyPair.getPublic(),
chainName,
null,
null,
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/casper/sdk/how_to/HowToUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static String getNctlHome() {
return nctlHome.replaceFirst("^~", System.getProperty("user.home"));
}

public static KeyPairStreams getNodeKeyPairStreams(final int nodeNumber) throws IOException {
public static KeyPairStreams getNodeKeyPairStreams(final int nodeNumber) throws IOException {

final String userNPath = String.format("%s/assets/net-1/nodes/node-%d/keys", getNctlHome(), nodeNumber);

Expand All @@ -40,13 +40,13 @@ public static KeyPairStreams getNodeKeyPairStreams(final int nodeNumber) throws

static KeyPairStreams getFaucetKeyPair() throws FileNotFoundException {
return new KeyPairStreams(
new FileInputStream(new File(getNctlHome() + "/faucet", "public_key.pem")),
new FileInputStream(new File(getNctlHome() + "/faucet", "secret_key.pem"))
new FileInputStream(new File(getNctlHome() + "/assets/net-1/faucet", "public_key.pem")),
new FileInputStream(new File(getNctlHome() + "/assets/net-1/faucet", "secret_key.pem"))
);

}

static InputStream getWasmIn(String wasmPath) {
return HowToUtils.class.getResourceAsStream(getNctlHome() + wasmPath);
public static InputStream getWasmIn(final String wasmPath) {
return HowToUtils.class.getResourceAsStream(wasmPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.casper.sdk.service.serialization.types;

import com.casper.sdk.service.hash.HashService;
import com.casper.sdk.service.signing.SigningService;
import com.casper.sdk.service.json.JsonConversionService;
import com.casper.sdk.service.serialization.cltypes.CLValueBuilder;
import com.casper.sdk.service.serialization.cltypes.TypesFactory;
import com.casper.sdk.service.serialization.util.ByteUtils;
import com.casper.sdk.service.serialization.util.CollectionUtils;
import com.casper.sdk.service.signing.SigningService;
import com.casper.sdk.types.*;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Collections;

import static com.casper.sdk.Constants.*;
import static com.casper.sdk.how_to.HowToUtils.getWasmIn;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

Expand All @@ -19,6 +26,8 @@
*/
class DeployExecutableByteSerializerTest {

public static final String EXPECTED_BYTES_TXT = "/com/casper/sdk/service/serialization/types/expected-bytes.txt";
public static final String ERC_20_WASM = "/com/casper/sdk/how_to/erc20.wasm";
private final ByteSerializerFactory byteSerializerFactory = new ByteSerializerFactory();
private final DeployExecutableByteSerializer serializer = (DeployExecutableByteSerializer) byteSerializerFactory.getByteSerializerByType(DeployExecutable.class);
private final TypesFactory typesFactory = new TypesFactory();
Expand Down Expand Up @@ -142,4 +151,32 @@ void storedContractByHashToBytes() {
assertThat(actual, is(expected));

}

/**
* Tests that a module_bytes containing a .wasm file can be converted to bytes
*/
@Test
void moduleBytesWithWasmToBytes() throws IOException {

final int tokenDecimals = 11;
final String tokenName = "Acme Token";
final Number tokenTotalSupply = 1e15;
final String tokenSymbol = "ACME";
//noinspection ConstantConditions
final String expected = IOUtils.toString(getClass().getResource(EXPECTED_BYTES_TXT), StandardCharsets.UTF_8);

final ModuleBytes moduleBytes = new ModuleBytes(
IOUtils.toByteArray(getWasmIn(ERC_20_WASM)),
new DeployNamedArgBuilder()
.add(TOKEN_DECIMALS, CLValueBuilder.u8(tokenDecimals))
.add(TOKEN_NAME, CLValueBuilder.string(tokenName))
.add(TOKEN_SYMBOL, CLValueBuilder.string(tokenSymbol))
.add(TOKEN_TOTAL_SUPPLYL, CLValueBuilder.u256(tokenTotalSupply))
.build()
);

final String hexBytes = ByteUtils.encodeHexString(serializer.toBytes(moduleBytes));
assertThat(hexBytes, is(expected));

}
}
Binary file not shown.

Large diffs are not rendered by default.

0 comments on commit 6c86361

Please sign in to comment.