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

Commit

Permalink
[PRIV-55] Allow private contract invocations in multiple privacy grou…
Browse files Browse the repository at this point in the history
…ps (#1318)

* Allow private contract invocations in multiple privacy groups
  • Loading branch information
iikirilov authored and rojotek committed Apr 26, 2019
1 parent 9a5c073 commit 7b82ff3
Show file tree
Hide file tree
Showing 23 changed files with 720 additions and 328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package tech.pegasys.pantheon.tests.acceptance.dsl.privacy;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc.Eea;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode;
Expand All @@ -34,5 +35,6 @@ public void verify(final PantheonNode node, final String transactionHash) {
getPrivateTransactionReceipt(node, transactionHash);

assertEquals(contractAddress, privateTxReceipt.getContractAddress());
assertNotEquals("0x", privateTxReceipt.getOutput());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 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.
*/
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 {
@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;

public PrivateAcceptanceTestBase() {
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 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.
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.privacy;

import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eea.EeaGetTransactionCountTransaction;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eea.EeaSendRawTransactionTransaction;

public class PrivateTransactions {

public PrivateTransactions() {}

public EeaSendRawTransactionTransaction deployPrivateSmartContract(
final String signedRawPrivateTransaction) {
return new EeaSendRawTransactionTransaction(signedRawPrivateTransaction);
}

public EeaSendRawTransactionTransaction createPrivateRawTransaction(
final String signedRawPrivateTransaction) {
return new EeaSendRawTransactionTransaction(signedRawPrivateTransaction);
}

public EeaGetTransactionCountTransaction getTransactionCount(
final String address, final String privacyGroupId) {
return new EeaGetTransactionCountTransaction(address, privacyGroupId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.assertj.core.util.Lists;
import org.web3j.protocol.Web3jService;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;

public class EeaJsonRpcRequestFactory {

Expand All @@ -45,4 +46,13 @@ public Request<?, PrivateTransactionReceiptResponse> eeaGetTransactionReceipt(
web3jService,
PrivateTransactionReceiptResponse.class);
}

public Request<?, EthGetTransactionCount> eeaGetTransactionCount(
final String accountAddress, final String privacyGroupId) {
return new Request<>(
"eea_getTransactionCount",
Lists.newArrayList(accountAddress, privacyGroupId),
web3jService,
EthGetTransactionCount.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.account.TransferTransactionBuilder;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.account.TransferTransactionSet;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eea.EeaGetTransactionReceiptTransaction;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eea.EeaSendRawTransactionTransaction;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthGetTransactionCountTransaction;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthGetTransactionReceiptTransaction;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm.PermAddAccountsToWhitelistTransaction;
Expand Down Expand Up @@ -91,16 +90,6 @@ public TransferTransaction createTransfer(
.build();
}

public EeaSendRawTransactionTransaction deployPrivateSmartContract(
final String signedRawPrivateTransaction) {
return new EeaSendRawTransactionTransaction(signedRawPrivateTransaction);
}

public EeaSendRawTransactionTransaction createPrivateRawTransaction(
final String signedRawPrivateTransaction) {
return new EeaSendRawTransactionTransaction(signedRawPrivateTransaction);
}

public TransferTransactionSet createIncrementalTransfers(
final Account sender, final Account recipient, final int etherAmount) {
final List<TransferTransaction> transfers = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2019 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.
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eea;

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

import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.JsonRequestFactories;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction;

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

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

public class EeaGetTransactionCountTransaction implements Transaction<BigInteger> {

private final String accountAddress;
private String privacyGroupId;

public EeaGetTransactionCountTransaction(
final String accountAddress, final String privacyGroupId) {
this.accountAddress = accountAddress;
this.privacyGroupId = privacyGroupId;
}

@Override
public BigInteger execute(final JsonRequestFactories node) {
try {
EthGetTransactionCount result =
node.eea().eeaGetTransactionCount(accountAddress, privacyGroupId).send();
assertThat(result).isNotNull();
return result.getTransactionCount();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public class EeaTransactions {
public EeaGetTransactionReceiptTransaction getTransactionReceipt(final String transactionHash) {
return new EeaGetTransactionReceiptTransaction(transactionHash);
}

public EeaGetTransactionCountTransaction getTransactionCount(
final String address, final String privacyGroupId) {
return new EeaGetTransactionCountTransaction(address, privacyGroupId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransaction;
import tech.pegasys.pantheon.ethereum.rlp.RLP;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.List;

public class PrivateTransactionFactory {
public class PrivateTransactionBuilder {

private static BytesValue EVENT_EMITTER_CONSTRUCTOR =
BytesValue.fromHexString(
Expand Down Expand Up @@ -51,56 +52,85 @@ public class PrivateTransactionFactory {

private static BytesValue GET_FUNCTION_CALL = BytesValue.fromHexString("0x3fa4f245");

public PrivateTransaction createContractTransaction(
final long nonce,
final Address from,
final BytesValue privateFrom,
final List<BytesValue> privateFor,
final SECP256K1.KeyPair keypair) {
return privateTransaction(
nonce, null, EVENT_EMITTER_CONSTRUCTOR, from, privateFrom, privateFor, keypair);
public enum TransactionType {
CREATE_CONTRACT,
STORE,
GET
}

public PrivateTransaction storeFunctionTransaction(
final long nonce,
final Address to,
final Address from,
final BytesValue privateFrom,
final List<BytesValue> privateFor,
final SECP256K1.KeyPair keypair) {
return privateTransaction(nonce, to, SET_FUNCTION_CALL, from, privateFrom, privateFor, keypair);
public static PrivateTransactionBuilder.Builder builder() {
return new PrivateTransactionBuilder.Builder();
}

public PrivateTransaction getFunctionTransaction(
final long nonce,
final Address to,
final Address from,
final BytesValue privateFrom,
final List<BytesValue> privateFor,
final SECP256K1.KeyPair keypair) {
return privateTransaction(nonce, to, GET_FUNCTION_CALL, from, privateFrom, privateFor, keypair);
}
public static class Builder {
long nonce;
Address from;
Address to;
BytesValue privateFrom;
List<BytesValue> privateFor;
SECP256K1.KeyPair keyPair;

public Builder nonce(final long nonce) {
this.nonce = nonce;
return this;
}

public Builder from(final Address from) {
this.from = from;
return this;
}

public Builder to(final Address to) {
this.to = to;
return this;
}

public Builder privateFrom(final BytesValue privateFrom) {
this.privateFrom = privateFrom;
return this;
}

public Builder privateFor(final List<BytesValue> privateFor) {
this.privateFor = privateFor;
return this;
}

public Builder keyPair(final SECP256K1.KeyPair keyPair) {
this.keyPair = keyPair;
return this;
}

public PrivateTransaction privateTransaction(
final long nonce,
final Address to,
final BytesValue payload,
final Address from,
final BytesValue privateFrom,
final List<BytesValue> privateFor,
final SECP256K1.KeyPair keypair) {
return PrivateTransaction.builder()
.nonce(nonce)
.gasPrice(Wei.of(1000))
.gasLimit(3000000)
.to(to)
.value(Wei.ZERO)
.payload(payload)
.sender(from)
.chainId(2018)
.privateFrom(privateFrom)
.privateFor(privateFor)
.restriction(BytesValue.wrap("restricted".getBytes(UTF_8)))
.signAndBuild(keypair);
public String build(final TransactionType type) {
BytesValue payload;
switch (type) {
case CREATE_CONTRACT:
payload = EVENT_EMITTER_CONSTRUCTOR;
break;
case STORE:
payload = SET_FUNCTION_CALL;
break;
case GET:
payload = GET_FUNCTION_CALL;
break;
default:
throw new IllegalStateException("Unexpected value: " + type);
}
return RLP.encode(
PrivateTransaction.builder()
.nonce(nonce)
.gasPrice(Wei.of(1000))
.gasLimit(63992)
.to(to)
.value(Wei.ZERO)
.payload(payload)
.sender(from)
.chainId(2018)
.privateFrom(privateFrom)
.privateFor(privateFor)
.restriction(BytesValue.wrap("restricted".getBytes(UTF_8)))
.signAndBuild(keyPair)
::writeTo)
.toString();
}
}
}
Loading

0 comments on commit 7b82ff3

Please sign in to comment.