Search in sources :

Example 26 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class BatchResendManagerImplTest method useMaxResultsWhenBatchSizeNotProvided.

@Test
public void useMaxResultsWhenBatchSizeNotProvided() {
    final ResendBatchRequest request = ResendBatchRequest.Builder.create().withPublicKey(KEY_STRING).build();
    List<EncryptedTransaction> transactions = IntStream.range(0, 5).mapToObj(i -> mock(EncryptedTransaction.class)).collect(Collectors.toUnmodifiableList());
    when(encryptedTransactionDAO.transactionCount()).thenReturn(101L);
    BatchWorkflow batchWorkflow = mock(BatchWorkflow.class);
    when(batchWorkflow.getPublishedMessageCount()).thenReturn(// arbitary total that's returned as result.getTotal()
    999L);
    when(batchWorkflowFactory.create(101L)).thenReturn(batchWorkflow);
    when(encryptedTransactionDAO.retrieveTransactions(lt(100), anyInt())).thenReturn(transactions);
    when(encryptedTransactionDAO.retrieveTransactions(gt(99), anyInt())).thenReturn(List.of(mock(EncryptedTransaction.class)));
    final ResendBatchResponse result = manager.resendBatch(request);
    assertThat(result.getTotal()).isEqualTo(999L);
    verify(batchWorkflow, times(101)).execute(any(BatchWorkflowContext.class));
    verify(encryptedTransactionDAO, times(21)).retrieveTransactions(anyInt(), anyInt());
    verify(encryptedTransactionDAO).transactionCount();
    verify(batchWorkflowFactory).create(101L);
}
Also used : AdditionalMatchers.lt(org.mockito.AdditionalMatchers.lt) ResendBatchRequest(com.quorum.tessera.recovery.resend.ResendBatchRequest) IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BatchWorkflowFactory(com.quorum.tessera.recovery.workflow.BatchWorkflowFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) Base64Codec(com.quorum.tessera.base64.Base64Codec) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) Collections.singletonList(java.util.Collections.singletonList) PushBatchRequest(com.quorum.tessera.recovery.resend.PushBatchRequest) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) After(org.junit.After) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) AdditionalMatchers.gt(org.mockito.AdditionalMatchers.gt) com.quorum.tessera.enclave(com.quorum.tessera.enclave) Nonce(com.quorum.tessera.encryption.Nonce) ServiceLoader(java.util.ServiceLoader) Test(org.junit.Test) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) List(java.util.List) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) Optional(java.util.Optional) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) ResendBatchRequest(com.quorum.tessera.recovery.resend.ResendBatchRequest) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 27 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class EncryptedTransactionDAOImpl method update.

@Override
public EncryptedTransaction update(final EncryptedTransaction entity) {
    return entityManagerTemplate.execute(entityManager -> {
        EncryptedTransaction existing = entityManager.find(EncryptedTransaction.class, entity.getHash());
        existing.setPayload(entity.getPayload());
        existing.setEncodedPayload(null);
        existing.setHash(entity.getHash());
        EncryptedTransaction merged = entityManager.merge(existing);
        LOGGER.debug("Updated transaction {}", entity.getHash());
        return merged;
    });
}
Also used : EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction)

Example 28 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class EncryptedTransactionDAOImpl method delete.

@Override
public void delete(final MessageHash hash) {
    LOGGER.info("Deleting transaction with hash {}", hash);
    entityManagerTemplate.execute(entityManager -> {
        final EncryptedTransaction message = entityManager.createNamedQuery("EncryptedTransaction.FindByHash", EncryptedTransaction.class).setParameter("hash", hash.getHashBytes()).getResultStream().findAny().orElseThrow(EntityNotFoundException::new);
        entityManager.remove(message);
        return message;
    });
}
Also used : EntityNotFoundException(jakarta.persistence.EntityNotFoundException) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction)

Example 29 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class EncryptedTransactionDAOImpl method findByHashes.

