Search in sources :

Example 16 with PrivacyGroup

use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.

the class PrivCreatePrivacyGroupTest method verifyCreatePrivacyGroupWithoutOptionalParams.

@Test
public void verifyCreatePrivacyGroupWithoutOptionalParams() {
    final String expected = "a wonderful group";
    final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES);
    when(privacyController.createPrivacyGroup(ADDRESSES, null, null, ENCLAVE_PUBLIC_KEY)).thenReturn(privacyGroup);
    when(privacyParameters.getPrivacyUserId()).thenReturn(FROM);
    final PrivCreatePrivacyGroup privCreatePrivacyGroup = new PrivCreatePrivacyGroup(privacyController, privacyIdProvider);
    final Object[] params = new Object[] { new Object() {

        public List<String> getAddresses() {
            return ADDRESSES;
        }
    } };
    final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_createPrivacyGroup", params));
    final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) privCreatePrivacyGroup.response(request);
    final String result = (String) response.getResult();
    assertThat(result).isEqualTo(expected);
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) JsonObject(io.vertx.core.json.JsonObject) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) PrivacyGroup(org.hyperledger.besu.enclave.types.PrivacyGroup) Test(org.junit.Test)

Example 17 with PrivacyGroup

use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.

the class PrivCreatePrivacyGroupTest method returnsCorrectExceptionInvalidParam.

@Test
public void returnsCorrectExceptionInvalidParam() {
    final String expected = "a wonderful group";
    final PrivacyGroup privacyGroup = new PrivacyGroup(expected, PrivacyGroup.Type.PANTHEON, NAME, DESCRIPTION, ADDRESSES);
    when(enclave.createPrivacyGroup(any(), any(), any(), any())).thenReturn(privacyGroup);
    when(privacyParameters.getPrivacyUserId()).thenReturn(FROM);
    final PrivCreatePrivacyGroup privCreatePrivacyGroup = new PrivCreatePrivacyGroup(privacyController, privacyIdProvider);
    final Object[] params = new Object[] { new Object() {

        public String getName() {
            return NAME;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    } };
    final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("1", "priv_createPrivacyGroup", params));
    final Throwable response = catchThrowableOfType(() -> privCreatePrivacyGroup.response(request), InvalidJsonRpcParameters.class);
    assertThat(response.getMessage()).isEqualTo("Invalid json rpc parameter at index 0");
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) JsonObject(io.vertx.core.json.JsonObject) PrivacyGroup(org.hyperledger.besu.enclave.types.PrivacyGroup) Test(org.junit.Test)

Example 18 with PrivacyGroup

use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.

the class PrivDebugGetStateRoot method resultByBlockNumber.

@Override
protected Object resultByBlockNumber(final JsonRpcRequestContext requestContext, final long blockNumber) {
    final String privacyGroupId = requestContext.getRequiredParameter(0, String.class);
    final String privacyUserId = privacyIdProvider.getPrivacyUserId(requestContext.getUser());
    if (LOG.isTraceEnabled()) {
        LOG.trace("Executing {}", getName());
    }
    final Optional<PrivacyGroup> privacyGroup;
    try {
        privacyGroup = privacyController.findPrivacyGroupByGroupId(privacyGroupId, privacyUserId);
    } catch (final MultiTenancyValidationException e) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
    } catch (final EnclaveClientException e) {
        final Pattern pattern = Pattern.compile("^Privacy group.*not found$");
        if (e.getMessage().equals(JsonRpcError.ENCLAVE_PRIVACY_GROUP_MISSING.getMessage()) || pattern.matcher(e.getMessage()).find()) {
            LOG.error("Failed to retrieve privacy group");
            return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
        } else {
            return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.ENCLAVE_ERROR);
        }
    } catch (final Exception e) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
    }
    if (privacyGroup.isEmpty()) {
        LOG.error("Failed to retrieve privacy group");
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
    }
    return privacyController.getStateRootByBlockNumber(privacyGroupId, privacyUserId, blockNumber).<JsonRpcResponse>map(stateRootHash -> new JsonRpcSuccessResponse(requestContext.getRequest().getId(), stateRootHash.toString())).orElse(new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INTERNAL_ERROR));
}
Also used : MultiTenancyValidationException(org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException) MultiTenancyValidationException(org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException) PrivacyController(org.hyperledger.besu.ethereum.privacy.PrivacyController) Logger(org.slf4j.Logger) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) PrivacyGroup(org.hyperledger.besu.enclave.types.PrivacyGroup) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) LoggerFactory(org.slf4j.LoggerFactory) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) EnclaveClientException(org.hyperledger.besu.enclave.EnclaveClientException) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) PrivacyIdProvider(org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacyIdProvider) BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter) FIND_PRIVACY_GROUP_ERROR(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.FIND_PRIVACY_GROUP_ERROR) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) AbstractBlockParameterMethod(org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AbstractBlockParameterMethod) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Pattern(java.util.regex.Pattern) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) EnclaveClientException(org.hyperledger.besu.enclave.EnclaveClientException) PrivacyGroup(org.hyperledger.besu.enclave.types.PrivacyGroup) MultiTenancyValidationException(org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException) EnclaveClientException(org.hyperledger.besu.enclave.EnclaveClientException) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 19 with PrivacyGroup

