Skip to content

Commit

Permalink
on-chain-privacy-groups (hyperledger#423)
Browse files Browse the repository at this point in the history
Introduce on-chain privacy groups with add and remove functionality.

Signed-off-by: Ivaylo Kirilov <iikirilov@gmail.com>
  • Loading branch information
iikirilov authored Mar 5, 2020
1 parent 2a3d64e commit 9701a92
Show file tree
Hide file tree
Showing 88 changed files with 5,108 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.hyperledger.besu.tests.acceptance.dsl.condition.miner.MiningStatusCondition;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.eth.EthTransactions;

import java.math.BigInteger;
import java.util.List;

public class EthConditions {

private final EthTransactions transactions;
Expand Down Expand Up @@ -68,4 +71,10 @@ public Condition expectSuccessfulTransactionReceiptWithoutReason(final String tr
public Condition miningStatus(final boolean isMining) {
return new MiningStatusCondition(transactions.mining(), isMining);
}

public Condition expectNewPendingTransactions(
final BigInteger filterId, final List<String> transactionHashes) {
return new NewPendingTransactionFilterChangesCondition(
transactions.filterChanges(filterId), transactionHashes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.condition.eth;

import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.tests.acceptance.dsl.WaitUtils;
import org.hyperledger.besu.tests.acceptance.dsl.condition.Condition;
import org.hyperledger.besu.tests.acceptance.dsl.node.Node;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.eth.EthFilterChangesTransaction;

import java.util.List;

import org.web3j.protocol.core.methods.response.EthLog;

public class NewPendingTransactionFilterChangesCondition implements Condition {

private final EthFilterChangesTransaction filterChanges;
private final List<String> transactionHashes;

public NewPendingTransactionFilterChangesCondition(
final EthFilterChangesTransaction filterChanges, final List<String> transactionHashes) {

this.filterChanges = filterChanges;
this.transactionHashes = transactionHashes;
}

@Override
public void verify(final Node node) {
WaitUtils.waitFor(
() -> {
final EthLog response = node.execute(filterChanges);
assertThat(response).isNotNull();
assertThat(response.getResult().size()).isEqualTo(transactionHashes.size());
for (int i = 0; i < transactionHashes.size(); ++i) {
assertThat(response.getLogs().get(0).get()).isEqualTo(transactionHashes.get(0));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@

public class PrivacyNodeConfiguration {

private final int privacyAddress;
private final BesuNodeConfiguration besuConfig;
private final OrionKeyConfiguration orionConfig;

PrivacyNodeConfiguration(
final BesuNodeConfiguration besuConfig, final OrionKeyConfiguration orionConfig) {
final int privacyAddress,
final BesuNodeConfiguration besuConfig,
final OrionKeyConfiguration orionConfig) {
this.privacyAddress = privacyAddress;
this.besuConfig = besuConfig;
this.orionConfig = orionConfig;
}

public int getPrivacyAddress() {
return privacyAddress;
}

public BesuNodeConfiguration getBesuConfig() {
return besuConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.tests.acceptance.dsl.node.configuration.privacy;

import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.BesuNodeConfigurationBuilder;
import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.NodeConfigurationFactory;
import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.genesis.GenesisConfigurationFactory;
Expand Down Expand Up @@ -41,8 +42,15 @@ private PrivacyNode create(final PrivacyNodeConfiguration privacyNodeConfig) thr

public PrivacyNode createPrivateTransactionEnabledMinerNode(
final String name, final PrivacyAccount privacyAccount) throws IOException {
return createPrivateTransactionEnabledMinerNode(name, privacyAccount, Address.PRIVACY);
}

public PrivacyNode createPrivateTransactionEnabledMinerNode(
final String name, final PrivacyAccount privacyAccount, final int privacyAddress)
throws IOException {
return create(
new PrivacyNodeConfiguration(
privacyAddress,
new BesuNodeConfigurationBuilder()
.name(name)
.miningEnabled()
Expand All @@ -57,8 +65,15 @@ public PrivacyNode createPrivateTransactionEnabledMinerNode(

public PrivacyNode createPrivateTransactionEnabledNode(
final String name, final PrivacyAccount privacyAccount) throws IOException {
return createPrivateTransactionEnabledNode(name, privacyAccount, Address.PRIVACY);
}

public PrivacyNode createPrivateTransactionEnabledNode(
final String name, final PrivacyAccount privacyAccount, final int privacyAddress)
throws IOException {
return create(
new PrivacyNodeConfiguration(
privacyAddress,
new BesuNodeConfigurationBuilder()
.name(name)
.jsonRpcEnabled()
Expand All @@ -72,8 +87,15 @@ public PrivacyNode createPrivateTransactionEnabledNode(

public PrivacyNode createIbft2NodePrivacyEnabled(
final String name, final PrivacyAccount privacyAccount) throws IOException {
return createIbft2NodePrivacyEnabled(name, privacyAccount, Address.PRIVACY);
}

public PrivacyNode createIbft2NodePrivacyEnabled(
final String name, final PrivacyAccount privacyAccount, final int privacyAddress)
throws IOException {
return create(
new PrivacyNodeConfiguration(
privacyAddress,
new BesuNodeConfigurationBuilder()
.name(name)
.miningEnabled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class PrivacyNode implements AutoCloseable {
private final OrionTestHarness orion;
private final BesuNode besu;
private final Vertx vertx;
private final Integer privacyAddress;

public PrivacyNode(final PrivacyNodeConfiguration privacyConfiguration, final Vertx vertx)
throws IOException {
Expand All @@ -78,6 +79,8 @@ public PrivacyNode(final PrivacyNodeConfiguration privacyConfiguration, final Ve

final BesuNodeConfiguration besuConfig = privacyConfiguration.getBesuConfig();

privacyAddress = privacyConfiguration.getPrivacyAddress();

this.besu =
new BesuNode(
besuConfig.getName(),
Expand Down Expand Up @@ -167,6 +170,7 @@ public void start(final BesuNodeRunner runner) {
.setStorageProvider(createKeyValueStorageProvider(dataDir, dbDir))
.setPrivateKeyPath(KeyPairUtil.getDefaultKeyFile(besu.homeDirectory()).toPath())
.setEnclaveFactory(new EnclaveFactory(vertx))
.setPrivacyAddress(privacyAddress)
.build();
} catch (IOException e) {
throw new RuntimeException();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.condition;

import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode;
import org.hyperledger.besu.tests.acceptance.dsl.privacy.transaction.PrivacyTransactions;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivacyRequestFactory;

import java.util.List;

import org.awaitility.Awaitility;
import org.web3j.utils.Base64String;

public class ExpectValidOnChainPrivacyGroupCreated implements PrivateCondition {

private final PrivacyTransactions transactions;
private final PrivacyRequestFactory.OnChainPrivacyGroup expected;

public ExpectValidOnChainPrivacyGroupCreated(
final PrivacyTransactions transactions,
final PrivacyRequestFactory.OnChainPrivacyGroup expected) {
this.transactions = transactions;
this.expected = expected;
}

@Override
public void verify(final PrivacyNode node) {
Awaitility.await()
.untilAsserted(
() -> {
final List<PrivacyRequestFactory.OnChainPrivacyGroup> groups =
node.execute(
transactions.findOnChainPrivacyGroup(
Base64String.unwrapList(expected.getMembers())));
assertThat(groups).contains(expected);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public void verify(final Contract contract) {
assertThat(receipt.isPresent()).isTrue();
final TransactionReceipt transactionReceipt = receipt.get();

assertThat(transactionReceipt.isStatusOK()).isTrue();

// Contract transaction has no 'to' address or contract address
assertThat(transactionReceipt.getTo()).isNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,34 @@
public class ExpectValidPrivateTransactionReceipt implements PrivateCondition {
private final PrivacyTransactions transactions;
private final String transactionHash;
private final PrivateTransactionReceipt receipt;
private final PrivateTransactionReceipt expectedReceipt;

public ExpectValidPrivateTransactionReceipt(
final PrivacyTransactions transactions,
final String transactionHash,
final PrivateTransactionReceipt receipt) {
final PrivateTransactionReceipt expectedReceipt) {

this.transactions = transactions;
this.transactionHash = transactionHash;
this.receipt = receipt;
this.expectedReceipt = expectedReceipt;
}

@Override
public void verify(final PrivacyNode node) {
assertThat(node.execute(transactions.getPrivateTransactionReceipt(transactionHash)))
.isEqualToIgnoringGivenFields(receipt, "commitmentHash");
final PrivateTransactionReceipt actualReceipt =
node.execute(transactions.getPrivateTransactionReceipt(transactionHash));
assertThat(actualReceipt)
.usingRecursiveComparison()
.ignoringFields("commitmentHash", "logs")
.isEqualTo(expectedReceipt);

assertThat(actualReceipt.getLogs().size()).isEqualTo(expectedReceipt.getLogs().size());

for (int i = 0; i < expectedReceipt.getLogs().size(); i++) {
assertThat(actualReceipt.getLogs().get(i))
.usingRecursiveComparison()
.ignoringFields("blockHash", "blockNumber")
.isEqualTo(expectedReceipt.getLogs().get(i));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PrivGetTransactionReceiptTransaction(final String transactionHash) {
public PrivateTransactionReceipt execute(final NodeRequests node) {
final Besu besu = node.privacy().getBesuClient();
final PollingPrivateTransactionReceiptProcessor receiptProcessor =
new PollingPrivateTransactionReceiptProcessor(besu, 15000, 3);
new PollingPrivateTransactionReceiptProcessor(besu, 1000, 15);
try {
final PrivateTransactionReceipt result =
receiptProcessor.waitForTransactionReceipt(transactionHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.tests.acceptance.dsl.privacy.condition;

import org.hyperledger.besu.tests.acceptance.dsl.privacy.transaction.PrivacyTransactions;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivacyRequestFactory;

import org.web3j.protocol.besu.response.privacy.PrivacyGroup;
import org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt;
Expand All @@ -41,6 +42,11 @@ public ExpectValidPrivacyGroupCreated validPrivacyGroupCreated(final PrivacyGrou
return new ExpectValidPrivacyGroupCreated(transactions, expected);
}

public ExpectValidOnChainPrivacyGroupCreated validOnChainPrivacyGroupExists(
final PrivacyRequestFactory.OnChainPrivacyGroup expected) {
return new ExpectValidOnChainPrivacyGroupCreated(transactions, expected);
}

public ExpectInternalErrorPrivateTransactionReceipt internalErrorPrivateTransactionReceipt(
final String transactionHash) {
return new ExpectInternalErrorPrivateTransactionReceipt(transactions, transactionHash);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.contract;

import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;

import java.io.IOException;
import java.math.BigInteger;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.besu.Besu;
import org.web3j.tx.BesuPrivateTransactionManager;
import org.web3j.tx.PrivateTransactionManager;
import org.web3j.tx.gas.BesuPrivacyGasProvider;
import org.web3j.utils.Base64String;

public class CallOnChianPermissioningPrivateSmartContractFunction implements Transaction<String> {

private static final BesuPrivacyGasProvider GAS_PROVIDER =
new BesuPrivacyGasProvider(BigInteger.valueOf(1000));
private final String contractAddress;
private final String encodedFunction;
private final Credentials senderCredentials;
private final long chainId;
private final Base64String privateFrom;
private final Base64String privacyGroupId;

public CallOnChianPermissioningPrivateSmartContractFunction(
final String contractAddress,
final String encodedFunction,
final String transactionSigningKey,
final long chainId,
final String privateFrom,
final String privacyGroupId) {

this.contractAddress = contractAddress;
this.encodedFunction = encodedFunction;
this.senderCredentials = Credentials.create(transactionSigningKey);
this.chainId = chainId;
this.privateFrom = Base64String.wrap(privateFrom);
this.privacyGroupId = Base64String.wrap(privacyGroupId);
}

@Override
public String execute(final NodeRequests node) {
final Besu besu = node.privacy().getBesuClient();

final PrivateTransactionManager privateTransactionManager =
new BesuPrivateTransactionManager(
besu, GAS_PROVIDER, senderCredentials, chainId, privateFrom, privacyGroupId);

try {
return privateTransactionManager
.sendTransaction(
GAS_PROVIDER.getGasPrice(),
GAS_PROVIDER.getGasLimit(),
contractAddress,
encodedFunction,
null)
.getTransactionHash();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit 9701a92

Please sign in to comment.