use of org.hyperledger.besu.enclave.EnclaveServerException 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;
}
});
}
}
use of org.hyperledger.besu.enclave.EnclaveServerException 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());
}
use of org.hyperledger.besu.enclave.EnclaveServerException in project besu by hyperledger.
the class GoQuorumBlockProcessorTest method enclaveNotAvailable.
@Test
public void enclaveNotAvailable() {
final Blockchain blockchain = new ReferenceTestBlockchain();
final GoQuorumBlockProcessor blockProcessor = new GoQuorumBlockProcessor(transactionProcessor, transactionReceiptFactory, Wei.ZERO, BlockHeader::getCoinbase, false, Optional.of(goQuorumPrivacyParameters));
final MutableWorldState worldState = ReferenceTestWorldState.create(emptyMap());
final Block block = mock(Block.class);
final BlockHeader blockHeader = mock(BlockHeader.class);
final BlockBody blockBody = mock(BlockBody.class);
final Transaction transaction = mock(Transaction.class);
when(transaction.getGasLimit()).thenReturn(1000L);
when(transaction.isGoQuorumPrivateTransaction(true)).thenReturn(true);
when(transaction.getPayload()).thenReturn(Bytes.wrap(new byte[] { (byte) 1 }));
when(block.getBody()).thenReturn(blockBody);
when(blockBody.getTransactions()).thenReturn(Collections.singletonList(transaction));
when(blockBody.getOmmers()).thenReturn(Collections.emptyList());
when(blockHeader.getNumber()).thenReturn(20000L);
when(blockHeader.getGasLimit()).thenReturn(20000L);
when(block.getHeader()).thenReturn(blockHeader);
when(goQuorumEnclave.receive(any())).thenThrow(new EnclaveServerException(1, "a"));
when(transactionProcessor.getTransactionValidator()).thenReturn(transactionValidator);
when(transactionValidator.getGoQuorumCompatibilityMode()).thenReturn(true);
assertThatThrownBy(() -> blockProcessor.processBlock(blockchain, worldState, worldState, block)).isExactlyInstanceOf(EnclaveServerException.class).hasMessageContaining("a");
}
Aggregations