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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations