Search in sources :

Example 76 with EncodedPayload

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

the class AsyncBatchPayloadPublisherTest method publishPayloadNoRecipientsDoesNothing.

@Test
public void publishPayloadNoRecipientsDoesNothing() {
    final EncodedPayload payload = mock(EncodedPayload.class);
    final List<PublicKey> recipients = Collections.emptyList();
    asyncPublisher.publishPayload(payload, recipients);
    verify(executorFactory).createCachedThreadPool();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 77 with EncodedPayload

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

the class RestPayloadPublisherTest method publishEnhancedTransactionsToNodesThatDoNotSupport.

@Test
public void publishEnhancedTransactionsToNodesThatDoNotSupport() {
    Map<PrivacyMode, Set<String>> privacyModeAndVersions = new HashMap<>();
    privacyModeAndVersions.put(PrivacyMode.PARTY_PROTECTION, Set.of("v1"));
    privacyModeAndVersions.put(PrivacyMode.PRIVATE_STATE_VALIDATION, Set.of("v1"));
    for (Map.Entry<PrivacyMode, Set<String>> pair : privacyModeAndVersions.entrySet()) {
        String targetUrl = "http://someplace.com";
        EncodedPayload encodedPayload = mock(EncodedPayload.class);
        when(encodedPayload.getPrivacyMode()).thenReturn(pair.getKey());
        byte[] payloadData = "Some Data".getBytes();
        when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
        PublicKey recipientKey = mock(PublicKey.class);
        NodeInfo nodeInfo = mock(NodeInfo.class);
        when(nodeInfo.supportedApiVersions()).thenReturn(pair.getValue());
        Recipient recipient = mock(Recipient.class);
        when(recipient.getKey()).thenReturn(recipientKey);
        when(recipient.getUrl()).thenReturn(targetUrl);
        when(nodeInfo.getRecipients()).thenReturn(Set.of(recipient));
        when(discovery.getRemoteNodeInfo(recipientKey)).thenReturn(nodeInfo);
        EnhancedPrivacyNotSupportedException exception = catchThrowableOfType(() -> payloadPublisher.publishPayload(encodedPayload, recipientKey), EnhancedPrivacyNotSupportedException.class);
        assertThat(exception).hasMessageContaining("Transactions with enhanced privacy is not currently supported");
        verify(discovery).getRemoteNodeInfo(eq(recipientKey));
    }
    payloadEncoderFactoryFunction.verify(times(2), () -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Recipient(com.quorum.tessera.partyinfo.node.Recipient) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) HashMap(java.util.HashMap) Map(java.util.Map) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException) Test(org.junit.Test)

Example 78 with EncodedPayload

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

the class RestResendBatchPublisherTest method publishBatchRecoveryClientFails.

@Test
public void publishBatchRecoveryClientFails() {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    RecoveryClient recoveryClient = mock(RecoveryClient.class);
    ArgumentCaptor<PushBatchRequest> requestArgumentCaptor = ArgumentCaptor.forClass(PushBatchRequest.class);
    when(recoveryClient.pushBatch(anyString(), requestArgumentCaptor.capture())).thenReturn(false);
    List<EncodedPayload> encodedPayloads = payloadDatList.stream().map(o -> {
        EncodedPayload encodedPayload = mock(EncodedPayload.class);
        when(payloadEncoder.encode(encodedPayload)).thenReturn(o);
        return encodedPayload;
    }).collect(Collectors.toList());
    RestResendBatchPublisher restRecoveryClient = new RestResendBatchPublisher(payloadEncoder, recoveryClient);
    PublishPayloadException ex = catchThrowableOfType(() -> restRecoveryClient.publishBatch(encodedPayloads, targetUrl), PublishPayloadException.class);
    assertThat(ex).hasMessage(String.format("Unable to push payload batch to recipient %s", targetUrl));
    verify(recoveryClient).pushBatch(targetUrl, requestArgumentCaptor.getValue());
    encodedPayloads.forEach(p -> {
        verify(payloadEncoder).encode(p);
    });
    assertThat(requestArgumentCaptor.getValue().getEncodedPayloads()).isEqualTo(payloadDatList);
    verifyNoMoreInteractions(recoveryClient);
    verifyNoMoreInteractions(payloadEncoder);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.catchThrowableOfType(org.assertj.core.api.Assertions.catchThrowableOfType) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) PublishPayloadException(com.quorum.tessera.transaction.publish.PublishPayloadException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collection(java.util.Collection) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) List(java.util.List) ArgumentCaptor(org.mockito.ArgumentCaptor) Map(java.util.Map) Parameterized(org.junit.runners.Parameterized) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) PublishPayloadException(com.quorum.tessera.transaction.publish.PublishPayloadException) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 79 with EncodedPayload

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

