Search in sources :

Example 6 with StagingTransaction

use of com.quorum.tessera.data.staging.StagingTransaction in project tessera by ConsenSys.

the class RecoveryImplTest method testSyncFailed.

@Test
public void testSyncFailed() {
    StagingTransaction version1 = mock(StagingTransaction.class);
    StagingTransaction version2 = mock(StagingTransaction.class);
    when(version1.getHash()).thenReturn("TXN1");
    when(version2.getHash()).thenReturn("TXN1");
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    EncodedPayload encodedPayload2 = mock(EncodedPayload.class);
    when(version1.getEncodedPayload()).thenReturn(encodedPayload);
    when(version2.getEncodedPayload()).thenReturn(encodedPayload2);
    List<StagingTransaction> stagingTransactions = List.of(version1, version2);
    when(stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(anyInt(), anyInt())).thenReturn(stagingTransactions);
    when(stagingEntityDAO.countAll()).thenReturn((long) stagingTransactions.size());
    when(transactionManager.storePayload(any())).thenThrow(PrivacyViolationException.class);
    RecoveryResult result = recovery.sync();
    assertThat(result).isEqualTo(RecoveryResult.FAILURE);
    verify(stagingEntityDAO).retrieveTransactionBatchOrderByStageAndHash(anyInt(), anyInt());
    verify(stagingEntityDAO, times(2)).countAll();
    verify(transactionManager, times(2)).storePayload(any());
}
Also used : StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) RecoveryResult(com.quorum.tessera.recovery.RecoveryResult) Test(org.junit.Test)

Example 7 with StagingTransaction

use of com.quorum.tessera.data.staging.StagingTransaction in project tessera by ConsenSys.

the class RecoveryImplTest method testSyncSuccess.

@Test
public void testSyncSuccess() {
    StagingTransaction version1 = mock(StagingTransaction.class);
    StagingTransaction version2 = mock(StagingTransaction.class);
    when(version1.getHash()).thenReturn("TXN1");
    when(version2.getHash()).thenReturn("TXN1");
    EncodedPayload firstPayload = mock(EncodedPayload.class);
    EncodedPayload secondPayload = mock(EncodedPayload.class);
    when(version1.getEncodedPayload()).thenReturn(firstPayload);
    when(version2.getEncodedPayload()).thenReturn(secondPayload);
    when(stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(anyInt(), anyInt())).thenReturn(List.of(version1, version2));
    when(stagingEntityDAO.countAll()).thenReturn(2L);
    when(transactionManager.storePayload(any())).thenReturn(new MessageHash("hash".getBytes()));
    RecoveryResult result = recovery.sync();
    assertThat(result).isEqualTo(RecoveryResult.SUCCESS);
    verify(stagingEntityDAO).retrieveTransactionBatchOrderByStageAndHash(anyInt(), anyInt());
    verify(stagingEntityDAO, times(2)).countAll();
    verify(transactionManager).storePayload(firstPayload);
    verify(transactionManager).storePayload(secondPayload);
}
Also used : StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) RecoveryResult(com.quorum.tessera.recovery.RecoveryResult) Test(org.junit.Test)

Example 8 with StagingTransaction

use of com.quorum.tessera.data.staging.StagingTransaction in project tessera by ConsenSys.

the class StagingEntityDAOTest method testUpdate.

@Test
public void testUpdate() {
    final String txnHash7 = transactions.get("TXN7").getHash();
    final Optional<StagingTransaction> stagingTransaction = stagingEntityDAO.retrieveByHash(txnHash7);
    assertThat(stagingTransaction).isPresent();
    StagingTransaction st = stagingTransaction.get();
    st.setValidationStage(123L);
    stagingEntityDAO.update(st);
    final Optional<StagingTransaction> stagingTransactionAfterUpdate = stagingEntityDAO.retrieveByHash(txnHash7);
    assertThat(stagingTransactionAfterUpdate).isPresent();
    assertThat(stagingTransactionAfterUpdate.get().getValidationStage()).isEqualTo(123L);
}
Also used : StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Test(org.junit.Test)

Example 9 with StagingTransaction

use of com.quorum.tessera.data.staging.StagingTransaction in project tessera by ConsenSys.

the class StagingEntityDAOTest method paginationCanCauseDifferentStagingValueButOrderShouldBeMaintained.

