Search in sources :

Example 1 with NodeInfo

use of com.quorum.tessera.partyinfo.node.NodeInfo 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 NodeInfo

use of com.quorum.tessera.partyinfo.node.NodeInfo 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 NodeInfo

use of com.quorum.tessera.partyinfo.node.NodeInfo in project tessera by ConsenSys.

the class RestPayloadPublisherTest method publishMandatoryRecipientsToNodesThatDoNotSupport.

@Test
public void publishMandatoryRecipientsToNodesThatDoNotSupport() {
    String targetUrl = "http://someplace.com";
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(encodedPayload.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
    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(Set.of("v2", "2.1", "3.0"));
    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);
    assertThatExceptionOfType(MandatoryRecipientsNotSupportedException.class).isThrownBy(() -> payloadPublisher.publishPayload(encodedPayload, recipientKey)).withMessageContaining("Transactions with mandatory recipients are not currently supported on recipient");
    verify(discovery).getRemoteNodeInfo(eq(recipientKey));
    payloadEncoderFactoryFunction.verify(() -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Recipient(com.quorum.tessera.partyinfo.node.Recipient) Test(org.junit.Test)

Example 4 with NodeInfo

use of com.quorum.tessera.partyinfo.node.NodeInfo 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 5 with NodeInfo

use of com.quorum.tessera.partyinfo.node.NodeInfo in project tessera by ConsenSys.

the class RestPayloadPublisherTest method handleConnectionError.

@Test
public void handleConnectionError() {
    final String targetUri = "http://jimmywhite.com";
    final PublicKey recipientKey = mock(PublicKey.class);
    Recipient recipient = mock(Recipient.class);
    when(recipient.getKey()).thenReturn(recipientKey);
    when(recipient.getUrl()).thenReturn(targetUri);
    NodeInfo nodeInfo = mock(NodeInfo.class);
    when(nodeInfo.getRecipients()).thenReturn(Set.of(recipient));
    when(nodeInfo.getUrl()).thenReturn(targetUri);
    when(discovery.getRemoteNodeInfo(recipientKey)).thenReturn(nodeInfo);
    Client client = mock(Client.class);
    when(client.target(targetUri)).thenThrow(ProcessingException.class);
    final EncodedPayload payload = mock(EncodedPayload.class);
    when(payload.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
    when(payloadEncoder.encode(payload)).thenReturn("SomeData".getBytes());
    RestPayloadPublisher restPayloadPublisher = new RestPayloadPublisher(client, discovery);
    try {
        restPayloadPublisher.publishPayload(payload, recipientKey);
        failBecauseExceptionWasNotThrown(NodeOfflineException.class);
    } catch (NodeOfflineException ex) {
        assertThat(ex).hasMessageContaining(targetUri);
        verify(client).target(targetUri);
        verify(discovery).getRemoteNodeInfo(eq(recipientKey));
        verify(payloadEncoder).encode(payload);
        verify(discovery).getRemoteNodeInfo(eq(recipientKey));
        payloadEncoderFactoryFunction.verify(() -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
    }
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) Recipient(com.quorum.tessera.partyinfo.node.Recipient) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Client(jakarta.ws.rs.client.Client) NodeOfflineException(com.quorum.tessera.transaction.publish.NodeOfflineException) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) Test(org.junit.Test)

Aggregations

NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)49 Test (org.junit.Test)37 PublicKey (com.quorum.tessera.encryption.PublicKey)31 Recipient (com.quorum.tessera.partyinfo.node.Recipient)19 ActiveNode (com.quorum.tessera.discovery.ActiveNode)12 Response (jakarta.ws.rs.core.Response)11 PartyInfo (com.quorum.tessera.partyinfo.model.PartyInfo)10 NodeUri (com.quorum.tessera.discovery.NodeUri)8 URI (java.net.URI)8 Collectors (java.util.stream.Collectors)8 RuntimeContext (com.quorum.tessera.context.RuntimeContext)7 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)7 Set (java.util.Set)7 KeyNotFoundException (com.quorum.tessera.encryption.KeyNotFoundException)6 Logger (org.slf4j.Logger)6 Discovery (com.quorum.tessera.discovery.Discovery)5 DiscoveryHelper (com.quorum.tessera.discovery.DiscoveryHelper)5 NetworkStore (com.quorum.tessera.discovery.NetworkStore)5 Enclave (com.quorum.tessera.enclave.Enclave)5 Entity (jakarta.ws.rs.client.Entity)5