Search in sources :

Example 31 with EncryptedTransaction

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

the class MigrationTest method doMigration.

@Test
public void doMigration() {
    MigrationCliAdapter migrationCommand = new MigrationCliAdapter();
    assertThat(migrationCommand.getType()).isEqualTo(CliType.MULTITENANCY_MIGRATION);
    final CommandLine commandLine = new CommandLine(migrationCommand);
    commandLine.registerConverter(Config.class, new ConfigConverter()).setSeparator(" ").setCaseInsensitiveEnumValuesAllowed(true);
    int exitCode = commandLine.execute(args.toArray(String[]::new));
    assertThat(exitCode).isZero();
    EntityManager secondaryEntityManager = secondaryEntityManagerFactory.createEntityManager();
    EntityManager primaryEntityManager = primaryEntityManagerFactory.createEntityManager();
    secondaryEntityManager.getTransaction().begin();
    primaryEntityManager.getTransaction().begin();
    secondaryEntityManager.createQuery("select count(e) from EncryptedTransaction e", Long.class).getResultStream().findFirst().ifPresent(count -> assertThat(count).isEqualTo(encryptedTransactionCount));
    primaryEntityManager.createQuery("select count(e) from EncryptedTransaction e", Long.class).getResultStream().findFirst().ifPresent(count -> assertThat(count).isEqualTo(encryptedTransactionCount));
    secondaryEntityManager.createQuery("select count(e) from EncryptedRawTransaction e", Long.class).getResultStream().findFirst().ifPresent(count -> assertThat(count).isEqualTo(encryptedRawTransactionCount));
    primaryEntityManager.createQuery("select count(e) from EncryptedRawTransaction e", Long.class).getResultStream().findFirst().ifPresent(count -> assertThat(count).isEqualTo(encryptedRawTransactionCount));
    secondaryEntityManager.createQuery("select e from EncryptedTransaction e", EncryptedTransaction.class).getResultStream().forEach(e -> {
        EncryptedTransaction copiedEncryptedTransaction = primaryEntityManager.find(EncryptedTransaction.class, e.getHash());
        assertThat(copiedEncryptedTransaction).isNotNull();
        assertThat(copiedEncryptedTransaction.getEncodedPayload()).isEqualTo(e.getEncodedPayload());
    });
    secondaryEntityManager.createQuery("select e from EncryptedRawTransaction e", EncryptedRawTransaction.class).getResultStream().forEach(e -> {
        EncryptedRawTransaction copiedEncryptedRawTransaction = primaryEntityManager.find(EncryptedRawTransaction.class, e.getHash());
        assertThat(copiedEncryptedRawTransaction).isNotNull();
        assertThat(copiedEncryptedRawTransaction.getEncryptedKey()).isEqualTo(e.getEncryptedKey());
        assertThat(copiedEncryptedRawTransaction.getEncryptedPayload()).isEqualTo(e.getEncryptedPayload());
        assertThat(copiedEncryptedRawTransaction.getSender()).isEqualTo(e.getSender());
        assertThat(copiedEncryptedRawTransaction.getNonce()).isEqualTo(e.getNonce());
    });
    secondaryEntityManager.getTransaction().rollback();
    primaryEntityManager.getTransaction().rollback();
    assertThat(commandLine.execute(args.toArray(String[]::new))).describedAs("Rerunning should throw no errors as there are exist checks before insert").isZero();
    primaryEntityManager.createQuery("select count(e) from EncryptedTransaction e", Long.class).getResultStream().findFirst().ifPresent(count -> assertThat(count).isEqualTo(encryptedTransactionCount));
    secondaryEntityManager.createQuery("select count(e) from EncryptedRawTransaction e", Long.class).getResultStream().findFirst().ifPresent(count -> assertThat(count).isEqualTo(encryptedRawTransactionCount));
}
Also used : ConfigConverter(com.quorum.tessera.cli.parsers.ConfigConverter) CommandLine(picocli.CommandLine) EntityManager(jakarta.persistence.EntityManager) EncryptedRawTransaction(com.quorum.tessera.data.EncryptedRawTransaction) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 32 with EncryptedTransaction

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

the class EncryptedTransactionMigrator method migrate.

public void migrate() {
    final long secondaryTxCount = secondaryEntityManager.createQuery("select count(e) from EncryptedTransaction e", Long.class).getSingleResult();
    final int batchCount = calculateBatchCount(maxBatchSize, secondaryTxCount);
    IntStream.range(0, batchCount).map(i -> i * maxBatchSize).mapToObj(offset -> secondaryEntityManager.createNamedQuery("EncryptedTransaction.FindAll", EncryptedTransaction.class).setFirstResult(offset).setMaxResults(maxBatchSize)).flatMap(TypedQuery::getResultStream).forEach(et -> {
        final Optional<EncryptedTransaction> existing = primaryEntityManager.createNamedQuery("EncryptedTransaction.FindByHash", EncryptedTransaction.class).setParameter("hash", et.getHash().getHashBytes()).getResultStream().findAny();
        if (existing.isEmpty()) {
            primaryEntityManager.getTransaction().begin();
            primaryEntityManager.persist(et);
            primaryEntityManager.getTransaction().commit();
            return;
        }
        final EncryptedTransaction outerTx = existing.get();
        final EncodedPayload primaryTx = outerTx.getPayload();
        final EncodedPayload secondaryTx = et.getPayload();
        final EncodedPayload updatedPayload = this.handleSingleTransaction(primaryTx, secondaryTx);
        outerTx.setPayload(updatedPayload);
        primaryEntityManager.getTransaction().begin();
        primaryEntityManager.merge(outerTx);
        primaryEntityManager.getTransaction().commit();
    });
}
Also used : IntStream(java.util.stream.IntStream) SecurityHash(com.quorum.tessera.enclave.SecurityHash) PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) TypedQuery(jakarta.persistence.TypedQuery) EntityManager(jakarta.persistence.EntityManager) TxHash(com.quorum.tessera.enclave.TxHash) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Collectors(java.util.stream.Collectors) RecipientBox(com.quorum.tessera.enclave.RecipientBox) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction)

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