From 62a91d8ff5baf35d778c83f64f30e6c8c08328e9 Mon Sep 17 00:00:00 2001 From: meywood Date: Tue, 26 Sep 2023 14:14:18 +0200 Subject: [PATCH 1/2] issues/222 - implemented missing scenarios for state identifiers --- .../steps/QueryBalanceStepDefinitions.java | 152 +++++++++++++++++- 1 file changed, 147 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/casper/sdk/e2e/steps/QueryBalanceStepDefinitions.java b/src/test/java/com/casper/sdk/e2e/steps/QueryBalanceStepDefinitions.java index 40e594f9a..4009a3231 100644 --- a/src/test/java/com/casper/sdk/e2e/steps/QueryBalanceStepDefinitions.java +++ b/src/test/java/com/casper/sdk/e2e/steps/QueryBalanceStepDefinitions.java @@ -1,30 +1,44 @@ package com.casper.sdk.e2e.steps; -import com.casper.sdk.e2e.utils.AssetUtils; -import com.casper.sdk.e2e.utils.CasperClientProvider; -import com.casper.sdk.e2e.utils.SimpleRcpClient; -import com.casper.sdk.e2e.utils.TestProperties; +import com.casper.sdk.e2e.utils.*; +import com.casper.sdk.helper.CasperTransferHelper; import com.casper.sdk.identifier.block.HashBlockIdentifier; +import com.casper.sdk.identifier.global.BlockHashIdentifier; +import com.casper.sdk.identifier.global.StateRootHashIdentifier; import com.casper.sdk.identifier.purse.MainPurseUnderAccountHash; import com.casper.sdk.identifier.purse.MainPurseUnderPublickey; import com.casper.sdk.identifier.purse.PurseIdentifier; import com.casper.sdk.identifier.purse.PurseUref; import com.casper.sdk.model.account.AccountData; import com.casper.sdk.model.balance.QueryBalanceData; +import com.casper.sdk.model.block.JsonBlockData; +import com.casper.sdk.model.common.Ttl; +import com.casper.sdk.model.deploy.Deploy; +import com.casper.sdk.model.deploy.DeployData; +import com.casper.sdk.model.deploy.DeployResult; +import com.casper.sdk.model.deploy.executionresult.Success; import com.casper.sdk.model.key.PublicKey; +import com.casper.sdk.model.stateroothash.StateRootHashData; import com.casper.sdk.model.uref.URef; import com.casper.sdk.service.CasperService; import com.fasterxml.jackson.databind.JsonNode; +import com.syntifi.crypto.key.AbstractPrivateKey; import com.syntifi.crypto.key.Ed25519PrivateKey; +import com.syntifi.crypto.key.Ed25519PublicKey; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; import java.io.IOException; import java.math.BigInteger; import java.net.URL; +import java.util.ArrayList; +import java.util.Date; +import java.util.Random; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsNull.notNullValue; @@ -40,6 +54,12 @@ public class QueryBalanceStepDefinitions { ); private QueryBalanceData queryBalanceData; private JsonNode queryBalanceJson; + private DeployData deployData; + private long transferAmount; + private long currentBalance; + private JsonBlockData initialBlock; + private QueryBalanceData initialBalance; + private String initialStateRootHash; @Given("that a query balance is obtained by main purse public key") public void thatAQueryBalanceIsObtainedByMainPursePublicKey() throws Exception { @@ -102,11 +122,133 @@ public void thatAQueryBalanceIsObtainedByMainPurseUref() throws Exception { queryBalanceJson = simpleRcpClient.queryBalance("purse_uref", uref); } + @When("a transfer of {long} is made to user-{int}'s purse") + public void aTransferOfIsMadeToUserPurse(long amount, int userId) throws Exception { + + this.transferAmount = amount; + initialBlock = casperService.getBlock(); + this.initialStateRootHash = casperService.getStateRootHash().getStateRootHash(); + + final AbstractPrivateKey faucetPrivateKey = getFaucetPrivateKey(); + + final PublicKey userPublicKey = getUserPublicKey(userId); + + initialBalance = casperService.queryBalance( + null, + MainPurseUnderPublickey.builder().publicKey(userPublicKey).build() + ); + + final Deploy deploy = CasperTransferHelper.buildTransferDeploy( + faucetPrivateKey, + userPublicKey, + BigInteger.valueOf(amount), + "casper-net-1", + Math.abs(new Random().nextLong()), + BigInteger.valueOf(100000000L), + 1L, + Ttl.builder().ttl("30m").build(), + new Date(), + new ArrayList<>()); + + final DeployResult deployResult = casperService.putDeploy(deploy); + deployData = DeployUtils.waitForDeploy(deployResult.getDeployHash(), 300, casperService); + + // Assert successful transfer + assertThat(deployData.getExecutionResults().size(), is(greaterThan(0))); + assertThat(deployData.getExecutionResults().get(0).getResult(), is(instanceOf(Success.class))); + assertThat(deployData.getExecutionResults().get(0).getBlockHash(), is(not(initialBlock.getBlock().getHash().toString()))); + } + + @And("that a query balance is obtained by user-{int}'s main purse public and latest block identifier") + public void thatAQueryBalanceIsObtainedByMainPursePublicKeyOfUserAndLatestBlockIdentifier(int userId) throws Exception { + final PublicKey publicKey = getUserPublicKey(userId); + final PurseIdentifier purseIdentifier = MainPurseUnderPublickey.builder().publicKey(publicKey).build(); + + // obtain using block updated in transfer + queryBalanceData = casperService.queryBalance( + BlockHashIdentifier.builder().hash(deployData.getExecutionResults().get(0).getBlockHash()).build(), + purseIdentifier + ); + + this.currentBalance = queryBalanceData.getBalance().longValue(); + } + + @Then("the balance includes the transferred amount") + public void theBalanceIncludesTheTransferredAmount() { + assertThat(queryBalanceData.getBalance().longValue(), is(initialBalance.getBalance().longValue() + transferAmount)); + } + + @When("that a query balance is obtained by user-{int}'s main purse public key and previous block identifier") + public void thatAQueryBalanceIsObtainedByMainPursePublicKeyOfUserAndPreviousBlockIdentifier(int userId) throws Exception { + final PublicKey publicKey = getUserPublicKey(userId); + + final PurseIdentifier purseIdentifier = MainPurseUnderPublickey.builder() + .publicKey(publicKey) + .build(); + + // obtain using initial block before transfer + queryBalanceData = casperService.queryBalance( + BlockHashIdentifier.builder().hash(initialBlock.getBlock().getHash().toString()).build(), + purseIdentifier + ); + } + + @And("that a query balance is obtained by user-{int}'s main purse public and latest state root hash identifier") + public void thatAQueryBalanceIsObtainedByUserSMainPursePublicAndLatestStateRootHashIdentifier(int userId) throws Exception { + final PublicKey publicKey = getUserPublicKey(userId); + + final PurseIdentifier purseIdentifier = MainPurseUnderPublickey.builder() + .publicKey(publicKey) + .build(); + + final StateRootHashData stateRootHash = casperService.getStateRootHash(); + assertThat(stateRootHash.getStateRootHash(), is(not(initialStateRootHash))); + + // obtain using initial block before transfer + queryBalanceData = casperService.queryBalance( + StateRootHashIdentifier.builder().hash(stateRootHash.getStateRootHash()).build(), + purseIdentifier + ); + } + + @When("that a query balance is obtained by user-{int}'s main purse public key and previous state root hash identifier") + public void thatAQueryBalanceIsObtainedByUserSMainPursePublicKeyAndPreviousStateRootHashIdentifier(int userId) throws Exception { + final PublicKey publicKey = getUserPublicKey(userId); + + final PurseIdentifier purseIdentifier = MainPurseUnderPublickey.builder() + .publicKey(publicKey) + .build(); + + // obtain using initial block before transfer + queryBalanceData = casperService.queryBalance( + StateRootHashIdentifier.builder().hash(initialStateRootHash).build(), + purseIdentifier + ); + } + + @Then("the balance is the pre transfer amount") + public void theBalanceIsThePreTransferAmount() { + assertThat(queryBalanceData.getBalance().longValue(), is(initialBalance.getBalance().longValue())); + } + private static PublicKey getFaucetPublicKey() throws IOException { + return PublicKey.fromAbstractPublicKey(getFaucetPrivateKey().derivePublicKey()); + } + + private static AbstractPrivateKey getFaucetPrivateKey() throws IOException { final URL faucetPrivateKeyUrl = AssetUtils.getFaucetAsset(1, "secret_key.pem"); assertThat(faucetPrivateKeyUrl, is(notNullValue())); final Ed25519PrivateKey privateKey = new Ed25519PrivateKey(); privateKey.readPrivateKey(faucetPrivateKeyUrl.getFile()); - return PublicKey.fromAbstractPublicKey(privateKey.derivePublicKey()); + return privateKey; + } + + private static PublicKey getUserPublicKey(final int userId) throws IOException { + final URL user1KeyUrl = AssetUtils.getUserKeyAsset(1, userId, "public_key.pem"); + final Ed25519PublicKey publicKey = new Ed25519PublicKey(); + publicKey.readPublicKey(user1KeyUrl.getFile()); + return PublicKey.fromAbstractPublicKey(publicKey); } + + } From cea0bf108d0f2904293e83e2f5a430ba59721e60 Mon Sep 17 00:00:00 2001 From: meywood Date: Tue, 26 Sep 2023 14:24:00 +0200 Subject: [PATCH 2/2] issues/222 - implemented missing scenarios for state identifiers --- .../com/casper/sdk/e2e/steps/QueryBalanceStepDefinitions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/casper/sdk/e2e/steps/QueryBalanceStepDefinitions.java b/src/test/java/com/casper/sdk/e2e/steps/QueryBalanceStepDefinitions.java index 4009a3231..7afa9ef1d 100644 --- a/src/test/java/com/casper/sdk/e2e/steps/QueryBalanceStepDefinitions.java +++ b/src/test/java/com/casper/sdk/e2e/steps/QueryBalanceStepDefinitions.java @@ -193,7 +193,7 @@ public void thatAQueryBalanceIsObtainedByMainPursePublicKeyOfUserAndPreviousBloc ); } - @And("that a query balance is obtained by user-{int}'s main purse public and latest state root hash identifier") + @And("that a query balance is obtained by user-{int}'s main purse public and latest state root hash identifier") public void thatAQueryBalanceIsObtainedByUserSMainPursePublicAndLatestStateRootHashIdentifier(int userId) throws Exception { final PublicKey publicKey = getUserPublicKey(userId);