use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.

the class PrivFindPrivacyGroup method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    LOG.trace("Executing {}", RpcMethod.PRIV_FIND_PRIVACY_GROUP.getMethodName());
    final String[] addresses = requestContext.getRequiredParameter(0, String[].class);
    LOG.trace("Finding a privacy group with members {}", Arrays.toString(addresses));
    final List<PrivacyGroup> response;
    try {
        response = Arrays.asList(privacyController.findPrivacyGroupByMembers(Arrays.asList(addresses), privacyIdProvider.getPrivacyUserId(requestContext.getUser())));
    } catch (final MultiTenancyValidationException e) {
        LOG.error("Unauthorized privacy multi-tenancy rpc request. {}", e.getMessage());
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
    } catch (final Exception e) {
        LOG.error("Failed to fetch privacy group", e);
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
    }
    return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), response);
}
Also used : MultiTenancyValidationException(org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) PrivacyGroup(org.hyperledger.besu.enclave.types.PrivacyGroup) MultiTenancyValidationException(org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 20 with PrivacyGroup

use of org.hyperledger.besu.enclave.types.PrivacyGroup in project besu by hyperledger.

the class RestrictedFlexibleEeaSendRawTransaction method createPrivateMarkerTransaction.

@Override
protected Transaction createPrivateMarkerTransaction(final Address sender, final PrivateTransaction privateTransaction, final Optional<User> user) {
    final Optional<Bytes> maybePrivacyGroupId = privateTransaction.getPrivacyGroupId();
    if (maybePrivacyGroupId.isEmpty()) {
        throw new JsonRpcErrorResponseException(JsonRpcError.FLEXIBLE_PRIVACY_GROUP_ID_NOT_AVAILABLE);
    }
    final Bytes privacyGroupId = maybePrivacyGroupId.get();
    final String privacyUserId = privacyIdProvider.getPrivacyUserId(user);
    Optional<PrivacyGroup> maybePrivacyGroup = privacyController.findPrivacyGroupByGroupId(privacyGroupId.toBase64String(), privacyUserId);
    final boolean isGroupAdditionTransaction = FlexibleUtil.isGroupAdditionTransaction(privateTransaction);
    if (maybePrivacyGroup.isEmpty() && !isGroupAdditionTransaction) {
        throw new JsonRpcErrorResponseException(JsonRpcError.FLEXIBLE_PRIVACY_GROUP_DOES_NOT_EXIST);
    }
    if (isGroupAdditionTransaction) {
        final List<String> participantsFromParameter = FlexibleUtil.getParticipantsFromParameter(privateTransaction.getPayload());
        if (maybePrivacyGroup.isEmpty()) {
            maybePrivacyGroup = Optional.of(new PrivacyGroup(privacyGroupId.toBase64String(), PrivacyGroup.Type.FLEXIBLE, null, null, participantsFromParameter));
        } else {
            maybePrivacyGroup.get().addMembers(participantsFromParameter);
        }
    }
    if (!maybePrivacyGroup.get().getMembers().contains(privacyUserId)) {
        throw new JsonRpcErrorResponseException(JsonRpcError.FLEXIBLE_PRIVACY_GROUP_DOES_NOT_EXIST);
    }
    final String pmtPayload = privacyController.createPrivateMarkerTransactionPayload(privateTransaction, privacyUserId, maybePrivacyGroup);
    return createPrivateMarkerTransaction(sender, FLEXIBLE_PRIVACY, pmtPayload, privateTransaction, privacyUserId);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PrivacyGroup(org.hyperledger.besu.enclave.types.PrivacyGroup)

Aggregations

PrivacyGroup (org.hyperledger.besu.enclave.types.PrivacyGroup)35 Test (org.junit.Test)19 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)13 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)8 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)7 JsonObject (io.vertx.core.json.JsonObject)5 Bytes (org.apache.tuweni.bytes.Bytes)5 ReceiveResponse (org.hyperledger.besu.enclave.types.ReceiveResponse)5 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)5 Enclave (org.hyperledger.besu.enclave.Enclave)4 MultiTenancyValidationException (org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException)4 PrivateTransaction (org.hyperledger.besu.ethereum.privacy.PrivateTransaction)4 Test (org.junit.jupiter.api.Test)4 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)3 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)3 PrecompiledContract (org.hyperledger.besu.evm.precompile.PrecompiledContract)3 Before (org.junit.Before)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CreatePrivacyGroupParameter (org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.parameters.CreatePrivacyGroupParameter)2