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));
}
}
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();
}
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)));
}
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);
}
Aggregations