@Override
public List<EncryptedTransaction> findByHashes(Collection<MessageHash> messageHashes) {
    if (Objects.isNull(messageHashes) || messageHashes.isEmpty()) {
        return Collections.EMPTY_LIST;
    }
    return entityManagerTemplate.execute(entityManager -> {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<EncryptedTransaction> query = criteriaBuilder.createQuery(EncryptedTransaction.class);
        Root<EncryptedTransaction> root = query.from(EncryptedTransaction.class);
        return entityManager.createQuery(query.select(root).where(root.get("hash").in(messageHashes))).getResultList();
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction)

Example 30 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class MigrationTest method beforeTest.

@Before
public void beforeTest() throws IOException {
    Config primaryConfig = new Config();
    primaryConfig.setJdbcConfig(new JdbcConfig());
    primaryConfig.getJdbcConfig().setUsername("junit");
    primaryConfig.getJdbcConfig().setPassword("junit");
    String primaryJdbcUrl = "jdbc:h2:" + workDir.getRoot().toPath().resolve("primary.db").toString();
    primaryConfig.getJdbcConfig().setUrl(primaryJdbcUrl);
    Config secondaryConfig = new Config();
    secondaryConfig.setJdbcConfig(new JdbcConfig());
    secondaryConfig.getJdbcConfig().setUsername("junit");
    secondaryConfig.getJdbcConfig().setPassword("junit");
    String secondaryJdbcUrl = "jdbc:h2:" + workDir.getRoot().toPath().resolve("secondary.db").toString();
    secondaryConfig.getJdbcConfig().setUrl(secondaryJdbcUrl);
    primaryConfigPath = workDir.getRoot().toPath().toAbsolutePath().resolve("primary-confg.json");
    try (OutputStream outputStream = Files.newOutputStream(primaryConfigPath)) {
        JaxbUtil.marshalWithNoValidation(primaryConfig, outputStream);
    }
    secondaryConfigPath = workDir.getRoot().toPath().toAbsolutePath().resolve("secondary-confg.json");
    try (OutputStream outputStream = Files.newOutputStream(secondaryConfigPath)) {
        JaxbUtil.marshalWithNoValidation(secondaryConfig, outputStream);
    }
    args = List.of("--primary", primaryConfigPath.toString(), "--secondary", secondaryConfigPath.toString());
    primaryEntityManagerFactory = Optional.of(primaryConfig).map(Config::getJdbcConfig).map(JdbcConfigUtil::toMap).map(m -> new HashMap(m)).map(p -> {
        p.put("jakarta.persistence.schema-generation.database.action", "drop-and-create");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("tessera", p);
        emf.createEntityManager();
        return emf;
    }).get();
    secondaryEntityManagerFactory = Optional.of(secondaryConfig).map(Config::getJdbcConfig).map(JdbcConfigUtil::toMap).map(m -> new HashMap(m)).map(p -> {
        p.put("jakarta.persistence.schema-generation.database.action", "create");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("tessera", p);
        return emf;
    }).get();
    EntityManager secondaryEntityManager = secondaryEntityManagerFactory.createEntityManager();
    secondaryEntityManager.getTransaction().begin();
    IntStream.range(0, encryptedTransactionCount).forEach(i -> {
        EncryptedTransaction encryptedTransaction = generateEncryptedTransaction();
        secondaryEntityManager.persist(encryptedTransaction);
    });
    secondaryEntityManager.getTransaction().commit();
    secondaryEntityManager.getTransaction().begin();
    IntStream.range(0, encryptedRawTransactionCount).forEach(i -> {
        EncryptedRawTransaction encryptedRawTransaction = generateEncryptedRawTransaction();
        secondaryEntityManager.persist(encryptedRawTransaction);
    });
    secondaryEntityManager.getTransaction().commit();
}
Also used : IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) JaxbUtil(com.quorum.tessera.config.util.JaxbUtil) After(org.junit.After) MessageHash(com.quorum.tessera.data.MessageHash) Path(java.nio.file.Path) Parameterized(org.junit.runners.Parameterized) CommandLine(picocli.CommandLine) Before(org.junit.Before) OutputStream(java.io.OutputStream) EncryptedRawTransaction(com.quorum.tessera.data.EncryptedRawTransaction) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Files(java.nio.file.Files) JdbcConfig(com.quorum.tessera.config.JdbcConfig) IOException(java.io.IOException) Test(org.junit.Test) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Rule(org.junit.Rule) Persistence(jakarta.persistence.Persistence) ConfigConverter(com.quorum.tessera.cli.parsers.ConfigConverter) EntityManager(jakarta.persistence.EntityManager) Config(com.quorum.tessera.config.Config) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) CliType(com.quorum.tessera.cli.CliType) TemporaryFolder(org.junit.rules.TemporaryFolder) EntityManager(jakarta.persistence.EntityManager) JdbcConfig(com.quorum.tessera.config.JdbcConfig) Config(com.quorum.tessera.config.Config) OutputStream(java.io.OutputStream) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JdbcConfig(com.quorum.tessera.config.JdbcConfig) EncryptedRawTransaction(com.quorum.tessera.data.EncryptedRawTransaction) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Before(org.junit.Before)

Aggregations

EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)32 Test (org.junit.Test)24 PublicKey (com.quorum.tessera.encryption.PublicKey)21 MessageHash (com.quorum.tessera.data.MessageHash)19 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)13 Collectors (java.util.stream.Collectors)6 IntStream (java.util.stream.IntStream)6 ResendRequest (com.quorum.tessera.recovery.resend.ResendRequest)5 BatchWorkflow (com.quorum.tessera.recovery.workflow.BatchWorkflow)5 BatchWorkflowContext (com.quorum.tessera.recovery.workflow.BatchWorkflowContext)5 EncryptedTransactionDAO (com.quorum.tessera.data.EncryptedTransactionDAO)4 ResendResponse (com.quorum.tessera.recovery.resend.ResendResponse)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 After (org.junit.After)4 Before (org.junit.Before)4 Base64Codec (com.quorum.tessera.base64.Base64Codec)3 StagingEntityDAO (com.quorum.tessera.data.staging.StagingEntityDAO)3 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)3 com.quorum.tessera.enclave (com.quorum.tessera.enclave)3 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)3