Search in sources :

Example 1 with PublishPayloadException

use of com.quorum.tessera.transaction.publish.PublishPayloadException in project tessera by ConsenSys.

the class RestPayloadPublisher method publishPayload.

@Override
public void publishPayload(EncodedPayload payload, PublicKey recipientKey) {
    final NodeInfo remoteNodeInfo = discovery.getRemoteNodeInfo(recipientKey);
    final Set<String> supportedApiVersions = remoteNodeInfo.supportedApiVersions();
    final EncodedPayloadCodec preferredCodec = EncodedPayloadCodec.getPreferredCodec(supportedApiVersions);
    final PayloadEncoder payloadEncoder = PayloadEncoder.create(preferredCodec);
    if (PrivacyMode.STANDARD_PRIVATE != payload.getPrivacyMode() && !supportedApiVersions.contains(EnhancedPrivacyVersion.API_VERSION_2)) {
        throw new EnhancedPrivacyNotSupportedException("Transactions with enhanced privacy is not currently supported on recipient " + recipientKey.encodeToBase64());
    }
    if (PrivacyMode.MANDATORY_RECIPIENTS == payload.getPrivacyMode() && !supportedApiVersions.contains(MandatoryRecipientsVersion.API_VERSION_4)) {
        throw new MandatoryRecipientsNotSupportedException("Transactions with mandatory recipients are not currently supported on recipient " + recipientKey.encodeToBase64());
    }
    final String targetUrl = remoteNodeInfo.getUrl();
    LOGGER.info("Publishing message to {}", targetUrl);
    final byte[] encoded = payloadEncoder.encode(payload);
    try (Response response = client.target(targetUrl).path("/push").request().post(Entity.entity(encoded, MediaType.APPLICATION_OCTET_STREAM_TYPE))) {
        if (Response.Status.OK.getStatusCode() != response.getStatus() && Response.Status.CREATED.getStatusCode() != response.getStatus()) {
            throw new PublishPayloadException("Unable to push payload to recipient url " + targetUrl);
        }
        LOGGER.info("Published to {}", targetUrl);
    } catch (ProcessingException ex) {
        LOGGER.debug("", ex);
        throw new NodeOfflineException(URI.create(targetUrl));
    }
}
Also used : Response(jakarta.ws.rs.core.Response) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) PublishPayloadException(com.quorum.tessera.transaction.publish.PublishPayloadException) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) MandatoryRecipientsNotSupportedException(com.quorum.tessera.transaction.exception.MandatoryRecipientsNotSupportedException) NodeOfflineException(com.quorum.tessera.transaction.publish.NodeOfflineException) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException) ProcessingException(jakarta.ws.rs.ProcessingException)

Example 2 with PublishPayloadException

use of com.quorum.tessera.transaction.publish.PublishPayloadException in project tessera by ConsenSys.

the class AsyncBatchPayloadPublisherTest method publishPayloadCancelsCountDownLatchIfOneTaskFails.

@Test
public void publishPayloadCancelsCountDownLatchIfOneTaskFails() throws InterruptedException {
    final Executor realExecutor = Executors.newCachedThreadPool();
    when(executorFactory.createCachedThreadPool()).thenReturn(realExecutor);
    asyncPublisher = new AsyncBatchPayloadPublisher(executorFactory, countDownLatchFactory, publisher);
    final PublicKey recipient = PublicKey.from("RECIPIENT".getBytes());
    final PublicKey otherRecipient = PublicKey.from("OTHERRECIPIENT".getBytes());
    final List<PublicKey> recipients = List.of(recipient, otherRecipient);
    final EncodedPayload payload = EncodedPayload.Builder.create().withSenderKey(mock(PublicKey.class)).withRecipientKeys(recipients).withRecipientBoxes(List.of("box1".getBytes(), "box2".getBytes())).build();
    final PublishPayloadException cause = new PublishPayloadException("some exception");
    doThrow(cause).doNothing().when(publisher).publishPayload(any(EncodedPayload.class), any(PublicKey.class));
    doAnswer(invocation -> {
        // sleep main thread so publish threads can work
        Thread.sleep(200);
        return null;
    }).when(countDownLatch).await();
    asyncPublisher.publishPayload(payload, recipients);
    verify(executorFactory, times(2)).createCachedThreadPool();
    verify(countDownLatchFactory).create(2);
    verify(publisher).publishPayload(any(), eq(recipient));
    verify(publisher).publishPayload(any(), eq(otherRecipient));
    verify(countDownLatch).countDown();
    verify(countDownLatch).cancelWithException(cause);
    verify(countDownLatch).await();
}
Also used : Executor(java.util.concurrent.Executor) PublishPayloadException(com.quorum.tessera.transaction.publish.PublishPayloadException) BatchPublishPayloadException(com.quorum.tessera.transaction.publish.BatchPublishPayloadException) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 3 with PublishPayloadException

