Skip to content

Commit

Permalink
Added more tests and fail scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Nischal Sharma <[email protected]>
  • Loading branch information
NickSneo committed Apr 29, 2024
1 parent cefbf82 commit 45c5137
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
import java.math.BigInteger;
import java.util.Optional;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.web3j.utils.Restriction;

public class PrivTraceTransactionAcceptanceTest extends ParameterizedEnclaveTestBase {

private final PrivacyNode node;

private final PrivacyNode wrongNode;

public PrivTraceTransactionAcceptanceTest(
final Restriction restriction,
final EnclaveType enclaveType,
Expand All @@ -54,7 +58,18 @@ public PrivTraceTransactionAcceptanceTest(
false,
restriction == UNRESTRICTED);

wrongNode =
privacyBesu.createPrivateTransactionEnabledMinerNode(
restriction + "-node",
PrivacyAccountResolver.BOB.resolve(enclaveEncryptorType),
enclaveType,
Optional.empty(),
false,
false,
restriction == UNRESTRICTED);

privacyCluster.start(node);
privacyCluster.start(wrongNode);
}

@Test
Expand All @@ -66,19 +81,81 @@ public void getTransactionTrace() {
Updating the contract value
*/
Hash transactionHash =
Hash.fromHexString(doTransaction(privacyGroupId, simpleStorageContract, 1));
Hash.fromHexString(doTransaction(privacyGroupId, simpleStorageContract, 0));

final String result =
node.execute(privacyTransactions.privTraceTransaction(privacyGroupId, transactionHash));

System.out.println("privTransactionTrace = " + result);
assertThat(result).isNotNull();
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode rootNode = mapper.readTree(result);
JsonNode resultNode = rootNode.get("result");

assertThat(resultNode).isNotNull();
assertThat(resultNode.isArray()).isTrue();
assertThat(resultNode.size()).isGreaterThan(0);

JsonNode trace = resultNode.get(0);
assertThat(trace.get("action").get("callType").asText()).isEqualTo("call");
assertThat(trace.get("action").get("from").asText()).isEqualTo(node.getAddress().toString());
assertThat(trace.get("action").get("input").asText()).startsWith("0x60fe47b1");
assertThat(trace.get("action").get("to").asText())
.isEqualTo(simpleStorageContract.getContractAddress());
assertThat(trace.get("action").get("value").asText()).isEqualTo("0x0");
assertThat(trace.get("blockHash").asText()).isNotEmpty();
assertThat(trace.get("blockNumber").asInt()).isGreaterThan(0);
assertThat(trace.get("transactionHash").asText()).isEqualTo(transactionHash.toString());
assertThat(trace.get("type").asText()).isEqualTo("call");

} catch (Exception e) {
e.printStackTrace();
}

final String wrongPrivacyGroupId = createWrongPrivacyGroup();

final String resultEmpty =
wrongNode.execute(
privacyTransactions.privTraceTransaction(wrongPrivacyGroupId, transactionHash));

ObjectMapper mapperEmpty = new ObjectMapper();
try {
JsonNode rootNode = mapperEmpty.readTree(resultEmpty);
JsonNode resultNode = rootNode.get("result");

assertThat(resultNode).isNotNull();
assertThat(resultNode.isArray()).isTrue();
assertThat(resultNode.isEmpty()).isTrue();

} catch (Exception e) {
e.printStackTrace();
}

final String resultWrongHash =
wrongNode.execute(privacyTransactions.privTraceTransaction(privacyGroupId, Hash.EMPTY));

ObjectMapper mapperWrongHash = new ObjectMapper();
try {
JsonNode rootNode = mapperWrongHash.readTree(resultWrongHash);
JsonNode resultNode = rootNode.get("result");

assertThat(resultNode).isNotNull();
assertThat(resultNode.isArray()).isTrue();
assertThat(resultNode.isEmpty()).isTrue();

} catch (Exception e) {
e.printStackTrace();
}
}

private String createPrivacyGroup() {
return node.execute(createPrivacyGroup("myGroupName", "my group description", node));
}

private String createWrongPrivacyGroup() {
return wrongNode.execute(createPrivacyGroup("myGroupName", "my group description", wrongNode));
}

private SimpleStorage deploySimpleStorageContract(final String privacyGroupId) {
final SimpleStorage simpleStorage =
node.execute(
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Calculated : ${currentHash}
tasks.register('checkAPIChanges', FileStateChecker) {
description = "Checks that the API for the Plugin-API project does not change without deliberate thought"
files = sourceSets.main.allJava.files
knownHash = 'zns89fore/VSEiEk1mmdfIO/pX0ktMJ8Ks4j1Z6Ecoo='
knownHash = '53dRJ5mnFaM86Wslf2y/UE33DHPsJJV6kbH8vhUsY2o='
}
check.dependsOn('checkAPIChanges')

Expand Down

0 comments on commit 45c5137

Please sign in to comment.