diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/ParameterizedEnclaveTestBase.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/ParameterizedEnclaveTestBase.java deleted file mode 100644 index 581fd71722d7..000000000000 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/ParameterizedEnclaveTestBase.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.tests.acceptance.dsl.privacy; - -import static org.hyperledger.enclave.testutil.EnclaveEncryptorType.EC; -import static org.hyperledger.enclave.testutil.EnclaveEncryptorType.NACL; -import static org.hyperledger.enclave.testutil.EnclaveType.NOOP; -import static org.hyperledger.enclave.testutil.EnclaveType.TESSERA; -import static org.web3j.utils.Restriction.RESTRICTED; -import static org.web3j.utils.Restriction.UNRESTRICTED; - -import org.hyperledger.besu.tests.acceptance.dsl.privacy.transaction.PluginCreateRandomPrivacyGroupIdTransaction; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.transaction.RestrictedCreatePrivacyGroupTransaction; -import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction; -import org.hyperledger.enclave.testutil.EnclaveEncryptorType; -import org.hyperledger.enclave.testutil.EnclaveType; - -import java.util.Arrays; -import java.util.Collection; - -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import org.web3j.utils.Restriction; - -@RunWith(Parameterized.class) -public abstract class ParameterizedEnclaveTestBase extends PrivacyAcceptanceTestBase { - protected final Restriction restriction; - protected final EnclaveType enclaveType; - protected final EnclaveEncryptorType enclaveEncryptorType; - - protected ParameterizedEnclaveTestBase( - final Restriction restriction, - final EnclaveType enclaveType, - final EnclaveEncryptorType enclaveEncryptorType) { - this.restriction = restriction; - this.enclaveType = enclaveType; - this.enclaveEncryptorType = enclaveEncryptorType; - } - - @Parameters(name = "{0} tx with {1} enclave and {2} encryptor type") - public static Collection params() { - return Arrays.asList( - new Object[][] { - {RESTRICTED, TESSERA, NACL}, - {RESTRICTED, TESSERA, EC}, - {UNRESTRICTED, NOOP, EnclaveEncryptorType.NOOP} - }); - } - - public Transaction createPrivacyGroup( - final String name, final String description, final PrivacyNode... nodes) { - - if (restriction == RESTRICTED) { - return new RestrictedCreatePrivacyGroupTransaction(name, description, nodes); - } else if (restriction == UNRESTRICTED) { - return new PluginCreateRandomPrivacyGroupIdTransaction(); - } else { - throw new RuntimeException("Do not know how to handle " + restriction); - } - } -} diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyAcceptanceTestBase.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyAcceptanceTestBase.java deleted file mode 100644 index 8e12cbc7a80d..000000000000 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyAcceptanceTestBase.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.tests.acceptance.dsl.privacy; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.hyperledger.besu.tests.acceptance.dsl.WaitUtils; -import org.hyperledger.besu.tests.acceptance.dsl.condition.eth.EthConditions; -import org.hyperledger.besu.tests.acceptance.dsl.condition.net.NetConditions; -import org.hyperledger.besu.tests.acceptance.dsl.condition.priv.PrivConditions; -import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.privacy.PrivacyNodeFactory; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.condition.PrivateContractVerifier; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.condition.PrivateTransactionVerifier; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.contract.PrivateContractTransactions; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.transaction.PrivacyTransactions; -import org.hyperledger.besu.tests.acceptance.dsl.transaction.contract.ContractTransactions; -import org.hyperledger.besu.tests.acceptance.dsl.transaction.eth.EthTransactions; -import org.hyperledger.besu.tests.acceptance.dsl.transaction.net.NetTransactions; - -import java.math.BigInteger; - -import io.vertx.core.Vertx; -import org.junit.After; -import org.junit.ClassRule; -import org.junit.rules.TemporaryFolder; - -public class PrivacyAcceptanceTestBase { - @ClassRule public static final TemporaryFolder privacy = new TemporaryFolder(); - - protected final PrivacyTransactions privacyTransactions; - protected final PrivateContractVerifier privateContractVerifier; - protected final PrivateTransactionVerifier privateTransactionVerifier; - protected final PrivacyNodeFactory privacyBesu; - protected final PrivateContractTransactions privateContractTransactions; - protected final PrivConditions priv; - protected final PrivacyCluster privacyCluster; - protected final ContractTransactions contractTransactions; - protected final NetConditions net; - protected final EthTransactions ethTransactions; - protected final EthConditions eth; - private final Vertx vertx = Vertx.vertx(); - - public PrivacyAcceptanceTestBase() { - ethTransactions = new EthTransactions(); - net = new NetConditions(new NetTransactions()); - privacyTransactions = new PrivacyTransactions(); - privateContractVerifier = new PrivateContractVerifier(); - privateTransactionVerifier = new PrivateTransactionVerifier(privacyTransactions); - privacyBesu = new PrivacyNodeFactory(vertx); - privateContractTransactions = new PrivateContractTransactions(); - privacyCluster = new PrivacyCluster(net); - priv = - new PrivConditions( - new org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy - .PrivacyTransactions()); - contractTransactions = new ContractTransactions(); - eth = new EthConditions(ethTransactions); - } - - protected void waitForBlockHeight(final PrivacyNode node, final long blockchainHeight) { - WaitUtils.waitFor( - 120, - () -> - assertThat(node.execute(ethTransactions.blockNumber())) - .isGreaterThanOrEqualTo(BigInteger.valueOf(blockchainHeight))); - } - - @After - public void tearDownAcceptanceTestBase() { - privacyCluster.close(); - vertx.close(); - } -} diff --git a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/BftPrivacyClusterAcceptanceTest.java b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/BftPrivacyClusterAcceptanceTest.java deleted file mode 100644 index 4475627fd1d3..000000000000 --- a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/BftPrivacyClusterAcceptanceTest.java +++ /dev/null @@ -1,318 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.tests.acceptance.privacy; - -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.account.PrivacyAccountResolver; -import org.hyperledger.besu.tests.acceptance.dsl.transaction.bft.ConsensusType; -import org.hyperledger.besu.tests.web3j.generated.EventEmitter; -import org.hyperledger.enclave.testutil.EnclaveEncryptorType; -import org.hyperledger.enclave.testutil.EnclaveType; - -import java.io.IOException; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Optional; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.testcontainers.containers.Network; -import org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt; -import org.web3j.utils.Restriction; - -@RunWith(Parameterized.class) -public class BftPrivacyClusterAcceptanceTest extends PrivacyAcceptanceTestBase { - private final BftPrivacyType bftPrivacyType; - - public static class BftPrivacyType { - private final EnclaveType enclaveType; - private final EnclaveEncryptorType enclaveEncryptorType; - private final ConsensusType consensusType; - private final Restriction restriction; - - public BftPrivacyType( - final EnclaveType enclaveType, - final EnclaveEncryptorType enclaveEncryptorType, - final ConsensusType consensusType, - final Restriction restriction) { - this.enclaveType = enclaveType; - this.enclaveEncryptorType = enclaveEncryptorType; - this.consensusType = consensusType; - this.restriction = restriction; - } - - @Override - public String toString() { - return String.join( - ",", - enclaveType.toString(), - enclaveEncryptorType.toString(), - consensusType.toString(), - restriction.toString()); - } - } - - public BftPrivacyClusterAcceptanceTest(final BftPrivacyType bftPrivacyType) { - this.bftPrivacyType = bftPrivacyType; - } - - @Parameterized.Parameters(name = "{0}") - public static Collection bftPrivacyTypes() { - final List bftPrivacyTypes = new ArrayList<>(); - for (EnclaveType x : EnclaveType.valuesForTests()) { - for (ConsensusType consensusType : ConsensusType.values()) { - bftPrivacyTypes.add( - new BftPrivacyType( - x, EnclaveEncryptorType.NACL, consensusType, Restriction.RESTRICTED)); - bftPrivacyTypes.add( - new BftPrivacyType(x, EnclaveEncryptorType.EC, consensusType, Restriction.RESTRICTED)); - } - } - - for (ConsensusType consensusType : ConsensusType.values()) { - bftPrivacyTypes.add( - new BftPrivacyType( - EnclaveType.NOOP, - EnclaveEncryptorType.NOOP, - consensusType, - Restriction.UNRESTRICTED)); - } - - return bftPrivacyTypes; - } - - private PrivacyNode alice; - private PrivacyNode bob; - private PrivacyNode charlie; - - @Before - public void setUp() throws Exception { - final Network containerNetwork = Network.newNetwork(); - - alice = createNode(containerNetwork, "node1", 0); - bob = createNode(containerNetwork, "node2", 1); - charlie = createNode(containerNetwork, "node3", 2); - - privacyCluster.start(alice, bob, charlie); - alice.verify(priv.syncingStatus(false)); - bob.verify(priv.syncingStatus(false)); - charlie.verify(priv.syncingStatus(false)); - } - - private PrivacyNode createNode( - final Network containerNetwork, final String nodeName, final int privacyAccount) - throws IOException { - if (bftPrivacyType.consensusType == ConsensusType.IBFT2) { - return privacyBesu.createIbft2NodePrivacyEnabled( - nodeName, - PrivacyAccountResolver.values()[privacyAccount].resolve( - bftPrivacyType.enclaveEncryptorType), - true, - bftPrivacyType.enclaveType, - Optional.of(containerNetwork), - false, - false, - bftPrivacyType.restriction == Restriction.UNRESTRICTED, - "0xAA"); - } else if (bftPrivacyType.consensusType == ConsensusType.QBFT) { - return privacyBesu.createQbftNodePrivacyEnabled( - nodeName, - PrivacyAccountResolver.values()[privacyAccount].resolve( - bftPrivacyType.enclaveEncryptorType), - bftPrivacyType.enclaveType, - Optional.of(containerNetwork), - false, - false, - bftPrivacyType.restriction == Restriction.UNRESTRICTED, - "0xAA"); - } else { - throw new IllegalStateException("Unknown consensus type " + bftPrivacyType.consensusType); - } - } - - @Test - public void onlyAliceAndBobCanExecuteContract() { - // Contract address is generated from sender address and transaction nonce - final String contractAddress = - EnclaveEncryptorType.EC.equals(bftPrivacyType.enclaveEncryptorType) - ? "0x3e5d325a03ad3ce5640502219833d30b89ce3ce1" - : "0xebf56429e6500e84442467292183d4d621359838"; - - final EventEmitter eventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(contractAddress, alice.getAddress().toString()) - .verify(eventEmitter); - - final String transactionHash = - alice.execute( - privateContractTransactions.callSmartContract( - eventEmitter.getContractAddress(), - eventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - alice.getTransactionSigningKey(), - bftPrivacyType.restriction, - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - final PrivateTransactionReceipt expectedReceipt = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash)); - - bob.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - transactionHash, expectedReceipt)); - - if (bftPrivacyType.restriction != Restriction.UNRESTRICTED) { - charlie.verify(privateTransactionVerifier.noPrivateTransactionReceipt(transactionHash)); - } - } - - @Test - public void aliceCanDeployMultipleTimesInSingleGroup() { - final String firstDeployedAddress = - EnclaveEncryptorType.EC.equals(bftPrivacyType.enclaveEncryptorType) - ? "0x3e5d325a03ad3ce5640502219833d30b89ce3ce1" - : "0xebf56429e6500e84442467292183d4d621359838"; - - privacyCluster.stopNode(charlie); - - final EventEmitter firstEventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(firstDeployedAddress, alice.getAddress().toString()) - .verify(firstEventEmitter); - - final String secondDeployedAddress = - EnclaveEncryptorType.EC.equals(bftPrivacyType.enclaveEncryptorType) - ? "0x5194e214fae257530710d18c868df7a295d9d53b" - : "0x10f807f8a905da5bd319196da7523c6bd768690f"; - - final EventEmitter secondEventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(secondDeployedAddress, alice.getAddress().toString()) - .verify(secondEventEmitter); - } - - @Test - public void canInteractWithMultiplePrivacyGroups() { - // alice deploys contract - final String firstDeployedAddress = - EnclaveEncryptorType.EC.equals(bftPrivacyType.enclaveEncryptorType) - ? "0x760359bc605b3848f5199829bde6b382d90fb8eb" - : "0xff206d21150a8da5b83629d8a722f3135ed532b1"; - - final EventEmitter firstEventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey(), - charlie.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(firstDeployedAddress, alice.getAddress().toString()) - .verify(firstEventEmitter); - - // charlie interacts with contract - final String firstTransactionHash = - charlie.execute( - privateContractTransactions.callSmartContract( - firstEventEmitter.getContractAddress(), - firstEventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - charlie.getTransactionSigningKey(), - bftPrivacyType.restriction, - charlie.getEnclaveKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - // alice gets receipt from charlie's interaction - final PrivateTransactionReceipt aliceReceipt = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(firstTransactionHash)); - - // verify bob and charlie have access to the same receipt - bob.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - firstTransactionHash, aliceReceipt)); - charlie.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - firstTransactionHash, aliceReceipt)); - - // alice deploys second contract - final String secondDeployedAddress = - EnclaveEncryptorType.EC.equals(bftPrivacyType.enclaveEncryptorType) - ? "0x3e5d325a03ad3ce5640502219833d30b89ce3ce1" - : "0xebf56429e6500e84442467292183d4d621359838"; - - final EventEmitter secondEventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(secondDeployedAddress, alice.getAddress().toString()) - .verify(secondEventEmitter); - - // bob interacts with contract - final String secondTransactionHash = - bob.execute( - privateContractTransactions.callSmartContract( - secondEventEmitter.getContractAddress(), - secondEventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - bob.getTransactionSigningKey(), - bftPrivacyType.restriction, - bob.getEnclaveKey(), - alice.getEnclaveKey())); - - // alice gets receipt from bob's interaction - final PrivateTransactionReceipt secondExpectedReceipt = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(secondTransactionHash)); - - bob.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - secondTransactionHash, secondExpectedReceipt)); - - // charlie cannot see the receipt - if (bftPrivacyType.restriction != Restriction.UNRESTRICTED) { - charlie.verify(privateTransactionVerifier.noPrivateTransactionReceipt(secondTransactionHash)); - } - } -} diff --git a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/EnclaveErrorAcceptanceTest.java b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/EnclaveErrorAcceptanceTest.java deleted file mode 100644 index 3eab2b724309..000000000000 --- a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/EnclaveErrorAcceptanceTest.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.tests.acceptance.privacy; - -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.hyperledger.enclave.testutil.EnclaveEncryptorType.EC; -import static org.hyperledger.enclave.testutil.EnclaveEncryptorType.NACL; -import static org.hyperledger.enclave.testutil.EnclaveType.TESSERA; - -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.account.PrivacyAccountResolver; -import org.hyperledger.besu.tests.web3j.generated.EventEmitter; -import org.hyperledger.enclave.testutil.EnclaveEncryptorType; -import org.hyperledger.enclave.testutil.EnclaveType; - -import java.io.IOException; -import java.math.BigInteger; -import java.security.KeyPairGenerator; -import java.security.spec.ECGenParameterSpec; -import java.util.Arrays; -import java.util.Base64; -import java.util.Collection; -import java.util.Optional; - -import org.apache.tuweni.crypto.sodium.Box; -import org.assertj.core.api.Condition; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import org.testcontainers.containers.Network; -import org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt; -import org.web3j.utils.Restriction; - -@RunWith(Parameterized.class) -public class EnclaveErrorAcceptanceTest extends PrivacyAcceptanceTestBase { - - private final PrivacyNode alice; - private final PrivacyNode bob; - private final String wrongPublicKey; - - @Parameters(name = "{0} enclave type with {1} encryptor") - public static Collection enclaveParameters() { - return Arrays.asList( - new Object[][] { - {TESSERA, NACL}, - {TESSERA, EC} - }); - } - - public EnclaveErrorAcceptanceTest( - final EnclaveType enclaveType, final EnclaveEncryptorType enclaveEncryptorType) - throws IOException { - - final Network containerNetwork = Network.newNetwork(); - - alice = - privacyBesu.createIbft2NodePrivacyEnabled( - "node1", - PrivacyAccountResolver.ALICE.resolve(enclaveEncryptorType), - false, - enclaveType, - Optional.of(containerNetwork), - false, - false, - false, - "0xAA"); - bob = - privacyBesu.createIbft2NodePrivacyEnabled( - "node2", - PrivacyAccountResolver.BOB.resolve(enclaveEncryptorType), - false, - enclaveType, - Optional.of(containerNetwork), - false, - false, - false, - "0xBB"); - privacyCluster.start(alice, bob); - - alice.verify(priv.syncingStatus(false)); - bob.verify(priv.syncingStatus(false)); - - final byte[] wrongPublicKeyBytes = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? getSECP256r1PublicKeyByteArray() - : Box.KeyPair.random().publicKey().bytesArray(); - - wrongPublicKey = Base64.getEncoder().encodeToString(wrongPublicKeyBytes); - } - - @Test - public void aliceCannotSendTransactionFromBobNode() { - final Throwable throwable = - catchThrowable( - () -> - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - wrongPublicKey, - bob.getEnclaveKey()))); - - assertThat(throwable) - .hasMessageContaining( - RpcErrorType.PRIVATE_FROM_DOES_NOT_MATCH_ENCLAVE_PUBLIC_KEY.getMessage()); - } - - @Test - public void enclaveNoPeerUrlError() { - final Throwable throwable = - catchThrowable( - () -> - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - wrongPublicKey))); - - final String tesseraMessage = RpcErrorType.TESSERA_NODE_MISSING_PEER_URL.getMessage(); - - assertThat(throwable.getMessage()).has(matchTesseraEnclaveMessage(tesseraMessage)); - } - - @Test - public void whenEnclaveIsDisconnectedGetReceiptReturnsInternalError() { - final EventEmitter eventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed( - eventEmitter.getContractAddress(), alice.getAddress().toString()) - .verify(eventEmitter); - - final String transactionHash = - alice.execute( - privateContractTransactions.callSmartContract( - eventEmitter.getContractAddress(), - eventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - alice.getTransactionSigningKey(), - Restriction.RESTRICTED, - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - final PrivateTransactionReceipt receiptBeforeEnclaveLosesConnection = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash)); - - alice.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - transactionHash, receiptBeforeEnclaveLosesConnection)); - - alice.getEnclave().stop(); - - alice.verify( - privateTransactionVerifier.internalErrorPrivateTransactionReceipt(transactionHash)); - } - - @Test - @Ignore("Web3J is broken by PR #1426") - public void transactionFailsIfPartyIsOffline() { - // Contract address is generated from sender address and transaction nonce - final String contractAddress = "0xebf56429e6500e84442467292183d4d621359838"; - - final EventEmitter eventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(contractAddress, alice.getAddress().toString()) - .verify(eventEmitter); - - bob.getEnclave().stop(); - - final Throwable throwable = - catchThrowable( - () -> - alice.execute( - privateContractTransactions.callSmartContract( - eventEmitter.getContractAddress(), - eventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - alice.getTransactionSigningKey(), - Restriction.RESTRICTED, - alice.getEnclaveKey(), - bob.getEnclaveKey()))); - - assertThat(throwable).hasMessageContaining("NodePropagatingToAllPeers"); - } - - @Test - public void createPrivacyGroupReturnsCorrectError() { - final Throwable throwable = - catchThrowable(() -> alice.execute(privacyTransactions.createPrivacyGroup(null, null))); - final String tesseraMessage = RpcErrorType.TESSERA_CREATE_GROUP_INCLUDE_SELF.getMessage(); - - assertThat(throwable.getMessage()).has(matchTesseraEnclaveMessage(tesseraMessage)); - } - - private Condition matchTesseraEnclaveMessage(final String enclaveMessage) { - return new Condition<>( - message -> message.contains(enclaveMessage), - "Message did not match Tessera expected output"); - } - - private byte[] getSECP256r1PublicKeyByteArray() { - try { - final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); - final ECGenParameterSpec spec = new ECGenParameterSpec("secp256r1"); - keyGen.initialize(spec); - return keyGen.generateKeyPair().getPublic().getEncoded(); - } catch (Exception exception) { - return new byte[0]; - } - } -} diff --git a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/FlexiblePrivacyAcceptanceTestBase.java b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/FlexiblePrivacyAcceptanceTestBase.java deleted file mode 100644 index 9e4f1638dad9..000000000000 --- a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/FlexiblePrivacyAcceptanceTestBase.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.tests.acceptance.privacy; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.hyperledger.besu.ethereum.core.PrivacyParameters.FLEXIBLE_PRIVACY_PROXY; -import static org.hyperledger.besu.ethereum.privacy.group.FlexibleGroupManagement.GET_PARTICIPANTS_METHOD_SIGNATURE; - -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.condition.ExpectValidFlexiblePrivacyGroupCreated; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.transaction.CreateFlexiblePrivacyGroupTransaction; -import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivacyRequestFactory; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -import org.apache.tuweni.bytes.Bytes; -import org.web3j.abi.FunctionEncoder; -import org.web3j.abi.Utils; -import org.web3j.abi.datatypes.DynamicArray; -import org.web3j.abi.datatypes.DynamicBytes; -import org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt; -import org.web3j.protocol.core.methods.response.TransactionReceipt; -import org.web3j.tx.Contract; -import org.web3j.utils.Base64String; - -public class FlexiblePrivacyAcceptanceTestBase extends PrivacyAcceptanceTestBase { - - protected String createFlexiblePrivacyGroup(final PrivacyNode... members) { - final List addresses = - Arrays.stream(members).map(PrivacyNode::getEnclaveKey).collect(Collectors.toList()); - return createFlexiblePrivacyGroup(members[0].getEnclaveKey(), addresses, members); - } - - /** - * Create an flexible privacy group. The privacy group id will be randomly generated. - * - *

This method also checks that each node member has successfully processed the transaction and - * has the expected list of member for the group. - * - * @param members the list of members of the privacy group. The first member of the list will be - * the creator of the group. - * @return the id of the privacy group - */ - protected String createFlexiblePrivacyGroup( - final String privateFrom, final List addresses, final PrivacyNode... members) { - - final PrivacyNode groupCreator = members[0]; - - final CreateFlexiblePrivacyGroupTransaction createTx = - privacyTransactions.createFlexiblePrivacyGroup(groupCreator, privateFrom, addresses); - - final PrivacyRequestFactory.PrivxCreatePrivacyGroupResponse createResponse = - groupCreator.execute(createTx); - final String privacyGroupId = createResponse.getPrivacyGroupId(); - - final List membersEnclaveKeys = - Arrays.stream(members) - .map(m -> Base64String.wrap(m.getEnclaveKey())) - .collect(Collectors.toList()); - - for (final PrivacyNode member : members) { - member.verify(flexiblePrivacyGroupExists(privacyGroupId, membersEnclaveKeys)); - } - - final String commitmentHash = - callGetParticipantsMethodAndReturnCommitmentHash(privacyGroupId, groupCreator, privateFrom); - final PrivateTransactionReceipt expectedReceipt = - buildExpectedAddMemberTransactionReceipt(privacyGroupId, groupCreator, addresses); - - for (final PrivacyNode member : members) { - member.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - commitmentHash, expectedReceipt)); - } - - return privacyGroupId; - } - - protected String callGetParticipantsMethodAndReturnCommitmentHash( - final String privacyGroupId, final PrivacyNode groupCreator, final String privateFrom) { - return groupCreator.execute( - privateContractTransactions.callOnchainPermissioningSmartContract( - FLEXIBLE_PRIVACY_PROXY.toHexString(), - GET_PARTICIPANTS_METHOD_SIGNATURE.toString(), - groupCreator.getTransactionSigningKey(), - privateFrom, - privacyGroupId)); - } - - protected PrivateTransactionReceipt buildExpectedAddMemberTransactionReceipt( - final String privacyGroupId, final PrivacyNode groupCreator, final List members) { - return buildExpectedAddMemberTransactionReceipt( - privacyGroupId, groupCreator, groupCreator.getEnclaveKey(), members); - } - - protected PrivateTransactionReceipt buildExpectedAddMemberTransactionReceipt( - final String privacyGroupId, - final PrivacyNode groupCreator, - final String privateFrom, - final List members) { - - final StringBuilder output = new StringBuilder(); - // hex prefix - output.append("0x"); - - final String encodedParameters = - FunctionEncoder.encode( - "", - Arrays.asList( - new DynamicArray<>( - DynamicBytes.class, - Utils.typeMap( - members.stream() - .map(Bytes::fromBase64String) - .map(Bytes::toArrayUnsafe) - .collect(Collectors.toList()), - DynamicBytes.class)))); - - output.append(encodedParameters); - - return new PrivateTransactionReceipt( - null, - groupCreator.getAddress().toHexString(), - FLEXIBLE_PRIVACY_PROXY.toHexString(), - output.toString(), - Collections.emptyList(), - null, - null, - privateFrom, - null, - privacyGroupId, - "0x1", - null); - } - - protected ExpectValidFlexiblePrivacyGroupCreated flexiblePrivacyGroupExists( - final String privacyGroupId, final List members) { - return privateTransactionVerifier.flexiblePrivacyGroupExists(privacyGroupId, members); - } - - protected String getContractDeploymentCommitmentHash(final Contract contract) { - final Optional transactionReceipt = contract.getTransactionReceipt(); - assertThat(transactionReceipt).isPresent(); - final PrivateTransactionReceipt privateTransactionReceipt = - (PrivateTransactionReceipt) transactionReceipt.get(); - return privateTransactionReceipt.getcommitmentHash(); - } - - /** - * This method will check if a privacy group with the specified id and list of members exists. - * Each one of the members node will be queried to ensure that they all have the same privacy - * group in their private state. - * - * @param privacyGroupId the id of the privacy group - * @param members the list of member in the privacy group - */ - protected void checkFlexiblePrivacyGroupExists( - final String privacyGroupId, final PrivacyNode... members) { - final List membersEnclaveKeys = - Arrays.stream(members) - .map(PrivacyNode::getEnclaveKey) - .map(Base64String::wrap) - .collect(Collectors.toList()); - - for (final PrivacyNode member : members) { - member.verify(flexiblePrivacyGroupExists(privacyGroupId, membersEnclaveKeys)); - } - } -} diff --git a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/PluginPrivacySigningAcceptanceTest.java b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/PluginPrivacySigningAcceptanceTest.java deleted file mode 100644 index 330f00554d9e..000000000000 --- a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/PluginPrivacySigningAcceptanceTest.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.tests.acceptance.privacy; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.BesuNodeConfigurationBuilder; -import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.privacy.PrivacyNodeConfiguration; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.account.PrivacyAccount; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.account.PrivacyAccountResolver; -import org.hyperledger.besu.tests.web3j.generated.EventEmitter; -import org.hyperledger.enclave.testutil.EnclaveEncryptorType; -import org.hyperledger.enclave.testutil.EnclaveKeyConfiguration; -import org.hyperledger.enclave.testutil.EnclaveType; - -import java.io.IOException; -import java.math.BigInteger; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.web3j.protocol.core.DefaultBlockParameter; -import org.web3j.protocol.core.methods.response.EthBlock.Block; -import org.web3j.protocol.core.methods.response.TransactionReceipt; - -@RunWith(Parameterized.class) -public class PluginPrivacySigningAcceptanceTest extends PrivacyAcceptanceTestBase { - private PrivacyNode minerNode; - - private final EnclaveEncryptorType enclaveEncryptorType; - - public PluginPrivacySigningAcceptanceTest(final EnclaveEncryptorType enclaveEncryptorType) { - this.enclaveEncryptorType = enclaveEncryptorType; - } - - @Parameterized.Parameters(name = "{0}") - public static Collection enclaveEncryptorTypes() { - return Arrays.stream(EnclaveEncryptorType.values()) - .filter(encryptorType -> !EnclaveEncryptorType.NOOP.equals(encryptorType)) - .collect(Collectors.toList()); - } - - @Before - public void setup() throws IOException { - final PrivacyAccount BOB = PrivacyAccountResolver.BOB.resolve(enclaveEncryptorType); - - minerNode = - privacyBesu.create( - new PrivacyNodeConfiguration( - false, - false, - true, - new BesuNodeConfigurationBuilder() - .name("miner") - .miningEnabled() - .jsonRpcEnabled() - .webSocketEnabled() - .enablePrivateTransactions() - .keyFilePath(BOB.getPrivateKeyPath()) - .plugins(Collections.singletonList("testPlugins")) - .extraCLIOptions( - List.of( - "--plugin-privacy-service-encryption-prefix=0xAA", - "--plugin-privacy-service-signing-enabled=true", - "--plugin-privacy-service-signing-key=8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63")) - .build(), - new EnclaveKeyConfiguration( - BOB.getEnclaveKeyPaths(), - BOB.getEnclavePrivateKeyPaths(), - BOB.getEnclaveEncryptorType())), - EnclaveType.NOOP, - Optional.empty()); - - privacyCluster.start(minerNode); - - minerNode.verify(priv.syncingStatus(false)); - } - - @Test - public void canDeployContractSignedByPlugin() throws Exception { - final String contractAddress = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? "0xf01ec73d91fdeb8bb9388ec74e6a3981da86e021" - : "0xd0152772c54cecfa7684f09f7616dcc825545dff"; - - final EventEmitter eventEmitter = - minerNode.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - minerNode.getTransactionSigningKey(), - minerNode.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(contractAddress, minerNode.getAddress().toString()) - .verify(eventEmitter); - privateContractVerifier.validContractCodeProvided().verify(eventEmitter); - - final BigInteger blockNumberContractDeployed = - eventEmitter.getTransactionReceipt().get().getBlockNumber(); - final Block blockContractDeployed = - minerNode.execute( - ethTransactions.block(DefaultBlockParameter.valueOf(blockNumberContractDeployed))); - - assertThat(blockContractDeployed.getTransactions().size()).isEqualTo(1); - - final String transactionHashContractDeployed = - (String) blockContractDeployed.getTransactions().get(0).get(); - final TransactionReceipt pmtReceipt = - minerNode - .execute(ethTransactions.getTransactionReceipt(transactionHashContractDeployed)) - .get(); - - assertThat(pmtReceipt.getStatus()).isEqualTo("0x1"); - assertThat(pmtReceipt.getFrom()).isEqualTo("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"); - } -} diff --git a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/PrivacyClusterAcceptanceTest.java b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/PrivacyClusterAcceptanceTest.java deleted file mode 100644 index e1bb0a980ead..000000000000 --- a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/PrivacyClusterAcceptanceTest.java +++ /dev/null @@ -1,407 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.tests.acceptance.privacy; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.hyperledger.besu.ethereum.core.PrivacyParameters.DEFAULT_PRIVACY; -import static org.hyperledger.enclave.testutil.EnclaveEncryptorType.EC; -import static org.hyperledger.enclave.testutil.EnclaveEncryptorType.NACL; -import static org.hyperledger.enclave.testutil.EnclaveType.TESSERA; -import static org.web3j.utils.Restriction.RESTRICTED; - -import org.hyperledger.besu.enclave.Enclave; -import org.hyperledger.besu.enclave.EnclaveFactory; -import org.hyperledger.besu.enclave.types.ReceiveResponse; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.account.PrivacyAccountResolver; -import org.hyperledger.besu.tests.web3j.generated.EventEmitter; -import org.hyperledger.enclave.testutil.EnclaveEncryptorType; -import org.hyperledger.enclave.testutil.EnclaveType; - -import java.io.IOException; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Optional; - -import io.vertx.core.Vertx; -import org.apache.tuweni.bytes.Bytes; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import org.testcontainers.containers.Network; -import org.web3j.crypto.Credentials; -import org.web3j.crypto.RawTransaction; -import org.web3j.crypto.TransactionEncoder; -import org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt; -import org.web3j.protocol.eea.crypto.PrivateTransactionEncoder; -import org.web3j.protocol.eea.crypto.RawPrivateTransaction; -import org.web3j.utils.Base64String; -import org.web3j.utils.Numeric; - -@RunWith(Parameterized.class) -public class PrivacyClusterAcceptanceTest extends PrivacyAcceptanceTestBase { - - private final PrivacyNode alice; - private final PrivacyNode bob; - private final PrivacyNode charlie; - private final EnclaveEncryptorType enclaveEncryptorType; - private final Vertx vertx = Vertx.vertx(); - private final EnclaveFactory enclaveFactory = new EnclaveFactory(vertx); - - @Parameters(name = "{0} enclave type with {1} encryptor") - public static Collection enclaveParameters() { - return Arrays.asList( - new Object[][] { - {TESSERA, NACL}, - {TESSERA, EC} - }); - } - - public PrivacyClusterAcceptanceTest( - final EnclaveType enclaveType, final EnclaveEncryptorType enclaveEncryptorType) - throws IOException { - this.enclaveEncryptorType = enclaveEncryptorType; - final Network containerNetwork = Network.newNetwork(); - alice = - privacyBesu.createPrivateTransactionEnabledMinerNode( - "node1", - PrivacyAccountResolver.ALICE.resolve(enclaveEncryptorType), - enclaveType, - Optional.of(containerNetwork), - false, - false, - false); - bob = - privacyBesu.createPrivateTransactionEnabledNode( - "node2", - PrivacyAccountResolver.BOB.resolve(enclaveEncryptorType), - enclaveType, - Optional.of(containerNetwork), - false, - false, - false); - charlie = - privacyBesu.createPrivateTransactionEnabledNode( - "node3", - PrivacyAccountResolver.CHARLIE.resolve(enclaveEncryptorType), - enclaveType, - Optional.of(containerNetwork), - false, - false, - false); - privacyCluster.start(alice, bob, charlie); - - alice.verify(priv.syncingStatus(false)); - bob.verify(priv.syncingStatus(false)); - charlie.verify(priv.syncingStatus(false)); - } - - @After - public void cleanUp() { - vertx.close(); - } - - @Test - public void onlyAliceAndBobCanExecuteContract() { - // Contract address is generated from sender address and transaction nonce - final String contractAddress = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? "0x3e5d325a03ad3ce5640502219833d30b89ce3ce1" - : "0xebf56429e6500e84442467292183d4d621359838"; - - final EventEmitter eventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(contractAddress, alice.getAddress().toString()) - .verify(eventEmitter); - - final String transactionHash = - alice.execute( - privateContractTransactions.callSmartContract( - eventEmitter.getContractAddress(), - eventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - alice.getTransactionSigningKey(), - RESTRICTED, - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - final PrivateTransactionReceipt expectedReceipt = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash)); - - bob.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - transactionHash, expectedReceipt)); - - charlie.verify(privateTransactionVerifier.noPrivateTransactionReceipt(transactionHash)); - - // When Alice executes a contract call in the wrong privacy group the transaction should pass - // but it should NOT return any output - final String transactionHash2 = - alice.execute( - privateContractTransactions.callSmartContract( - eventEmitter.getContractAddress(), - eventEmitter.value().encodeFunctionCall(), - alice.getTransactionSigningKey(), - RESTRICTED, - alice.getEnclaveKey(), - charlie.getEnclaveKey())); - - final PrivateTransactionReceipt expectedReceipt2 = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash2)); - - assertThat(expectedReceipt2.getOutput()).isEqualTo("0x"); - - charlie.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - transactionHash2, expectedReceipt2)); - } - - @Test - public void aliceCanUsePrivDistributeTransaction() { - // Contract address is generated from sender address and transaction nonce - final String contractAddress = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? "0x3e5d325a03ad3ce5640502219833d30b89ce3ce1" - : "0xebf56429e6500e84442467292183d4d621359838"; - final String receiptPrivacyGroupId = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? "MjuFB4b9Hz+f8zvkWWasxZWRjHWXU4t7B2nOHo4mekA=" - : "DyAOiF/ynpc+JXa2YAGB0bCitSlOMNm+ShmB/7M6C4w="; - - final RawPrivateTransaction rawPrivateTransaction = - RawPrivateTransaction.createContractTransaction( - BigInteger.ZERO, - BigInteger.ZERO, - BigInteger.ZERO, - Numeric.prependHexPrefix(EventEmitter.BINARY), - Base64String.wrap(alice.getEnclaveKey()), - Collections.singletonList(Base64String.wrap(bob.getEnclaveKey())), - Base64String.wrap(receiptPrivacyGroupId), - RESTRICTED); - - final String signedPrivateTransaction = - Numeric.toHexString( - PrivateTransactionEncoder.signMessage( - rawPrivateTransaction, Credentials.create(alice.getTransactionSigningKey()))); - final String transactionKey = - alice.execute(privacyTransactions.privDistributeTransaction(signedPrivateTransaction)); - - final Enclave aliceEnclave = enclaveFactory.createVertxEnclave(alice.getEnclave().clientUrl()); - final ReceiveResponse aliceRR = - aliceEnclave.receive( - Bytes.fromHexString(transactionKey).toBase64String(), alice.getEnclaveKey()); - - final Enclave bobEnclave = enclaveFactory.createVertxEnclave(bob.getEnclave().clientUrl()); - final ReceiveResponse bobRR = - bobEnclave.receive( - Bytes.fromHexString(transactionKey).toBase64String(), bob.getEnclaveKey()); - - assertThat(bobRR).usingRecursiveComparison().isEqualTo(aliceRR); - - final RawTransaction pmt = - RawTransaction.createTransaction( - BigInteger.ZERO, - BigInteger.valueOf(1000), - BigInteger.valueOf(65000), - DEFAULT_PRIVACY.toString(), - transactionKey); - - final String signedPmt = - Numeric.toHexString( - TransactionEncoder.signMessage( - pmt, Credentials.create(alice.getTransactionSigningKey()))); - - final String transactionHash = alice.execute(ethTransactions.sendRawTransaction(signedPmt)); - - final String receiptPrivateFrom = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAES8nC4qT/KdoAoTSF3qs/47DUsDihyVbWiRjZAiyvqp9eSDkqV1RzlM+58oOwnpFRwvWNZM+AxMVxT+MvxdsqMA==" - : "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="; - final ArrayList receiptPrivateFor = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? new ArrayList<>( - Collections.singletonList( - "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXIgZqRA25V+3nN+Do6b5r0jiUunub6ubjPhqwHpPxP44uUYh9RKCQNRnsqCJ9PjeTnC8R3ieJk7HWAlycU1bug==")) - : new ArrayList<>( - Collections.singletonList("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=")); - - final PrivateTransactionReceipt expectedReceipt = - new PrivateTransactionReceipt( - contractAddress, - "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73", - null, - null, // ignored in the following call, checked separately below - Collections.emptyList(), - "0x023955c49d6265c579561940287449242704d5fd239ff07ea36a3fc7aface61c", - "0x82e521ee16ff13104c5f81e8354ecaaafd5450b710b07f620204032bfe76041a", - receiptPrivateFrom, - receiptPrivateFor, - receiptPrivacyGroupId, - "0x1", - null); - - alice.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - transactionHash, expectedReceipt, true)); - - final PrivateTransactionReceipt alicePrivateTransactionReceipt = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash)); - assertThat(EventEmitter.BINARY) - .contains(alicePrivateTransactionReceipt.getOutput().substring(2)); - - bob.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - transactionHash, expectedReceipt, true)); - - final PrivateTransactionReceipt bobPrivateTransactionReceipt = - bob.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash)); - assertThat(EventEmitter.BINARY).contains(bobPrivateTransactionReceipt.getOutput().substring(2)); - } - - @Test - public void aliceCanDeployMultipleTimesInSingleGroup() { - final String firstDeployedAddress = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? "0x3e5d325a03ad3ce5640502219833d30b89ce3ce1" - : "0xebf56429e6500e84442467292183d4d621359838"; - - final EventEmitter firstEventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(firstDeployedAddress, alice.getAddress().toString()) - .verify(firstEventEmitter); - - final String secondDeployedAddress = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? "0x5194e214fae257530710d18c868df7a295d9d53b" - : "0x10f807f8a905da5bd319196da7523c6bd768690f"; - - final EventEmitter secondEventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(secondDeployedAddress, alice.getAddress().toString()) - .verify(secondEventEmitter); - } - - @Test - public void canInteractWithMultiplePrivacyGroups() { - // alice deploys contract - final String firstDeployedAddress = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? "0x760359bc605b3848f5199829bde6b382d90fb8eb" - : "0xff206d21150a8da5b83629d8a722f3135ed532b1"; - - final EventEmitter firstEventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey(), - charlie.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(firstDeployedAddress, alice.getAddress().toString()) - .verify(firstEventEmitter); - - // charlie interacts with contract - final String firstTransactionHash = - charlie.execute( - privateContractTransactions.callSmartContract( - firstEventEmitter.getContractAddress(), - firstEventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - charlie.getTransactionSigningKey(), - RESTRICTED, - charlie.getEnclaveKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - // alice gets receipt from charlie's interaction - final PrivateTransactionReceipt firstExpectedReceipt = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(firstTransactionHash)); - - // verify bob and charlie have access to the same receipt - bob.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - firstTransactionHash, firstExpectedReceipt)); - charlie.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - firstTransactionHash, firstExpectedReceipt)); - - // alice deploys second contract - final String secondDeployedAddress = - EnclaveEncryptorType.EC.equals(enclaveEncryptorType) - ? "0x3e5d325a03ad3ce5640502219833d30b89ce3ce1" - : "0xebf56429e6500e84442467292183d4d621359838"; - - final EventEmitter secondEventEmitter = - alice.execute( - privateContractTransactions.createSmartContract( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - bob.getEnclaveKey())); - - privateContractVerifier - .validPrivateContractDeployed(secondDeployedAddress, alice.getAddress().toString()) - .verify(secondEventEmitter); - - // bob interacts with contract - final String secondTransactionHash = - bob.execute( - privateContractTransactions.callSmartContract( - secondEventEmitter.getContractAddress(), - secondEventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - bob.getTransactionSigningKey(), - RESTRICTED, - bob.getEnclaveKey(), - alice.getEnclaveKey())); - - // alice gets receipt from bob's interaction - final PrivateTransactionReceipt secondExpectedReceipt = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(secondTransactionHash)); - - bob.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - secondTransactionHash, secondExpectedReceipt)); - - // charlie cannot see the receipt - charlie.verify(privateTransactionVerifier.noPrivateTransactionReceipt(secondTransactionHash)); - } -} diff --git a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/PrivacyGroupAcceptanceTest.java b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/PrivacyGroupAcceptanceTest.java deleted file mode 100644 index ea94bb293af5..000000000000 --- a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/privacy/PrivacyGroupAcceptanceTest.java +++ /dev/null @@ -1,255 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.tests.acceptance.privacy; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.hyperledger.enclave.testutil.EnclaveEncryptorType.EC; -import static org.hyperledger.enclave.testutil.EnclaveEncryptorType.NACL; -import static org.hyperledger.enclave.testutil.EnclaveType.TESSERA; -import static org.web3j.utils.Restriction.RESTRICTED; - -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode; -import org.hyperledger.besu.tests.acceptance.dsl.privacy.account.PrivacyAccountResolver; -import org.hyperledger.besu.tests.web3j.generated.EventEmitter; -import org.hyperledger.besu.util.LogConfigurator; -import org.hyperledger.enclave.testutil.EnclaveEncryptorType; -import org.hyperledger.enclave.testutil.EnclaveType; - -import java.io.IOException; -import java.math.BigInteger; -import java.util.Arrays; -import java.util.Collection; -import java.util.Optional; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import org.testcontainers.containers.Network; -import org.web3j.protocol.besu.response.privacy.PrivacyGroup; -import org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt; -import org.web3j.utils.Base64String; - -@RunWith(Parameterized.class) -public class PrivacyGroupAcceptanceTest extends PrivacyAcceptanceTestBase { - - private final PrivacyNode alice; - private final PrivacyNode bob; - private final PrivacyNode charlie; - - @Parameters(name = "{0} enclave type with {1} encryptor") - public static Collection enclaveParameters() { - return Arrays.asList( - new Object[][] { - {TESSERA, NACL}, - {TESSERA, EC} - }); - } - - public PrivacyGroupAcceptanceTest( - final EnclaveType enclaveType, final EnclaveEncryptorType enclaveEncryptorType) - throws IOException { - - final Network containerNetwork = Network.newNetwork(); - - alice = - privacyBesu.createPrivateTransactionEnabledMinerNode( - "node1", - PrivacyAccountResolver.ALICE.resolve(enclaveEncryptorType), - enclaveType, - Optional.of(containerNetwork), - false, - false, - false); - bob = - privacyBesu.createPrivateTransactionEnabledNode( - "node2", - PrivacyAccountResolver.BOB.resolve(enclaveEncryptorType), - enclaveType, - Optional.of(containerNetwork), - false, - false, - false); - - charlie = - privacyBesu.createPrivateTransactionEnabledNode( - "node3", - PrivacyAccountResolver.CHARLIE.resolve(enclaveEncryptorType), - enclaveType, - Optional.of(containerNetwork), - false, - false, - false); - privacyCluster.start(alice, bob, charlie); - - alice.verify(priv.syncingStatus(false)); - bob.verify(priv.syncingStatus(false)); - charlie.verify(priv.syncingStatus(false)); - } - - @Test - public void nodeCanCreatePrivacyGroup() { - LogConfigurator.setLevel("", "DEBUG"); - final String privacyGroupId = - alice.execute( - privacyTransactions.createPrivacyGroup( - "myGroupName", "my group description", alice, bob)); - - assertThat(privacyGroupId).isNotNull(); - - final PrivacyGroup expected = - new PrivacyGroup( - privacyGroupId, - PrivacyGroup.Type.PANTHEON, - "myGroupName", - "my group description", - Base64String.wrapList(alice.getEnclaveKey(), bob.getEnclaveKey())); - - alice.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected)); - - bob.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected)); - } - - @Test - public void nodeCanCreatePrivacyGroupWithoutName() { - final String privacyGroupId = - alice.execute( - privacyTransactions.createPrivacyGroup(null, "my group description", alice, bob)); - - assertThat(privacyGroupId).isNotNull(); - - final PrivacyGroup expected = - new PrivacyGroup( - privacyGroupId, - PrivacyGroup.Type.PANTHEON, - "", - "my group description", - Base64String.wrapList(alice.getEnclaveKey(), bob.getEnclaveKey())); - - alice.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected)); - - bob.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected)); - } - - @Test - public void nodeCanCreatePrivacyGroupWithoutDescription() { - final String privacyGroupId = - alice.execute(privacyTransactions.createPrivacyGroup("myGroupName", null, alice, bob)); - - assertThat(privacyGroupId).isNotNull(); - - final PrivacyGroup expected = - new PrivacyGroup( - privacyGroupId, - PrivacyGroup.Type.PANTHEON, - "myGroupName", - "", - Base64String.wrapList(alice.getEnclaveKey(), bob.getEnclaveKey())); - - alice.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected)); - - bob.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected)); - } - - @Test - public void nodeCanCreatePrivacyGroupWithoutOptionalParams() { - final String privacyGroupId = - alice.execute(privacyTransactions.createPrivacyGroup(null, null, alice)); - - assertThat(privacyGroupId).isNotNull(); - - final PrivacyGroup expected = - new PrivacyGroup( - privacyGroupId, - PrivacyGroup.Type.PANTHEON, - "", - "", - Base64String.wrapList(alice.getEnclaveKey())); - - alice.verify(privateTransactionVerifier.validPrivacyGroupCreated(expected)); - } - - @Test - public void canInteractWithMultiplePrivacyGroups() { - final String privacyGroupIdABC = - alice.execute(privacyTransactions.createPrivacyGroup(null, null, alice, bob, charlie)); - - final EventEmitter firstEventEmitter = - alice.execute( - privateContractTransactions.createSmartContractWithPrivacyGroupId( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - privacyGroupIdABC)); - - // charlie interacts with contract - final String firstTransactionHash = - charlie.execute( - privateContractTransactions.callSmartContractWithPrivacyGroupId( - firstEventEmitter.getContractAddress(), - firstEventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - charlie.getTransactionSigningKey(), - RESTRICTED, - charlie.getEnclaveKey(), - privacyGroupIdABC)); - - // alice gets receipt from charlie's interaction - final PrivateTransactionReceipt firstExpectedReceipt = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(firstTransactionHash)); - - // verify bob and charlie have access to the same receipt - bob.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - firstTransactionHash, firstExpectedReceipt)); - charlie.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - firstTransactionHash, firstExpectedReceipt)); - - // alice deploys second contract - final String privacyGroupIdAB = - alice.execute(privacyTransactions.createPrivacyGroup(null, null, alice, bob)); - - final EventEmitter secondEventEmitter = - alice.execute( - privateContractTransactions.createSmartContractWithPrivacyGroupId( - EventEmitter.class, - alice.getTransactionSigningKey(), - alice.getEnclaveKey(), - privacyGroupIdAB)); - - // bob interacts with contract - final String secondTransactionHash = - bob.execute( - privateContractTransactions.callSmartContractWithPrivacyGroupId( - secondEventEmitter.getContractAddress(), - secondEventEmitter.store(BigInteger.ONE).encodeFunctionCall(), - bob.getTransactionSigningKey(), - RESTRICTED, - bob.getEnclaveKey(), - privacyGroupIdAB)); - - // alice gets receipt from bob's interaction - final PrivateTransactionReceipt secondExpectedReceipt = - alice.execute(privacyTransactions.getPrivateTransactionReceipt(secondTransactionHash)); - - bob.verify( - privateTransactionVerifier.validPrivateTransactionReceipt( - secondTransactionHash, secondExpectedReceipt)); - - // charlie cannot see the receipt - charlie.verify(privateTransactionVerifier.noPrivateTransactionReceipt(secondTransactionHash)); - } -}