BESU-134: Prevent execution of PRIV/EEA methods when privacy is disabled (#226)

* Add warning when enabling PRIV/EEA APIs with privacy disabled
* Prevent execution of PRIV/EEA methods when privacy is disabled

Signed-off-by: Lucas Saldanha <lucas.saldanha@consensys.net>
pull/230/head
Lucas Saldanha 5 years ago committed by GitHub
parent 5f4bf595f4
commit f84c3d2111
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
  2. 48
      besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
  3. 9
      ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java
  4. 8
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/PrivacySendTransaction.java
  5. 11
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransaction.java
  6. 8
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroup.java
  7. 8
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroup.java
  8. 11
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransaction.java
  9. 11
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroup.java
  10. 10
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetEeaTransactionCount.java
  11. 21
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivacyPrecompileAddress.java
  12. 14
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransaction.java
  13. 11
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCount.java
  14. 14
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceipt.java
  15. 43
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivacyApiMethod.java
  16. 2
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/response/JsonRpcError.java
  17. 4
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EeaJsonRpcMethods.java
  18. 13
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivJsonRpcMethods.java
  19. 26
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/privacy/eea/PrivGetEeaTransactionCountTest.java
  20. 18
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/EeaSendRawTransactionTest.java
  21. 16
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivCreatePrivacyGroupTest.java
  22. 58
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDeletePrivacyGroupTest.java
  23. 20
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivDistributeRawTransactionTest.java
  24. 57
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivFindPrivacyGroupTest.java
  25. 28
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetPrivateTransactionTest.java
  26. 35
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionCountTest.java
  27. 37
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetTransactionReceiptTest.java

@ -1384,11 +1384,22 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
privacyParametersBuilder.setStorageProvider(
privacyKeyStorageProvider(keyValueStorageName + "-privacy"));
privacyParametersBuilder.setEnclaveFactory(new EnclaveFactory(vertx));
} else {
if (anyPrivacyApiEnabled()) {
logger.warn("Privacy is disabled. Cannot use EEA/PRIV API methods when not using Privacy.");
}
}
return privacyParametersBuilder.build();
}
private boolean anyPrivacyApiEnabled() {
return rpcHttpApis.contains(RpcApis.EEA)
|| rpcWsApis.contains(RpcApis.EEA)
|| rpcHttpApis.contains(RpcApis.PRIV)
|| rpcWsApis.contains(RpcApis.PRIV);
}
private PrivacyKeyValueStorageProvider privacyKeyStorageProvider(final String name) {
return new PrivacyKeyValueStorageProviderBuilder()
.withStorageFactory(

@ -3090,4 +3090,52 @@ public class BesuCommandTest extends CommandTestAbstract {
.contains(
"Unable to authenticate JSON-RPC WebSocket endpoint without a supplied credentials file or authentication public key file");
}
@Test
public void privHttpApisWithPrivacyDisabledLogsWarning() {
parseCommand("--privacy-enabled=false", "--rpc-http-api", "PRIV", "--rpc-http-enabled");
verify(mockRunnerBuilder).build();
verify(mockLogger)
.warn("Privacy is disabled. Cannot use EEA/PRIV API methods when not using Privacy.");
assertThat(commandOutput.toString()).isEmpty();
assertThat(commandErrorOutput.toString()).isEmpty();
}
@Test
public void privWsApisWithPrivacyDisabledLogsWarning() {
parseCommand("--privacy-enabled=false", "--rpc-ws-api", "PRIV", "--rpc-ws-enabled");
verify(mockRunnerBuilder).build();
verify(mockLogger)
.warn("Privacy is disabled. Cannot use EEA/PRIV API methods when not using Privacy.");
assertThat(commandOutput.toString()).isEmpty();
assertThat(commandErrorOutput.toString()).isEmpty();
}
@Test
public void eeaHttpApisWithPrivacyDisabledLogsWarning() {
parseCommand("--privacy-enabled=false", "--rpc-http-api", "EEA", "--rpc-http-enabled");
verify(mockRunnerBuilder).build();
verify(mockLogger)
.warn("Privacy is disabled. Cannot use EEA/PRIV API methods when not using Privacy.");
assertThat(commandOutput.toString()).isEmpty();
assertThat(commandErrorOutput.toString()).isEmpty();
}
@Test
public void eeaWsApisWithPrivacyDisabledLogsWarning() {
parseCommand("--privacy-enabled=false", "--rpc-ws-api", "EEA", "--rpc-ws-enabled");
verify(mockRunnerBuilder).build();
verify(mockLogger)
.warn("Privacy is disabled. Cannot use EEA/PRIV API methods when not using Privacy.");
assertThat(commandOutput.toString()).isEmpty();
assertThat(commandErrorOutput.toString()).isEmpty();
}
}

@ -54,6 +54,7 @@ import java.util.Optional;
import com.google.common.collect.Lists;
import io.vertx.core.Vertx;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
@ -134,11 +135,17 @@ public class PrivGetPrivateTransactionIntegrationTest {
private final BlockchainQueries blockchain = mock(BlockchainQueries.class);
@Before
public void before() {
when(privacyParameters.isEnabled()).thenReturn(true);
when(privacyParameters.getEnclave()).thenReturn(enclave);
}
@Test
public void returnsStoredPrivateTransaction() {
final PrivGetPrivateTransaction privGetPrivateTransaction =
new PrivGetPrivateTransaction(blockchain, enclave, privacyParameters);
new PrivGetPrivateTransaction(blockchain, privacyParameters);
when(blockchain.transactionByHash(any(Hash.class)))
.thenReturn(Optional.of(returnedTransaction));

@ -17,9 +17,11 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcRequestException;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivacyApiMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.privacy.PrivateTransaction;
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler;
@ -31,16 +33,18 @@ import org.hyperledger.besu.util.bytes.BytesValue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class AbstractSendTransaction {
public abstract class PrivacySendTransaction extends PrivacyApiMethod {
private static final Logger LOG = LogManager.getLogger();
protected final PrivateTransactionHandler privateTransactionHandler;
protected final TransactionPool transactionPool;
public AbstractSendTransaction(
public PrivacySendTransaction(
final PrivacyParameters privacyParameters,
final PrivateTransactionHandler privateTransactionHandler,
final TransactionPool transactionPool) {
super(privacyParameters);
this.privateTransactionHandler = privateTransactionHandler;
this.transactionPool = transactionPool;
}

@ -18,22 +18,23 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.AbstractSendTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.privacy.PrivateTransaction;
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler;
public class EeaSendRawTransaction extends AbstractSendTransaction implements JsonRpcMethod {
public class EeaSendRawTransaction extends PrivacySendTransaction {
public EeaSendRawTransaction(
final PrivacyParameters privacyParameters,
final PrivateTransactionHandler privateTransactionHandler,
final TransactionPool transactionPool) {
super(privateTransactionHandler, transactionPool);
super(privacyParameters, privateTransactionHandler, transactionPool);
}
@Override
@ -42,7 +43,7 @@ public class EeaSendRawTransaction extends AbstractSendTransaction implements Js
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
PrivateTransaction privateTransaction;
try {
privateTransaction = validateAndDecodeRequest(requestContext);

@ -22,7 +22,6 @@ import org.hyperledger.besu.enclave.types.PrivacyGroup;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.parameters.CreatePrivacyGroupParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
@ -31,15 +30,14 @@ import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.apache.logging.log4j.Logger;
public class PrivCreatePrivacyGroup implements JsonRpcMethod {
public class PrivCreatePrivacyGroup extends PrivacyApiMethod {
private static final Logger LOG = getLogger();
private final Enclave enclave;
private PrivacyParameters privacyParameters;
public PrivCreatePrivacyGroup(final PrivacyParameters privacyParameters) {
super(privacyParameters);
this.enclave = privacyParameters.getEnclave();
this.privacyParameters = privacyParameters;
}
@Override
@ -48,7 +46,7 @@ public class PrivCreatePrivacyGroup implements JsonRpcMethod {
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
LOG.trace("Executing {}", RpcMethod.PRIV_CREATE_PRIVACY_GROUP.getMethodName());
final CreatePrivacyGroupParameter parameter =

@ -20,7 +20,6 @@ import org.hyperledger.besu.enclave.Enclave;
import org.hyperledger.besu.enclave.types.DeletePrivacyGroupRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
@ -28,15 +27,14 @@ import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.apache.logging.log4j.Logger;
public class PrivDeletePrivacyGroup implements JsonRpcMethod {
public class PrivDeletePrivacyGroup extends PrivacyApiMethod {
private static final Logger LOG = getLogger();
private final Enclave enclave;
private PrivacyParameters privacyParameters;
public PrivDeletePrivacyGroup(final PrivacyParameters privacyParameters) {
super(privacyParameters);
this.enclave = privacyParameters.getEnclave();
this.privacyParameters = privacyParameters;
}
@Override
@ -45,7 +43,7 @@ public class PrivDeletePrivacyGroup implements JsonRpcMethod {
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
LOG.trace("Executing {}", RpcMethod.PRIV_DELETE_PRIVACY_GROUP.getMethodName());
final String privacyGroupId = requestContext.getRequiredParameter(0, String.class);

@ -17,22 +17,23 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.AbstractSendTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.privacy.PrivateTransaction;
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler;
import org.hyperledger.besu.util.bytes.BytesValues;
public class PrivDistributeRawTransaction extends AbstractSendTransaction implements JsonRpcMethod {
public class PrivDistributeRawTransaction extends PrivacySendTransaction {
public PrivDistributeRawTransaction(
final PrivacyParameters privacyParameters,
final PrivateTransactionHandler privateTransactionHandler,
final TransactionPool transactionPool) {
super(privateTransactionHandler, transactionPool);
super(privacyParameters, privateTransactionHandler, transactionPool);
}
@Override
@ -41,7 +42,7 @@ public class PrivDistributeRawTransaction extends AbstractSendTransaction implem
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
PrivateTransaction privateTransaction;
try {
privateTransaction = validateAndDecodeRequest(requestContext);

@ -21,22 +21,23 @@ import org.hyperledger.besu.enclave.types.FindPrivacyGroupRequest;
import org.hyperledger.besu.enclave.types.PrivacyGroup;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import java.util.Arrays;
import org.apache.logging.log4j.Logger;
public class PrivFindPrivacyGroup implements JsonRpcMethod {
public class PrivFindPrivacyGroup extends PrivacyApiMethod {
private static final Logger LOG = getLogger();
private final Enclave enclave;
public PrivFindPrivacyGroup(final Enclave enclave) {
this.enclave = enclave;
public PrivFindPrivacyGroup(final PrivacyParameters privacyParameters) {
super(privacyParameters);
this.enclave = privacyParameters.getEnclave();
}
@Override
@ -45,7 +46,7 @@ public class PrivFindPrivacyGroup implements JsonRpcMethod {
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
LOG.trace("Executing {}", RpcMethod.PRIV_FIND_PRIVACY_GROUP.getMethodName());
final String[] addresses = requestContext.getRequiredParameter(0, String[].class);

@ -18,23 +18,25 @@ import static org.apache.logging.log4j.LogManager.getLogger;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.apache.logging.log4j.Logger;
public class PrivGetEeaTransactionCount implements JsonRpcMethod {
public class PrivGetEeaTransactionCount extends PrivacyApiMethod {
private static final Logger LOG = getLogger();
private final PrivateEeaNonceProvider nonceProvider;
public PrivGetEeaTransactionCount(final PrivateEeaNonceProvider nonceProvider) {
public PrivGetEeaTransactionCount(
final PrivacyParameters privacyParameters, final PrivateEeaNonceProvider nonceProvider) {
super(privacyParameters);
this.nonceProvider = nonceProvider;
}
@ -44,7 +46,7 @@ public class PrivGetEeaTransactionCount implements JsonRpcMethod {
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
if (requestContext.getRequest().getParamLength() != 3) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);

@ -16,22 +16,18 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
public class PrivGetPrivacyPrecompileAddress implements JsonRpcMethod {
public class PrivGetPrivacyPrecompileAddress extends PrivacyApiMethod {
private final Integer privacyAddress;
private final Boolean privacyEnabled;
public PrivGetPrivacyPrecompileAddress(final PrivacyParameters privacyParameters) {
super(privacyParameters);
privacyAddress = privacyParameters.getPrivacyAddress();
privacyEnabled = privacyParameters.isEnabled();
}
@Override
@ -40,15 +36,8 @@ public class PrivGetPrivacyPrecompileAddress implements JsonRpcMethod {
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
if (privacyEnabled) {
return new JsonRpcSuccessResponse(
requestContext.getRequest().getId(),
Address.privacyPrecompiled(privacyAddress).toString());
} else {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.PRIVACY_NOT_ENABLED);
}
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
return new JsonRpcSuccessResponse(
requestContext.getRequest().getId(), Address.privacyPrecompiled(privacyAddress).toString());
}
}

@ -21,7 +21,6 @@ import org.hyperledger.besu.enclave.types.ReceiveRequest;
import org.hyperledger.besu.enclave.types.ReceiveResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionGroupResult;
@ -36,21 +35,18 @@ import org.hyperledger.besu.util.bytes.BytesValues;
import org.apache.logging.log4j.Logger;
public class PrivGetPrivateTransaction implements JsonRpcMethod {
public class PrivGetPrivateTransaction extends PrivacyApiMethod {
private static final Logger LOG = getLogger();
private final BlockchainQueries blockchain;
private final Enclave enclave;
private final PrivacyParameters privacyParameters;
public PrivGetPrivateTransaction(
final BlockchainQueries blockchain,
final Enclave enclave,
final PrivacyParameters privacyParameters) {
final BlockchainQueries blockchain, final PrivacyParameters privacyParameters) {
super(privacyParameters);
this.blockchain = blockchain;
this.enclave = enclave;
this.privacyParameters = privacyParameters;
this.enclave = privacyParameters.getEnclave();
}
@Override
@ -59,7 +55,7 @@ public class PrivGetPrivateTransaction implements JsonRpcMethod {
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
LOG.trace("Executing {}", RpcMethod.PRIV_GET_PRIVATE_TRANSACTION.getMethodName());
final Hash hash = requestContext.getRequiredParameter(0, Hash.class);

@ -16,20 +16,23 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler;
public class PrivGetTransactionCount implements JsonRpcMethod {
public class PrivGetTransactionCount extends PrivacyApiMethod {
private final PrivateTransactionHandler privateTransactionHandler;
public PrivGetTransactionCount(final PrivateTransactionHandler privateTransactionHandler) {
public PrivGetTransactionCount(
final PrivacyParameters privacyParameters,
final PrivateTransactionHandler privateTransactionHandler) {
super(privacyParameters);
this.privateTransactionHandler = privateTransactionHandler;
}
@ -39,7 +42,7 @@ public class PrivGetTransactionCount implements JsonRpcMethod {
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
if (requestContext.getRequest().getParamLength() != 2) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);

@ -23,7 +23,6 @@ import org.hyperledger.besu.enclave.types.ReceiveResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
@ -50,21 +49,18 @@ import java.util.Optional;
import org.apache.logging.log4j.Logger;
public class PrivGetTransactionReceipt implements JsonRpcMethod {
public class PrivGetTransactionReceipt extends PrivacyApiMethod {
private static final Logger LOG = getLogger();
private final BlockchainQueries blockchain;
private final Enclave enclave;
private final PrivacyParameters privacyParameters;
public PrivGetTransactionReceipt(
final BlockchainQueries blockchain,
final Enclave enclave,
final PrivacyParameters privacyParameters) {
final BlockchainQueries blockchain, final PrivacyParameters privacyParameters) {
super(privacyParameters);
this.blockchain = blockchain;
this.enclave = enclave;
this.privacyParameters = privacyParameters;
this.enclave = privacyParameters.getEnclave();
}
@Override
@ -73,7 +69,7 @@ public class PrivGetTransactionReceipt implements JsonRpcMethod {
}
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
public JsonRpcResponse doResponse(final JsonRpcRequestContext requestContext) {
LOG.trace("Executing {}", RpcMethod.PRIV_GET_TRANSACTION_RECEIPT.getMethodName());
final Hash transactionHash = requestContext.getRequiredParameter(0, Hash.class);
final Optional<TransactionLocation> maybeLocation =

@ -0,0 +1,43 @@
/*
* Copyright 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
public abstract class PrivacyApiMethod implements JsonRpcMethod {
protected final PrivacyParameters privacyParameters;
protected PrivacyApiMethod(final PrivacyParameters privacyParameters) {
this.privacyParameters = privacyParameters;
}
@Override
public final JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
if (privacyParameters.isEnabled()) {
return doResponse(requestContext);
} else {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.PRIVACY_NOT_ENABLED);
}
}
public abstract JsonRpcResponse doResponse(final JsonRpcRequestContext request);
}

@ -107,7 +107,7 @@ public enum JsonRpcError {
// Private transaction errors
ENCLAVE_ERROR(-50100, "Error communicating with enclave"),
UNIMPLEMENTED_PRIVATE_TRANSACTION_TYPE(-50100, "Unimplemented private transaction type"),
PRIVACY_NOT_ENABLED(-50100, "Privacy is not enabled to get the precompiled address"),
PRIVACY_NOT_ENABLED(-50100, "Privacy is not enabled"),
CREATE_PRIVACY_GROUP_ERROR(-50100, "Error creating privacy group"),
DELETE_PRIVACY_GROUP_ERROR(-50100, "Error deleting privacy group"),
FIND_PRIVACY_GROUP_ERROR(-50100, "Error finding privacy group"),

@ -47,8 +47,10 @@ public class EeaJsonRpcMethods extends PrivacyApiGroupJsonRpcMethods {
protected Map<String, JsonRpcMethod> create(
final PrivateTransactionHandler privateTransactionHandler) {
return mapOf(
new EeaSendRawTransaction(privateTransactionHandler, getTransactionPool()),
new EeaSendRawTransaction(
getPrivacyParameters(), privateTransactionHandler, getTransactionPool()),
new PrivGetEeaTransactionCount(
getPrivacyParameters(),
new PrivateEeaNonceProvider(
getPrivacyParameters().getEnclave(), privateTransactionHandler)));
}

@ -14,7 +14,6 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;
import org.hyperledger.besu.enclave.Enclave;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
@ -52,15 +51,15 @@ public class PrivJsonRpcMethods extends PrivacyApiGroupJsonRpcMethods {
@Override
protected Map<String, JsonRpcMethod> create(
final PrivateTransactionHandler privateTransactionHandler) {
final Enclave enclave = getPrivacyParameters().getEnclave();
return mapOf(
new PrivGetTransactionReceipt(getBlockchainQueries(), enclave, getPrivacyParameters()),
new PrivGetTransactionReceipt(getBlockchainQueries(), getPrivacyParameters()),
new PrivCreatePrivacyGroup(getPrivacyParameters()),
new PrivDeletePrivacyGroup(getPrivacyParameters()),
new PrivFindPrivacyGroup(getPrivacyParameters().getEnclave()),
new PrivFindPrivacyGroup(getPrivacyParameters()),
new PrivGetPrivacyPrecompileAddress(getPrivacyParameters()),
new PrivGetTransactionCount(privateTransactionHandler),
new PrivGetPrivateTransaction(getBlockchainQueries(), enclave, getPrivacyParameters()),
new PrivDistributeRawTransaction(privateTransactionHandler, getTransactionPool()));
new PrivGetTransactionCount(getPrivacyParameters(), privateTransactionHandler),
new PrivGetPrivateTransaction(getBlockchainQueries(), getPrivacyParameters()),
new PrivDistributeRawTransaction(
getPrivacyParameters(), privateTransactionHandler, getTransactionPool()));
}
}

@ -27,6 +27,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorR
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.junit.Before;
import org.junit.Test;
@ -34,6 +35,7 @@ import org.junit.Test;
public class PrivGetEeaTransactionCountTest {
private final PrivateEeaNonceProvider nonceProvider = mock(PrivateEeaNonceProvider.class);
private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class);
private JsonRpcRequestContext request;
private final String privateFrom = "thePrivateFromKey";
@ -42,6 +44,7 @@ public class PrivGetEeaTransactionCountTest {
@Before
public void setup() {
when(privacyParameters.isEnabled()).thenReturn(true);
final Object[] jsonBody = new Object[] {address.toString(), privateFrom, privateFor};
request =
new JsonRpcRequestContext(
@ -51,7 +54,8 @@ public class PrivGetEeaTransactionCountTest {
@Test
public void validRequestProducesExpectedNonce() {
final long reportedNonce = 8L;
final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(nonceProvider);
final PrivGetEeaTransactionCount method =
new PrivGetEeaTransactionCount(privacyParameters, nonceProvider);
when(nonceProvider.determineNonce(privateFrom, privateFor, address)).thenReturn(reportedNonce);
@ -65,7 +69,8 @@ public class PrivGetEeaTransactionCountTest {
@Test
public void nonceProviderThrowsRuntimeExceptionProducesErrorResponse() {
final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(nonceProvider);
final PrivGetEeaTransactionCount method =
new PrivGetEeaTransactionCount(privacyParameters, nonceProvider);
when(nonceProvider.determineNonce(privateFrom, privateFor, address))
.thenThrow(RuntimeException.class);
@ -80,7 +85,8 @@ public class PrivGetEeaTransactionCountTest {
@Test
public void nonceProviderThrowsAnExceptionProducesErrorResponse() {
final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(nonceProvider);
final PrivGetEeaTransactionCount method =
new PrivGetEeaTransactionCount(privacyParameters, nonceProvider);
when(nonceProvider.determineNonce(privateFrom, privateFor, address))
.thenThrow(RuntimeException.class);
@ -92,4 +98,18 @@ public class PrivGetEeaTransactionCountTest {
assertThat(errorResponse.getError())
.isEqualTo(JsonRpcError.GET_PRIVATE_TRANSACTION_NONCE_ERROR);
}
@Test
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() {
when(privacyParameters.isEnabled()).thenReturn(false);
final PrivGetEeaTransactionCount method =
new PrivGetEeaTransactionCount(privacyParameters, nonceProvider);
final JsonRpcRequestContext request =
new JsonRpcRequestContext(
new JsonRpcRequest("1", "priv_getEeaTransactionCount", new Object[] {}));
final JsonRpcErrorResponse response = (JsonRpcErrorResponse) method.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED);
}
}

@ -29,6 +29,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorR
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.Wei;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
@ -114,9 +115,12 @@ public class EeaSendRawTransactionTest {
@Mock private PrivateTransactionHandler privateTxHandler;
@Mock private PrivacyParameters privacyParameters;
@Before
public void before() {
method = new EeaSendRawTransaction(privateTxHandler, transactionPool);
when(privacyParameters.isEnabled()).thenReturn(true);
method = new EeaSendRawTransaction(privacyParameters, privateTxHandler, transactionPool);
}
@Test
@ -383,4 +387,16 @@ public class EeaSendRawTransactionTest {
public void getMethodReturnsExpectedName() {
assertThat(method.getName()).matches("eea_sendRawTransaction");
}
@Test
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() {
when(privacyParameters.isEnabled()).thenReturn(false);
final JsonRpcRequestContext request =
new JsonRpcRequestContext(
new JsonRpcRequest("1", "eea_sendRawTransaction", new Object[] {}));
final JsonRpcErrorResponse response = (JsonRpcErrorResponse) method.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED);
}
}

@ -52,6 +52,7 @@ public class PrivCreatePrivacyGroupTest {
when(failingEnclave.createPrivacyGroup(any(CreatePrivacyGroupRequest.class)))
.thenThrow(new EnclaveException(""));
when(privacyParameters.getEnclave()).thenReturn(enclave);
when(privacyParameters.isEnabled()).thenReturn(true);
}
@Test
@ -256,4 +257,19 @@ public class PrivCreatePrivacyGroupTest {
assertThat(result).isEqualTo(JsonRpcError.ENCLAVE_ERROR);
}
@Test
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() {
when(privacyParameters.isEnabled()).thenReturn(false);
final PrivCreatePrivacyGroup privCreatePrivacyGroup =
new PrivCreatePrivacyGroup(privacyParameters);
final JsonRpcRequestContext request =
new JsonRpcRequestContext(
new JsonRpcRequest("1", "priv_createPrivacyGroup", new Object[] {}));
final JsonRpcErrorResponse response =
(JsonRpcErrorResponse) privCreatePrivacyGroup.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED);
}
}

@ -0,0 +1,58 @@
/*
* Copyright 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.enclave.Enclave;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PrivDeletePrivacyGroupTest {
@Mock private Enclave enclave;
@Mock private PrivacyParameters privacyParameters;
private PrivDeletePrivacyGroup privDeletePrivacyGroup;
@Before
public void before() {
when(privacyParameters.getEnclave()).thenReturn(enclave);
privDeletePrivacyGroup = new PrivDeletePrivacyGroup(privacyParameters);
}
@Test
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() {
when(privacyParameters.isEnabled()).thenReturn(false);
final JsonRpcRequestContext request =
new JsonRpcRequestContext(
new JsonRpcRequest("1", "priv_deletePrivacyGroup", new Object[] {}));
final JsonRpcErrorResponse response =
(JsonRpcErrorResponse) privDeletePrivacyGroup.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED);
}
}

@ -21,8 +21,11 @@ import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.ethereum.privacy.PrivateTransaction;
@ -56,9 +59,12 @@ public class PrivDistributeRawTransactionTest {
@Mock private PrivateTransactionHandler privateTxHandler;
@Mock private PrivacyParameters privacyParameters;
@Before
public void before() {
method = new PrivDistributeRawTransaction(privateTxHandler, transactionPool);
when(privacyParameters.isEnabled()).thenReturn(true);
method = new PrivDistributeRawTransaction(privacyParameters, privateTxHandler, transactionPool);
}
@Test
@ -89,4 +95,16 @@ public class PrivDistributeRawTransactionTest {
verify(privateTxHandler)
.validatePrivateTransaction(any(PrivateTransaction.class), any(String.class));
}
@Test
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() {
when(privacyParameters.isEnabled()).thenReturn(false);
final JsonRpcRequestContext request =
new JsonRpcRequestContext(
new JsonRpcRequest("1", "priv_distributeRawTransaction", new Object[] {}));
final JsonRpcErrorResponse response = (JsonRpcErrorResponse) method.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED);
}
}

@ -0,0 +1,57 @@
/*
* Copyright 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.enclave.Enclave;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PrivFindPrivacyGroupTest {
@Mock private Enclave enclave;
@Mock private PrivacyParameters privacyParameters;
private PrivFindPrivacyGroup privFindPrivacyGroup;
@Before
public void before() {
when(privacyParameters.getEnclave()).thenReturn(enclave);
privFindPrivacyGroup = new PrivFindPrivacyGroup(privacyParameters);
}
@Test
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() {
when(privacyParameters.isEnabled()).thenReturn(false);
final JsonRpcRequestContext request =
new JsonRpcRequestContext(new JsonRpcRequest("1", "findPrivacyGroup", new Object[] {}));
final JsonRpcErrorResponse response =
(JsonRpcErrorResponse) privFindPrivacyGroup.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED);
}
}

@ -26,6 +26,8 @@ import org.hyperledger.besu.enclave.types.ReceiveRequest;
import org.hyperledger.besu.enclave.types.ReceiveResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionGroupResult;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionLegacyResult;
@ -48,6 +50,7 @@ import java.util.Base64;
import java.util.Optional;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@ -101,6 +104,12 @@ public class PrivGetPrivateTransactionTest {
private final Transaction justTransaction = mock(Transaction.class);
@Before
public void before() {
when(privacyParameters.getEnclave()).thenReturn(enclave);
when(privacyParameters.isEnabled()).thenReturn(true);
}
@Test
public void returnsPrivateTransactionLegacy() throws Exception {
when(blockchain.transactionByHash(any(Hash.class)))
@ -118,7 +127,7 @@ public class PrivGetPrivateTransactionTest {
new PrivateTransactionLegacyResult(privateTransaction);
final PrivGetPrivateTransaction privGetPrivateTransaction =
new PrivGetPrivateTransaction(blockchain, enclave, privacyParameters);
new PrivGetPrivateTransaction(blockchain, privacyParameters);
final Object[] params = new Object[] {enclaveKey};
final JsonRpcRequestContext request =
new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getPrivateTransaction", params));
@ -152,7 +161,7 @@ public class PrivGetPrivateTransactionTest {
new PrivateTransactionGroupResult(privateTransaction);
final PrivGetPrivateTransaction privGetPrivateTransaction =
new PrivGetPrivateTransaction(blockchain, enclave, privacyParameters);
new PrivGetPrivateTransaction(blockchain, privacyParameters);
final Object[] params = new Object[] {enclaveKey};
final JsonRpcRequestContext request =
@ -171,4 +180,19 @@ public class PrivGetPrivateTransactionTest {
assertThat(result).isEqualToComparingFieldByField(privateTransactionGroupResult);
}
@Test
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() {
when(privacyParameters.isEnabled()).thenReturn(false);
final PrivGetPrivateTransaction privGetPrivateTransaction =
new PrivGetPrivateTransaction(blockchain, privacyParameters);
final JsonRpcRequestContext request =
new JsonRpcRequestContext(
new JsonRpcRequest("1", "priv_getPrivacyTransaction", new Object[] {}));
final JsonRpcErrorResponse response =
(JsonRpcErrorResponse) privGetPrivateTransaction.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED);
}
}

@ -21,16 +21,24 @@ import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler;
import org.hyperledger.besu.util.bytes.BytesValue;
import org.hyperledger.besu.util.bytes.BytesValues;
import org.junit.Before;
import org.junit.Test;
public class PrivGetTransactionCountTest {
private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class);
private final PrivateTransactionHandler privateTransactionHandler =
mock(PrivateTransactionHandler.class);
private final String privacyGroupId =
BytesValues.asBase64String(BytesValue.wrap("0x123".getBytes(UTF_8)));
@ -38,14 +46,16 @@ public class PrivGetTransactionCountTest {
Address.fromHexString("0x627306090abab3a6e1400e9345bc60c78a8bef57");
private final long NONCE = 5;
@Test
public void verifyTransactionCount() {
final PrivateTransactionHandler privateTransactionHandler =
mock(PrivateTransactionHandler.class);
@Before
public void before() {
when(privacyParameters.isEnabled()).thenReturn(true);
when(privateTransactionHandler.getSenderNonce(senderAddress, privacyGroupId)).thenReturn(NONCE);
}
@Test
public void verifyTransactionCount() {
final PrivGetTransactionCount privGetTransactionCount =
new PrivGetTransactionCount(privateTransactionHandler);
new PrivGetTransactionCount(privacyParameters, privateTransactionHandler);
final Object[] params = new Object[] {senderAddress, privacyGroupId};
final JsonRpcRequestContext request =
@ -56,4 +66,19 @@ public class PrivGetTransactionCountTest {
assertThat(response.getResult()).isEqualTo(String.format("0x%X", NONCE));
}
@Test
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() {
when(privacyParameters.isEnabled()).thenReturn(false);
final PrivGetTransactionCount privGetTransactionCount =
new PrivGetTransactionCount(privacyParameters, privateTransactionHandler);
final JsonRpcRequestContext request =
new JsonRpcRequestContext(
new JsonRpcRequest("1", "priv_getTransactionCount", new Object[] {}));
final JsonRpcErrorResponse response =
(JsonRpcErrorResponse) privGetTransactionCount.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED);
}
}

@ -29,6 +29,8 @@ import org.hyperledger.besu.enclave.types.ReceiveRequest;
import org.hyperledger.besu.enclave.types.ReceiveResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionReceiptResult;
@ -164,6 +166,7 @@ public class PrivGetTransactionReceiptTest {
when(blockchain.getBlockHeader(any(Hash.class))).thenReturn(Optional.of(mockBlockHeader));
when(mockBlockHeader.getNumber()).thenReturn(0L);
when(privacyParameters.isEnabled()).thenReturn(true);
when(privacyParameters.getPrivateStateStorage()).thenReturn(privateStateStorage);
when(privateStateStorage.getTransactionLogs(any(Bytes32.class))).thenReturn(Optional.empty());
when(privateStateStorage.getTransactionOutput(any(Bytes32.class))).thenReturn(Optional.empty());
@ -173,8 +176,10 @@ public class PrivGetTransactionReceiptTest {
@Test
public void returnReceiptIfTransactionExists() {
when(privacyParameters.getEnclave()).thenReturn(enclave);
final PrivGetTransactionReceipt privGetTransactionReceipt =
new PrivGetTransactionReceipt(blockchainQueries, enclave, privacyParameters);
new PrivGetTransactionReceipt(blockchainQueries, privacyParameters);
final Object[] params = new Object[] {transaction.getHash()};
final JsonRpcRequestContext request =
new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionReceipt", params));
@ -189,11 +194,12 @@ public class PrivGetTransactionReceiptTest {
@Test
public void enclavePayloadNotFoundResultsInSuccessButNullResponse() {
when(privacyParameters.getEnclave()).thenReturn(failingEnclave);
when(failingEnclave.receive(any(ReceiveRequest.class)))
.thenThrow(new EnclaveException("EnclavePayloadNotFound"));
final PrivGetTransactionReceipt privGetTransactionReceipt =
new PrivGetTransactionReceipt(blockchainQueries, failingEnclave, privacyParameters);
new PrivGetTransactionReceipt(blockchainQueries, privacyParameters);
final Object[] params = new Object[] {transaction.getHash()};
final JsonRpcRequestContext request =
new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionReceipt", params));
@ -209,9 +215,10 @@ public class PrivGetTransactionReceiptTest {
@Test
public void markerTransactionNotAvailableResultsInNullResponse() {
when(blockchain.getTransactionLocation(nullable(Hash.class))).thenReturn(Optional.empty());
when(privacyParameters.getEnclave()).thenReturn(enclave);
final PrivGetTransactionReceipt privGetTransactionReceipt =
new PrivGetTransactionReceipt(blockchainQueries, enclave, privacyParameters);
new PrivGetTransactionReceipt(blockchainQueries, privacyParameters);
final Object[] params = new Object[] {transaction.getHash()};
final JsonRpcRequestContext request =
new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionReceipt", params));
@ -226,8 +233,10 @@ public class PrivGetTransactionReceiptTest {
@Test
public void enclaveConnectionIssueThrowsRuntimeException() {
when(privacyParameters.getEnclave()).thenReturn(failingEnclave);
final PrivGetTransactionReceipt privGetTransactionReceipt =
new PrivGetTransactionReceipt(blockchainQueries, failingEnclave, privacyParameters);
new PrivGetTransactionReceipt(blockchainQueries, privacyParameters);
final Object[] params = new Object[] {transaction.getHash()};
final JsonRpcRequestContext request =
new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_getTransactionReceipt", params));
@ -238,11 +247,12 @@ public class PrivGetTransactionReceiptTest {
@Test
public void transactionReceiptContainsRevertReasonWhenInvalidTransactionOccurs() {
when(privacyParameters.getEnclave()).thenReturn(enclave);
when(privateStateStorage.getRevertReason(any(Bytes32.class)))
.thenReturn(Optional.of(BytesValue.fromHexString("0x01")));
final PrivGetTransactionReceipt privGetTransactionReceipt =
new PrivGetTransactionReceipt(blockchainQueries, enclave, privacyParameters);
new PrivGetTransactionReceipt(blockchainQueries, privacyParameters);
final Object[] params = new Object[] {transaction.getHash()};
final JsonRpcRequest request = new JsonRpcRequest("1", "priv_getTransactionReceipt", params);
@ -254,4 +264,21 @@ public class PrivGetTransactionReceiptTest {
assertThat(result.getRevertReason()).isEqualTo("0x01");
}
@Test
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() {
when(privacyParameters.getEnclave()).thenReturn(enclave);
when(privacyParameters.isEnabled()).thenReturn(false);
final PrivGetTransactionReceipt privGetTransactionReceipt =
new PrivGetTransactionReceipt(blockchainQueries, privacyParameters);
final JsonRpcRequestContext request =
new JsonRpcRequestContext(
new JsonRpcRequest("1", "priv_getTransactionReceipt", new Object[] {}));
final JsonRpcErrorResponse response =
(JsonRpcErrorResponse) privGetTransactionReceipt.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED);
}
}

Loading…
Cancel
Save