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..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 @@ -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; @@ -24,11 +26,15 @@ 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.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; +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 +46,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 +63,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 +129,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 +151,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 0f5bae48d4..7ccab5ccf2 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 @@ -343,7 +343,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..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 @@ -23,11 +23,15 @@ 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.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; +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 +44,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 +95,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 +118,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 +138,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 +151,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);