the class RestResendBatchPublisherTest method publishBatch.

@Test
public void publishBatch() {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    RecoveryClient recoveryClient = mock(RecoveryClient.class);
    ArgumentCaptor<PushBatchRequest> requestArgumentCaptor = ArgumentCaptor.forClass(PushBatchRequest.class);
    when(recoveryClient.pushBatch(anyString(), requestArgumentCaptor.capture())).thenReturn(true);
    List<EncodedPayload> encodedPayloads = payloadDatList.stream().map(o -> {
        EncodedPayload encodedPayload = mock(EncodedPayload.class);
        when(payloadEncoder.encode(encodedPayload)).thenReturn(o);
        return encodedPayload;
    }).collect(Collectors.toList());
    RestResendBatchPublisher restRecoveryClient = new RestResendBatchPublisher(payloadEncoder, recoveryClient);
    restRecoveryClient.publishBatch(encodedPayloads, targetUrl);
    verify(recoveryClient).pushBatch(targetUrl, requestArgumentCaptor.getValue());
    encodedPayloads.forEach(p -> {
        verify(payloadEncoder).encode(p);
    });
    assertThat(requestArgumentCaptor.getValue().getEncodedPayloads()).isEqualTo(payloadDatList);
    verifyNoMoreInteractions(recoveryClient);
    verifyNoMoreInteractions(payloadEncoder);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.catchThrowableOfType(org.assertj.core.api.Assertions.catchThrowableOfType) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) PublishPayloadException(com.quorum.tessera.transaction.publish.PublishPayloadException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collection(java.util.Collection) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) List(java.util.List) ArgumentCaptor(org.mockito.ArgumentCaptor) Map(java.util.Map) Parameterized(org.junit.runners.Parameterized) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 80 with EncodedPayload

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

the class AsyncBatchPayloadPublisher method publishPayload.

/**
 * Asynchronously strips (leaving data intended only for that particular recipient) and publishes
 * the payload to each recipient identified by the provided keys.
 *
 * <p>This method blocks until all pushes return successfully; if a push fails with an exception,
 * the method exits immediately and does not wait for the remaining responses.
 *
 * @param payload the payload object to be stripped and pushed
 * @param recipientKeys list of public keys identifying the target nodes
 */
@Override
public void publishPayload(EncodedPayload payload, List<PublicKey> recipientKeys) {
    if (recipientKeys.size() == 0) {
        return;
    }
    final CancellableCountDownLatch latch = countDownLatchFactory.create(recipientKeys.size());
    recipientKeys.forEach(recipient -> executor.execute(() -> {
        try {
            final EncodedPayload outgoing = EncodedPayload.Builder.forRecipient(payload, recipient).build();
            publisher.publishPayload(outgoing, recipient);
            latch.countDown();
        } catch (RuntimeException e) {
            LOGGER.info("unable to publish payload in batch: {}", e.getMessage());
            latch.cancelWithException(e);
        }
    }));
    try {
        latch.await();
    } catch (InterruptedException e) {
        throw new BatchPublishPayloadException(e);
    }
}
Also used : CancellableCountDownLatch(com.quorum.tessera.threading.CancellableCountDownLatch) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) BatchPublishPayloadException(com.quorum.tessera.transaction.publish.BatchPublishPayloadException)

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