Search in sources :

Example 61 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class FilterPayload method filter.

@Override
public boolean filter(BatchWorkflowContext context) {
    EncodedPayload encodedPayload = context.getEncodedPayload();
    PublicKey recipientPublicKey = context.getRecipientKey();
    final boolean isCurrentNodeSender = encodedPayload.getRecipientKeys().contains(recipientPublicKey) && enclave.getPublicKeys().contains(encodedPayload.getSenderKey());
    final boolean isRequestedNodeSender = Objects.equals(encodedPayload.getSenderKey(), recipientPublicKey);
    return isCurrentNodeSender || isRequestedNodeSender;
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 62 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class EncryptedTransactionDAOTest method saveTransactionWithCallbackException.

@Test
public void saveTransactionWithCallbackException() throws Exception {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    byte[] payloadData = "payloadData".getBytes();
    when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
    when(payloadEncoder.decode(payloadData)).thenReturn(encodedPayload);
    try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
        createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
        MessageHash transactionHash = new MessageHash(UUID.randomUUID().toString().getBytes());
        EncryptedTransaction transaction = new EncryptedTransaction();
        transaction.setPayload(encodedPayload);
        transaction.setHash(transactionHash);
        Callable<Void> callback = mock(Callable.class);
        when(callback.call()).thenThrow(new Exception("OUCH"));
        try {
            encryptedTransactionDAO.save(transaction, callback);
            failBecauseExceptionWasNotThrown(PersistenceException.class);
        } catch (PersistenceException ex) {
            assertThat(ex).isNotNull().hasMessageContaining("OUCH");
        }
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        final EncryptedTransaction result = entityManager.find(EncryptedTransaction.class, transactionHash);
        assertThat(result).isNull();
        verify(callback).call();
    }
    verify(payloadEncoder).encode(encodedPayload);
    verifyNoMoreInteractions(payloadEncoder);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 63 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class EncryptedTransactionDAOTest method saveDoesntAllowNullEncodedPayload.

@Test
public void saveDoesntAllowNullEncodedPayload() {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(payloadEncoder.encode(encodedPayload)).thenReturn(null);
    try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
        createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
        EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
        encryptedTransaction.setHash(new MessageHash(new byte[] { 5 }));
        encryptedTransaction.setPayload(encodedPayload);
        try {
            encryptedTransactionDAO.save(encryptedTransaction);
            failBecauseExceptionWasNotThrown(PersistenceException.class);
        } catch (PersistenceException ex) {
            String expectedMessage = String.format(testConfig.getRequiredFieldColumnTemplate(), "ENCODED_PAYLOAD");
            assertThat(ex).isInstanceOf(PersistenceException.class).hasMessageContaining(expectedMessage).hasMessageContaining("ENCODED_PAYLOAD");
        }
    }
    verify(payloadEncoder).encode(encodedPayload);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 64 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class EncryptedTransactionDAOTest method findByHashes.

@Test
public void findByHashes() {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    final byte[] payloadData = "PAYLOADATA".getBytes();
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
    try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
        createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        List<EncryptedTransaction> transactions = IntStream.range(0, 100).mapToObj(i -> UUID.randomUUID().toString().getBytes()).map(MessageHash::new).map(h -> {
            EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
            encryptedTransaction.setHash(h);
            encryptedTransaction.setPayload(encodedPayload);
            entityManager.persist(encryptedTransaction);
            return encryptedTransaction;
        }).collect(Collectors.toList());
        entityManager.getTransaction().commit();
        Collection<MessageHash> hashes = transactions.stream().map(EncryptedTransaction::getHash).collect(Collectors.toList());
        List<EncryptedTransaction> results = encryptedTransactionDAO.findByHashes(hashes);
        assertThat(results).isNotEmpty().containsExactlyInAnyOrderElementsOf(transactions);
        assertThat(results.stream().allMatch(r -> Arrays.equals(r.getEncodedPayload(), payloadData))).isTrue();
    }
    verify(payloadEncoder, times(100)).encode(encodedPayload);
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) jakarta.persistence(jakarta.persistence) RunWith(org.junit.runner.RunWith) Callable(java.util.concurrent.Callable) Mockito.when(org.mockito.Mockito.when) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) com.quorum.tessera.data(com.quorum.tessera.data) org.junit(org.junit) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) Parameterized(org.junit.runners.Parameterized) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 65 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class EncryptedTransactionDAOTest method persistAddsTimestampToEntity.

@Test
public void persistAddsTimestampToEntity() {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    final byte[] payloadData = "PAYLOADATA".getBytes();
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
    when(payloadEncoder.decode(payloadData)).thenReturn(encodedPayload);
    try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
        createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
        MessageHash messageHash = new MessageHash(UUID.randomUUID().toString().getBytes());
        final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
        encryptedTransaction.setPayload(encodedPayload);
        encryptedTransaction.setHash(messageHash);
        encryptedTransactionDAO.save(encryptedTransaction);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        final EncryptedTransaction retrieved = entityManager.find(EncryptedTransaction.class, messageHash);
        entityManager.getTransaction().commit();
        assertThat(retrieved).isNotNull();
        assertThat(retrieved.getTimestamp()).isNotZero().isGreaterThan(0L);
    }
    verify(payloadEncoder).decode(payloadData);
    verify(payloadEncoder).encode(encodedPayload);
    verifyNoMoreInteractions(payloadEncoder);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Aggregations

EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)91 Test (org.junit.Test)60 PublicKey (com.quorum.tessera.encryption.PublicKey)50 PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)23 Response (jakarta.ws.rs.core.Response)20 MessageHash (com.quorum.tessera.data.MessageHash)13 Collectors (java.util.stream.Collectors)12 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)11 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)11 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)9 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)8 Recipient (com.quorum.tessera.partyinfo.node.Recipient)8 java.util (java.util)8 Invocation (jakarta.ws.rs.client.Invocation)6 WebTarget (jakarta.ws.rs.client.WebTarget)6 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)5 PrivacyMetadata (com.quorum.tessera.enclave.PrivacyMetadata)5 RecipientBox (com.quorum.tessera.enclave.RecipientBox)5 TxHash (com.quorum.tessera.enclave.TxHash)5 ResendRequest (com.quorum.tessera.p2p.resend.ResendRequest)5