Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Refactor privacy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iikirilov committed May 22, 2019
1 parent 221ea89 commit dea23f6
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1,002 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,30 @@
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.privacy;

import tech.pegasys.orion.testutil.OrionTestHarness;
import tech.pegasys.orion.testutil.OrionTestHarnessFactory;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase;
import tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc.Eea;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eea.EeaTransactions;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eea.PrivateTransactionBuilder;

import java.io.IOException;

import org.junit.ClassRule;
import org.junit.rules.TemporaryFolder;

public class PrivateAcceptanceTestBase extends AcceptanceTestBase {
public class PrivacyAcceptanceTestBase extends AcceptanceTestBase {
@ClassRule public static final TemporaryFolder privacy = new TemporaryFolder();

protected final Eea eea;
protected final PrivateTransactions privateTransactions;
protected static PrivateTransactionBuilder.Builder privateTransactionBuilder;
protected final PrivateTransactionVerifier privateTransactionVerifier;
protected final PrivacyPantheonNodeFactory privacyPantheon;

public PrivateAcceptanceTestBase() {
public PrivacyAcceptanceTestBase() {
final EeaTransactions eeaTransactions = new EeaTransactions();

privateTransactions = new PrivateTransactions();
eea = new Eea(eeaTransactions);
privateTransactionBuilder = PrivateTransactionBuilder.builder();
privateTransactionVerifier = new PrivateTransactionVerifier(eea, transactions);
}

protected static OrionTestHarness createEnclave(
final String pubKey, final String privKey, final String... othernode) throws Exception {
return OrionTestHarnessFactory.create(privacy.newFolder().toPath(), pubKey, privKey, othernode);
}

protected static PrivacyParameters getPrivacyParameters(final OrionTestHarness testHarness)
throws IOException {
return new PrivacyParameters.Builder()
.setEnabled(true)
.setEnclaveUrl(testHarness.clientUrl())
.setEnclavePublicKeyUsingFile(testHarness.getConfig().publicKeys().get(0).toFile())
.setDataDir(privacy.newFolder().toPath())
.build();
privacyPantheon = new PrivacyPantheonNodeFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,127 +12,54 @@
*/
package tech.pegasys.pantheon.tests.web3j.privacy;

import static java.nio.charset.StandardCharsets.UTF_8;
import static tech.pegasys.pantheon.tests.acceptance.dsl.WaitUtils.waitFor;
import tech.pegasys.pantheon.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase;
import tech.pegasys.pantheon.tests.acceptance.dsl.privacy.PrivacyNet;

import tech.pegasys.orion.testutil.OrionTestHarness;
import tech.pegasys.pantheon.crypto.SECP256K1;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode;
import tech.pegasys.pantheon.tests.acceptance.dsl.privacy.PrivateAcceptanceTestBase;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eea.PrivateTransactionBuilder.TransactionType;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.math.BigInteger;

import com.google.common.collect.Lists;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DeployPrivateSmartContractAcceptanceTest extends PrivateAcceptanceTestBase {
public class DeployPrivateSmartContractAcceptanceTest extends PrivacyAcceptanceTestBase {

// Contract address is generated from sender address and transaction nonce and privacy group id
protected static final Address CONTRACT_ADDRESS =
Address.fromHexString("0x06088ead8384df709132151403e08c2b978beb85");
protected static final String PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=";
private SECP256K1.KeyPair keypair =
SECP256K1.KeyPair.create(
SECP256K1.PrivateKey.create(
new BigInteger(
"8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", 16)));
protected static final String CONTRACT_NAME = "Event Emitter";

private PantheonNode minerNode;
private static OrionTestHarness enclave;
private String deployContract;
private String storeValue;
private String getValue;
private EventEmmiterHarness eventEmmiterHarness;
private PrivacyNet privacyNet;

@Before
public void setUp() throws Exception {
enclave = createEnclave("orion_key_0.pub", "orion_key_0.key");
minerNode =
pantheon.createPrivateTransactionEnabledMinerNode(
"miner-node", getPrivacyParameters(enclave), "key");
cluster.start(minerNode);

deployContract =
privateTransactionBuilder
.nonce(0)
.from(minerNode.getAddress())
.to(null)
.privateFrom(BytesValue.wrap(PUBLIC_KEY.getBytes(UTF_8)))
.privateFor(Lists.newArrayList())
.keyPair(keypair)
.build(TransactionType.CREATE_CONTRACT);

storeValue =
privateTransactionBuilder
.nonce(1)
.from(minerNode.getAddress())
.to(CONTRACT_ADDRESS)
.privateFrom(BytesValue.wrap(PUBLIC_KEY.getBytes(UTF_8)))
.privateFor(Lists.newArrayList())
.keyPair(keypair)
.build(TransactionType.STORE);

getValue =
privateTransactionBuilder
.nonce(2)
.from(minerNode.getAddress())
.to(CONTRACT_ADDRESS)
.privateFrom(BytesValue.wrap(PUBLIC_KEY.getBytes(UTF_8)))
.privateFor(Lists.newArrayList())
.keyPair(keypair)
.build(TransactionType.GET);
privacyNet =
PrivacyNet.builder(privacy, privacyPantheon, cluster, false).addMinerNode("Alice").build();
privacyNet.startPrivacyNet();
eventEmmiterHarness =
new EventEmmiterHarness(
privateTransactionBuilder,
privacyNet,
privateTransactions,
privateTransactionVerifier,
eea);
}

@Test
public void deployingMustGiveValidReceipt() {
final String transactionHash =
minerNode.execute(privateTransactions.deployPrivateSmartContract(deployContract));

privateTransactionVerifier
.validPrivateContractDeployed(CONTRACT_ADDRESS.toString())
.verify(minerNode, transactionHash);
eventEmmiterHarness.deploy(CONTRACT_NAME, "Alice");
}

@Test
public void privateSmartContractMustEmitEvents() {
String transactionHash =
minerNode.execute(privateTransactions.deployPrivateSmartContract(deployContract));

waitForTransactionToBeMined(transactionHash);

transactionHash =
minerNode.execute(privateTransactions.createPrivateRawTransaction(storeValue));
privateTransactionVerifier.validEventReturned("1000").verify(minerNode, transactionHash);
eventEmmiterHarness.deploy(CONTRACT_NAME, "Alice");
eventEmmiterHarness.store(CONTRACT_NAME, "Alice");
}

@Test
public void privateSmartContractMustReturnValues() {

String transactionHash =
minerNode.execute(privateTransactions.deployPrivateSmartContract(deployContract));

waitForTransactionToBeMined(transactionHash);

transactionHash =
minerNode.execute(privateTransactions.createPrivateRawTransaction(storeValue));

waitForTransactionToBeMined(transactionHash);

transactionHash = minerNode.execute(privateTransactions.createPrivateRawTransaction(getValue));

privateTransactionVerifier.validOutputReturned("1000").verify(minerNode, transactionHash);
eventEmmiterHarness.deploy(CONTRACT_NAME, "Alice");
eventEmmiterHarness.store(CONTRACT_NAME, "Alice");
eventEmmiterHarness.get(CONTRACT_NAME, "Alice");
}

@After
public void tearDown() {
enclave.getOrion().stop();
}

public void waitForTransactionToBeMined(final String transactionHash) {
waitFor(() -> minerNode.verify(eea.expectSuccessfulTransactionReceipt(transactionHash)));
privacyNet.stopPrivacyNet();
}
}
Loading

0 comments on commit dea23f6

Please sign in to comment.