Skip to content

Commit

Permalink
Merge pull request #295 from /issues/282
Browse files Browse the repository at this point in the history
Issues/282 - EraEndV2 and BlockV1
  • Loading branch information
cnorburn authored Jun 11, 2024
2 parents 482d04b + 7628650 commit 8e04e01
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 173 deletions.
5 changes: 1 addition & 4 deletions src/main/java/com/casper/sdk/model/block/BlockBody.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.casper.sdk.model.block;

import com.casper.sdk.model.common.Digest;
import com.casper.sdk.model.key.PublicKey;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
Expand All @@ -19,8 +18,6 @@ public abstract class BlockBody {
@JsonProperty("proposer")
private PublicKey proposer;

/** The body's hash. */
@JsonProperty("hash")
private Digest hash;

}

3 changes: 3 additions & 0 deletions src/main/java/com/casper/sdk/model/block/BlockBodyV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
@AllArgsConstructor
@NoArgsConstructor
public class BlockBodyV2 extends BlockBody {
/** The body's hash. */
@JsonProperty("hash")
private Digest hash;

/** List of Hex-encoded hash digest */
@JsonProperty("deploy_hashes")
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/casper/sdk/model/block/BlockV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import java.util.List;

/**
* A JSON-friendly representation of `Block`
*
Expand All @@ -27,8 +25,4 @@ public class BlockV1 extends Block<JsonBlockHeader, BlockBodyV1> {
/** {@link BlockBodyV1} */
@JsonProperty("body")
private BlockBodyV1 body;

/** List of {@link JsonProof} */
@JsonProperty("proofs")
private List<JsonProof> proofs;
}
3 changes: 3 additions & 0 deletions src/main/java/com/casper/sdk/model/era/EraEndV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Map;

