Search in sources :

Example 16 with Enclave

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

the class RestrictedDefaultPrivacyControllerTest method brokenMockEnclave.

private Enclave brokenMockEnclave() {
    final Enclave mockEnclave = mock(Enclave.class);
    when(mockEnclave.send(anyString(), anyString(), anyList())).thenThrow(EnclaveServerException.class);
    return mockEnclave;
}
Also used : Enclave(org.hyperledger.besu.enclave.Enclave)

Example 17 with Enclave

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

the class PrivacyClusterAcceptanceTest method aliceCanUsePrivDistributeTransaction.

@Test
public void aliceCanUsePrivDistributeTransaction() {
    // Contract address is generated from sender address and transaction nonce
    final String contractAddress = "0xebf56429e6500e84442467292183d4d621359838";
    final RawPrivateTransaction rawPrivateTransaction = RawPrivateTransaction.createContractTransaction(BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, Numeric.prependHexPrefix(EventEmitter.BINARY), Base64String.wrap(alice.getEnclaveKey()), Collections.singletonList(Base64String.wrap(bob.getEnclaveKey())), RESTRICTED);
    final String signedPrivateTransaction = Numeric.toHexString(PrivateTransactionEncoder.signMessage(rawPrivateTransaction, Credentials.create(alice.getTransactionSigningKey())));
    final String transactionKey = alice.execute(privacyTransactions.privDistributeTransaction(signedPrivateTransaction));
    final Enclave aliceEnclave = enclaveFactory.createVertxEnclave(alice.getEnclave().clientUrl());
    final ReceiveResponse aliceRR = aliceEnclave.receive(Bytes.fromHexString(transactionKey).toBase64String(), alice.getEnclaveKey());
    final Enclave bobEnclave = enclaveFactory.createVertxEnclave(bob.getEnclave().clientUrl());
    final ReceiveResponse bobRR = bobEnclave.receive(Bytes.fromHexString(transactionKey).toBase64String(), bob.getEnclaveKey());
    assertThat(bobRR).usingRecursiveComparison().isEqualTo(aliceRR);
    final RawTransaction pmt = RawTransaction.createTransaction(BigInteger.ZERO, BigInteger.valueOf(1000), BigInteger.valueOf(65000), DEFAULT_PRIVACY.toString(), transactionKey);
    final String signedPmt = Numeric.toHexString(TransactionEncoder.signMessage(pmt, Credentials.create(alice.getTransactionSigningKey())));
    final String transactionHash = alice.execute(ethTransactions.sendRawTransaction(signedPmt));
    final PrivateTransactionReceipt expectedReceipt = new PrivateTransactionReceipt(contractAddress, "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73", null, // ignored in the following call, checked separately below
    null, Collections.emptyList(), "0x023955c49d6265c579561940287449242704d5fd239ff07ea36a3fc7aface61c", "0x82e521ee16ff13104c5f81e8354ecaaafd5450b710b07f620204032bfe76041a", "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=", new ArrayList<>(Collections.singletonList("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=")), "DyAOiF/ynpc+JXa2YAGB0bCitSlOMNm+ShmB/7M6C4w=", "0x1", null);
    alice.verify(privateTransactionVerifier.validPrivateTransactionReceipt(transactionHash, expectedReceipt, true));
    final PrivateTransactionReceipt alicePrivateTransactionReceipt = alice.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash));
    assertThat(EventEmitter.BINARY).contains(alicePrivateTransactionReceipt.getOutput().substring(2));
    bob.verify(privateTransactionVerifier.validPrivateTransactionReceipt(transactionHash, expectedReceipt, true));
    final PrivateTransactionReceipt bobPrivateTransactionReceipt = bob.execute(privacyTransactions.getPrivateTransactionReceipt(transactionHash));
    assertThat(EventEmitter.BINARY).contains(bobPrivateTransactionReceipt.getOutput().substring(2));
}
Also used : Enclave(org.hyperledger.besu.enclave.Enclave) ReceiveResponse(org.hyperledger.besu.enclave.types.ReceiveResponse) PrivateTransactionReceipt(org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt) RawTransaction(org.web3j.crypto.RawTransaction) Base64String(org.web3j.utils.Base64String) RawPrivateTransaction(org.web3j.protocol.eea.crypto.RawPrivateTransaction) Test(org.junit.Test)

Example 18 with Enclave

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

the class FlexiblePrivacyPrecompiledContractTest method assertThatComputeReturnsEmptyGivenContractMembershipQueryReturns.

private void assertThatComputeReturnsEmptyGivenContractMembershipQueryReturns(final Bytes memberList) {
    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, memberList, null)));
    final FlexiblePrivacyPrecompiledContract contractSpy = spy(contract);
    Mockito.doReturn(false).when(contractSpy).isContractLocked(any(), any());
    Mockito.doReturn(true).when(contractSpy).flexiblePrivacyGroupVersionMatches(any(), any(), any());
    final VersionedPrivateTransaction privateTransaction = versionedPrivateTransactionBesu();
    final byte[] payload = convertVersionedPrivateTransactionToBytes(privateTransaction);
    final String privateFrom = privateTransaction.getPrivateTransaction().getPrivateFrom().toBase64String();
    final ReceiveResponse response = new ReceiveResponse(payload, PAYLOAD_TEST_PRIVACY_GROUP_ID, privateFrom);
    when(enclave.receive(any(String.class))).thenReturn(response);
    final PrecompiledContract.PrecompileContractResult result = contractSpy.computePrecompile(privateTransactionLookupId, messageFrame);
    final Bytes actual = result.getOutput();
    assertThat(actual).isEqualTo(Bytes.EMPTY);
}
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)

