use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.
the class FilterPayload method filter.
@Override
public boolean filter(BatchWorkflowContext context) {
EncodedPayload encodedPayload = context.getEncodedPayload();
PublicKey recipientPublicKey = context.getRecipientKey();
final boolean isCurrentNodeSender = encodedPayload.getRecipientKeys().contains(recipientPublicKey) && enclave.getPublicKeys().contains(encodedPayload.getSenderKey());
final boolean isRequestedNodeSender = Objects.equals(encodedPayload.getSenderKey(), recipientPublicKey);
return isCurrentNodeSender || isRequestedNodeSender;
}
use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method saveTransactionWithCallbackException.
@Test
public void saveTransactionWithCallbackException() throws Exception {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
EncodedPayload encodedPayload = mock(EncodedPayload.class);
byte[] payloadData = "payloadData".getBytes();
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
when(payloadEncoder.decode(payloadData)).thenReturn(encodedPayload);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
MessageHash transactionHash = new MessageHash(UUID.randomUUID().toString().getBytes());
EncryptedTransaction transaction = new EncryptedTransaction();
transaction.setPayload(encodedPayload);
transaction.setHash(transactionHash);
Callable<Void> callback = mock(Callable.class);
when(callback.call()).thenThrow(new Exception("OUCH"));
try {
encryptedTransactionDAO.save(transaction, callback);
failBecauseExceptionWasNotThrown(PersistenceException.class);
} catch (PersistenceException ex) {
assertThat(ex).isNotNull().hasMessageContaining("OUCH");
}
EntityManager entityManager = entityManagerFactory.createEntityManager();
final EncryptedTransaction result = entityManager.find(EncryptedTransaction.class, transactionHash);
assertThat(result).isNull();
verify(callback).call();
}
verify(payloadEncoder).encode(encodedPayload);
verifyNoMoreInteractions(payloadEncoder);
}
use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method saveDoesntAllowNullEncodedPayload.
@Test
public void saveDoesntAllowNullEncodedPayload() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(payloadEncoder.encode(encodedPayload)).thenReturn(null);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setHash(new MessageHash(new byte[] { 5 }));
encryptedTransaction.setPayload(encodedPayload);
try {
encryptedTransactionDAO.save(encryptedTransaction);
failBecauseExceptionWasNotThrown(PersistenceException.class);
} catch (PersistenceException ex) {
String expectedMessage = String.format(testConfig.getRequiredFieldColumnTemplate(), "ENCODED_PAYLOAD");
assertThat(ex).isInstanceOf(PersistenceException.class).hasMessageContaining(expectedMessage).hasMessageContaining("ENCODED_PAYLOAD");
}
}
verify(payloadEncoder).encode(encodedPayload);
}
use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method findByHashes.
@Test
public void findByHashes() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
final byte[] payloadData = "PAYLOADATA".getBytes();
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
List<EncryptedTransaction> transactions = IntStream.range(0, 100).mapToObj(i -> UUID.randomUUID().toString().getBytes()).map(MessageHash::new).map(h -> {
EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setHash(h);
encryptedTransaction.setPayload(encodedPayload);
entityManager.persist(encryptedTransaction);
return encryptedTransaction;
}).collect(Collectors.toList());
entityManager.getTransaction().commit();
Collection<MessageHash> hashes = transactions.stream().map(EncryptedTransaction::getHash).collect(Collectors.toList());
List<EncryptedTransaction> results = encryptedTransactionDAO.findByHashes(hashes);
assertThat(results).isNotEmpty().containsExactlyInAnyOrderElementsOf(transactions);
assertThat(results.stream().allMatch(r -> Arrays.equals(r.getEncodedPayload(), payloadData))).isTrue();
}
verify(payloadEncoder, times(100)).encode(encodedPayload);
}
use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method persistAddsTimestampToEntity.
@Test
public void persistAddsTimestampToEntity() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
final byte[] payloadData = "PAYLOADATA".getBytes();
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
when(payloadEncoder.decode(payloadData)).thenReturn(encodedPayload);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
MessageHash messageHash = new MessageHash(UUID.randomUUID().toString().getBytes());
final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setPayload(encodedPayload);
encryptedTransaction.setHash(messageHash);
encryptedTransactionDAO.save(encryptedTransaction);
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
final EncryptedTransaction retrieved = entityManager.find(EncryptedTransaction.class, messageHash);
entityManager.getTransaction().commit();
assertThat(retrieved).isNotNull();
assertThat(retrieved.getTimestamp()).isNotZero().isGreaterThan(0L);
}
verify(payloadEncoder).decode(payloadData);
verify(payloadEncoder).encode(encodedPayload);
verifyNoMoreInteractions(payloadEncoder);
}
Aggregations