Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into issues/91
Browse files Browse the repository at this point in the history
# Conflicts:
#	pom.xml
  • Loading branch information
meywood committed Jul 13, 2022
2 parents b946a4f + a15acc5 commit e2f97dc
Show file tree
Hide file tree
Showing 54 changed files with 4,355 additions and 22 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/casper/sdk/CasperSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,16 @@ public String getBlockTransfers() {
return nodeClient.getBlockTransfers();
}

/**
* Returns on-chain block transfers information as JSON for a specific block hash
*
* @param blockHash the block hash to otain the transfers for
* @return the block transfers information as JSON
*/
public String getBlockTransfers(final String blockHash) {
return nodeClient.getBlockTransfers(blockHash);
}

/**
* Obtains the chain ero info by switch block as JSON
*
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 @@ -7,6 +7,7 @@ public class Constants {
public static final String ARGS = "args";
public static final String BALANCE_VALUE = "balance_value";
public static final String BLOCK = "block";
public static final String BLOCK_HASH = "Hash";
public static final String BLOCK_IDENTIFIER = "block_identifier";
public static final String CHAIN_GET_BLOCK = "chain_get_block";
public static final String CHAIN_GET_BLOCK_TRANSFERS = "chain_get_block_transfers";
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/com/casper/sdk/service/http/rpc/NodeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ public String getLatestBlockInfo() {
public String getBlockInfo(final Digest blockHash) {
final Map<String, Object> params = CollectionUtils.Map.of(
Constants.BLOCK_IDENTIFIER,
CollectionUtils.Map.of(HASH, blockHash.toString()
));
CollectionUtils.Map.of(HASH, blockHash)
);
return getChainBlockInfo(params);
}

public String getBlockInfoByHeight(final Number height) {
final Map<String, Object> params = CollectionUtils.Map.of(
Constants.BLOCK_IDENTIFIER,
CollectionUtils.Map.of(Constants.HEIGHT, height.toString())
CollectionUtils.Map.of(Constants.HEIGHT, height)
);
return getChainBlockInfo(params);
}
Expand Down Expand Up @@ -185,6 +185,25 @@ public String getBlockTransfers() {
);
}

/**
* Obtains the nodes chain block transfers
*
* @param blockHash block hash to obtain transfers for
* @return the JSON of the chain block transfers
*/
public String getBlockTransfers(final String blockHash) {

final Map<String, Object> params = CollectionUtils.Map.of(
Constants.BLOCK_IDENTIFIER,
CollectionUtils.Map.of(Constants.BLOCK_HASH, blockHash)
);
return rcpCallMethodMap(
new Method(Constants.CHAIN_GET_BLOCK_TRANSFERS, params),
CHAIN_GET_BLOCK_TRANSFERS::getValue
);
}