@Test
public void paginationCanCauseDifferentStagingValueButOrderShouldBeMaintained() {
    final List<StagingTransaction> preStaging = stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(0, Integer.MAX_VALUE);
    final AtomicLong stage = new AtomicLong(0);
    final int batchSize = 1;
    assertThat(preStaging.size()).isEqualTo(7);
    preStaging.forEach(stagingTransaction -> {
        assertThat(stagingTransaction.getValidationStage()).isNull();
    });
    while (stagingEntityDAO.updateStageForBatch(batchSize, stage.incrementAndGet()) != 0) {
    }
    final List<StagingTransaction> verifiedTransactions = stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(0, Integer.MAX_VALUE);
    // Order increase by one due to pagination
    assertThat(verifiedTransactions.get(0).getValidationStage()).isEqualTo(1L);
    assertThat(verifiedTransactions.get(1).getValidationStage()).isEqualTo(2L);
    assertThat(verifiedTransactions.get(2).getValidationStage()).isEqualTo(3L);
    assertThat(verifiedTransactions.get(3).getValidationStage()).isEqualTo(4L);
    assertThat(verifiedTransactions.get(4).getValidationStage()).isEqualTo(5L);
    assertThat(verifiedTransactions.get(5).getValidationStage()).isEqualTo(6L);
    assertThat(verifiedTransactions.get(6).getValidationStage()).isNull();
    final List<String> possibleOrdering = Arrays.asList("1,21,22,3,4,7,5", "1,3,21,22,4,7,5", "1,3,4,21,22,7,5", "1,3,4,7,21,22,5");
    final String order = verifiedTransactions.stream().map(StagingTransaction::getId).map(String::valueOf).collect(Collectors.joining(","));
    assertThat(possibleOrdering.contains(order));
    final List<StagingTransaction> allTransactions = stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(0, Integer.MAX_VALUE);
    assertThat(allTransactions.stream().filter(et -> et.getValidationStage() == null).count()).isEqualTo(1);
    assertThat(stagingEntityDAO.countAll()).isEqualTo(7);
    assertThat(stagingEntityDAO.countStaged()).isEqualTo(6);
    assertThat(stagingEntityDAO.countAllAffected()).isEqualTo(7);
}
Also used : java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StagingAffectedTransaction(com.quorum.tessera.data.staging.StagingAffectedTransaction) RunWith(org.junit.runner.RunWith) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) After(org.junit.After) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) Utils(com.quorum.tessera.data.Utils) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) jakarta.persistence(jakarta.persistence) Test(org.junit.Test) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestConfig(com.quorum.tessera.data.TestConfig) MockedStatic(org.mockito.MockedStatic) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Root(jakarta.persistence.criteria.Root) Stream(java.util.stream.Stream) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) AtomicLong(java.util.concurrent.atomic.AtomicLong) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Test(org.junit.Test)

Example 10 with StagingTransaction

use of com.quorum.tessera.data.staging.StagingTransaction in project tessera by ConsenSys.

the class StagingEntityDAOTest method testStagingQuery.

@Test
public void testStagingQuery() {
    final List<StagingTransaction> preStaging = stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(0, Integer.MAX_VALUE);
    final AtomicLong stage = new AtomicLong(0);
    final int batchSize = 10;
    assertThat(preStaging.size()).isEqualTo(7);
    preStaging.forEach(stagingTransaction -> {
        assertThat(stagingTransaction.getValidationStage()).isNull();
    });
    while (stagingEntityDAO.updateStageForBatch(batchSize, stage.incrementAndGet()) != 0) {
    }
    final List<StagingTransaction> verifiedTransactions = stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(0, Integer.MAX_VALUE);
    // First tx to process will need to be Tx1
    assertThat(verifiedTransactions.get(0).getValidationStage()).isEqualTo(1L);
    assertThat(verifiedTransactions.get(0).getId()).isEqualTo(1L);
    // Then tx2 (2 versions) and 3
    assertThat(verifiedTransactions.get(1).getValidationStage()).isEqualTo(2L);
    assertThat(verifiedTransactions.get(2).getValidationStage()).isEqualTo(2L);
    assertThat(verifiedTransactions.get(3).getValidationStage()).isEqualTo(2L);
    // Then transaction 4 as its affected tx (3) had been validated
    assertThat(verifiedTransactions.get(4).getValidationStage()).isEqualTo(3L);
    assertThat(verifiedTransactions.get(4).getId()).isEqualTo(4L);
    // Then transaction 7 as all of its affected txs (1 and 4) had been validated
    assertThat(verifiedTransactions.get(5).getValidationStage()).isEqualTo(4L);
    assertThat(verifiedTransactions.get(5).getId()).isEqualTo(7L);
    // Transaction 5 can never be validated as it depends on an unknown tx6
    assertThat(verifiedTransactions.get(6).getValidationStage()).isNull();
    assertThat(verifiedTransactions.get(6).getId()).isEqualTo(5L);
    final List<StagingTransaction> allTransactions = stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(0, Integer.MAX_VALUE);
    assertThat(allTransactions.stream().filter(et -> et.getValidationStage() == null).count()).isEqualTo(1);
    assertThat(stagingEntityDAO.countAll()).isEqualTo(7);
    assertThat(stagingEntityDAO.countStaged()).isEqualTo(6);
    assertThat(stagingEntityDAO.countAllAffected()).isEqualTo(7);
}
Also used : java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StagingAffectedTransaction(com.quorum.tessera.data.staging.StagingAffectedTransaction) RunWith(org.junit.runner.RunWith) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) After(org.junit.After) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) Utils(com.quorum.tessera.data.Utils) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) jakarta.persistence(jakarta.persistence) Test(org.junit.Test) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestConfig(com.quorum.tessera.data.TestConfig) MockedStatic(org.mockito.MockedStatic) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Root(jakarta.persistence.criteria.Root) Stream(java.util.stream.Stream) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) AtomicLong(java.util.concurrent.atomic.AtomicLong) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Test(org.junit.Test)

Aggregations

StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)13 Test (org.junit.Test)10 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)7 StagingAffectedTransaction (com.quorum.tessera.data.staging.StagingAffectedTransaction)4 RecoveryResult (com.quorum.tessera.recovery.RecoveryResult)4 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)4 StagingEntityDAO (com.quorum.tessera.data.staging.StagingEntityDAO)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Collectors (java.util.stream.Collectors)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 After (org.junit.After)3 Before (org.junit.Before)3 Mockito (org.mockito.Mockito)3 TestConfig (com.quorum.tessera.data.TestConfig)2 Utils (com.quorum.tessera.data.Utils)2 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)2 PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)2 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)2 jakarta.persistence (jakarta.persistence)2 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)2