Search in sources :

Example 6 with TransactionManager

use of com.quorum.tessera.transaction.TransactionManager in project tessera by ConsenSys.

the class TransactionResource4 method send.

@POST
@Path("send")
@Consumes({ MIME_TYPE_JSON_4 })
@Produces({ MIME_TYPE_JSON_4 })
public Response send(@NotNull @Valid @PrivacyValid final SendRequest sendRequest) {
    final PublicKey sender = Optional.ofNullable(sendRequest.getFrom()).map(base64Decoder::decode).map(PublicKey::from).orElseGet(transactionManager::defaultPublicKey);
    final Optional<PrivacyGroup.Id> privacyGroupId = Optional.ofNullable(sendRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
    final List<PublicKey> recipientList = privacyGroupId.map(privacyGroupManager::retrievePrivacyGroup).map(PrivacyGroup::getMembers).orElse(Stream.of(sendRequest).filter(sr -> Objects.nonNull(sr.getTo())).flatMap(s -> Stream.of(s.getTo())).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList()));
    final Set<MessageHash> affectedTransactions = Stream.ofNullable(sendRequest.getAffectedContractTransactions()).flatMap(Arrays::stream).map(base64Decoder::decode).map(MessageHash::new).collect(Collectors.toSet());
    final byte[] execHash = Optional.ofNullable(sendRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
    final PrivacyMode privacyMode = PrivacyMode.fromFlag(sendRequest.getPrivacyFlag());
    final Set<PublicKey> mandatoryRecipients = Stream.ofNullable(sendRequest.getMandatoryRecipients()).flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toUnmodifiableSet());
    final com.quorum.tessera.transaction.SendRequest.Builder requestBuilder = com.quorum.tessera.transaction.SendRequest.Builder.create().withRecipients(recipientList).withSender(sender).withPayload(sendRequest.getPayload()).withExecHash(execHash).withPrivacyMode(privacyMode).withAffectedContractTransactions(affectedTransactions).withMandatoryRecipients(mandatoryRecipients);
    privacyGroupId.ifPresent(requestBuilder::withPrivacyGroupId);
    final com.quorum.tessera.transaction.SendResponse response = transactionManager.send(requestBuilder.build());
    final String encodedKey = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).get();
    final String[] managedParties = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getManagedParties).orElse(Collections.emptySet()).stream().map(PublicKey::encodeToBase64).toArray(String[]::new);
    final SendResponse sendResponse = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).map(messageHash -> new SendResponse(messageHash, managedParties, sender.encodeToBase64())).get();
    final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedKey, StandardCharsets.UTF_8)).build();
    return Response.created(location).entity(sendResponse).build();
}
Also used : SendSignedRequest(com.quorum.tessera.api.SendSignedRequest) PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) SendResponse(com.quorum.tessera.api.SendResponse) LoggerFactory(org.slf4j.LoggerFactory) Valid(jakarta.validation.Valid) NotNull(jakarta.validation.constraints.NotNull) TEXT_PLAIN(jakarta.ws.rs.core.MediaType.TEXT_PLAIN) PrivacyValid(com.quorum.tessera.api.constraint.PrivacyValid) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) MIME_TYPE_JSON_4(com.quorum.tessera.version.MandatoryRecipientsVersion.MIME_TYPE_JSON_4) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) URI(java.net.URI) MessageHash(com.quorum.tessera.data.MessageHash) Schema(io.swagger.v3.oas.annotations.media.Schema) Logger(org.slf4j.Logger) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) TransactionManager(com.quorum.tessera.transaction.TransactionManager) PrivacyGroupManager(com.quorum.tessera.privacygroup.PrivacyGroupManager) jakarta.ws.rs(jakarta.ws.rs) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Parameter(io.swagger.v3.oas.annotations.Parameter) URLEncoder(java.net.URLEncoder) SendRequest(com.quorum.tessera.api.SendRequest) Stream(java.util.stream.Stream) UriBuilder(jakarta.ws.rs.core.UriBuilder) Tag(io.swagger.v3.oas.annotations.tags.Tag) SendRequest(com.quorum.tessera.api.SendRequest) MessageHash(com.quorum.tessera.data.MessageHash) URI(java.net.URI) PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode)

Example 7 with TransactionManager

use of com.quorum.tessera.transaction.TransactionManager in project tessera by ConsenSys.

the class TransactionManagerHolderTest method storeAndGet.

