use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.
the class LegacyResendWorkflowFactoryTest method failureAtStepReturnsFalse.
@Test
public void failureAtStepReturnsFalse() {
final PublicKey targetResendKey = PublicKey.from("target".getBytes());
final EncodedPayload testPayload = mock(EncodedPayload.class);
// causes a failure
when(testPayload.getPrivacyMode()).thenReturn(PrivacyMode.PARTY_PROTECTION);
when(testPayload.getSenderKey()).thenReturn(targetResendKey);
final EncryptedTransaction encryptedTx = new EncryptedTransaction();
final BatchWorkflow batchWorkflow = wfFactory.create();
final BatchWorkflowContext context = new BatchWorkflowContext();
context.setEncryptedTransaction(encryptedTx);
context.setRecipientKey(targetResendKey);
context.setBatchSize(1);
final boolean success = batchWorkflow.execute(context);
assertThat(success).isFalse();
verify(enclave).status();
}
use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.
the class SearchRecipientKeyForPayloadTest method executeHandleEnclaveExceptions.
@Test
public void executeHandleEnclaveExceptions() {
final List<Class<? extends Exception>> handledExceptionTypes = List.of(EnclaveNotAvailableException.class, IndexOutOfBoundsException.class, EncryptorException.class);
handledExceptionTypes.forEach(t -> {
final BatchWorkflowContext workflowEvent = new BatchWorkflowContext();
final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setHash(new MessageHash("sampleHash".getBytes()));
workflowEvent.setEncryptedTransaction(encryptedTransaction);
final EncodedPayload encodedPayload = EncodedPayload.Builder.create().build();
workflowEvent.setPayloadsToPublish(Set.of(encodedPayload));
final PublicKey publicKey = PublicKey.from("sample-public-key".getBytes());
when(enclave.getPublicKeys()).thenReturn(Set.of(publicKey));
when(enclave.unencryptTransaction(encodedPayload, publicKey)).thenThrow(t);
final Throwable throwable = catchThrowable(() -> searchRecipientKeyForPayload.execute(workflowEvent));
assertThat(throwable).isInstanceOf(RecipientKeyNotFoundException.class).hasMessage("No key found as recipient of message c2FtcGxlSGFzaA==");
verify(enclave).unencryptTransaction(encodedPayload, publicKey);
verify(enclave).getPublicKeys();
verifyNoMoreInteractions(enclave);
reset(enclave);
});
}
use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.
the class BatchResendManagerImplTest method useMaxResultsAlsoWhenBatchSizeTooLarge.
@Test
public void useMaxResultsAlsoWhenBatchSizeTooLarge() {
final ResendBatchRequest request = ResendBatchRequest.Builder.create().withBatchSize(10000000).withPublicKey(KEY_STRING).build();
List<EncryptedTransaction> transactions = IntStream.range(0, 5).mapToObj(i -> mock(EncryptedTransaction.class)).collect(Collectors.toUnmodifiableList());
when(encryptedTransactionDAO.transactionCount()).thenReturn(101L);
when(encryptedTransactionDAO.retrieveTransactions(lt(100), anyInt())).thenReturn(transactions);
when(encryptedTransactionDAO.retrieveTransactions(gt(99), anyInt())).thenReturn(singletonList(mock(EncryptedTransaction.class)));
final BatchWorkflow batchWorkflow = mock(BatchWorkflow.class);
when(batchWorkflow.getPublishedMessageCount()).thenReturn(999L);
when(batchWorkflowFactory.create(101L)).thenReturn(batchWorkflow);
final ResendBatchResponse result = manager.resendBatch(request);
assertThat(result.getTotal()).isEqualTo(999L);
verify(batchWorkflow, times(101)).execute(any(BatchWorkflowContext.class));
verify(encryptedTransactionDAO, times(21)).retrieveTransactions(anyInt(), anyInt());
verify(encryptedTransactionDAO).transactionCount();
verify(batchWorkflowFactory).create(101L);
}
use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.
the class BatchResendManagerImplTest method resendBatch.
@Test
public void resendBatch() {
ResendBatchRequest request = ResendBatchRequest.Builder.create().withBatchSize(3).withPublicKey(KEY_STRING).build();
List<EncryptedTransaction> transactions = IntStream.range(0, 5).mapToObj(i -> mock(EncryptedTransaction.class)).collect(Collectors.toUnmodifiableList());
when(encryptedTransactionDAO.transactionCount()).thenReturn(101L);
when(encryptedTransactionDAO.retrieveTransactions(lt(100), anyInt())).thenReturn(transactions);
when(encryptedTransactionDAO.retrieveTransactions(gt(99), anyInt())).thenReturn(singletonList(mock(EncryptedTransaction.class)));
BatchWorkflow batchWorkflow = mock(BatchWorkflow.class);
when(batchWorkflow.getPublishedMessageCount()).thenReturn(999L);
when(batchWorkflowFactory.create(101L)).thenReturn(batchWorkflow);
final ResendBatchResponse result = manager.resendBatch(request);
assertThat(result.getTotal()).isEqualTo(999L);
verify(batchWorkflow).getPublishedMessageCount();
verify(batchWorkflow, times(101)).execute(any(BatchWorkflowContext.class));
verify(encryptedTransactionDAO, times(21)).retrieveTransactions(anyInt(), anyInt());
verify(encryptedTransactionDAO).transactionCount();
verify(batchWorkflowFactory).create(101L);
}
use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.
the class LegacyResendManagerImpl method resendIndividual.
protected ResendResponse resendIndividual(final PublicKey targetResendKey, final MessageHash messageHash) {
final EncryptedTransaction encryptedTransaction = encryptedTransactionDAO.retrieveByHash(messageHash).orElseThrow(() -> new TransactionNotFoundException("Message with hash " + messageHash + " was not found"));
final EncodedPayload payload = encryptedTransaction.getPayload();
if (payload.getPrivacyMode() != PrivacyMode.STANDARD_PRIVATE) {
throw new EnhancedPrivacyNotSupportedException("Cannot resend enhanced privacy transaction in legacy resend");
}
if (!Objects.equals(payload.getSenderKey(), targetResendKey)) {
final EncodedPayload formattedPayload = EncodedPayload.Builder.forRecipient(payload, targetResendKey).build();
return ResendResponse.Builder.create().withPayload(formattedPayload).build();
}
// split all the boxes out into their own payload
final Set<EncodedPayload> allTxns = payload.getRecipientBoxes().stream().map(box -> EncodedPayload.Builder.from(payload).withNewRecipientKeys(Collections.emptyList()).withRecipientBoxes(List.of(box.getData())).build()).collect(Collectors.toSet());
final BatchWorkflowContext context = new BatchWorkflowContext();
context.setPayloadsToPublish(allTxns);
context.setEncryptedTransaction(encryptedTransaction);
new SearchRecipientKeyForPayload(enclave).execute(context);
final EncodedPayload.Builder builder = EncodedPayload.Builder.from(payload).withNewRecipientKeys(new ArrayList<>()).withRecipientBoxes(new ArrayList<>());
context.getPayloadsToPublish().forEach(formattedPayload -> {
builder.withRecipientKey(formattedPayload.getRecipientKeys().get(0));
builder.withRecipientBox(formattedPayload.getRecipientBoxes().get(0).getData());
});
return ResendResponse.Builder.create().withPayload(builder.build()).build();
}
Aggregations