Example 19 with Enclave

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

the class FlexiblePrivacyPrecompiledContractTest method testPayloadNotMatchingPrivateFrom.

@Test
public void testPayloadNotMatchingPrivateFrom() {
    final Enclave enclave = mock(Enclave.class);
    final FlexiblePrivacyPrecompiledContract contract = buildPrivacyPrecompiledContract(enclave);
    final VersionedPrivateTransaction versionedPrivateTransaction = versionedPrivateTransactionBesu();
    final byte[] payload = convertVersionedPrivateTransactionToBytes(versionedPrivateTransaction);
    final String wrongSenderKey = Bytes.random(32).toBase64String();
    final ReceiveResponse responseWithWrongSenderKey = new ReceiveResponse(payload, PAYLOAD_TEST_PRIVACY_GROUP_ID, wrongSenderKey);
    when(enclave.receive(eq(privateTransactionLookupId.toBase64String()))).thenReturn(responseWithWrongSenderKey);
    final PrecompiledContract.PrecompileContractResult result = contract.computePrecompile(privateTransactionLookupId, messageFrame);
    final Bytes actual = result.getOutput();
    assertThat(actual).isEqualTo(Bytes.EMPTY);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PrecompiledContract(org.hyperledger.besu.evm.precompile.PrecompiledContract) 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 20 with Enclave

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

the class FlexiblePrivacyPrecompiledContractTest method testInvalidPrivateTransaction.

@Test
public void testInvalidPrivateTransaction() {
    final Enclave enclave = mock(Enclave.class);
    final FlexiblePrivacyPrecompiledContract contract = new FlexiblePrivacyPrecompiledContract(new SpuriousDragonGasCalculator(), enclave, worldStateArchive, privateStateRootResolver, privateStateGenesisAllocator);
    contract.setPrivateTransactionProcessor(mockPrivateTxProcessor(TransactionProcessingResult.invalid(ValidationResult.invalid(TransactionInvalidReason.INCORRECT_NONCE))));
    final FlexiblePrivacyPrecompiledContract contractSpy = spy(contract);
    Mockito.doReturn(true).when(contractSpy).canExecute(any(), any(), any(), any(), any(), any(), any(), any());
    final VersionedPrivateTransaction privateTransaction = versionedPrivateTransactionBesu();
    final byte[] payload = convertVersionedPrivateTransactionToBytes(privateTransaction);
    final String privateFrom = privateTransaction.getPrivateTransaction().getPrivateFrom().toBase64String();
    final ReceiveResponse response = new ReceiveResponse(payload, PAYLOAD_TEST_PRIVACY_GROUP_ID, privateFrom);
    when(enclave.receive(any(String.class))).thenReturn(response);
    final PrecompiledContract.PrecompileContractResult result = contractSpy.computePrecompile(privateTransactionLookupId, messageFrame);
    final Bytes actual = result.getOutput();
    assertThat(actual).isEqualTo(Bytes.EMPTY);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PrecompiledContract(org.hyperledger.besu.evm.precompile.PrecompiledContract) SpuriousDragonGasCalculator(org.hyperledger.besu.evm.gascalculator.SpuriousDragonGasCalculator) Enclave(org.hyperledger.besu.enclave.Enclave) ReceiveResponse(org.hyperledger.besu.enclave.types.ReceiveResponse) VersionedPrivateTransaction(org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction) Test(org.junit.Test)

Aggregations

Enclave (org.hyperledger.besu.enclave.Enclave)26 Test (org.junit.Test)20 Bytes (org.apache.tuweni.bytes.Bytes)17 PrecompiledContract (org.hyperledger.besu.evm.precompile.PrecompiledContract)16 ReceiveResponse (org.hyperledger.besu.enclave.types.ReceiveResponse)14 PrivateTransaction (org.hyperledger.besu.ethereum.privacy.PrivateTransaction)7 VersionedPrivateTransaction (org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction)7 ArrayList (java.util.ArrayList)5 Log (org.hyperledger.besu.evm.log.Log)5 Collections (java.util.Collections)3 PrivacyGroup (org.hyperledger.besu.enclave.types.PrivacyGroup)3 Vertx (io.vertx.core.Vertx)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 List (java.util.List)2 Optional (java.util.Optional)2 Address (org.hyperledger.besu.datatypes.Address)2 EnclaveConfigurationException (org.hyperledger.besu.enclave.EnclaveConfigurationException)2 EnclaveFactory (org.hyperledger.besu.enclave.EnclaveFactory)2 PrivateStateGenesisAllocator (org.hyperledger.besu.ethereum.privacy.PrivateStateGenesisAllocator)2