Search in sources :

Example 36 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class RecoveryImpl method sync.

@Override
public RecoveryResult sync() {
    final AtomicInteger payloadCount = new AtomicInteger(0);
    final AtomicInteger syncFailureCount = new AtomicInteger(0);
    final int maxResult = BATCH_SIZE;
    for (int offset = 0; offset < stagingEntityDAO.countAll(); offset += maxResult) {
        final List<StagingTransaction> transactions = stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(offset, maxResult);
        final Map<String, List<StagingTransaction>> grouped = transactions.stream().collect(Collectors.groupingBy(StagingTransaction::getHash, LinkedHashMap::new, toList()));
        grouped.forEach((key, value) -> value.stream().filter(t -> {
            payloadCount.incrementAndGet();
            EncodedPayload encodedPayload = t.getEncodedPayload();
            try {
                transactionManager.storePayload(encodedPayload);
            } catch (PrivacyViolationException | PersistenceException ex) {
                LOGGER.error("An error occurred during batch resend sync stage.", ex);
                syncFailureCount.incrementAndGet();
            }
            return PrivacyMode.PRIVATE_STATE_VALIDATION == t.getPrivacyMode();
        }).findFirst());
    }
    if (syncFailureCount.get() > 0) {
        LOGGER.warn("There have been issues during the synchronisation process. " + "Problematic transactions have been ignored.");
        if (syncFailureCount.get() == payloadCount.get()) {
            return RecoveryResult.FAILURE;
        }
        return RecoveryResult.PARTIAL_SUCCESS;
    }
    return RecoveryResult.SUCCESS;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) PersistenceException(jakarta.persistence.PersistenceException) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Collectors.toList(java.util.stream.Collectors.toList) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 37 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class EncodedPayloadPublisherTest method outstandingPayloadsReachTotalExpected.