@Test
public void storeAndGet() {
    TransactionManagerHolder transactionManagerHolder = TransactionManagerHolder.INSTANCE;
    assertThat(transactionManagerHolder.getTransactionManager()).isNotPresent();
    TransactionManager transactionManager = mock(TransactionManager.class);
    assertThat(transactionManagerHolder.store(transactionManager)).isSameAs(transactionManager);
    assertThat(transactionManagerHolder.getTransactionManager()).containsSame(transactionManager);
}
Also used : TransactionManager(com.quorum.tessera.transaction.TransactionManager) Test(org.junit.Test)

Example 8 with TransactionManager

use of com.quorum.tessera.transaction.TransactionManager in project tessera by ConsenSys.

the class TransactionManagerProviderTest method provider.

@Test
public void provider() {
    try (var mockedStaticConfigFactory = mockStatic(ConfigFactory.class);
        var mockedStaticEncryptedTransactionDAO = mockStatic(EncryptedTransactionDAO.class);
        var mockedStaticEnclave = mockStatic(Enclave.class);
        var mockedStaticEncryptedRawTransactionDAO = mockStatic(EncryptedRawTransactionDAO.class);
        var mockedStaticPayloadPublisher = mockStatic(PayloadPublisher.class);
        var mockedStaticBatchPayloadPublisher = mockStatic(BatchPayloadPublisher.class);
        var mockedStaticPrivacyHelper = mockStatic(PrivacyHelper.class);
        var mockedStaticResendManager = mockStatic(ResendManager.class);
        var mockedStaticPayloadDigest = mockStatic(PayloadDigest.class)) {
        ConfigFactory configFactory = mock(ConfigFactory.class);
        Config config = mock(Config.class);
        when(configFactory.getConfig()).thenReturn(config);
        mockedStaticConfigFactory.when(ConfigFactory::create).thenReturn(configFactory);
        PayloadPublisher payloadPublisher = mock(PayloadPublisher.class);
        mockedStaticPayloadPublisher.when(PayloadPublisher::create).thenReturn(payloadPublisher);
        BatchPayloadPublisher batchPayloadPublisher = mock(BatchPayloadPublisher.class);
        mockedStaticBatchPayloadPublisher.when(BatchPayloadPublisher::create).thenReturn(batchPayloadPublisher);
        mockedStaticEncryptedRawTransactionDAO.when(EncryptedRawTransactionDAO::create).thenReturn(mock(EncryptedRawTransactionDAO.class));
        mockedStaticEnclave.when(Enclave::create).thenReturn(mock(Enclave.class));
        mockedStaticEncryptedTransactionDAO.when(EncryptedTransactionDAO::create).thenReturn(mock(EncryptedTransactionDAO.class));
        mockedStaticPrivacyHelper.when(PrivacyHelper::create).thenReturn(mock(PrivacyHelper.class));
        mockedStaticResendManager.when(ResendManager::create).thenReturn(mock(ResendManager.class));
        mockedStaticPayloadDigest.when(PayloadDigest::create).thenReturn(mock(PayloadDigest.class));
        TransactionManager transactionManager = TransactionManagerProvider.provider();
        assertThat(transactionManager).isNotNull();
        assertThat(TransactionManagerProvider.provider()).describedAs("Second invocation should return same instance").isSameAs(transactionManager);
        mockedStaticEncryptedTransactionDAO.verify(EncryptedTransactionDAO::create);
        mockedStaticEncryptedTransactionDAO.verifyNoMoreInteractions();
        mockedStaticEnclave.verify(Enclave::create);
        mockedStaticEnclave.verifyNoMoreInteractions();
        mockedStaticEncryptedRawTransactionDAO.verify(EncryptedRawTransactionDAO::create);
        mockedStaticEncryptedRawTransactionDAO.verifyNoMoreInteractions();
        mockedStaticPayloadPublisher.verify(PayloadPublisher::create);
        mockedStaticPayloadPublisher.verifyNoMoreInteractions();
        mockedStaticBatchPayloadPublisher.verify(BatchPayloadPublisher::create);
        mockedStaticBatchPayloadPublisher.verifyNoMoreInteractions();
        mockedStaticPrivacyHelper.verify(PrivacyHelper::create);
        mockedStaticPrivacyHelper.verifyNoMoreInteractions();
        mockedStaticResendManager.verify(ResendManager::create);
        mockedStaticResendManager.verifyNoMoreInteractions();
        mockedStaticPayloadDigest.verify(PayloadDigest::create);
        mockedStaticPayloadDigest.verifyNoMoreInteractions();
    }
}
Also used : ResendManager(com.quorum.tessera.transaction.resend.ResendManager) BatchPayloadPublisher(com.quorum.tessera.transaction.publish.BatchPayloadPublisher) PayloadPublisher(com.quorum.tessera.transaction.publish.PayloadPublisher) BatchPayloadPublisher(com.quorum.tessera.transaction.publish.BatchPayloadPublisher) Config(com.quorum.tessera.config.Config) EncryptedRawTransactionDAO(com.quorum.tessera.data.EncryptedRawTransactionDAO) Enclave(com.quorum.tessera.enclave.Enclave) PayloadDigest(com.quorum.tessera.enclave.PayloadDigest) TransactionManager(com.quorum.tessera.transaction.TransactionManager) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) PrivacyHelper(com.quorum.tessera.transaction.PrivacyHelper) ConfigFactory(com.quorum.tessera.config.ConfigFactory) Test(org.junit.Test)