use of com.quorum.tessera.transaction.publish.PublishPayloadException in project tessera by ConsenSys.

the class RestPayloadPublisherTest method publish.

@Test
public void publish() {
    final String targetUrl = "nodeUrl";
    final EncodedPayload encodedPayload = mock(EncodedPayload.class);
    final PublicKey publicKey = mock(PublicKey.class);
    for (Response.Status expectedResponseStatus : Response.Status.values()) {
        for (PrivacyMode privacyMode : PrivacyMode.values()) {
            when(encodedPayload.getPrivacyMode()).thenReturn(privacyMode);
            final NodeInfo nodeInfo = mock(NodeInfo.class);
            when(nodeInfo.supportedApiVersions()).thenReturn(Set.of(EnhancedPrivacyVersion.API_VERSION_2, "2.1", "3.0", "4.0"));
            when(nodeInfo.getUrl()).thenReturn(targetUrl);
            when(discovery.getRemoteNodeInfo(publicKey)).thenReturn(nodeInfo);
            final byte[] payloadData = "Payload".getBytes();
            when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
            WebTarget webTarget = mock(WebTarget.class);
            when(client.target(targetUrl)).thenReturn(webTarget);
            when(webTarget.path("/push")).thenReturn(webTarget);
            Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
            Response response = Response.status(expectedResponseStatus).build();
            when(invocationBuilder.post(Entity.entity(payloadData, MediaType.APPLICATION_OCTET_STREAM_TYPE))).thenReturn(response);
            when(webTarget.request()).thenReturn(invocationBuilder);
            if (expectedResponseStatus == Response.Status.OK || expectedResponseStatus == Response.Status.CREATED) {
                payloadPublisher.publishPayload(encodedPayload, publicKey);
            } else {
                PublishPayloadException publishPayloadException = Assertions.catchThrowableOfType(() -> payloadPublisher.publishPayload(encodedPayload, publicKey), PublishPayloadException.class);
                assertThat(publishPayloadException).hasMessage(String.format("Unable to push payload to recipient url %s", targetUrl));
            }
        }
    }
    int iterations = Response.Status.values().length * PrivacyMode.values().length;
    verify(client, times(iterations)).target(targetUrl);
    verify(discovery, times(iterations)).getRemoteNodeInfo(publicKey);
    verify(payloadEncoder, times(iterations)).encode(encodedPayload);
    payloadEncoderFactoryFunction.verify(times(iterations), () -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
}
Also used : PublishPayloadException(com.quorum.tessera.transaction.publish.PublishPayloadException) Invocation(jakarta.ws.rs.client.Invocation) PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Response(jakarta.ws.rs.core.Response) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 4 with PublishPayloadException

use of com.quorum.tessera.transaction.publish.PublishPayloadException 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)

Aggregations

PublishPayloadException (com.quorum.tessera.transaction.publish.PublishPayloadException)4 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)3 Test (org.junit.Test)3 PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)2 PublicKey (com.quorum.tessera.encryption.PublicKey)2 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)2 Response (jakarta.ws.rs.core.Response)2 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)1 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)1 EnhancedPrivacyNotSupportedException (com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException)1 MandatoryRecipientsNotSupportedException (com.quorum.tessera.transaction.exception.MandatoryRecipientsNotSupportedException)1 BatchPublishPayloadException (com.quorum.tessera.transaction.publish.BatchPublishPayloadException)1 NodeOfflineException (com.quorum.tessera.transaction.publish.NodeOfflineException)1 ProcessingException (jakarta.ws.rs.ProcessingException)1 Invocation (jakarta.ws.rs.client.Invocation)1 WebTarget (jakarta.ws.rs.client.WebTarget)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1