@Test
public void outstandingPayloadsReachTotalExpected() {
    final PublicKey recipientKey = PublicKey.from("test-key".getBytes());
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    final BatchWorkflowContext batchWorkflowContext = new BatchWorkflowContext();
    batchWorkflowContext.setBatchSize(100);
    batchWorkflowContext.setExpectedTotal(4L);
    batchWorkflowContext.setPayloadsToPublish(Set.of(encodedPayload));
    batchWorkflowContext.setRecipientKey(recipientKey);
    batchWorkflowContext.setRecipient(Recipient.of(recipientKey, "http://junit.com"));
    encodedPayloadPublisher.execute(batchWorkflowContext);
    batchWorkflowContext.setExpectedTotal(3L);
    encodedPayloadPublisher.checkOutstandingPayloads(batchWorkflowContext);
    assertThat(encodedPayloadPublisher.getPublishedCount()).isEqualTo(0);
    encodedPayloadPublisher.execute(batchWorkflowContext);
    batchWorkflowContext.setExpectedTotal(2L);
    encodedPayloadPublisher.checkOutstandingPayloads(batchWorkflowContext);
    assertThat(encodedPayloadPublisher.getPublishedCount()).isEqualTo(2);
    List<EncodedPayload> sent = List.of(encodedPayload, encodedPayload);
    verify(resendBatchPublisher).publishBatch(sent, "http://junit.com");
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 38 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class EncodedPayloadPublisherTest method outstandingPayloadsPlusPublishedReachTotalExpected.

@Test
public void outstandingPayloadsPlusPublishedReachTotalExpected() {
    BatchWorkflowContext batchWorkflowContext = new BatchWorkflowContext();
    batchWorkflowContext.setBatchSize(2);
    batchWorkflowContext.setExpectedTotal(6L);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    batchWorkflowContext.setPayloadsToPublish(Set.of(encodedPayload));
    PublicKey recipientKey = mock(PublicKey.class);
    Recipient recipient = mock(Recipient.class);
    when(recipient.getUrl()).thenReturn("http://junit.com");
    batchWorkflowContext.setRecipientKey(recipientKey);
    batchWorkflowContext.setRecipient(recipient);
    encodedPayloadPublisher.execute(batchWorkflowContext);
    batchWorkflowContext.setExpectedTotal(5L);
    encodedPayloadPublisher.checkOutstandingPayloads(batchWorkflowContext);
    assertThat(encodedPayloadPublisher.getPublishedCount()).isEqualTo(0);
    encodedPayloadPublisher.execute(batchWorkflowContext);
    batchWorkflowContext.setExpectedTotal(4L);
    encodedPayloadPublisher.checkOutstandingPayloads(batchWorkflowContext);
    assertThat(encodedPayloadPublisher.getPublishedCount()).isEqualTo(2);
    // Not publish yet as not yet reach batch size
    encodedPayloadPublisher.execute(batchWorkflowContext);
    assertThat(encodedPayloadPublisher.getPublishedCount()).isEqualTo(2);
    batchWorkflowContext.setExpectedTotal(3L);
    encodedPayloadPublisher.checkOutstandingPayloads(batchWorkflowContext);
    // Publish here because message count + outstanding = expected total
    assertThat(encodedPayloadPublisher.getPublishedCount()).isEqualTo(3);
    verify(resendBatchPublisher, times(2)).publishBatch(anyList(), eq("http://junit.com"));
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Recipient(com.quorum.tessera.partyinfo.node.Recipient) Test(org.junit.Test)

Example 39 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class EncodedPayloadPublisherTest method executeIfNumberOfPayloadsReachBatchSize.

@Test
public void executeIfNumberOfPayloadsReachBatchSize() {
    BatchWorkflowContext batchWorkflowContext = new BatchWorkflowContext();
    batchWorkflowContext.setBatchSize(2);
    batchWorkflowContext.setExpectedTotal(9L);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    batchWorkflowContext.setPayloadsToPublish(Set.of(encodedPayload));
    PublicKey recipientKey = mock(PublicKey.class);
    Recipient recipient = mock(Recipient.class);
    when(recipient.getUrl()).thenReturn("http://junit.com");
    batchWorkflowContext.setRecipientKey(recipientKey);
    batchWorkflowContext.setRecipient(recipient);
    boolean result = IntStream.range(0, batchSize).allMatch(i -> encodedPayloadPublisher.execute(batchWorkflowContext));
    assertThat(result).isTrue();
    assertThat(encodedPayloadPublisher.getPublishedCount()).isEqualTo(9);
    List<EncodedPayload> sent2 = List.of(encodedPayload, encodedPayload);
    List<EncodedPayload> sent1 = List.of(encodedPayload);
    verify(resendBatchPublisher, times(4)).publishBatch(sent2, "http://junit.com");
    verify(resendBatchPublisher).publishBatch(sent1, "http://junit.com");
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Recipient(com.quorum.tessera.partyinfo.node.Recipient) Test(org.junit.Test)

Example 40 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class EncodedPayloadPublisherTest method executeIfNumberOfPayloadsReachTotal.

@Test
public void executeIfNumberOfPayloadsReachTotal() {
    BatchWorkflowContext batchWorkflowContext = new BatchWorkflowContext();
    batchWorkflowContext.setBatchSize(100);
    batchWorkflowContext.setExpectedTotal(9L);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    batchWorkflowContext.setPayloadsToPublish(Set.of(encodedPayload));
    PublicKey recipientKey = mock(PublicKey.class);
    Recipient recipient = mock(Recipient.class);
    when(recipient.getUrl()).thenReturn("http://junit.com");
    batchWorkflowContext.setRecipientKey(recipientKey);
    batchWorkflowContext.setRecipient(recipient);
    boolean result = IntStream.range(0, batchSize).allMatch(i -> encodedPayloadPublisher.execute(batchWorkflowContext));
    assertThat(result).isTrue();
    assertThat(encodedPayloadPublisher.getPublishedCount()).isEqualTo(9);
    List<EncodedPayload> sent = Arrays.asList(new EncodedPayload[9]);
    Collections.fill(sent, encodedPayload);
    verify(resendBatchPublisher).publishBatch(sent, "http://junit.com");
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Recipient(com.quorum.tessera.partyinfo.node.Recipient) Test(org.junit.Test)

Aggregations

EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)91 Test (org.junit.Test)60 PublicKey (com.quorum.tessera.encryption.PublicKey)50 PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)23 Response (jakarta.ws.rs.core.Response)20 MessageHash (com.quorum.tessera.data.MessageHash)13 Collectors (java.util.stream.Collectors)12 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)11 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)11 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)9 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)8 Recipient (com.quorum.tessera.partyinfo.node.Recipient)8 java.util (java.util)8 Invocation (jakarta.ws.rs.client.Invocation)6 WebTarget (jakarta.ws.rs.client.WebTarget)6 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)5 PrivacyMetadata (com.quorum.tessera.enclave.PrivacyMetadata)5 RecipientBox (com.quorum.tessera.enclave.RecipientBox)5 TxHash (com.quorum.tessera.enclave.TxHash)5 ResendRequest (com.quorum.tessera.p2p.resend.ResendRequest)5