Search in sources :

Example 1 with VersionedPrivateTransaction

use of org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction in project besu by hyperledger.

the class FlexiblePrivacyPrecompiledContractTest method testPayloadFoundInEnclave.

@Test
public void testPayloadFoundInEnclave() {
    final Enclave enclave = mock(Enclave.class);
    final FlexiblePrivacyPrecompiledContract contract = buildPrivacyPrecompiledContract(enclave);
    final List<Log> logs = new ArrayList<>();
    contract.setPrivateTransactionProcessor(mockPrivateTxProcessor(TransactionProcessingResult.successful(logs, 0, 0, Bytes.fromHexString(DEFAULT_OUTPUT), null)));
    final VersionedPrivateTransaction versionedPrivateTransaction = versionedPrivateTransactionBesu();
    final byte[] payload = convertVersionedPrivateTransactionToBytes(versionedPrivateTransaction);
    final String privateFrom = versionedPrivateTransaction.getPrivateTransaction().getPrivateFrom().toBase64String();
    final ReceiveResponse response = new ReceiveResponse(payload, PAYLOAD_TEST_PRIVACY_GROUP_ID, privateFrom);
    when(enclave.receive(any())).thenReturn(response);
    final FlexiblePrivacyPrecompiledContract contractSpy = spy(contract);
    Mockito.doReturn(true).when(contractSpy).canExecute(any(), any(), any(), any(), any(), any(), any(), any());
    final PrecompiledContract.PrecompileContractResult result = contractSpy.computePrecompile(privateTransactionLookupId, messageFrame);
    final Bytes actual = result.getOutput();
    assertThat(actual).isEqualTo(Bytes.fromHexString(DEFAULT_OUTPUT));
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PrecompiledContract(org.hyperledger.besu.evm.precompile.PrecompiledContract) Log(org.hyperledger.besu.evm.log.Log) Enclave(org.hyperledger.besu.enclave.Enclave) ReceiveResponse(org.hyperledger.besu.enclave.types.ReceiveResponse) ArrayList(java.util.ArrayList) VersionedPrivateTransaction(org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction) Test(org.junit.Test)

Example 2 with VersionedPrivateTransaction

use of org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction in project besu by hyperledger.

the class FlexiblePrivacyPrecompiledContractTest method testEnclaveBelowRequiredVersion.

@Test
public void testEnclaveBelowRequiredVersion() {
    final Enclave enclave = mock(Enclave.class);
    final FlexiblePrivacyPrecompiledContract contract = buildPrivacyPrecompiledContract(enclave);
    final VersionedPrivateTransaction versionedPrivateTransaction = versionedPrivateTransactionBesu();
    final byte[] payload = convertVersionedPrivateTransactionToBytes(versionedPrivateTransaction);
    final ReceiveResponse responseWithoutSenderKey = new ReceiveResponse(payload, PAYLOAD_TEST_PRIVACY_GROUP_ID, null);
    when(enclave.receive(eq(privateTransactionLookupId.toBase64String()))).thenReturn(responseWithoutSenderKey);
    assertThatThrownBy(() -> contract.computePrecompile(privateTransactionLookupId, messageFrame)).isInstanceOf(EnclaveConfigurationException.class).hasMessage("Incompatible Orion version. Orion version must be 1.6.0 or greater.");
}
Also used : EnclaveConfigurationException(org.hyperledger.besu.enclave.EnclaveConfigurationException) Enclave(org.hyperledger.besu.enclave.Enclave) ReceiveResponse(org.hyperledger.besu.enclave.types.ReceiveResponse) VersionedPrivateTransaction(org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction) Test(org.junit.Test)

Example 3 with VersionedPrivateTransaction

use of org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction in project besu by hyperledger.

the class FlexiblePrivacyPrecompiledContractTest method testPayloadFoundInEnclaveWith32ByteResult.

@Test
public void testPayloadFoundInEnclaveWith32ByteResult() {
    final Enclave enclave = mock(Enclave.class);
    final FlexiblePrivacyPrecompiledContract contract = buildPrivacyPrecompiledContract(enclave);
    final List<Log> logs = new ArrayList<>();
    contract.setPrivateTransactionProcessor(mockPrivateTxProcessor(TransactionProcessingResult.successful(logs, 0, 0, Bytes.fromHexString(DEFAULT_OUTPUT), null)));
    final VersionedPrivateTransaction versionedPrivateTransaction = versionedPrivateTransactionBesu();
    final byte[] payload = convertVersionedPrivateTransactionToBytes(versionedPrivateTransaction);
    final String privateFrom = versionedPrivateTransaction.getPrivateTransaction().getPrivateFrom().toBase64String();
    final ReceiveResponse response = new ReceiveResponse(payload, PAYLOAD_TEST_PRIVACY_GROUP_ID, privateFrom);
    when(enclave.receive(any())).thenReturn(response);
    final FlexiblePrivacyPrecompiledContract contractSpy = spy(contract);
    Mockito.doReturn(true).when(contractSpy).canExecute(any(), any(), any(), any(), any(), any(), any(), any());
    final PrecompiledContract.PrecompileContractResult result = contractSpy.computePrecompile(privateTransactionLookupId, messageFrame);
    final Bytes actual = result.getOutput();
    assertThat(actual).isEqualTo(Bytes.fromHexString(DEFAULT_OUTPUT));
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PrecompiledContract(org.hyperledger.besu.evm.precompile.PrecompiledContract) Log(org.hyperledger.besu.evm.log.Log) Enclave(org.hyperledger.besu.enclave.Enclave) ReceiveResponse(org.hyperledger.besu.enclave.types.ReceiveResponse) ArrayList(java.util.ArrayList) VersionedPrivateTransaction(org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction) Test(org.junit.Test)

Example 4 with VersionedPrivateTransaction

use of org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction in project besu by hyperledger.

the class FlexiblePrivacyPrecompiledContract method computePrecompile.

@Nonnull
@Override
public PrecompileContractResult computePrecompile(final Bytes input, @Nonnull final MessageFrame messageFrame) {
    if (skipContractExecution(messageFrame)) {
        return NO_RESULT;
    }
    if (input == null || (input.size() != 32 && input.size() != 64)) {
        LOG.error("Can not fetch private transaction payload with key of invalid length {}", input);
        return NO_RESULT;
    }
    final Hash pmtHash = messageFrame.getContextVariable(KEY_TRANSACTION_HASH);
    final String key = input.slice(0, 32).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 VersionedPrivateTransaction versionedPrivateTransaction = VersionedPrivateTransaction.readFrom(bytesValueRLPInput);
    final PrivateTransaction privateTransaction = versionedPrivateTransaction.getPrivateTransaction();
    final Bytes privateFrom = privateTransaction.getPrivateFrom();
    if (!privateFromMatchesSenderKey(privateFrom, receiveResponse.getSenderKey())) {
        return NO_RESULT;
    }
    final Optional<Bytes> maybeGroupId = privateTransaction.getPrivacyGroupId();
    if (maybeGroupId.isEmpty()) {
        return NO_RESULT;
    }
    final Bytes32 privacyGroupId = Bytes32.wrap(maybeGroupId.get());
    LOG.debug("Processing private transaction {} in privacy group {}", pmtHash, privacyGroupId);
    final ProcessableBlockHeader currentBlockHeader = (ProcessableBlockHeader) messageFrame.getBlockValues();
    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, currentBlockHeader.getNumber());
    if (!canExecute(messageFrame, currentBlockHeader, privateTransaction, versionedPrivateTransaction.getVersion(), privacyGroupId, disposablePrivateState, privateWorldStateUpdater, privateFrom)) {
        return NO_RESULT;
    }
    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;
    }
    sendParticipantRemovedEvent(privateTransaction);
    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) VersionedPrivateTransaction(org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) 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) VersionedPrivateTransaction(org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction) Bytes32(org.apache.tuweni.bytes.Bytes32) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult) Bytes(org.apache.tuweni.bytes.Bytes) ProcessableBlockHeader(org.hyperledger.besu.ethereum.core.ProcessableBlockHeader) 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)

