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;
}
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));
}
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);
}
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);
}
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);
}
Aggregations