Skip to content

Commit

Permalink
Sandbox Classic: Privatize everything. (#7130)
Browse files Browse the repository at this point in the history
* sandbox-classic: Split out `LedgerBackedWriteService`.

* sandbox-classic: Privatize everything.

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
SamirTalwar authored Aug 14, 2020
1 parent 66ae38d commit dcb21dc
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.daml.ledger.api.domain.LedgerId
import com.daml.platform.sandbox.config.LedgerName
import scalaz.syntax.tag._

class LedgerIdGenerator(name: LedgerName) {
private[sandbox] final class LedgerIdGenerator(name: LedgerName) {
def generateRandomId(): LedgerId =
LedgerId(s"${name.unwrap.toLowerCase()}-${UUID.randomUUID().toString}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import com.daml.platform.store.Contract.{ActiveContract, DivulgedContract}
import com.daml.platform.store._
import scalaz.syntax.std.map._

case class InMemoryActiveLedgerState(
private[sandbox] case class InMemoryActiveLedgerState(
activeContracts: Map[ContractId, ActiveContract],
divulgedContracts: Map[ContractId, DivulgedContract],
keys: Map[GlobalKey, ContractId],
reverseKeys: Map[ContractId, GlobalKey],
parties: Map[Party, PartyDetails])
extends ActiveLedgerState[InMemoryActiveLedgerState] {
parties: Map[Party, PartyDetails],
) extends ActiveLedgerState[InMemoryActiveLedgerState] {

def isVisibleForDivulgees(contractId: ContractId, forParty: Party): Boolean =
activeContracts
Expand Down Expand Up @@ -174,7 +174,7 @@ case class InMemoryActiveLedgerState(
}
}

object InMemoryActiveLedgerState {
private[sandbox] object InMemoryActiveLedgerState {
def empty: InMemoryActiveLedgerState =
InMemoryActiveLedgerState(Map.empty, Map.empty, Map.empty, Map.empty, Map.empty)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.platform.sandbox.stores

import java.util.concurrent.CompletionStage

import com.daml.api.util.TimeProvider
import com.daml.daml_lf_dev.DamlLf.Archive
import com.daml.ledger.api.health.HealthStatus
import com.daml.ledger.participant.state.v1.{
Configuration,
SubmissionId,
SubmissionResult,
SubmittedTransaction,
SubmitterInfo,
TransactionMeta,
WriteService
}
import com.daml.lf.data.Ref.Party
import com.daml.lf.data.Time
import com.daml.logging.LoggingContext
import com.daml.logging.LoggingContext.withEnrichedLoggingContext
import com.daml.platform.sandbox.stores.ledger.{Ledger, PartyIdGenerator}

import scala.compat.java8.FutureConverters

private[stores] final class LedgerBackedWriteService(ledger: Ledger, timeProvider: TimeProvider)(
implicit loggingContext: LoggingContext,
) extends WriteService {

override def currentHealth(): HealthStatus = ledger.currentHealth()

override def submitTransaction(
submitterInfo: SubmitterInfo,
transactionMeta: TransactionMeta,
transaction: SubmittedTransaction,
): CompletionStage[SubmissionResult] =
withEnrichedLoggingContext(
"submitter" -> submitterInfo.submitter,
"applicationId" -> submitterInfo.applicationId,
"commandId" -> submitterInfo.commandId,
"deduplicateUntil" -> submitterInfo.deduplicateUntil.toString,
"submissionTime" -> transactionMeta.submissionTime.toInstant.toString,
"workflowId" -> transactionMeta.workflowId.getOrElse(""),
"ledgerTime" -> transactionMeta.ledgerEffectiveTime.toInstant.toString,
) { implicit loggingContext =>
FutureConverters.toJava(
ledger.publishTransaction(submitterInfo, transactionMeta, transaction)
)
}

override def allocateParty(
hint: Option[Party],
displayName: Option[String],
submissionId: SubmissionId,
): CompletionStage[SubmissionResult] = {
val party = hint.getOrElse(PartyIdGenerator.generateRandomId())
withEnrichedLoggingContext(
"party" -> party,
"submissionId" -> submissionId,
) { implicit loggingContext =>
FutureConverters.toJava(ledger.publishPartyAllocation(submissionId, party, displayName))
}
}

// WritePackagesService
override def uploadPackages(
submissionId: SubmissionId,
payload: List[Archive],
sourceDescription: Option[String]
): CompletionStage[SubmissionResult] =
withEnrichedLoggingContext(
"submissionId" -> submissionId,
"description" -> sourceDescription.getOrElse(""),
"packageHashes" -> payload.iterator.map(_.getHash).mkString(","),
) { implicit loggingContext =>
FutureConverters.toJava(
ledger
.uploadPackages(submissionId, timeProvider.getCurrentTime, sourceDescription, payload))
}

// WriteConfigService
override def submitConfiguration(
maxRecordTime: Time.Timestamp,
submissionId: SubmissionId,
config: Configuration,
): CompletionStage[SubmissionResult] =
withEnrichedLoggingContext(
"maxRecordTime" -> maxRecordTime.toInstant.toString,
"submissionId" -> submissionId,
"configGeneration" -> config.generation.toString,
"configMaxDeduplicationTime" -> config.maxDeduplicationTime.toString,
) { implicit loggingContext =>
FutureConverters.toJava(ledger.publishConfiguration(maxRecordTime, submissionId, config))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,15 @@
package com.daml.platform.sandbox.stores

import java.time.Instant
import java.util.concurrent.CompletionStage

import akka.stream.Materializer
import akka.stream.scaladsl.{Sink, Source}
import com.daml.api.util.TimeProvider
import com.daml.daml_lf_dev.DamlLf.Archive
import com.daml.ledger.api.health.HealthStatus
import com.daml.ledger.participant.state.index.v2._
import com.daml.ledger.participant.state.v1.{
ApplicationId => _,
LedgerId => _,
TransactionId => _,
_
}
import com.daml.ledger.participant.state.{v1 => ParticipantState}
import com.daml.lf.data.Ref.Party
import com.daml.lf.data.{ImmArray, Time}
import com.daml.ledger.participant.state.index.v2.IndexService
import com.daml.ledger.participant.state.v1.{ParticipantId, WriteService}
import com.daml.lf.data.ImmArray
import com.daml.lf.transaction.TransactionCommitter
import com.daml.logging.LoggingContext
import com.daml.logging.LoggingContext.withEnrichedLoggingContext
import com.daml.metrics.Metrics
import com.daml.platform.common.LedgerIdMode
import com.daml.platform.configuration.ServerRole
Expand All @@ -32,24 +21,23 @@ import com.daml.platform.packages.InMemoryPackageStore
import com.daml.platform.sandbox.LedgerIdGenerator
import com.daml.platform.sandbox.config.LedgerName
import com.daml.platform.sandbox.stores.ledger.ScenarioLoader.LedgerEntryOrBump
import com.daml.platform.sandbox.stores.ledger._
import com.daml.platform.sandbox.stores.ledger.inmemory.InMemoryLedger
import com.daml.platform.sandbox.stores.ledger.sql.{SqlLedger, SqlStartMode}
import com.daml.platform.sandbox.stores.ledger.{Ledger, MeteredLedger}
import com.daml.platform.store.dao.events.LfValueTranslation
import com.daml.resources.{Resource, ResourceOwner}
import org.slf4j.LoggerFactory

import scala.compat.java8.FutureConverters
import scala.concurrent.duration._
import scala.concurrent.duration.{DurationInt, FiniteDuration}
import scala.concurrent.{ExecutionContext, Future}

trait IndexAndWriteService {
private[sandbox] trait IndexAndWriteService {
def indexService: IndexService

def writeService: WriteService
}

object SandboxIndexAndWriteService {
private[sandbox] object SandboxIndexAndWriteService {
//TODO: internalise the template store as well
private val logger = LoggerFactory.getLogger(SandboxIndexAndWriteService.getClass)

Expand Down Expand Up @@ -172,72 +160,3 @@ object SandboxIndexAndWriteService {
}
}
}

final class LedgerBackedWriteService(ledger: Ledger, timeProvider: TimeProvider)(
implicit loggingContext: LoggingContext,
) extends WriteService {

override def currentHealth(): HealthStatus = ledger.currentHealth()

override def submitTransaction(
submitterInfo: ParticipantState.SubmitterInfo,
transactionMeta: ParticipantState.TransactionMeta,
transaction: SubmittedTransaction,
): CompletionStage[ParticipantState.SubmissionResult] =
withEnrichedLoggingContext(
"submitter" -> submitterInfo.submitter,
"applicationId" -> submitterInfo.applicationId,
"commandId" -> submitterInfo.commandId,
"deduplicateUntil" -> submitterInfo.deduplicateUntil.toString,
"submissionTime" -> transactionMeta.submissionTime.toInstant.toString,
"workflowId" -> transactionMeta.workflowId.getOrElse(""),
"ledgerTime" -> transactionMeta.ledgerEffectiveTime.toInstant.toString,
) { implicit loggingContext =>
FutureConverters.toJava(
ledger.publishTransaction(submitterInfo, transactionMeta, transaction)
)
}

override def allocateParty(
hint: Option[Party],
displayName: Option[String],
submissionId: SubmissionId): CompletionStage[SubmissionResult] = {
val party = hint.getOrElse(PartyIdGenerator.generateRandomId())
withEnrichedLoggingContext(
"party" -> party,
"submissionId" -> submissionId,
) { implicit loggingContext =>
FutureConverters.toJava(ledger.publishPartyAllocation(submissionId, party, displayName))
}
}

// WritePackagesService
override def uploadPackages(
submissionId: SubmissionId,
payload: List[Archive],
sourceDescription: Option[String]
): CompletionStage[SubmissionResult] =
withEnrichedLoggingContext(
"submissionId" -> submissionId,
"description" -> sourceDescription.getOrElse(""),
"packageHashes" -> payload.iterator.map(_.getHash).mkString(","),
) { implicit loggingContext =>
FutureConverters.toJava(
ledger
.uploadPackages(submissionId, timeProvider.getCurrentTime, sourceDescription, payload))
}

// WriteConfigService
override def submitConfiguration(
maxRecordTime: Time.Timestamp,
submissionId: SubmissionId,
config: Configuration): CompletionStage[SubmissionResult] =
withEnrichedLoggingContext(
"maxRecordTime" -> maxRecordTime.toInstant.toString,
"submissionId" -> submissionId,
"configGeneration" -> config.generation.toString,
"configMaxDeduplicationTime" -> config.maxDeduplicationTime.toString,
) { implicit loggingContext =>
FutureConverters.toJava(ledger.publishConfiguration(maxRecordTime, submissionId, config))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.daml.platform.store.ReadOnlyLedger

import scala.concurrent.Future

trait Ledger extends ReadOnlyLedger {
private[sandbox] trait Ledger extends ReadOnlyLedger {

def publishTransaction(
submitterInfo: SubmitterInfo,
Expand Down Expand Up @@ -50,7 +50,7 @@ trait Ledger extends ReadOnlyLedger {

}

object Ledger {
private[sandbox] object Ledger {

type Divulgence = Relation[ContractId, Party]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ private class MeteredLedger(ledger: Ledger, metrics: Metrics)

}

object MeteredLedger {
private[sandbox] object MeteredLedger {
def apply(ledger: Ledger, metrics: Metrics): Ledger = new MeteredLedger(ledger, metrics)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.util.UUID

import com.daml.lf.data.Ref

object PartyIdGenerator {
private[stores] object PartyIdGenerator {
def generateRandomId(): Ref.Party =
Ref.Party.assertFromString(s"party-${UUID.randomUUID().toString.take(8)}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.daml.ledger.participant.state.v1.Offset
// This utility object makes sure that the offset representation is always
// padded with zeros up to 8 bytes.
// This should only be used in SqlLedger and InMemoryLedger.
object SandboxOffset {
private[ledger] object SandboxOffset {

def numBytes = 8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import scala.annotation.tailrec
import scala.collection.breakOut
import scala.collection.mutable.ArrayBuffer

object ScenarioLoader {
private[sandbox] object ScenarioLoader {

/** When loading from the scenario, we also specify by how much to bump the
* ledger end after each entry. This is because in the scenario transaction
Expand Down
Loading

0 comments on commit dcb21dc

Please sign in to comment.