From 7b9e491774ab4bee8a74aa6b7687acc8095ef2d2 Mon Sep 17 00:00:00 2001 From: Vinicius Stevam <45455812+vinistevam@users.noreply.github.com> Date: Mon, 4 Mar 2019 23:17:44 +0000 Subject: [PATCH] [PRIV] Generate Private Contract Address (#1004) * Create a new PrivateContractAddress generation * Remove resolveAccountFromPublicState * Change method sign to BytesValue Signed-off-by: Adrian Sutton --- .../pantheon/ethereum/core/Address.java | 22 +++++++++++++++++++ .../privacy/PrivateTransactionProcessor.java | 18 +++++---------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/Address.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/Address.java index 2c99725730..877c19f534 100644 --- a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/Address.java +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/Address.java @@ -129,6 +129,28 @@ public class Address extends DelegatingBytesValue { }))); } + /** + * Address of the created private contract. + * + * @param senderAddress the address of the transaction sender. + * @param nonce the nonce of this transaction. + * @param privacyGroupId hash of participants list ordered from Enclave response. + * @return The generated address of the created private contract. + */ + public static Address privateContractAddress( + final Address senderAddress, final long nonce, final BytesValue privacyGroupId) { + return Address.extract( + Hash.hash( + RLP.encode( + out -> { + out.startList(); + out.writeBytesValue(senderAddress); + out.writeLongScalar(nonce); + out.writeBytesValue(privacyGroupId); + out.endList(); + }))); + } + @Override public Address copy() { final BytesValue copiedStorage = wrapped.copy(); diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/privacy/PrivateTransactionProcessor.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/privacy/PrivateTransactionProcessor.java index 88525397d4..5dffaf3e3a 100644 --- a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/privacy/PrivateTransactionProcessor.java +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/privacy/PrivateTransactionProcessor.java @@ -154,7 +154,7 @@ public class PrivateTransactionProcessor { final MutableAccount sender = maybePrivateSender != null ? maybePrivateSender - : resolveAccountFromPublicState(publicWorldState, privateWorldState, senderAddress); + : privateWorldState.createAccount(senderAddress, 0, Wei.ZERO); final long previousNonce = sender.incrementNonce(); LOG.trace( @@ -166,8 +166,8 @@ public class PrivateTransactionProcessor { final MessageFrame initialFrame; final Deque messageFrameStack = new ArrayDeque<>(); if (transaction.isContractCreation()) { - final Address contractAddress = - Address.contractAddress(senderAddress, sender.getNonce() - 1L); + final Address privateContractAddress = + Address.privateContractAddress(senderAddress, sender.getNonce() - 1L, BytesValue.EMPTY); initialFrame = MessageFrame.builder() @@ -175,9 +175,9 @@ public class PrivateTransactionProcessor { .messageFrameStack(messageFrameStack) .blockchain(blockchain) .worldState(privateWorldState.updater()) - .address(contractAddress) + .address(privateContractAddress) .originator(senderAddress) - .contract(contractAddress) + .contract(privateContractAddress) .initialGas(Gas.MAX_VALUE) .gasPrice(transaction.getGasPrice()) .inputData(BytesValue.EMPTY) @@ -239,14 +239,6 @@ public class PrivateTransactionProcessor { } } - private MutableAccount resolveAccountFromPublicState( - final WorldUpdater publicWorldState, - final WorldUpdater privateWorldState, - final Address senderAddress) { - final MutableAccount publicSender = publicWorldState.getOrCreate(senderAddress); - return privateWorldState.createAccount(senderAddress, publicSender.getNonce(), Wei.ZERO); - } - private static void clearEmptyAccounts(final WorldUpdater worldState) { worldState.getTouchedAccounts().stream() .filter(Account::isEmpty)