/**
* Obtains the chain era info by switch block result as JSON
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public static CLMap map(final Map<CLValue, CLValue> map) {
throw new ValueNotFoundException("Maps must contain at least one key pair");
}

return new CLMap(TYPES_FACTORY.getInstance(CLType.MAP).serialize(map),
final byte[] bytes = TYPES_FACTORY.getInstance(CLType.MAP).serialize(map);

return new CLMap(
bytes,
new CLMapTypeInfo(
map.keySet().iterator().next().getCLTypeInfo(),
map.values().iterator().next().getCLTypeInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,38 @@ public MapSerializer(final TypesFactory typesFactory) {
public byte[] serialize(final Object toSerialize) {

if (toSerialize instanceof CLMap) {
if (((CLMap) toSerialize).isModified() || ((CLMap) toSerialize).getBytes() == null) {

// The map is mutable to the bytes need to be reserialized if modified or not yet created
if (isModifiedOrNotYetSerialized((CLMap) toSerialize)) {
((CLMap) toSerialize).setModified(false);
return ByteUtils.concat(
typesFactory.getInstance(CLType.U32).serialize(((CLMap) toSerialize).size()),
buildMapBytes((CLMap)toSerialize)
);
//noinspection unchecked
return serializeMap((Map<CLValue, CLValue>) toSerialize);

} else {
// The map has not been modified so write as is
// The map is not new and has not been modified so write as is
return ((CLMap) toSerialize).getBytes();
}
} else if (toSerialize instanceof Map) {
//noinspection unchecked
return serializeMap((Map<CLValue, CLValue>) toSerialize);
} else {
return new byte[0];
}
}

private byte[] buildMapBytes(final CLMap clMap) {
private boolean isModifiedOrNotYetSerialized(final CLMap toSerialize) {
return toSerialize.isModified() || toSerialize.getBytes() == null;
}

private byte[] serializeMap(final Map<CLValue, CLValue> toSerialize) {
return ByteUtils.concat(
typesFactory.getInstance(CLType.U32).serialize(toSerialize.size()),
buildKeyValueBytes(toSerialize)
);
}

private byte[] buildKeyValueBytes(final Map<CLValue, CLValue> clMap) {

final ByteArrayBuilder builder = new ByteArrayBuilder();

for (Map.Entry<CLValue, CLValue> entry : clMap.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.casper.sdk.service.serialization.cltypes.TypesFactory;
import com.casper.sdk.service.serialization.cltypes.TypesSerializer;
import com.casper.sdk.service.serialization.util.ByteArrayBuilder;
import com.casper.sdk.types.CLByteArrayInfo;
import com.casper.sdk.types.CLOptionTypeInfo;
import com.casper.sdk.types.CLType;
import com.casper.sdk.types.CLTypeInfo;
import com.casper.sdk.types.*;

abstract class AbstractByteSerializer<T> implements ByteSerializer<T> {

Expand Down Expand Up @@ -40,11 +37,22 @@ byte[] toBytesForCLTypeInfo(final CLTypeInfo typeInfo) {
case OPTION:
return getOptionType(typeInfo);

case MAP:
return getMapType((CLMapTypeInfo) typeInfo);

default:
throw new IllegalArgumentException("Wrong type " + typeInfo.getType());
}
}

private byte[] getMapType(final CLMapTypeInfo typeInfo) {
return new ByteArrayBuilder()
.append(getTypeBytes(typeInfo))
.append(getTypeBytes(typeInfo.getKeyType()))
.append(getTypeBytes(typeInfo.getValueType()))
.toByteArray();
}

public TypesSerializer getU32Serializer() {
return u32Serializer;
}
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/com/casper/sdk/Batch6Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.casper.sdk;

import com.casper.sdk.service.serialization.cltypes.CLValueBuilder;
import com.casper.sdk.service.serialization.util.CollectionUtils;
import com.casper.sdk.types.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.math.BigInteger;
import java.security.PublicKey;
import java.time.Instant;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class Batch6Test {

private CasperSdk casperSdk;

@BeforeEach
void setUp() {
casperSdk = new CasperSdk("http://localhost", 11101);
}

/**
* Tests that a StoredContractByHash can be made with CLMap parameters
*/
@Test
void testMakeDeployWithCLMap() throws IOException {

final PublicKey platformPublicKey = casperSdk.loadKey(CasperSdkIntegrationTest.class.getResourceAsStream("/track-4/assets/keys/wm/public_key.pem"));

final String contractHash = "D5f63F80B885B849443Ef758FdC97f69910B84440Ff41463B4ab3F4bE02Ad16a";
final String contractuuid = "ISIN:DE000XXB2UL2";

final CLValue key1 = CLValueBuilder.byteArray("e07cA98F1b5C15bC9ce75e8adB8a3b4D334A1B1Fa14DD16CfD3320bf77Cc3aAb");
final CLValue value = CLValueBuilder.u256(0.4e6);
final CLValue key2 = CLValueBuilder.byteArray("e3D394334Ce46C6043BCd33E4686D2B7a369C606BfCce4C26ca14d2C73Fac824");

final Deploy deploy = casperSdk.makeDeploy(
new DeployParams(
platformPublicKey,
"integration-test",
1,
Instant.now().toEpochMilli(),
DeployParams.DEFAULT_TTL,
null),
new StoredContractByHash(
new ContractHash(contractHash),
"set_state",
new DeployNamedArgBuilder()
.add("token_id", CLValueBuilder.string("token-id"))
.add("instrument_id", CLValueBuilder.string(contractuuid))
.add("asset_decimals", CLValueBuilder.u256(1))
.add("asset_units", CLValueBuilder.u256(50000))
.add("asset_holders", CLValueBuilder.map(CollectionUtils.Map.of(key1, value)))
.add("liability_decimals", CLValueBuilder.u256(1))
.add("liability_units", CLValueBuilder.u256(40000))
.add("liability_holders", CLValueBuilder.map(CollectionUtils.Map.of(key2, value)))
.build()),
casperSdk.standardPayment(new BigInteger("10000000000"))
);

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

final String jsonDeploy = casperSdk.deployToJson(deploy);

assertThat(jsonDeploy, is(notNullValue()));
}
}
26 changes: 24 additions & 2 deletions src/test/java/com/casper/sdk/CasperSdkIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.util.CollectionUtils;
import com.casper.sdk.types.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -15,6 +17,7 @@
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;
Expand All @@ -29,7 +32,9 @@
//@Disabled // Remove this comment to test against a network
class CasperSdkIntegrationTest {

/** Path the nctl folder can be overridden with -Dnctl.home=some-path */
/**
* Path the nctl folder can be overridden with -Dnctl.home=some-path
*/

private final Logger logger = LoggerFactory.getLogger(CasperSdkIntegrationTest.class);
private final byte[] expectedSerializedBody = {
Expand All @@ -54,7 +59,9 @@ class CasperSdkIntegrationTest {
(byte) 135, (byte) 250, (byte) 107, (byte) 113, (byte) 237, (byte) 101, (byte) 127, (byte) 51,
(byte) 144, (byte) 81, (byte) 19, (byte) 196, (byte) 35, (byte) 39
};
/** The SDK under test the NCTL test nodes must be running for these tests to execute */
/**
* The SDK under test the NCTL test nodes must be running for these tests to execute
*/
private CasperSdk casperSdk;

@BeforeEach
Expand Down Expand Up @@ -188,6 +195,21 @@ void getBlockInfo() throws JsonProcessingException {
assertThat(blockInfo, hasJsonPath("$.header.height", is(height)));
}

@Test
void getBlockTransfers() throws JsonProcessingException {

final String blockTransfers = casperSdk.getBlockTransfers();
assertThat(blockTransfers, is(notNullValue()));
assertThat(blockTransfers, hasJsonPath("$.block_hash"));
assertThat(blockTransfers, hasJsonPath("$.transfers"));

final JsonNode jsonNode = new ObjectMapper().readTree(blockTransfers);
final String blockHash = jsonNode.get("block_hash").textValue();

final String blockTransfersByHash = casperSdk.getBlockTransfers(blockHash);
assertThat(blockTransfersByHash, hasJsonPath("$.transfers"));
}

private KeyPair geUserKeyPair(int userNumber) throws IOException {
final KeyPairStreams streams = getUserKeyPairStreams(userNumber);
return casperSdk.loadKeyPair(streams.getPublicKeyIn(), streams.getPrivateKeyIn());
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/casper/sdk/CasperSdkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ void getBlockTransfers() {
assertThat(blockTransfers, hasJsonPath("$.transfers"));
assertThat(blockTransfers, hasJsonPath("$.transfers[0].deploy_hash", is("5db09c0275c4c1ba54ebcc69784ed767350bf9c2b0be7ab4fec1ca84acd1c47d")));
assertThat(blockTransfers, hasJsonPath("$.transfers[0].amount", is("7000000000")));

final String blockTransfersByHash = casperSdk.getBlockTransfers("5db09c0275c4c1ba54ebcc69784ed767350bf9c2b0be7ab4fec1ca84acd1c47d");
assertThat(blockTransfersByHash, hasJsonPath("$.transfers[0].deploy_hash", is("5db09c0275c4c1ba54ebcc69784ed767350bf9c2b0be7ab4fec1ca84acd1c47d")));
assertThat(blockTransfersByHash, hasJsonPath("$.transfers[0].amount", is("7000000000")));


}

@Test
Expand All @@ -102,6 +108,14 @@ void getEraInfoBySwitchBlock() {
assertThat(eraInfoBySwitchBlock, hasJsonPath("$.era_summary"));
}

@Test
void getBlockInfo() {
final String blockInfo = casperSdk.getBlockInfo(new Digest("ce4e6b534c69b2b29f834c6ce73a4b119090de84485149cfc8f2b10b6737166e"));
assertThat(blockInfo, hasJsonPath("$.hash", is("ce4e6b534c69b2b29f834c6ce73a4b119090de84485149cfc8f2b10b6737166e")));
assertThat(blockInfo, hasJsonPath("$.header.parent_hash", is("9aaaa6eb668ac38815d7f13e6a03f59c09c289cf99fabf8074de208ef181b1b9")));
assertThat(blockInfo, hasJsonPath("$.header.state_root_hash", is("16ab9e0fb81ca9fb3b8678d1d53bef9136e781ceae55221fd63762468a86844a")));
}

@Test
public void getNodeMetrics() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.casper.sdk.service.serialization.cltypes.CLValueBuilder;
import com.casper.sdk.service.serialization.cltypes.TypesFactory;
import com.casper.sdk.service.serialization.util.CollectionUtils;
import com.casper.sdk.types.*;
import org.junit.jupiter.api.Test;

import java.util.LinkedHashMap;
import java.util.Map;

import static com.casper.sdk.service.serialization.util.ByteUtils.concat;
import static com.casper.sdk.service.serialization.util.ByteUtils.decodeHex;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -40,7 +38,6 @@ void u64toBytes() {
@Test
void u512ValueToBytes() {


final CLValue source = new CLValue(
decodeHex("0500e40b5402"),
CLType.U512,
Expand Down Expand Up @@ -95,7 +92,6 @@ void byteOptionArrayKeyValue() {
assertThat(byteSerializer.toBytes(optionValue), is(expected));
}


@Test
void keyValueToBytes() {

Expand All @@ -118,4 +114,23 @@ void keyValueToBytes() {

assertThat(byteSerializer.toBytes(clKeyValue), is(expected));
}


@Test
void clMapTypeBytesTest() {

final CLValue key = CLValueBuilder.string("ABC");
final CLValue value = CLValueBuilder.i32(10);
final byte[] expectedBytes = {1, 0, 0, 0, 3, 0, 0, 0, 65, 66, 67, 10, 0, 0, 0};

// Assert the value builder can generate the bytes
final CLMap clMap = CLValueBuilder.map(CollectionUtils.Map.of(key, value));
assertThat(clMap.getBytes(), is(expectedBytes));

final byte[] expectedWithTypeBytes = {15, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 65, 66, 67, 10, 0, 0, 0, 17, 10, 1};

// Obtain the value with its type info
final byte[] bytes = byteSerializer.toBytes(clMap);
assertThat(bytes, is(expectedWithTypeBytes));
}
}
1 change: 1 addition & 0 deletions src/test/java/com/casper/sdk/types/DeployServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.casper.sdk.service.hash.HashService;
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.types.ByteSerializerFactory;
import com.casper.sdk.service.serialization.util.CollectionUtils;
Expand Down
Loading

0 comments on commit e2f97dc

Please sign in to comment.