Skip to content

Commit

Permalink
Prepare for testing
Browse files Browse the repository at this point in the history
* Do not forward party allocation on duplicate parties
* Use timeProvider for currentTimestamp in successMappers
* Specialize the submission success mappers
* Move SequencerState and PreparedSubmission to `validate` package

changelog_begin
changelog_end
  • Loading branch information
tudor-da committed Jan 11, 2022
1 parent c6e5fc8 commit a947c55
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ object LedgerBridge {
)
else
ResourceOwner.forValue(() =>
new PassThroughLedgerBridge(participantId = participantConfig.participantId)
new PassThroughLedgerBridge(participantConfig.participantId, timeProvider)
)

private def buildConfigCheckingLedgerBridge(
Expand Down Expand Up @@ -92,6 +92,66 @@ object LedgerBridge {
servicesThreadPoolSize = servicesThreadPoolSize,
)

private[bridge] def packageUploadSuccess(
s: Submission.UploadPackages,
currentTimestamp: Time.Timestamp,
): Update.PublicPackageUpload =
Update.PublicPackageUpload(
archives = s.archives,
sourceDescription = s.sourceDescription,
recordTime = currentTimestamp,
submissionId = Some(s.submissionId),
)

private[bridge] def configChangedSuccess(
s: Submission.Config,
participantId: ParticipantId,
currentTimestamp: Time.Timestamp,
): Update.ConfigurationChanged =
Update.ConfigurationChanged(
recordTime = currentTimestamp,
submissionId = s.submissionId,
participantId = participantId,
newConfiguration = s.config,
)

private[bridge] def partyAllocationSuccess(
s: Submission.AllocateParty,
participantId: ParticipantId,
currentTimestamp: Time.Timestamp,
): Update.PartyAddedToParticipant = {
val party = s.hint.getOrElse(UUID.randomUUID().toString)
Update.PartyAddedToParticipant(
party = Ref.Party.assertFromString(party),
displayName = s.displayName.getOrElse(party),
participantId = participantId,
recordTime = currentTimestamp,
submissionId = Some(s.submissionId),
)
}

private[sandbox] def transactionAccepted(
transactionSubmission: Submission.Transaction,
index: Long,
currentTimestamp: Time.Timestamp,
): Update.TransactionAccepted = {
val submittedTransaction = transactionSubmission.transaction
val completionInfo = Some(
transactionSubmission.submitterInfo.toCompletionInfo(
Some(TransactionNodeStatistics(submittedTransaction))
)
)
Update.TransactionAccepted(
optCompletionInfo = completionInfo,
transactionMeta = transactionSubmission.transactionMeta,
transaction = CommittedTransaction(submittedTransaction),
transactionId = Ref.TransactionId.assertFromString(index.toString),
recordTime = currentTimestamp,
divulgedContracts = Nil,
blindingInfo = None,
)
}

private[bridge] def fromOffset(offset: Offset): Long = {
val offsetBytes = offset.toByteArray
if (offsetBytes.length > 8)
Expand All @@ -102,60 +162,5 @@ object LedgerBridge {
)
}

private[bridge] def partyAllocationSuccessMapper(
party: Ref.Party,
displayName: Option[String],
submissionId: Ref.SubmissionId,
participantId: ParticipantId,
): Update.PartyAddedToParticipant =
Update.PartyAddedToParticipant(
party = party,
displayName = displayName.getOrElse(party),
participantId = participantId,
recordTime = Time.Timestamp.now(),
submissionId = Some(submissionId),
)

def successMapper(submission: Submission, index: Long, participantId: Ref.ParticipantId): Update =
submission match {
case s: Submission.AllocateParty =>
val party = s.hint.getOrElse(UUID.randomUUID().toString)
Update.PartyAddedToParticipant(
party = Ref.Party.assertFromString(party),
displayName = s.displayName.getOrElse(party),
participantId = participantId,
recordTime = Time.Timestamp.now(),
submissionId = Some(s.submissionId),
)

case s: Submission.Config =>
Update.ConfigurationChanged(
recordTime = Time.Timestamp.now(),
submissionId = s.submissionId,
participantId = participantId,
newConfiguration = s.config,
)

case s: Submission.UploadPackages =>
Update.PublicPackageUpload(
archives = s.archives,
sourceDescription = s.sourceDescription,
recordTime = Time.Timestamp.now(),
submissionId = Some(s.submissionId),
)

case s: Submission.Transaction =>
Update.TransactionAccepted(
optCompletionInfo =
Some(s.submitterInfo.toCompletionInfo(Some(TransactionNodeStatistics(s.transaction)))),
transactionMeta = s.transactionMeta,
transaction = s.transaction.asInstanceOf[CommittedTransaction],
transactionId = Ref.TransactionId.assertFromString(index.toString),
recordTime = Time.Timestamp.now(),
divulgedContracts = Nil,
blindingInfo = None,
)
}

private[bridge] def toOffset(index: Long): Offset = Offset.fromByteArray(Longs.toByteArray(index))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,42 @@ package ledger.sandbox.bridge

import ledger.offset.Offset
import ledger.participant.state.v2.Update
import ledger.sandbox.bridge.LedgerBridge.{successMapper, toOffset}
import ledger.sandbox.bridge.LedgerBridge.{
configChangedSuccess,
packageUploadSuccess,
partyAllocationSuccess,
toOffset,
transactionAccepted,
}
import ledger.sandbox.domain.Submission
import lf.data.Ref

import lf.data.{Ref, Time}
import akka.NotUsed
import akka.stream.scaladsl.Flow
import com.daml.api.util.TimeProvider

private[bridge] class PassThroughLedgerBridge(participantId: Ref.ParticipantId)
extends LedgerBridge {
private[bridge] class PassThroughLedgerBridge(
participantId: Ref.ParticipantId,
timeProvider: TimeProvider,
) extends LedgerBridge {
override def flow: Flow[Submission, (Offset, Update), NotUsed] =
Flow[Submission].zipWithIndex
.map { case (submission, index) =>
val nextOffset = toOffset(index)
val update = successMapper(submission, index, participantId)
val update =
successMapper(submission, index, participantId, timeProvider.getCurrentTimestamp)
nextOffset -> update
}

private def successMapper(
submission: Submission,
index: Long,
participantId: Ref.ParticipantId,
currentTimestamp: Time.Timestamp,
): Update =
submission match {
case s: Submission.AllocateParty => partyAllocationSuccess(s, participantId, currentTimestamp)
case s: Submission.Config => configChangedSuccess(s, participantId, currentTimestamp)
case s: Submission.UploadPackages => packageUploadSuccess(s, currentTimestamp)
case s: Submission.Transaction => transactionAccepted(s, index, currentTimestamp)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import com.daml.ledger.offset.Offset
import com.daml.ledger.participant.state.index.v2.IndexService
import ConflictCheckingLedgerBridge._
import com.daml.ledger.participant.state.v2.CompletionInfo
import com.daml.ledger.sandbox.bridge.{
BridgeMetrics,
PreparedSubmission,
PreparedTransactionSubmission,
}
import com.daml.ledger.sandbox.bridge.BridgeMetrics
import com.daml.ledger.sandbox.domain.Rejection._
import com.daml.ledger.sandbox.domain.Submission.Transaction
import com.daml.lf.data.Ref
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.daml.ledger.offset.Offset
import com.daml.ledger.participant.state.index.v2.IndexService
import com.daml.ledger.participant.state.v2.Update
import com.daml.ledger.sandbox.bridge.validate.ConflictCheckingLedgerBridge._
import com.daml.ledger.sandbox.bridge.{BridgeMetrics, LedgerBridge, PreparedSubmission}
import com.daml.ledger.sandbox.bridge.{BridgeMetrics, LedgerBridge}
import com.daml.ledger.sandbox.domain._
import com.daml.lf.data.Ref
import com.daml.lf.transaction.{Transaction => LfTransaction}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ package com.daml.ledger.sandbox.bridge.validate
import com.daml.error.ContextualizedErrorLogger
import com.daml.ledger.participant.state.v2.CompletionInfo
import ConflictCheckingLedgerBridge._
import com.daml.ledger.sandbox.bridge.{
BridgeMetrics,
NoOpPreparedSubmission,
PreparedSubmission,
PreparedTransactionSubmission,
}
import com.daml.ledger.sandbox.bridge.BridgeMetrics
import com.daml.ledger.sandbox.domain.Rejection._
import com.daml.ledger.sandbox.domain._
import com.daml.lf.engine.Blinding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.sandbox.bridge
package com.daml.ledger.sandbox.bridge.validate

import com.daml.ledger.sandbox.domain.Submission
import com.daml.lf.data.Ref
Expand Down
Loading

0 comments on commit a947c55

Please sign in to comment.