Example 9 with TransactionManager

use of com.quorum.tessera.transaction.TransactionManager in project tessera by ConsenSys.

the class RecoveryProviderTest method provider.

@Test
public void provider() {
    try (var staticStagingEntityDAO = mockStatic(StagingEntityDAO.class);
        var staticDiscovery = mockStatic(Discovery.class);
        var staticBatchTransactionRequester = mockStatic(BatchTransactionRequester.class);
        var staticTransactionManager = mockStatic(TransactionManager.class)) {
        staticStagingEntityDAO.when(StagingEntityDAO::create).thenReturn(mock(StagingEntityDAO.class));
        staticDiscovery.when(Discovery::create).thenReturn(mock(Discovery.class));
        staticBatchTransactionRequester.when(BatchTransactionRequester::create).thenReturn(mock(BatchTransactionRequester.class));
        TransactionManager transactionManager = mock(TransactionManager.class);
        staticTransactionManager.when(TransactionManager::create).thenReturn(transactionManager);
        Recovery recovery = RecoveryProvider.provider();
        assertThat(recovery).isNotNull().isExactlyInstanceOf(RecoveryImpl.class);
        staticStagingEntityDAO.verify(StagingEntityDAO::create);
        staticStagingEntityDAO.verifyNoMoreInteractions();
        verifyNoMoreInteractions(transactionManager);
        staticDiscovery.verify(Discovery::create);
        staticDiscovery.verifyNoMoreInteractions();
        staticBatchTransactionRequester.verify(BatchTransactionRequester::create);
        staticBatchTransactionRequester.verifyNoMoreInteractions();
        staticTransactionManager.verify(TransactionManager::create);
        staticTransactionManager.verifyNoMoreInteractions();
    }
}
Also used : BatchTransactionRequester(com.quorum.tessera.recovery.resend.BatchTransactionRequester) TransactionManager(com.quorum.tessera.transaction.TransactionManager) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) Discovery(com.quorum.tessera.discovery.Discovery) Recovery(com.quorum.tessera.recovery.Recovery) Test(org.junit.Test)

Example 10 with TransactionManager

use of com.quorum.tessera.transaction.TransactionManager in project tessera by ConsenSys.

the class RecoveryProvider method provider.

public static Recovery provider() {
    StagingEntityDAO stagingEntityDAO = StagingEntityDAO.create();
    Discovery discovery = Discovery.create();
    BatchTransactionRequester batchTransactionRequester = BatchTransactionRequester.create();
    TransactionManager transactionManager = TransactionManager.create();
    return new RecoveryImpl(stagingEntityDAO, discovery, batchTransactionRequester, transactionManager);
}
Also used : BatchTransactionRequester(com.quorum.tessera.recovery.resend.BatchTransactionRequester) TransactionManager(com.quorum.tessera.transaction.TransactionManager) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) Discovery(com.quorum.tessera.discovery.Discovery)

Aggregations

TransactionManager (com.quorum.tessera.transaction.TransactionManager)16 Logger (org.slf4j.Logger)9 LoggerFactory (org.slf4j.LoggerFactory)9 MessageHash (com.quorum.tessera.data.MessageHash)8 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)8 PublicKey (com.quorum.tessera.encryption.PublicKey)8 PrivacyGroupManager (com.quorum.tessera.privacygroup.PrivacyGroupManager)8 Response (jakarta.ws.rs.core.Response)8 java.util (java.util)8 Stream (java.util.stream.Stream)8 Operation (io.swagger.v3.oas.annotations.Operation)7 Content (io.swagger.v3.oas.annotations.media.Content)7 Schema (io.swagger.v3.oas.annotations.media.Schema)7 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)7 Tag (io.swagger.v3.oas.annotations.tags.Tag)7 Valid (jakarta.validation.Valid)7 NotNull (jakarta.validation.constraints.NotNull)7 Collectors (java.util.stream.Collectors)7 URI (java.net.URI)6 PrivacyValid (com.quorum.tessera.api.constraint.PrivacyValid)5