Search in sources :

Example 1 with EnclaveIOException

use of org.hyperledger.besu.enclave.EnclaveIOException in project besu by hyperledger.

the class PrivacyNode method testEnclaveConnection.

public void testEnclaveConnection(final List<PrivacyNode> otherNodes) {
    if (this.isPrivacyPluginEnabled) {
        LOG.info("Skipping test as node has no enclave (isPrivacyPluginEnabled=true)");
        return;
    }
    if (!otherNodes.isEmpty()) {
        LOG.debug(String.format("Testing Enclave connectivity between %s (%s) and %s (%s)", besu.getName(), enclave.nodeUrl(), Arrays.toString(otherNodes.stream().map(node -> node.besu.getName()).toArray()), Arrays.toString(otherNodes.stream().map(node -> node.enclave.nodeUrl()).toArray())));
        final EnclaveFactory factory = new EnclaveFactory(vertx);
        final Enclave enclaveClient = factory.createVertxEnclave(enclave.clientUrl());
        final String payload = "SGVsbG8sIFdvcmxkIQ==";
        final List<String> to = otherNodes.stream().map(node -> node.enclave.getDefaultPublicKey()).collect(Collectors.toList());
        Awaitility.await().until(() -> {
            try {
                enclaveClient.send(payload, enclave.getDefaultPublicKey(), to);
                return true;
            } catch (final EnclaveClientException | EnclaveIOException | EnclaveServerException e) {
                LOG.warn("Waiting for enclave connectivity between {} and {}: " + e.getMessage(), enclave.getDefaultPublicKey(), to.get(0));
                return false;
            }
        });
    }
}
Also used : EnclaveFactory(org.hyperledger.besu.enclave.EnclaveFactory) KeyPairUtil(org.hyperledger.besu.crypto.KeyPairUtil) PrivacyKeyValueStorageProviderBuilder(org.hyperledger.besu.ethereum.privacy.storage.keyvalue.PrivacyKeyValueStorageProviderBuilder) Arrays(java.util.Arrays) RocksDBKeyValueStorageFactory(org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBKeyValueStorageFactory) EnclaveServerException(org.hyperledger.besu.enclave.EnclaveServerException) EnclaveIOException(org.hyperledger.besu.enclave.EnclaveIOException) NoopEnclaveTestHarness(org.hyperledger.enclave.testutil.NoopEnclaveTestHarness) LoggerFactory(org.slf4j.LoggerFactory) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) Address(org.hyperledger.besu.datatypes.Address) BesuNode(org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode) Network(org.testcontainers.containers.Network) EnclaveFactory(org.hyperledger.besu.enclave.EnclaveFactory) Transaction(org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction) TesseraTestHarnessFactory(org.hyperledger.enclave.testutil.TesseraTestHarnessFactory) Enclave(org.hyperledger.besu.enclave.Enclave) PrivateCondition(org.hyperledger.besu.tests.acceptance.dsl.privacy.condition.PrivateCondition) EnclaveTestHarness(org.hyperledger.enclave.testutil.EnclaveTestHarness) URI(java.net.URI) PrivacyStorageProvider(org.hyperledger.besu.ethereum.privacy.storage.PrivacyStorageProvider) Path(java.nio.file.Path) RocksDBFactoryConfiguration(org.hyperledger.besu.plugin.services.storage.rocksdb.configuration.RocksDBFactoryConfiguration) Logger(org.slf4j.Logger) BesuNodeConfiguration(org.hyperledger.besu.tests.acceptance.dsl.node.configuration.BesuNodeConfiguration) Files(java.nio.file.Files) EnclaveType(org.hyperledger.enclave.testutil.EnclaveType) KeyValueSegmentIdentifier(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier) Vertx(io.vertx.core.Vertx) NodeConfiguration(org.hyperledger.besu.tests.acceptance.dsl.node.configuration.NodeConfiguration) IOException(java.io.IOException) RocksDBKeyValuePrivacyStorageFactory(org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBKeyValuePrivacyStorageFactory) DATABASE_PATH(org.hyperledger.besu.controller.BesuController.DATABASE_PATH) Collectors(java.util.stream.Collectors) BesuConfigurationImpl(org.hyperledger.besu.services.BesuConfigurationImpl) EnclaveClientException(org.hyperledger.besu.enclave.EnclaveClientException) BesuNodeRunner(org.hyperledger.besu.tests.acceptance.dsl.node.BesuNodeRunner) List(java.util.List) RocksDBMetricsFactory(org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBMetricsFactory) Optional(java.util.Optional) PrivacyNodeConfiguration(org.hyperledger.besu.tests.acceptance.dsl.node.configuration.privacy.PrivacyNodeConfiguration) Collections(java.util.Collections) Awaitility(org.awaitility.Awaitility) PrivacyParameters(org.hyperledger.besu.ethereum.core.PrivacyParameters) Condition(org.hyperledger.besu.tests.acceptance.dsl.condition.Condition) EnclaveServerException(org.hyperledger.besu.enclave.EnclaveServerException) Enclave(org.hyperledger.besu.enclave.Enclave) EnclaveIOException(org.hyperledger.besu.enclave.EnclaveIOException) EnclaveClientException(org.hyperledger.besu.enclave.EnclaveClientException)

Example 2 with EnclaveIOException

use of org.hyperledger.besu.enclave.EnclaveIOException in project besu by hyperledger.

the class PrivacyPrecompiledContract method computePrecompile.

