Skip to content

Commit

Permalink
Defining first endpoints for ledger-service/http-json (digital-asset#…
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-da authored Jul 3, 2019
1 parent 4dc9cd0 commit d06191e
Show file tree
Hide file tree
Showing 31 changed files with 1,120 additions and 68 deletions.
1 change: 1 addition & 0 deletions daml-lf/interface/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ da_scala_library(
"//daml-lf:__subpackages__",
"//extractor:__subpackages__",
"//language-support:__subpackages__",
"//ledger-service:__subpackages__",
"//navigator:__subpackages__",
],
deps = [
Expand Down
3 changes: 1 addition & 2 deletions extractor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ load(
compileDependencies = [
# this has to come first other as somehow a different instance of grpc-core.jar
"//3rdparty/jvm/io/grpc:grpc_services",
"//daml-lf/archive:daml_lf_java_proto",
"//daml-lf/archive:daml_lf_archive_scala",
"//daml-lf/data",
"//daml-lf/interface",
"//daml-lf/transaction",
Expand All @@ -25,6 +23,7 @@ compileDependencies = [
"//language-support/scala/bindings",
"//ledger/ledger-api-client",
"//ledger/ledger-api-domain",
"//ledger-service/utils",
"//3rdparty/jvm/io/netty:netty_tcnative_boringssl_static",
"//3rdparty/jvm/com/chuusai:shapeless",
"//3rdparty/jvm/org/spire_math:kind_projector",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import akka.stream.{KillSwitches, ActorMaterializer}
import akka.stream.scaladsl.{RestartSource, Sink}
import com.digitalasset.extractor.Types._
import com.digitalasset.extractor.config.{ExtractorConfig, SnapshotEndSetting}
import com.digitalasset.extractor.ledger.LedgerReader
import com.digitalasset.ledger.service.LedgerReader
import com.digitalasset.extractor.ledger.types.TransactionTree
import com.digitalasset.extractor.ledger.types.TransactionTree._
import com.digitalasset.extractor.ledger.LedgerReader.PackageStore
import com.digitalasset.ledger.service.LedgerReader.PackageStore
import com.digitalasset.extractor.targets.Target
import com.digitalasset.extractor.writers.Writer
import com.digitalasset.extractor.writers.Writer.RefreshPackages
Expand Down Expand Up @@ -115,7 +115,8 @@ class Extractor[T <: Target](config: ExtractorConfig, target: T) extends StrictL

private def fetchPackages(client: LedgerClient, writer: Writer): Future[PackageStore] = {
for {
packageStoreE <- LedgerReader.createPackageStore(client): Future[String \/ PackageStore]
packageStoreE <- LedgerReader.createPackageStore(client.packageClient): Future[
String \/ PackageStore]
packageStore <- toFuture(packageStoreE): Future[PackageStore]
_ <- writer.handlePackages(packageStore)
} yield packageStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package com.digitalasset.extractor

import com.digitalasset.daml.lf.iface
import com.digitalasset.daml.lf.iface.{Enum, TypeConName}
import com.digitalasset.extractor.ledger.LedgerReader.PackageStore
import com.digitalasset.ledger.service.LedgerReader.PackageStore
import doobie.util.fragment.Fragment

import scala.util.control.NoStackTrace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,14 @@

package com.digitalasset.extractor.helpers

import com.digitalasset.daml.lf.data.Ref.QualifiedName
import com.digitalasset.daml.lf.iface.Interface
import com.digitalasset.daml.lf.iface.InterfaceType.Template
import com.digitalasset.extractor.config.TemplateConfig
import com.digitalasset.ledger.api.v1.value.Identifier

import scala.collection.breakOut

object TemplateIds {

def getTemplateIds(interfaces: Set[Interface]): Set[Identifier] =
interfaces.flatMap(getTemplateIds)

private def getTemplateIds(interface: Interface): Set[Identifier] =
interface.typeDecls.collect {
case (qn: QualifiedName, _: Template) =>
Identifier(
packageId = interface.packageId,
name = qn.qualifiedName,
moduleName = qn.module.dottedName,
entityName = qn.name.dottedName)
}(breakOut)
com.digitalasset.ledger.service.TemplateIds.getTemplateIds(interfaces)

def intersection(known: Set[Identifier], requested: Set[TemplateConfig]): Set[Identifier] =
known.filter(a => requested.contains(asConf(a)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package com.digitalasset.extractor.writers

import com.digitalasset.daml.lf.iface.{Interface, InterfaceType}
import com.digitalasset.extractor.config.ExtractorConfig
import com.digitalasset.extractor.ledger.LedgerReader
import com.digitalasset.extractor.ledger.LedgerReader.PackageStore
import com.digitalasset.ledger.service.LedgerReader
import com.digitalasset.ledger.service.LedgerReader.PackageStore
import com.digitalasset.extractor.ledger.types._
import com.digitalasset.extractor.targets.PostgreSQLTarget
import com.digitalasset.extractor.Types._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.digitalasset.extractor.writers

import com.digitalasset.daml.lf.iface.Interface
import com.digitalasset.extractor.ledger.LedgerReader.PackageStore
import com.digitalasset.ledger.service.LedgerReader.PackageStore
import com.digitalasset.extractor.ledger.types.{Event, TransactionTree}
import com.digitalasset.extractor.writers.Writer.RefreshPackages

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.digitalasset.extractor.writers

import com.digitalasset.extractor.config.ExtractorConfig
import com.digitalasset.extractor.ledger.LedgerReader.PackageStore
import com.digitalasset.ledger.service.LedgerReader.PackageStore
import com.digitalasset.extractor.ledger.types.{Identifier, TransactionTree}
import com.digitalasset.extractor.targets._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package com.digitalasset.extractor.writers.postgresql

import com.digitalasset.daml.lf.iface
import com.digitalasset.extractor.ledger.types._
import com.digitalasset.extractor.ledger.LedgerReader.PackageStore
import com.digitalasset.ledger.service.LedgerReader.PackageStore
import com.digitalasset.extractor.writers.Writer.RefreshPackages

import doobie.free.connection.ConnectionIO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package com.digitalasset.extractor.writers.postgresql
import com.digitalasset.daml.lf.iface
import com.digitalasset.daml.lf.iface.reader.InterfaceType
import com.digitalasset.daml.lf.iface.Record
import com.digitalasset.extractor.ledger.LedgerReader.PackageStore
import com.digitalasset.ledger.service.LedgerReader.PackageStore
import com.digitalasset.extractor.ledger.types._
import com.digitalasset.extractor.Types.{DataIntegrityError, FullyAppliedType}
import com.digitalasset.extractor.Types.FullyAppliedType._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.digitalasset.extractor.writers.postgresql

import com.digitalasset.extractor.ledger.LedgerReader.PackageStore
import com.digitalasset.ledger.service.LedgerReader.PackageStore
import com.digitalasset.extractor.ledger.types.{CreatedEvent, ExercisedEvent, TransactionTree}
import com.digitalasset.extractor.writers.Writer
import com.digitalasset.extractor.writers.postgresql.DataFormat.TemplateInfo
Expand Down
30 changes: 0 additions & 30 deletions ledger-api/http-json/BUILD.bazel

This file was deleted.

51 changes: 51 additions & 0 deletions ledger-service/http-json/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

load(
"//bazel_tools:scala.bzl",
"da_scala_binary",
"da_scala_library",
"da_scala_test",
)

http_json_deps = [
"//3rdparty/jvm/ch/qos/logback:logback_classic",
"//3rdparty/jvm/com/github/pureconfig",
"//3rdparty/jvm/com/github/scopt",
"//3rdparty/jvm/com/typesafe/akka:akka_http",
"//3rdparty/jvm/com/typesafe/akka:akka_http_spray_json",
"//3rdparty/jvm/com/typesafe/akka:akka_slf4j",
"//3rdparty/jvm/com/typesafe/scala_logging",
"//3rdparty/jvm/org/scalaz:scalaz_core",
"//ledger-service/utils",
"//daml-lf/interface",
"//language-support/scala/bindings-akka",
"//ledger-api/rs-grpc-bridge",
]

da_scala_library(
name = "http-json",
srcs = glob(["src/main/scala/**/*.scala"]),
tags = ["maven_coordinates=com.digitalasset.ledger-service:http-json:__VERSION__"],
deps = http_json_deps,
)

da_scala_binary(
name = "http-json-bin",
srcs = glob(["src/main/scala/**/*.scala"]),
main_class = "com.digitalasset.http.Main",
deps = [":http-json"] + http_json_deps,
)

da_scala_test(
name = "tests",
size = "small",
srcs = glob(["src/test/scala/**/*.scala"]),
deps = [
":http-json",
"//3rdparty/jvm/org/scalacheck",
"//3rdparty/jvm/org/scalaz:scalaz_scalacheck_binding",
"//3rdparty/jvm/org/scalatest:scalatest",
"//ledger/sandbox:sandbox",
] + http_json_deps,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.digitalasset.http

import akka.stream.Materializer
import akka.stream.scaladsl.Sink
import com.digitalasset.http.ContractsService._
import com.digitalasset.http.util.FutureUtil.toFuture
import com.digitalasset.ledger.api.v1.transaction_filter.{
Filters,
InclusiveFilters,
TransactionFilter
}
import com.digitalasset.ledger.api.v1.value.{Identifier, Value}
import com.digitalasset.ledger.client.services.acs.ActiveContractSetClient
import scalaz.\/
import scalaz.std.string._

import scala.concurrent.{ExecutionContext, Future}

class ContractsService(
resolveTemplateIds: ResolveTemplateIds,
activeContractSetClient: ActiveContractSetClient,
parallelism: Int = 8)(implicit ec: ExecutionContext, mat: Materializer) {

def lookup(
jwtPayload: domain.JwtPayload,
request: domain.ContractLookupRequest[Value]): Future[Option[domain.ActiveContract[Value]]] =
Future.failed(new RuntimeException("contract lookup not yet supported")) // TODO

def search(jwtPayload: domain.JwtPayload, request: domain.GetActiveContractsRequest)
: Future[Seq[domain.GetActiveContractsResponse[Value]]] =
search(jwtPayload.party, request.templateIds)

def search(party: String, templateIds: Set[domain.TemplateId.OptionalPkg])
: Future[Seq[domain.GetActiveContractsResponse[Value]]] =
for {
templateIds <- toFuture(resolveTemplateIds(templateIds))
activeContracts <- activeContractSetClient
.getActiveContracts(transactionFilter(party, templateIds), verbose = true)
.mapAsyncUnordered(parallelism)(gacr =>
toFuture(domain.GetActiveContractsResponse.fromLedgerApi(gacr)))
.runWith(Sink.seq)
} yield activeContracts

private def transactionFilter(party: String, templateIds: List[Identifier]): TransactionFilter = {
val filters =
if (templateIds.isEmpty) Filters.defaultInstance
else Filters(Some(InclusiveFilters(templateIds)))
TransactionFilter(Map(party -> filters))
}
}

object ContractsService {
final case class Error(message: String)

type ResolveTemplateIds =
Set[domain.TemplateId.OptionalPkg] => PackageService.Error \/ List[Identifier]
}
Loading

0 comments on commit d06191e

Please sign in to comment.