Search in sources :

Example 1 with PrivacyGroupPublishException

use of com.quorum.tessera.privacygroup.exception.PrivacyGroupPublishException in project tessera by ConsenSys.

the class AsyncBatchPrivacyGroupPublisher method publishPrivacyGroup.

@Override
public void publishPrivacyGroup(byte[] data, List<PublicKey> recipientKeys) {
    if (recipientKeys.size() == 0) {
        return;
    }
    final CancellableCountDownLatch latch = countDownLatchFactory.create(recipientKeys.size());
    recipientKeys.forEach(key -> executor.execute(() -> {
        try {
            publisher.publishPrivacyGroup(data, key);
            latch.countDown();
        } catch (RuntimeException e) {
            LOGGER.info("Unable to publish privacy group: {}", e.getMessage());
            latch.cancelWithException(e);
        }
    }));
    try {
        latch.await();
    } catch (InterruptedException e) {
        throw new PrivacyGroupPublishException(e.getMessage());
    }
}
Also used : PrivacyGroupPublishException(com.quorum.tessera.privacygroup.exception.PrivacyGroupPublishException) CancellableCountDownLatch(com.quorum.tessera.threading.CancellableCountDownLatch)

Example 2 with PrivacyGroupPublishException

use of com.quorum.tessera.privacygroup.exception.PrivacyGroupPublishException in project tessera by ConsenSys.

the class RestPrivacyGroupPublisher method publishPrivacyGroup.

@Override
public void publishPrivacyGroup(byte[] data, PublicKey recipientKey) {
    final NodeInfo remoteNodeInfo = discovery.getRemoteNodeInfo(recipientKey);
    if (!remoteNodeInfo.supportedApiVersions().contains(PrivacyGroupVersion.API_VERSION_3)) {
        throw new PrivacyGroupNotSupportedException("Transactions with privacy group is not currently supported on recipient " + recipientKey.encodeToBase64());
    }
    final String targetUrl = remoteNodeInfo.getUrl();
    LOGGER.info("Publishing privacy group to {}", targetUrl);
    try (Response response = restClient.target(targetUrl).path("/pushPrivacyGroup").request().post(Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE))) {
        if (Response.Status.OK.getStatusCode() != response.getStatus()) {
            throw new PrivacyGroupPublishException("Unable to push privacy group to recipient url " + targetUrl);
        }
        LOGGER.info("Published privacy group to {}", targetUrl);
    } catch (ProcessingException ex) {
        LOGGER.debug("", ex);
        throw new NodeOfflineException(URI.create(targetUrl));
    }
}
Also used : Response(jakarta.ws.rs.core.Response) PrivacyGroupNotSupportedException(com.quorum.tessera.privacygroup.exception.PrivacyGroupNotSupportedException) PrivacyGroupPublishException(com.quorum.tessera.privacygroup.exception.PrivacyGroupPublishException) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) NodeOfflineException(com.quorum.tessera.transaction.publish.NodeOfflineException) ProcessingException(jakarta.ws.rs.ProcessingException)

Example 3 with PrivacyGroupPublishException

use of com.quorum.tessera.privacygroup.exception.PrivacyGroupPublishException in project tessera by ConsenSys.

the class AsyncBatchPrivacyGroupPublisherTest method publishReturnsError.

@Test
public void publishReturnsError() throws InterruptedException {
    final Executor realExecutor = Executors.newSingleThreadExecutor();
    when(mockExecutorFactory.createCachedThreadPool()).thenReturn(realExecutor);
    final AsyncBatchPrivacyGroupPublisher publisher = new AsyncBatchPrivacyGroupPublisher(mockExecutorFactory, mockCountDownLatchFactory, mockPublisher);
    final byte[] data = new byte[5];
    doThrow(new PrivacyGroupPublishException("OUCH")).when(mockPublisher).publishPrivacyGroup(eq(data), eq(otherRecipient));
    doAnswer(invocation -> {
        // sleep main thread so publish threads can work
        Thread.sleep(200);
        return null;
    }).when(mockCountDownLatch).await();
    publisher.publishPrivacyGroup(data, List.of(recipient, otherRecipient));
    verify(mockCountDownLatch).cancelWithException(any());
}
Also used : Executor(java.util.concurrent.Executor) PrivacyGroupPublishException(com.quorum.tessera.privacygroup.exception.PrivacyGroupPublishException) Test(org.junit.Test)

Example 4 with PrivacyGroupPublishException

use of com.quorum.tessera.privacygroup.exception.PrivacyGroupPublishException in project tessera by ConsenSys.

the class RestPrivacyGroupPublisherTest method testPublishSingleError.

@Test
public void testPublishSingleError() {
    String targetUrl = "https://sometargeturl.com";
    PublicKey recipient = PublicKey.from("PUBLIC_KEY".getBytes());
    NodeInfo nodeInfo = mock(NodeInfo.class);
    when(nodeInfo.supportedApiVersions()).thenReturn(Set.of(PrivacyGroupVersion.API_VERSION_3));
    when(nodeInfo.getUrl()).thenReturn(targetUrl);
    when(discovery.getRemoteNodeInfo(recipient)).thenReturn(nodeInfo);
    Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
    when(invocationBuilder.post(any(Entity.class))).thenReturn(Response.serverError().build());
    WebTarget webTarget = mock(WebTarget.class);
    when(client.target(targetUrl)).thenReturn(webTarget);
    when(webTarget.path(anyString())).thenReturn(webTarget);
    when(webTarget.request()).thenReturn(invocationBuilder);
    final byte[] data = new byte[5];
    try {
        publisher.publishPrivacyGroup(data, recipient);
        failBecauseExceptionWasNotThrown(PrivacyGroupPublishException.class);
    } catch (PrivacyGroupPublishException ex) {
        verify(discovery).getRemoteNodeInfo(recipient);
        verify(client).target(targetUrl);
    }
}
Also used : Entity(jakarta.ws.rs.client.Entity) PrivacyGroupPublishException(com.quorum.tessera.privacygroup.exception.PrivacyGroupPublishException) Invocation(jakarta.ws.rs.client.Invocation) PublicKey(com.quorum.tessera.encryption.PublicKey) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.Test)

Aggregations

PrivacyGroupPublishException (com.quorum.tessera.privacygroup.exception.PrivacyGroupPublishException)4 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)2 Test (org.junit.Test)2 PublicKey (com.quorum.tessera.encryption.PublicKey)1 PrivacyGroupNotSupportedException (com.quorum.tessera.privacygroup.exception.PrivacyGroupNotSupportedException)1 CancellableCountDownLatch (com.quorum.tessera.threading.CancellableCountDownLatch)1 NodeOfflineException (com.quorum.tessera.transaction.publish.NodeOfflineException)1 ProcessingException (jakarta.ws.rs.ProcessingException)1 Entity (jakarta.ws.rs.client.Entity)1 Invocation (jakarta.ws.rs.client.Invocation)1 WebTarget (jakarta.ws.rs.client.WebTarget)1 Response (jakarta.ws.rs.core.Response)1 Executor (java.util.concurrent.Executor)1