@Nonnull
@Override
public PrecompileContractResult computePrecompile(final Bytes input, @Nonnull final MessageFrame messageFrame) {
    if (skipContractExecution(messageFrame)) {
        return NO_RESULT;
    }
    final org.hyperledger.besu.plugin.data.Hash pmtHash = messageFrame.getContextVariable(KEY_TRANSACTION_HASH);
    final String key = input.toBase64String();
    final ReceiveResponse receiveResponse;
    try {
        receiveResponse = getReceiveResponse(key);
    } catch (final EnclaveClientException e) {
        LOG.debug("Can not fetch private transaction payload with key {}", key, e);
        return NO_RESULT;
    }
    final BytesValueRLPInput bytesValueRLPInput = new BytesValueRLPInput(Bytes.wrap(Base64.getDecoder().decode(receiveResponse.getPayload())), false);
    final PrivateTransaction privateTransaction = PrivateTransaction.readFrom(bytesValueRLPInput.readAsRlp());
    final Bytes privateFrom = privateTransaction.getPrivateFrom();
    if (!privateFromMatchesSenderKey(privateFrom, receiveResponse.getSenderKey())) {
        return NO_RESULT;
    }
    final Bytes32 privacyGroupId = Bytes32.wrap(Bytes.fromBase64String(receiveResponse.getPrivacyGroupId()));
    try {
        if (privateTransaction.getPrivateFor().isEmpty() && !enclave.retrievePrivacyGroup(privacyGroupId.toBase64String()).getMembers().contains(privateFrom.toBase64String())) {
            return NO_RESULT;
        }
    } catch (final EnclaveClientException e) {
        // This exception is thrown when the privacy group can not be found
        return NO_RESULT;
    } catch (final EnclaveServerException e) {
        throw new IllegalStateException("Enclave is responding with an error, perhaps it has a misconfiguration?", e);
    } catch (final EnclaveIOException e) {
        throw new IllegalStateException("Can not communicate with enclave, is it up?", e);
    }
    LOG.debug("Processing private transaction {} in privacy group {}", pmtHash, privacyGroupId);
    final PrivateMetadataUpdater privateMetadataUpdater = messageFrame.getContextVariable(KEY_PRIVATE_METADATA_UPDATER);
    final Hash lastRootHash = privateStateRootResolver.resolveLastStateRoot(privacyGroupId, privateMetadataUpdater);
    final MutableWorldState disposablePrivateState = privateWorldStateArchive.getMutable(fromPlugin(lastRootHash), null).get();
    final WorldUpdater privateWorldStateUpdater = disposablePrivateState.updater();
    maybeApplyGenesisToPrivateWorldState(lastRootHash, disposablePrivateState, privateWorldStateUpdater, privacyGroupId, messageFrame.getBlockValues().getNumber());
    final TransactionProcessingResult result = processPrivateTransaction(messageFrame, privateTransaction, privacyGroupId, privateWorldStateUpdater);
    if (result.isInvalid() || !result.isSuccessful()) {
        LOG.error("Failed to process private transaction {}: {}", pmtHash, result.getValidationResult().getErrorMessage());
        privateMetadataUpdater.putTransactionReceipt(pmtHash, new PrivateTransactionReceipt(result));
        return NO_RESULT;
    }
    if (messageFrame.getContextVariable(KEY_IS_PERSISTING_PRIVATE_STATE, false)) {
        privateWorldStateUpdater.commit();
        disposablePrivateState.persist(null);
        storePrivateMetadata(pmtHash, privacyGroupId, disposablePrivateState, privateMetadataUpdater, result);
    }
    return new PrecompileContractResult(result.getOutput(), true, MessageFrame.State.CODE_EXECUTING, Optional.empty());
}
Also used : PrivateTransaction(org.hyperledger.besu.ethereum.privacy.PrivateTransaction) PrivateTransactionMetadata(org.hyperledger.besu.ethereum.privacy.storage.PrivateTransactionMetadata) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) EnclaveServerException(org.hyperledger.besu.enclave.EnclaveServerException) ReceiveResponse(org.hyperledger.besu.enclave.types.ReceiveResponse) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) PrivateTransactionReceipt(org.hyperledger.besu.ethereum.privacy.PrivateTransactionReceipt) Hash(org.hyperledger.besu.plugin.data.Hash) Bytes32(org.apache.tuweni.bytes.Bytes32) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult) Hash(org.hyperledger.besu.plugin.data.Hash) Bytes(org.apache.tuweni.bytes.Bytes) EnclaveIOException(org.hyperledger.besu.enclave.EnclaveIOException) EnclaveClientException(org.hyperledger.besu.enclave.EnclaveClientException) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) PrivateMetadataUpdater(org.hyperledger.besu.ethereum.privacy.storage.PrivateMetadataUpdater) Nonnull(javax.annotation.Nonnull)

Aggregations

EnclaveClientException (org.hyperledger.besu.enclave.EnclaveClientException)2 EnclaveIOException (org.hyperledger.besu.enclave.EnclaveIOException)2 EnclaveServerException (org.hyperledger.besu.enclave.EnclaveServerException)2 Vertx (io.vertx.core.Vertx)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Nonnull (javax.annotation.Nonnull)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 Awaitility (org.awaitility.Awaitility)1 DATABASE_PATH (org.hyperledger.besu.controller.BesuController.DATABASE_PATH)1 KeyPairUtil (org.hyperledger.besu.crypto.KeyPairUtil)1 Address (org.hyperledger.besu.datatypes.Address)1