Example 5 with VersionedPrivateTransaction

use of org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction in project besu by hyperledger.

the class PrivateTransactionDataFixture method encodePrivateTransaction.

public static Bytes encodePrivateTransaction(final PrivateTransaction privateTransaction, final Optional<Bytes32> version) {
    final BytesValueRLPOutput output = new BytesValueRLPOutput();
    if (version.isEmpty()) {
        privateTransaction.writeTo(output);
    } else {
        final VersionedPrivateTransaction versionedPrivateTransaction = new VersionedPrivateTransaction(privateTransaction, Bytes32.ZERO);
        versionedPrivateTransaction.writeTo(output);
    }
    return output.encoded();
}
Also used : BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) VersionedPrivateTransaction(org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction)

Aggregations

VersionedPrivateTransaction (org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction)10 ReceiveResponse (org.hyperledger.besu.enclave.types.ReceiveResponse)9 Bytes (org.apache.tuweni.bytes.Bytes)7 Enclave (org.hyperledger.besu.enclave.Enclave)7 PrecompiledContract (org.hyperledger.besu.evm.precompile.PrecompiledContract)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 Log (org.hyperledger.besu.evm.log.Log)4 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)2 Nonnull (javax.annotation.Nonnull)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 EnclaveClientException (org.hyperledger.besu.enclave.EnclaveClientException)1 EnclaveConfigurationException (org.hyperledger.besu.enclave.EnclaveConfigurationException)1 MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)1 ProcessableBlockHeader (org.hyperledger.besu.ethereum.core.ProcessableBlockHeader)1 PrivateTransaction (org.hyperledger.besu.ethereum.privacy.PrivateTransaction)1 PrivateTransactionReceipt (org.hyperledger.besu.ethereum.privacy.PrivateTransactionReceipt)1 PrivateMetadataUpdater (org.hyperledger.besu.ethereum.privacy.storage.PrivateMetadataUpdater)1 TransactionProcessingResult (org.hyperledger.besu.ethereum.processing.TransactionProcessingResult)1 BytesValueRLPInput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput)1