Skip to content

Commit

Permalink
Transplant tests from a removed class
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
hubert-da committed Jul 13, 2021
1 parent 450cc60 commit 40cec9a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private[transaction] class ModelConformanceValidator(engine: Engine, metrics: Me
}
}

private def validateCausalMonotonicity(
private[validation] def validateCausalMonotonicity(
transactionEntry: DamlTransactionEntrySummary,
commitContext: CommitContext,
rejections: Rejections,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@

package com.daml.ledger.participant.state.kvutils.committer.transaction.validation

import com.daml.ledger.participant.state.kvutils.TestHelpers.lfTuple
import java.time.{Instant, ZoneOffset, ZonedDateTime}

import com.codahale.metrics.MetricRegistry
import com.daml.ledger.participant.state.kvutils.Conversions
import com.daml.ledger.participant.state.kvutils.DamlKvutils.{
DamlContractState,
DamlStateKey,
DamlStateValue,
DamlTransactionEntry,
}
import com.daml.ledger.participant.state.kvutils.TestHelpers.{createCommitContext, lfTuple}
import com.daml.ledger.participant.state.kvutils.committer.StepContinue
import com.daml.ledger.participant.state.kvutils.committer.transaction.{
DamlTransactionEntrySummary,
Rejections,
}
import com.daml.ledger.participant.state.v1.{RejectionReason, RejectionReasonV0}
import com.daml.lf.engine.{Error => LfError}
import com.daml.lf.engine.{Engine, Error => LfError}
import com.daml.lf.transaction
import com.daml.lf.transaction.test.TransactionBuilder
import com.daml.lf.transaction.{
Expand All @@ -16,6 +31,10 @@ import com.daml.lf.transaction.{
Transaction,
}
import com.daml.lf.value.Value
import com.daml.logging.LoggingContext
import com.daml.metrics.Metrics
import com.google.protobuf.ByteString
import org.mockito.ArgumentMatchersSugar.eqTo
import org.mockito.MockitoSugar
import org.scalatest.Inspectors.forEvery
import org.scalatest.matchers.should.Matchers
Expand All @@ -24,11 +43,16 @@ import org.scalatest.wordspec.AnyWordSpec
class ModelConformanceValidatorSpec extends AnyWordSpec with Matchers with MockitoSugar {
import ModelConformanceValidatorSpec._

private implicit val loggingContext: LoggingContext = LoggingContext.ForTesting

private val metrics = new Metrics(new MetricRegistry)
private val modelConformanceValidator = new ModelConformanceValidator(new Engine(), metrics)

private val txBuilder = TransactionBuilder()

private val createInput = create("#inputContractId")
private val create1 = create("#someContractId")
private val create2 = create("#otherContractId")
private val create1 = create(aContractId)
private val create2 = create("#anotherContractId")
private val otherKeyCreate = create(
contractId = "#contractWithOtherKey",
signatories = Seq(aKeyMaintainer),
Expand Down Expand Up @@ -56,6 +80,54 @@ class ModelConformanceValidatorSpec extends AnyWordSpec with Matchers with Mocki
builder.build() -> lookupId
}

private val aTransactionEntry = DamlTransactionEntrySummary(
DamlTransactionEntry.newBuilder
.setSubmissionSeed(aSubmissionSeed)
.setLedgerEffectiveTime(Conversions.buildTimestamp(ledgerEffectiveTime))
.setTransaction(Conversions.encodeTransaction({
val transactionBuilder = TransactionBuilder()
transactionBuilder.add(lookup1)
transactionBuilder.buildSubmitted()
}))
.build
)

"validateCausalMonotonicity" should {
"create StepContinue in case of correct input" in {
modelConformanceValidator
.validateCausalMonotonicity(
aTransactionEntry,
createCommitContext(
None,
Map(aContractKey -> Some(aStateValueActiveAt(ledgerEffectiveTime.minusSeconds(1)))),
),
mock[Rejections],
) shouldBe StepContinue(aTransactionEntry)
}

"reject transaction in case of incorrect input" in {
val rejections = mock[Rejections]

modelConformanceValidator
.validateCausalMonotonicity(
aTransactionEntry,
createCommitContext(
None,
Map(aContractKey -> Some(aStateValueActiveAt(ledgerEffectiveTime.plusSeconds(1)))),
),
rejections,
)

verify(rejections).buildRejectionStep(
eqTo(aTransactionEntry),
eqTo(RejectionReasonV0.InvalidLedgerTime("Causal monotonicity violated")),
eqTo(None),
)(eqTo(loggingContext))

succeed
}
}

"rejectionReasonForValidationError" when {
"there is a mismatch in lookupByKey nodes" should {
"report an inconsistency if the contracts are not created in the same transaction" in {
Expand Down Expand Up @@ -145,10 +217,24 @@ class ModelConformanceValidatorSpec extends AnyWordSpec with Matchers with Mocki
}

object ModelConformanceValidatorSpec {
private val aContractId = "#aContractId"
private val aContractKey = DamlStateKey.newBuilder().setContractId(aContractId).build()

private val aKeyMaintainer = "maintainer"
private val aKey = "key"
private val aDummyValue = TransactionBuilder.record("field" -> "value")

private val aSubmissionSeed = ByteString.copyFromUtf8("a" * 32)
private val ledgerEffectiveTime =
ZonedDateTime.of(2021, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC).toInstant

private def aStateValueActiveAt(activeAt: Instant) =
DamlStateValue.newBuilder
.setContractState(
DamlContractState.newBuilder.setActiveAt(Conversions.buildTimestamp(activeAt))
)
.build

private def mkMismatch(
recorded: (Transaction.Transaction, NodeId),
replayed: (Transaction.Transaction, NodeId),
Expand Down

0 comments on commit 40cec9a

Please sign in to comment.