Skip to content

Commit

Permalink
issues/279 - BlockV1
Browse files Browse the repository at this point in the history
  • Loading branch information
meywood committed Jun 11, 2024
1 parent 0021d2f commit 7628650
Show file tree
Hide file tree
Showing 9 changed files with 173 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;
}
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
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
130 changes: 67 additions & 63 deletions src/test/resources/block-samples/block-end-era.json
Original file line number Diff line number Diff line change
@@ -1,68 +1,72 @@
{
"api_version" : "1.3.2",
"block" : {
"hash" : "f21dfd6c290d4e3fc6a9fc16471fd734c18d38c2fe5c7c305255f99acb2e3fa3",
"header" : {
"height" : 239119,
"state_root_hash" : "2c6af39cb8bcda66763047ddd424ee2b98f3506af02e2fc3cf5cae2357c8574b",
"random_bit" : true,
"era_end" : {
"era_report" : {
"equivocators" : [],
"rewards" : [
{
"validator" : "01026ca707c348ed8012ac6a1f28db031fadd6eb67203501a353b867a08c8b9a80",
"amount" : 2426183451049
},
{
"validator" : "01031cdce87d5fe53246492f9262932f9eb7421ea54b30da1eca06874fd2a7df60",
"amount" : 265833229
}
],
"inactive_validators" : []
},
"next_era_validator_weights" : [
{
"validator" : "01026ca707c348ed8012ac6a1f28db031fadd6eb67203501a353b867a08c8b9a80",
"weight" : "160318940675942026"
},
{
"validator" : "01031cdce87d5fe53246492f9262932f9eb7421ea54b30da1eca06874fd2a7df60",
"weight" : "17565906011961"
}
]
"api_version": "1.3.2",
"block_with_signatures": {
"block": {
"Version1": {
"hash": "f21dfd6c290d4e3fc6a9fc16471fd734c18d38c2fe5c7c305255f99acb2e3fa3",
"header": {
"height": 239119,
"state_root_hash": "2c6af39cb8bcda66763047ddd424ee2b98f3506af02e2fc3cf5cae2357c8574b",
"random_bit": true,
"era_end": {
"era_report": {
"equivocators": [],
"rewards": [
{
"validator": "01026ca707c348ed8012ac6a1f28db031fadd6eb67203501a353b867a08c8b9a80",
"amount": 2426183451049
},
{
"validator": "01031cdce87d5fe53246492f9262932f9eb7421ea54b30da1eca06874fd2a7df60",
"amount": 265833229
}
],
"inactive_validators": []
},
"body_hash" : "a35904b9316aa35b30a303077a2a55800d5413d30fb29489a2ad3a16c6fcb268",
"parent_hash" : "f8a21a93b2e51fc3833473fede8d353230c7f820b07e9eb7b0617b82576f3d95",
"accumulated_seed" : "0d4adfd35e846c6a567e99b6b926aca1d9ec3ee3a7bec961edf900a636b17ccb",
"timestamp" : "2021-09-30T14:43:26.336Z",
"era_id" : 2192,
"protocol_version" : "1.3.2"
},
"body" : {
"proposer" : "011117189c666f81c5160cd610ee383dc9b2d0361f004934754d39752eedc64957",
"deploy_hashes" : [],
"transfer_hashes" : [
"82c794e4310eda0a39af3f5099c8a533e0b4f9142bd614ee725b6b261e3d9579"
"next_era_validator_weights": [
{
"validator": "01026ca707c348ed8012ac6a1f28db031fadd6eb67203501a353b867a08c8b9a80",
"weight": "160318940675942026"
},
{
"validator": "01031cdce87d5fe53246492f9262932f9eb7421ea54b30da1eca06874fd2a7df60",
"weight": "17565906011961"
}
]
},
"proofs" : [
{
"signature" : "0110ef8f863dbaed2ce67709e694eb29a14866ad9aff5c55c98948248bd0526b2455941490764c3dfa83052680d552fe8a26e0714b7958bcec219ca75b3d325802",
"public_key" : "01026ca707c348ed8012ac6a1f28db031fadd6eb67203501a353b867a08c8b9a80"
},
{
"signature" : "013383fa1a734a7edd24b10a944ed69dd97ac528ca1d6559bd69570fa2bad15a5262878eaf4324a67aea47eea8aba5e26661a0f81cc11a4134e3ebaee96c76580d",
"public_key" : "01031cdce87d5fe53246492f9262932f9eb7421ea54b30da1eca06874fd2a7df60"
},
{
"signature" : "01dfbb12768fc71bc13a792626fc3e5c0ce744bcf2174f55e6a5a5e33d0f6f4e7256bc3c9c09fd9f4dc7b237295842cd10dc5219a1bd662cad157cf7d051364407",
"public_key" : "0103dd8b2b18ef0b9fd5b7c8e340b104ee4d966f2a167eb1a938963f8c8f699a45"
},
{
"signature" : "01503f8ac5391cbd5900807b46821e0caa7faba18dfdb6cb931625dc2f9b3b3574df84b68aedc3b7b3d7457c47d909c70f8112d41bb2bf816fc6909df07bcd7b01",
"public_key" : "010427c1d1227c9d2aafe8c06c6e6b276da8dcd8fd170ca848b8e3e8e1038a6dc8"
}
]
},
"body_hash": "a35904b9316aa35b30a303077a2a55800d5413d30fb29489a2ad3a16c6fcb268",
"parent_hash": "f8a21a93b2e51fc3833473fede8d353230c7f820b07e9eb7b0617b82576f3d95",
"accumulated_seed": "0d4adfd35e846c6a567e99b6b926aca1d9ec3ee3a7bec961edf900a636b17ccb",
"timestamp": "2021-09-30T14:43:26.336Z",
"era_id": 2192,
"protocol_version": "1.3.2"
},
"body": {
"proposer": "011117189c666f81c5160cd610ee383dc9b2d0361f004934754d39752eedc64957",
"deploy_hashes": [],
"transfer_hashes": [
"82c794e4310eda0a39af3f5099c8a533e0b4f9142bd614ee725b6b261e3d9579"
]
}
}
},
"proofs": [
{
"signature": "0110ef8f863dbaed2ce67709e694eb29a14866ad9aff5c55c98948248bd0526b2455941490764c3dfa83052680d552fe8a26e0714b7958bcec219ca75b3d325802",
"public_key": "01026ca707c348ed8012ac6a1f28db031fadd6eb67203501a353b867a08c8b9a80"
},
{
"signature": "013383fa1a734a7edd24b10a944ed69dd97ac528ca1d6559bd69570fa2bad15a5262878eaf4324a67aea47eea8aba5e26661a0f81cc11a4134e3ebaee96c76580d",
"public_key": "01031cdce87d5fe53246492f9262932f9eb7421ea54b30da1eca06874fd2a7df60"
},
{
"signature": "01dfbb12768fc71bc13a792626fc3e5c0ce744bcf2174f55e6a5a5e33d0f6f4e7256bc3c9c09fd9f4dc7b237295842cd10dc5219a1bd662cad157cf7d051364407",
"public_key": "0103dd8b2b18ef0b9fd5b7c8e340b104ee4d966f2a167eb1a938963f8c8f699a45"
},
{
"signature": "01503f8ac5391cbd5900807b46821e0caa7faba18dfdb6cb931625dc2f9b3b3574df84b68aedc3b7b3d7457c47d909c70f8112d41bb2bf816fc6909df07bcd7b01",
"public_key": "010427c1d1227c9d2aafe8c06c6e6b276da8dcd8fd170ca848b8e3e8e1038a6dc8"
}
]
}
}
Loading

0 comments on commit 7628650

Please sign in to comment.