/**
* Version 2 of the era end data.
*
* @author [email protected]
*/
@AllArgsConstructor
Expand All @@ -26,6 +28,7 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class EraEndV2 {

@JsonProperty("equivocators")
private List<PublicKey> equivocators;
@JsonProperty("inactive_validators")
private List<PublicKey> inactiveValidators;
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/casper/sdk/service/CasperService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.casper.sdk.model.balance.GetBalanceData;
import com.casper.sdk.model.balance.QueryBalanceData;
import com.casper.sdk.model.block.ChainGetBlockResponse;
import com.casper.sdk.model.block.JsonBlockData;
import com.casper.sdk.model.deploy.Deploy;
import com.casper.sdk.model.deploy.DeployData;
import com.casper.sdk.model.deploy.DeployResult;
Expand Down Expand Up @@ -89,11 +88,7 @@ static CasperService usingPeer(String ip, int port) throws MalformedURLException
* @return Object holding the api version and block
*/
@JsonRpcMethod("chain_get_block")
JsonBlockData getBlock();

@JsonRpcMethod("chain_get_block")
ChainGetBlockResponse getBlockV2();

ChainGetBlockResponse getBlock();

/**
* Retrieve block info by its {@link BlockIdentifier}
Expand All @@ -102,7 +97,7 @@ static CasperService usingPeer(String ip, int port) throws MalformedURLException
* @return Object holding the api version and block
*/
@JsonRpcMethod("chain_get_block")
JsonBlockData getBlock(@JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier);
ChainGetBlockResponse getBlock(@JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier);

/**
* Retrieve last block's transfers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ void validateJsonBlock_EraEndBlock() throws IOException, JSONException {

LOGGER.debug("Original JSON: {}", inputJson);

final JsonBlockData block = OBJECT_MAPPER.readValue(inputJson, JsonBlockData.class);
//noinspection VulnerableCodeUsages
final ChainGetBlockResponse block = OBJECT_MAPPER.readValue(inputJson, ChainGetBlockResponse.class);

assertNotNull(block.getBlock());
assertNotNull(block.getBlockWithSignatures().getBlock());

final String expectedJson = getPrettyJson(block);

Expand Down
61 changes: 61 additions & 0 deletions src/test/java/com/casper/sdk/model/era/EraEndV2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.casper.sdk.model.era;

import com.casper.sdk.model.key.PublicKey;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

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

/**
* Unit tests for {@link EraEndV2}.
*
* @author [email protected]
*/
class EraEndV2Test {

@Test
void parseEraEndV2() throws IOException, NoSuchAlgorithmException {

final InputStream jsonIn = Objects.requireNonNull(getClass().getResource("/era-info-samples/era_end_v2.json")).openStream();

//noinspection VulnerableCodeUsages
final EraEndV2 eraEndV2 = new ObjectMapper().readValue(jsonIn, EraEndV2.class);

assertThat(eraEndV2, is(notNullValue()));

assertThat(eraEndV2.getEquivocators().size(), is(3));
assertThat(eraEndV2.getEquivocators().get(0), is(PublicKey.fromTaggedHexString("01385e7e800dfcbf0abf1454d8e5300a09a6a42d7ca6d15bf9f45f932f2696bc0d")));
assertThat(eraEndV2.getEquivocators().get(2), is(PublicKey.fromTaggedHexString("01ca84d8c6ea00653473da0f46642f49e8465c82aa2ff281100e2002a9d70f51d7")));

assertThat(eraEndV2.getInactiveValidators().size(), is(3));
assertThat(eraEndV2.getInactiveValidators().get(1), is(PublicKey.fromTaggedHexString("01aed5e394a3e59b77ca6af6c97d584e0aa8d4ee40c4e51b27b635ec255f9ce1bd")));
assertThat(eraEndV2.getInactiveValidators().get(2), is(PublicKey.fromTaggedHexString("01e71a498829ab4247d2d48022d95e23374c2138b184ebb1bc0bc4a45c5b24f9d5")));

assertThat(eraEndV2.getNextEraValidatorWeights().size(), is(5));
assertThat(eraEndV2.getNextEraValidatorWeights().get(0).getValidator(), is(PublicKey.fromTaggedHexString("010b277da84a12c8814d5723eeb57123ff287f22466fd13faca1bb1fae57d2679b")));
assertThat(eraEndV2.getNextEraValidatorWeights().get(0).getWeight(), is(new BigInteger("11823890605832274469")));
assertThat(eraEndV2.getNextEraValidatorWeights().get(4).getValidator(), is(PublicKey.fromTaggedHexString("01f6e6e82759da039d479900ad1b930320082d1fc00aea6f5e5f7289c7a6c90536")));
assertThat(eraEndV2.getNextEraValidatorWeights().get(4).getWeight(), is(new BigInteger("11990568590760997935")));

assertThat(eraEndV2.getRewards().size(), is(5));
assertThat(
eraEndV2.getRewards().get(PublicKey.fromTaggedHexString("010b277da84a12c8814d5723eeb57123ff287f22466fd13faca1bb1fae57d2679b")),
hasItems(new BigInteger("4026058477024681"), new BigInteger("402627925137076"))
);
assertThat(
eraEndV2.getRewards().get(PublicKey.fromTaggedHexString("01f6e6e82759da039d479900ad1b930320082d1fc00aea6f5e5f7289c7a6c90536")),
hasItems(new BigInteger("13081818953982388"), new BigInteger("408309291908807"))
);

assertThat(eraEndV2.getNextEraGasPrice(), is(2));
}
}
28 changes: 15 additions & 13 deletions src/test/java/com/casper/sdk/service/CasperServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ void testIfBlockReturnedMatchesRequestedByHash() {
.withBody("$.params.block_identifier.Height", "2346915")
.thenDispatch(getClass().getResource("/block-samples/chain_get_block.json"));

final JsonBlockData resultByHeight = casperServiceMock.getBlock(new HeightBlockIdentifier(2346915));
final String hash = resultByHeight.getBlock().getHash().toString();
final ChainGetBlockResponse resultByHeight = casperServiceMock.getBlock(new HeightBlockIdentifier(2346915));
final String hash = resultByHeight.getBlockWithSignatures().getBlock().getHash().toString();

when.clear()
.withMethod("chain_get_block")
.withBody("$.params.block_identifier.Hash", hash)
.thenDispatch(getClass().getResource("/block-samples/chain_get_block.json"));

final JsonBlockData resultByHash = casperServiceMock.getBlock(new HashBlockIdentifier(hash));
assertEquals(resultByHash.getBlock().getHash().toString(), hash);
final ChainGetBlockResponse resultByHash = casperServiceMock.getBlock(new HashBlockIdentifier(hash));
assertEquals(resultByHash.getBlockWithSignatures().getBlock().getHash().toString(), hash);
}

/**
Expand All @@ -112,8 +112,8 @@ void testFirstBlocksPublicKeySerialization() {
.withBody("$.params.block_identifier.Height", "23469150")
.thenDispatch(getClass().getResource("/block-samples/chain_get_block.json"));

final JsonBlockData result = casperServiceMock.getBlock(new HeightBlockIdentifier(23469150));
final PublicKey key = result.getBlock().getBody().getProposer();
final ChainGetBlockResponse result = casperServiceMock.getBlock(new HeightBlockIdentifier(23469150));
final PublicKey key = result.getBlockWithSignatures().getBlock().getBody().getProposer();

assertEquals(AlgorithmTag.ED25519, key.getTag());
assertEquals("01753af321afc6906fcd9c897e5328f66190b5842671f16b022b69f2ddb7619c32", key.getAlgoTaggedHex());
Expand Down Expand Up @@ -142,8 +142,9 @@ void retrieveLastBlock() {
.withMethod("chain_get_block")
.thenDispatch(getClass().getResource("/block-samples/chain_get_block.json"));

final JsonBlockData blockData = casperServiceMock.getBlock();
final ChainGetBlockResponse blockData = casperServiceMock.getBlock();
assertNotNull(blockData);
assertThat(blockData.getBlockWithSignatures().getBlock().getHash(), is(new Digest("709a31cbaff23da43995e78d2209e7f5980905cf70ef850f6744b8d3cec9af13")));
}

@Test
Expand All @@ -154,10 +155,11 @@ void getBlockByHash() {
.withBody("$.params.block_identifier.Hash", "709a31cbaff23da43995e78d2209e7f5980905cf70ef850f6744b8d3cec9af13")
.thenDispatch(getClass().getResource("/block-samples/chain_get_block.json"));

final JsonBlockData blockData = casperServiceMock.getBlock(new HashBlockIdentifier("709a31cbaff23da43995e78d2209e7f5980905cf70ef850f6744b8d3cec9af13"));
final ChainGetBlockResponse blockData = casperServiceMock.getBlock(new HashBlockIdentifier("709a31cbaff23da43995e78d2209e7f5980905cf70ef850f6744b8d3cec9af13"));
assertNotNull(blockData);

final BlockV1 block = blockData.getBlock();
final BlockV1 block = blockData.getBlockWithSignatures().getBlock();

assertEquals("ee3da162c775f921e836ec6d41dedcb006bb972224d1058738e9413dea61fd5e", block.getHeader().getParentHash().toString());
assertEquals(2346915, block.getHeader().getHeight());
}
Expand All @@ -170,9 +172,9 @@ void getBlockByHeight() {
.withBody("$.params.block_identifier.Height", "2346915")
.thenDispatch(getClass().getResource("/block-samples/chain_get_block.json"));

JsonBlockData blockData = casperServiceMock.getBlock(new HeightBlockIdentifier(2346915));
ChainGetBlockResponse blockData = casperServiceMock.getBlock(new HeightBlockIdentifier(2346915));
assertNotNull(blockData);
BlockV1 block = blockData.getBlock();
BlockV1 block = blockData.getBlockWithSignatures().getBlock();
assertEquals("ee3da162c775f921e836ec6d41dedcb006bb972224d1058738e9413dea61fd5e", block.getHeader().getParentHash().toString());
assertEquals("709a31cbaff23da43995e78d2209e7f5980905cf70ef850f6744b8d3cec9af13", block.getHash().toString());
}
Expand Down Expand Up @@ -510,7 +512,7 @@ void chainGetBlockV2() {
//.withBody("$.params.deploy_hash", "abc")
.thenDispatch(getClass().getResource("/block-samples/chain_get_block_v2.json"));

final ChainGetBlockResponse blockWithSignatures = casperServiceMock.getBlockV2();
final ChainGetBlockResponse blockWithSignatures = casperServiceMock.getBlock();
assertThat(blockWithSignatures, is(notNullValue()));
assertThat(blockWithSignatures.getApiVersion(), is("2.0.0"));
assertThat(blockWithSignatures.getBlockWithSignatures(), is(notNullValue()));
Expand Down Expand Up @@ -551,7 +553,7 @@ void chainGetBlockEraEndV2() throws NoSuchAlgorithmException {
//.withBody("$.params.deploy_hash", "abc")
.thenDispatch(getClass().getResource("/block-samples/chain_get_block_era_end_v2.json"));

final ChainGetBlockResponse blockWithSignatures = casperServiceMock.getBlockV2();
final ChainGetBlockResponse blockWithSignatures = casperServiceMock.getBlock();
assertThat(blockWithSignatures, is(notNullValue()));
assertThat(blockWithSignatures.getApiVersion(), is("2.0.0"));
assertThat(blockWithSignatures.getBlockWithSignatures(), is(notNullValue()));
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/com/casper/sdk/service/CasperServiceTestsNctl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.casper.sdk.identifier.purse.MainPurseUnderPublickey;
import com.casper.sdk.identifier.purse.PurseIdentifier;
import com.casper.sdk.model.balance.QueryBalanceData;
import com.casper.sdk.model.block.JsonBlockData;
import com.casper.sdk.model.block.ChainGetBlockResponse;
import com.casper.sdk.model.era.EraInfoData;
import com.casper.sdk.model.key.PublicKey;
import com.casper.sdk.model.status.ChainspecData;
Expand All @@ -28,7 +28,7 @@
import static org.junit.jupiter.api.Assertions.*;

@Disabled
public class CasperServiceTestsNctl extends AbstractJsonRpcTests {
public class CasperServiceTestsNctl extends AbstractJsonRpcTests {

/**
* Test if get block matches requested by height
Expand All @@ -37,8 +37,8 @@ public class CasperServiceTestsNctl extends AbstractJsonRpcTests {
void testIfBlockReturnedMatchesRequestedByHeight() {
int blocks_to_check = 3;
for (int i = 0; i < blocks_to_check; i++) {
JsonBlockData result = casperServiceNctl.getBlock(new HeightBlockIdentifier(i));
assertEquals(result.getBlock().getHeader().getHeight(), i);
ChainGetBlockResponse result = casperServiceNctl.getBlock(new HeightBlockIdentifier(i));
assertEquals(result.getBlockWithSignatures().getBlock().getHeader().getHeight(), i);
}
}

Expand All @@ -52,8 +52,8 @@ void getStatus() {

@Test
void getEraSummaryByHash() {
JsonBlockData block = casperServiceNctl.getBlock(new HeightBlockIdentifier(10));
EraInfoData eraInfoData = casperServiceNctl.getEraSummary(new HashBlockIdentifier(block.getBlock().getHash().toString()));
ChainGetBlockResponse block = casperServiceNctl.getBlock(new HeightBlockIdentifier(10));
EraInfoData eraInfoData = casperServiceNctl.getEraSummary(new HashBlockIdentifier(block.getBlockWithSignatures().getBlock().getHash().toString()));

assertNotNull(eraInfoData);
assertNotNull(eraInfoData.getEraSummary());
Expand Down Expand Up @@ -157,7 +157,7 @@ void getGlobalStateAndSaveAsFile() throws IOException {
assertTrue(Files.size(temp) > 0);

String fileContent = new String(Files.readAllBytes(temp));
assertTrue(fileContent.length() > 0);
assertTrue(!fileContent.isEmpty());
} else {
assertThrowsExactly(CasperInvalidStateException.class, () -> chainspec.saveGlobalState(temp));
}
Expand Down
Loading

0 comments on commit 8e04e01

Please sign in to comment.