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

[PAN-2982] Modifies PrivGetPrivateTransaction to take public tx hash #1778

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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);

Expand All @@ -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="))
Expand All @@ -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);

Expand Down