From 539d4fb76a6fef9a3dd318cb26f872acb95dbb17 Mon Sep 17 00:00:00 2001 From: Joshua Richardson Date: Mon, 29 Jul 2019 15:10:14 +0100 Subject: [PATCH 1/2] Modifies PrivGetPrivateTransaction to take public tx hash --- ...vGetPrivateTransactionIntegrationTest.java | 29 +++++++++++++++---- .../jsonrpc/JsonRpcMethodsFactory.java | 3 +- .../priv/PrivGetPrivateTransaction.java | 18 ++++++++++-- .../eea/PrivGetPrivateTransactionTest.java | 28 ++++++++++++++---- 4 files changed, 63 insertions(+), 15 deletions(-) diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java index faedebb6dd..7ae39834b9 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java @@ -14,7 +14,9 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import tech.pegasys.orion.testutil.OrionTestHarness; import tech.pegasys.orion.testutil.OrionTestHarnessFactory; @@ -23,12 +25,12 @@ import tech.pegasys.pantheon.enclave.types.SendRequest; import tech.pegasys.pantheon.enclave.types.SendRequestLegacy; import tech.pegasys.pantheon.enclave.types.SendResponse; -import tech.pegasys.pantheon.ethereum.core.Address; -import tech.pegasys.pantheon.ethereum.core.PrivacyParameters; -import tech.pegasys.pantheon.ethereum.core.Wei; +import tech.pegasys.pantheon.ethereum.core.*; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.priv.PrivGetPrivateTransaction; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.TransactionWithMetadata; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.privacy.PrivateTransactionLegacyResult; import tech.pegasys.pantheon.ethereum.privacy.PrivateTransaction; @@ -40,6 +42,7 @@ import java.io.IOException; import java.math.BigInteger; import java.util.Base64; +import java.util.Optional; import com.google.common.collect.Lists; import org.junit.AfterClass; @@ -56,6 +59,10 @@ public class PrivGetPrivateTransactionIntegrationTest { private static OrionTestHarness testHarness; + private final TransactionWithMetadata returnedTransaction = mock(TransactionWithMetadata.class); + + private final Transaction justTransaction = mock(Transaction.class); + @BeforeClass public static void setUpOnce() throws Exception { folder.create(); @@ -118,10 +125,17 @@ public void testUpCheck() throws IOException { private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); + private final BlockchainQueries blockchain = mock(BlockchainQueries.class); + @Test public void returnsStoredPrivateTransaction() throws Exception { + final PrivGetPrivateTransaction privGetPrivateTransaction = - new PrivGetPrivateTransaction(enclave, parameters, privacyParameters); + new PrivGetPrivateTransaction(blockchain, enclave, parameters, privacyParameters); + + when(blockchain.transactionByHash(any(Hash.class))) + .thenReturn(Optional.of(returnedTransaction)); + when(returnedTransaction.getTransaction()).thenReturn(justTransaction); final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput(); privateTransaction.writeTo(bvrlp); @@ -133,8 +147,11 @@ public void returnsStoredPrivateTransaction() throws Exception { Lists.newArrayList("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=")); final SendResponse sendResponse = enclave.send(sendRequest); - final String hexKey = BytesValues.fromBase64(sendResponse.getKey()).toString(); - final Object[] params = new Object[] {hexKey}; + final BytesValue hexKey = BytesValues.fromBase64(sendResponse.getKey()); + when(justTransaction.getPayload()).thenReturn(hexKey); + + final Object[] params = new Object[] {Hash.ZERO}; + final JsonRpcRequest request = new JsonRpcRequest("1", "priv_getPrivateTransaction", params); final JsonRpcSuccessResponse response = diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcMethodsFactory.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcMethodsFactory.java index ce2855d216..4ca67ad108 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcMethodsFactory.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcMethodsFactory.java @@ -342,7 +342,8 @@ blockchainQueries, new TransactionTracer(blockReplay), parameter), new PrivFindPrivacyGroup(new Enclave(privacyParameters.getEnclaveUri()), parameter), new PrivGetPrivacyPrecompileAddress(privacyParameters), new PrivGetTransactionCount(parameter, privateTransactionHandler), - new PrivGetPrivateTransaction(enclave, parameter, privacyParameters)); + new PrivGetPrivateTransaction( + blockchainQueries, enclave, parameter, privacyParameters)); } } diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/priv/PrivGetPrivateTransaction.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/priv/PrivGetPrivateTransaction.java index 5d3f102629..5a6811735c 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/priv/PrivGetPrivateTransaction.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/priv/PrivGetPrivateTransaction.java @@ -17,18 +17,20 @@ import tech.pegasys.pantheon.enclave.Enclave; import tech.pegasys.pantheon.enclave.types.ReceiveRequest; import tech.pegasys.pantheon.enclave.types.ReceiveResponse; +import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.core.PrivacyParameters; import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.TransactionWithMetadata; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.privacy.PrivateTransactionGroupResult; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.privacy.PrivateTransactionLegacyResult; import tech.pegasys.pantheon.ethereum.privacy.PrivateTransaction; import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPInput; -import tech.pegasys.pantheon.util.bytes.BytesValue; import tech.pegasys.pantheon.util.bytes.BytesValues; import org.apache.logging.log4j.Logger; @@ -37,14 +39,17 @@ public class PrivGetPrivateTransaction implements JsonRpcMethod { private static final Logger LOG = getLogger(); + private final BlockchainQueries blockchain; private final Enclave enclave; private final JsonRpcParameter parameters; private final PrivacyParameters privacyParameters; public PrivGetPrivateTransaction( + final BlockchainQueries blockchain, final Enclave enclave, final JsonRpcParameter parameters, final PrivacyParameters privacyParameters) { + this.blockchain = blockchain; this.enclave = enclave; this.parameters = parameters; this.privacyParameters = privacyParameters; @@ -58,11 +63,18 @@ public String getName() { @Override public JsonRpcResponse response(final JsonRpcRequest request) { LOG.trace("Executing {}", RpcMethod.PRIV_GET_PRIVATE_TRANSACTION.getMethodName()); - final String enclaveKey = parameters.required(request.getParams(), 0, String.class); + + final Hash hash = parameters.required(request.getParams(), 0, Hash.class); + final TransactionWithMetadata resultTransaction = + blockchain.transactionByHash(hash).orElse(null); + + if (resultTransaction == null) { + return new JsonRpcSuccessResponse(request.getId(), null); + } try { ReceiveResponse receiveResponse = getReceiveResponseFromEnclave( - BytesValues.asBase64String(BytesValue.fromHexString(enclaveKey)), + BytesValues.asBase64String(resultTransaction.getTransaction().getPayload()), privacyParameters.getEnclavePublicKey()); LOG.trace("Received transaction information from Enclave"); diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/eea/PrivGetPrivateTransactionTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/eea/PrivGetPrivateTransactionTest.java index 3832e82e03..b63d3660c3 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/eea/PrivGetPrivateTransactionTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/eea/PrivGetPrivateTransactionTest.java @@ -22,12 +22,12 @@ import tech.pegasys.pantheon.enclave.Enclave; import tech.pegasys.pantheon.enclave.types.ReceiveRequest; import tech.pegasys.pantheon.enclave.types.ReceiveResponse; -import tech.pegasys.pantheon.ethereum.core.Address; -import tech.pegasys.pantheon.ethereum.core.PrivacyParameters; -import tech.pegasys.pantheon.ethereum.core.Wei; +import tech.pegasys.pantheon.ethereum.core.*; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.priv.PrivGetPrivateTransaction; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.TransactionWithMetadata; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.privacy.PrivateTransactionGroupResult; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.privacy.PrivateTransactionLegacyResult; @@ -40,6 +40,7 @@ import java.math.BigInteger; import java.util.Base64; +import java.util.Optional; import com.google.common.collect.Lists; import org.junit.Rule; @@ -90,8 +91,19 @@ public class PrivGetPrivateTransactionTest { private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); + private final BlockchainQueries blockchain = mock(BlockchainQueries.class); + + private final TransactionWithMetadata returnedTransaction = mock(TransactionWithMetadata.class); + + private final Transaction justTransaction = mock(Transaction.class); + @Test public void returnsPrivateTransactionLegacy() throws Exception { + when(blockchain.transactionByHash(any(Hash.class))) + .thenReturn(Optional.of(returnedTransaction)); + when(returnedTransaction.getTransaction()).thenReturn(justTransaction); + when(justTransaction.getPayload()).thenReturn(BytesValues.fromBase64("")); + final PrivateTransaction privateTransaction = privateTransactionBuilder .privateFor( @@ -102,7 +114,7 @@ public void returnsPrivateTransactionLegacy() throws Exception { new PrivateTransactionLegacyResult(privateTransaction); final PrivGetPrivateTransaction privGetPrivateTransaction = - new PrivGetPrivateTransaction(enclave, parameters, privacyParameters); + new PrivGetPrivateTransaction(blockchain, enclave, parameters, privacyParameters); final Object[] params = new Object[] {enclaveKey}; final JsonRpcRequest request = new JsonRpcRequest("1", "priv_getPrivateTransaction", params); @@ -122,6 +134,11 @@ public void returnsPrivateTransactionLegacy() throws Exception { @Test public void returnsPrivateTransactionGroup() throws Exception { + when(blockchain.transactionByHash(any(Hash.class))) + .thenReturn(Optional.of(returnedTransaction)); + when(returnedTransaction.getTransaction()).thenReturn(justTransaction); + when(justTransaction.getPayload()).thenReturn(BytesValues.fromBase64("")); + final PrivateTransaction privateTransaction = privateTransactionBuilder .privacyGroupId(BytesValues.fromBase64("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=")) @@ -130,7 +147,8 @@ public void returnsPrivateTransactionGroup() throws Exception { new PrivateTransactionGroupResult(privateTransaction); final PrivGetPrivateTransaction privGetPrivateTransaction = - new PrivGetPrivateTransaction(enclave, parameters, privacyParameters); + new PrivGetPrivateTransaction(blockchain, enclave, parameters, privacyParameters); + final Object[] params = new Object[] {enclaveKey}; final JsonRpcRequest request = new JsonRpcRequest("1", "priv_getPrivateTransaction", params); From bbb12d4edbbdec43ed398567ad9b58fb3676fc9e Mon Sep 17 00:00:00 2001 From: Joshua Richardson Date: Tue, 30 Jul 2019 09:32:42 +0100 Subject: [PATCH 2/2] Fix star imports --- .../methods/PrivGetPrivateTransactionIntegrationTest.java | 6 +++++- .../methods/privacy/eea/PrivGetPrivateTransactionTest.java | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java index 7ae39834b9..e55dde9a01 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java @@ -25,7 +25,11 @@ import tech.pegasys.pantheon.enclave.types.SendRequest; import tech.pegasys.pantheon.enclave.types.SendRequestLegacy; import tech.pegasys.pantheon.enclave.types.SendResponse; -import tech.pegasys.pantheon.ethereum.core.*; +import tech.pegasys.pantheon.ethereum.core.Address; +import tech.pegasys.pantheon.ethereum.core.Hash; +import tech.pegasys.pantheon.ethereum.core.PrivacyParameters; +import tech.pegasys.pantheon.ethereum.core.Transaction; +import tech.pegasys.pantheon.ethereum.core.Wei; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.priv.PrivGetPrivateTransaction; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/eea/PrivGetPrivateTransactionTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/eea/PrivGetPrivateTransactionTest.java index b63d3660c3..f5add17b09 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/eea/PrivGetPrivateTransactionTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/eea/PrivGetPrivateTransactionTest.java @@ -22,7 +22,11 @@ import tech.pegasys.pantheon.enclave.Enclave; import tech.pegasys.pantheon.enclave.types.ReceiveRequest; import tech.pegasys.pantheon.enclave.types.ReceiveResponse; -import tech.pegasys.pantheon.ethereum.core.*; +import tech.pegasys.pantheon.ethereum.core.Address; +import tech.pegasys.pantheon.ethereum.core.Hash; +import tech.pegasys.pantheon.ethereum.core.PrivacyParameters; +import tech.pegasys.pantheon.ethereum.core.Transaction; +import tech.pegasys.pantheon.ethereum.core.Wei; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.priv.PrivGetPrivateTransaction; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;