All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.dl.client.service.AuditorClient Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
package com.scalar.dl.client.service;

import com.google.inject.Inject;
import com.scalar.dl.client.config.ClientConfig;
import com.scalar.dl.client.exception.ClientException;
import com.scalar.dl.ledger.asset.AssetProof;
import com.scalar.dl.ledger.model.LedgerValidationResult;
import com.scalar.dl.ledger.rpc.ClientUtil;
import com.scalar.dl.ledger.service.StatusCode;
import com.scalar.dl.ledger.service.ThrowableConsumer;
import com.scalar.dl.ledger.service.ThrowableFunction;
import com.scalar.dl.rpc.AuditorGrpc;
import com.scalar.dl.rpc.AuditorPrivilegedGrpc;
import com.scalar.dl.rpc.CertificateRegistrationRequest;
import com.scalar.dl.rpc.ContractExecutionRequest;
import com.scalar.dl.rpc.ContractExecutionResponse;
import com.scalar.dl.rpc.ContractRegistrationRequest;
import com.scalar.dl.rpc.ContractsListingRequest;
import com.scalar.dl.rpc.ExecutionOrderingResponse;
import com.scalar.dl.rpc.ExecutionValidationRequest;
import com.scalar.dl.rpc.LedgerValidationRequest;
import com.scalar.dl.rpc.LedgerValidationResponse;
import io.grpc.ManagedChannel;
import io.grpc.netty.NettyChannelBuilder;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import javax.json.Json;
import javax.json.JsonObject;
import javax.net.ssl.SSLException;

public class AuditorClient extends AbstractAuditorClient {
  private final ManagedChannel channel;
  private final ManagedChannel privilegedChannel;
  private final AuditorGrpc.AuditorFutureStub auditorStub;
  private final AuditorPrivilegedGrpc.AuditorPrivilegedBlockingStub auditorPrivilegedStub;

  @Inject
  public AuditorClient(ClientConfig config) throws SSLException {
    NettyChannelBuilder builder =
        NettyChannelBuilder.forAddress(config.getAuditorHost(), config.getAuditorPort());

    ClientUtil.configureTls(
        builder, config.isAuditorTlsEnabled(), config.getAuditorTlsCaRootCert());
    ClientUtil.configureHeader(builder, config.getAuditorAuthorizationCredential());

    channel = builder.build();
    auditorStub = AuditorGrpc.newFutureStub(channel);

    privilegedChannel =
        NettyChannelBuilder.forAddress(config.getAuditorHost(), config.getAuditorPrivilegedPort())
            .usePlaintext()
            .build();
    auditorPrivilegedStub = AuditorPrivilegedGrpc.newBlockingStub(privilegedChannel);
  }

  @Override
  public void shutdown() {
    try {
      channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
      throw new ClientException(e.getMessage(), e, StatusCode.RUNTIME_ERROR);
    }
  }

  @Override
  public void register(CertificateRegistrationRequest request) {
    ThrowableConsumer f =
        r -> auditorPrivilegedStub.registerCert(request);
    try {
      accept(f, request);
    } catch (Exception e) {
      throwExceptionWithStatusCode(e);
    }
  }

  @Override
  public void register(ContractRegistrationRequest request) {
    ThrowableConsumer f =
        r -> auditorStub.registerContract(request).get();
    try {
      accept(f, request);
    } catch (Exception e) {
      throwExceptionWithStatusCode(e);
    }
  }

  @Override
  public JsonObject list(ContractsListingRequest request) {
    try {
      return toJsonObject(auditorStub.listContracts(request).get().getJson());
    } catch (Exception e) {
      throwExceptionWithStatusCode(e);
    }
    // Java compiler requires this line even though it won't come here
    return Json.createObjectBuilder().build();
  }

  @Override
  public ExecutionOrderingResponse order(ContractExecutionRequest request) {
    ThrowableFunction f =
        r -> auditorStub.orderExecution(request).get();
    try {
      return apply(f, request);
    } catch (Exception e) {
      throwExceptionWithStatusCode(e);
    }
    // Java compiler requires this line even though it won't come here
    return ExecutionOrderingResponse.getDefaultInstance();
  }

  @Override
  public ContractExecutionResponse validate(ExecutionValidationRequest request) {
    try {
      return auditorStub.validateExecution(request).get();
    } catch (Exception e) {
      throwExceptionWithStatusCode(e);
    }
    // Java compiler requires this line even though it won't come here
    return ContractExecutionResponse.getDefaultInstance();
  }

  @Override
  public LedgerValidationResult validate(LedgerValidationRequest request) {
    return validateAwait(validateAsync(request));
  }

  @Override
  public Future validateAsync(LedgerValidationRequest request) {
    try {
      return auditorStub.validateLedger(request);
    } catch (Exception e) {
      throwExceptionWithStatusCode(e);
    }
    // Java compiler requires this line even though it won't come here
    return CompletableFuture.completedFuture(LedgerValidationResponse.getDefaultInstance());
  }

  @Override
  public LedgerValidationResult validateAwait(Future future) {
    try {
      LedgerValidationResponse response = future.get();
      AssetProof proof = response.hasProof() ? new AssetProof(response.getProof()) : null;
      return new LedgerValidationResult(StatusCode.get(response.getStatusCode()), proof);
    } catch (Exception e) {
      throwExceptionWithStatusCode(e);
    }
    // Java compiler requires this line even though it won't come here
    return new LedgerValidationResult(StatusCode.RUNTIME_ERROR, null);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy