Search in sources :

Example 1 with EncryptedRawTransaction

use of com.quorum.tessera.data.EncryptedRawTransaction 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)

Example 2 with EncryptedRawTransaction

use of com.quorum.tessera.data.EncryptedRawTransaction 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)

Aggregations

ConfigConverter (com.quorum.tessera.cli.parsers.ConfigConverter)2 EncryptedRawTransaction (com.quorum.tessera.data.EncryptedRawTransaction)2 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)2 EntityManager (jakarta.persistence.EntityManager)2 Test (org.junit.Test)2 CommandLine (picocli.CommandLine)2 CliType (com.quorum.tessera.cli.CliType)1 Config (com.quorum.tessera.config.Config)1 JdbcConfig (com.quorum.tessera.config.JdbcConfig)1 JaxbUtil (com.quorum.tessera.config.util.JaxbUtil)1 MessageHash (com.quorum.tessera.data.MessageHash)1 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)1 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)1 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)1 PublicKey (com.quorum.tessera.encryption.PublicKey)1 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 Persistence (jakarta.persistence.Persistence)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Files (java.nio.file.Files)1