Search in sources :

Example 16 with EncodedPayload

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

the class AsyncBatchPayloadPublisherTest method publishPayloadStripsAndPublishes.

@Test
public void publishPayloadStripsAndPublishes() throws InterruptedException {
    final Executor realExecutor = Executors.newSingleThreadExecutor();
    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();
    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, times(2)).countDown();
    verify(countDownLatch).await();
}
Also used : Executor(java.util.concurrent.Executor) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 17 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload 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 18 with EncodedPayload

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

the class AsyncBatchPayloadPublisherTest method publishPayloadWrapsInterruptedException.

@Test
public void publishPayloadWrapsInterruptedException() throws InterruptedException {
    EncodedPayload payload = mock(EncodedPayload.class);
    PublicKey recipient = PublicKey.from("RECIPIENT".getBytes());
    PublicKey otherRecipient = PublicKey.from("OTHERRECIPIENT".getBytes());
    List<PublicKey> recipients = List.of(recipient, otherRecipient);
    InterruptedException cause = new InterruptedException("some exception");
    doThrow(cause).when(countDownLatch).await();
    Throwable ex = catchThrowable(() -> asyncPublisher.publishPayload(payload, recipients));
    assertThat(ex).isExactlyInstanceOf(BatchPublishPayloadException.class);
    assertThat(ex).hasCause(cause);
    verify(executorFactory).createCachedThreadPool();
    verify(executor, times(2)).execute(any(Runnable.class));
    verify(countDownLatchFactory).create(2);
    verify(countDownLatch).await();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) ThrowableAssert.catchThrowable(org.assertj.core.api.ThrowableAssert.catchThrowable) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 19 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload 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 20 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload 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)

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