Skip to content

Commit

Permalink
LF: Abstract VersionedValue and VersionedContractInstance (#11533)
Browse files Browse the repository at this point in the history
* LF: Abstract VersionedValue and VersionedContractInstance

CHANGELOG_BEGIN
CHANGELOG_END

* formatting
  • Loading branch information
remyhaemmerle-da authored Nov 9, 2021
1 parent b87acab commit 9218116
Show file tree
Hide file tree
Showing 41 changed files with 182 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ final class Conversions(
}

private def convertVersionedValue(value: V.VersionedValue): proto.Value =
convertValue(value.value)
convertValue(value.unversioned)

def convertValue(value: V): proto.Value = {
val builder = proto.Value.newBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ final class ValueEnricher(
versionedValue: VersionedValue,
): Result[VersionedValue] =
for {
value <- enrichValue(typ, versionedValue.value)
} yield versionedValue.copy(value = value)
value <- enrichValue(typ, versionedValue.unversioned)
} yield versionedValue.map(_ => value)

def enrichContract(
contract: Value.ContractInstance
Expand All @@ -58,8 +58,8 @@ final class ValueEnricher(
contract: Value.VersionedContractInstance
): Result[Value.VersionedContractInstance] =
for {
arg <- enrichValue(Ast.TTyCon(contract.template), contract.arg)
} yield contract.copy(arg = arg)
arg <- enrichValue(Ast.TTyCon(contract.unversioned.template), contract.unversioned.arg)
} yield contract.map(_.copy(arg = arg))

def enrichContract(tyCon: Identifier, value: Value): Result[Value] =
enrichValue(Ast.TTyCon(tyCon), value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@ import com.daml.bazeltools.BazelRunfiles
import com.daml.lf.archive.UniversalArchiveDecoder
import com.daml.lf.command.{ApiCommand, Commands, CreateCommand, ExerciseCommand}
import com.daml.lf.data.FrontStack
import com.daml.lf.data.Ref.{Name, Party, ParticipantId, PackageId, Identifier, QualifiedName}
import com.daml.lf.data.Ref.{Identifier, Name, PackageId, ParticipantId, Party, QualifiedName}
import com.daml.lf.data.Time
import com.daml.lf.data.{ImmArray, Bytes}
import com.daml.lf.data.{Bytes, ImmArray}
import com.daml.lf.language.Ast.Package
import com.daml.lf.ledger.FailedAuthorization.{
CreateMissingAuthorization,
ExerciseMissingAuthorization,
}
import com.daml.lf.transaction.GlobalKeyWithMaintainers
import com.daml.lf.transaction.{
GlobalKeyWithMaintainers,
SubmittedTransaction,
TransactionVersion,
Versioned,
}
import com.daml.lf.transaction.Transaction.Metadata
import com.daml.lf.transaction.{SubmittedTransaction, TransactionVersion}
import com.daml.lf.value.Value.{
ContractId,
ValueRecord,
ValueParty,
VersionedContractInstance,
ContractInstance,
ValueContractId,
ValueList,
ValueParty,
ValueRecord,
VersionedContractInstance,
}

import java.io.File

import org.scalatest.Inside
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
Expand Down Expand Up @@ -59,29 +63,31 @@ class AuthPropagationSpec extends AnyFreeSpec with Matchers with Inside with Baz
ContractId.V1.assertBuild(crypto.Hash.hashPrivateKey(s), dummySuffix)
}

private def t1InstanceFor(party: Party): VersionedContractInstance = {
VersionedContractInstance(
private def t1InstanceFor(party: Party): VersionedContractInstance =
Versioned(
TransactionVersion.VDev,
"T1",
ValueRecord(
Some("T1"),
ImmArray((Some[Name]("party"), ValueParty(party))),
ContractInstance(
"T1",
ValueRecord(
Some("T1"),
ImmArray((Some[Name]("party"), ValueParty(party))),
),
"",
),
"",
)
}

private def x1InstanceFor(party: Party): VersionedContractInstance = {
VersionedContractInstance(
private def x1InstanceFor(party: Party): VersionedContractInstance =
Versioned(
TransactionVersion.VDev,
"X1",
ValueRecord(
Some("X1"),
ImmArray((Some[Name]("party"), ValueParty(party))),
ContractInstance(
"X1",
ValueRecord(
Some("X1"),
ImmArray((Some[Name]("party"), ValueParty(party))),
),
"",
),
"",
)
}

private val defaultContracts: Map[ContractId, VersionedContractInstance] =
Map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ object ScenarioLedger {
//----------------------------------------------------------------------------

def collectCoids(value: VersionedValue): Set[ContractId] =
collectCoids(value.value)
collectCoids(value.unversioned)

/** Collect all contract ids appearing in a value
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private[lf] object Pretty {

def prettyVersionedKeyWithMaintainers(key: Node.KeyWithMaintainers[VersionedValue]): Doc =
// the maintainers are induced from the key -- so don't clutter
prettyValue(false)(key.key.value)
prettyValue(false)(key.key.unversioned)

def prettyEventInfo(l: ScenarioLedger, txId: TransactionId)(nodeId: NodeId): Doc = {
def arrowRight(d: Doc) = text("└─>") & d
Expand Down Expand Up @@ -308,7 +308,7 @@ private[lf] object Pretty {
text(id.qualifiedName.toString) + char('@') + prettyPackageId(id.packageId)

def prettyVersionedValue(verbose: Boolean)(v: VersionedValue): Doc =
prettyValue(verbose)(v.value)
prettyValue(verbose)(v.unversioned)

// Pretty print a value. If verbose then the top-level value is printed with type constructor
// if possible.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package speedy

import java.util
import java.util.regex.Pattern

import com.daml.lf.data.Ref._
import com.daml.lf.data._
import com.daml.lf.data.Numeric.Scale
Expand All @@ -18,9 +17,14 @@ import com.daml.lf.speedy.Speedy._
import com.daml.lf.speedy.SResult._
import com.daml.lf.speedy.SValue.{SValue => _, _}
import com.daml.lf.speedy.SValue.{SValue => SV}
import com.daml.lf.transaction.{Transaction => Tx}
import com.daml.lf.transaction.{
GlobalKey,
GlobalKeyWithMaintainers,
Node,
Versioned,
Transaction => Tx,
}
import com.daml.lf.value.{Value => V}
import com.daml.lf.transaction.{GlobalKey, GlobalKeyWithMaintainers, Node}
import com.daml.lf.value.Value.ValueArithmeticError
import com.daml.nameof.NameOf
import com.daml.scalautil.Statement.discard
Expand Down Expand Up @@ -1051,7 +1055,7 @@ private[lf] object SBuiltin {
coid,
templateId,
onLedger.committers,
{ case V.VersionedContractInstance(_, actualTmplId, arg, _) =>
{ case Versioned(_, V.ContractInstance(actualTmplId, arg, _)) =>
if (actualTmplId != templateId) {
machine.ctrl =
SEDamlException(IE.WronglyTypedContract(coid, templateId, actualTmplId))
Expand Down Expand Up @@ -1111,7 +1115,7 @@ private[lf] object SBuiltin {
coid,
ifaceId,
onLedger.committers,
{ case V.VersionedContractInstance(_, actualTmplId, arg, _) =>
{ case Versioned(_, V.ContractInstance(actualTmplId, arg, _)) =>
machine.compiledPackages.getDefinition(
ImplementsDefRef(actualTmplId, ifaceId)
) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ final class TransactionBuilder(pkgTxVersion: Ref.PackageId => TransactionVersion
def newCid: ContractId = TransactionBuilder.newV1Cid

def versionContract(contract: Value.ContractInstance): value.Value.VersionedContractInstance =
VersionedContractInstance(pkgTxVersion(contract.template.packageId), contract)
Versioned(pkgTxVersion(contract.template.packageId), contract)

def create(
id: ContractId,
Expand Down Expand Up @@ -299,7 +299,7 @@ object TransactionBuilder {
value: Value,
supportedVersions: VersionRange[TransactionVersion] = TransactionVersion.DevVersions,
): Either[String, TxValue] =
assignVersion(value, supportedVersions).map(Value.VersionedValue(_, value))
assignVersion(value, supportedVersions).map(Versioned(_, value))

@throws[IllegalArgumentException]
def assertAsVersionedValue(
Expand All @@ -313,7 +313,7 @@ object TransactionBuilder {
supportedVersions: VersionRange[TransactionVersion] = TransactionVersion.DevVersions,
): Either[String, VersionedContractInstance] =
assignVersion(contract.arg, supportedVersions)
.map(VersionedContractInstance(_, contract))
.map(Versioned(_, contract))

def assertAsVersionedContract(
contract: ContractInstance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ package test

import com.daml.lf.data.Ref._
import com.daml.lf.data._
import com.daml.lf.transaction.{Transaction, Node, NodeId, TransactionVersion, VersionedTransaction}
import com.daml.lf.transaction.{
Transaction,
Node,
NodeId,
TransactionVersion,
Versioned,
VersionedTransaction,
}
import com.daml.lf.transaction.test.TransactionBuilder
import com.daml.lf.value.Value._
import org.scalacheck.{Arbitrary, Gen}
Expand Down Expand Up @@ -246,7 +253,7 @@ object ValueGenerators {
value <- valueGen
minVersion = TransactionBuilder.assertAssignVersion(value)
version <- transactionVersionGen(minVersion)
} yield VersionedValue(version, value)
} yield Versioned(version, value)

private[lf] val genMaybeEmptyParties: Gen[Set[Party]] = Gen.listOf(party).map(_.toSet)

Expand All @@ -265,7 +272,7 @@ object ValueGenerators {
template <- idGen
arg <- versionedValueGen
agreement <- Arbitrary.arbitrary[String]
} yield Value.VersionedContractInstance(arg.version, template, arg.value, agreement)
} yield arg.map(Value.ContractInstance(template, _, agreement))

val keyWithMaintainersGen: Gen[Node.KeyWithMaintainers[Value]] = {
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object Node {
def byInterface: Option[TypeConName]

protected def versionValue[Cid2 >: ContractId](v: Value): VersionedValue =
VersionedValue(version, v)
Versioned(version, v)
}

@deprecated("use Node.LeafOnlyAction", since = "1.18.0")
Expand Down Expand Up @@ -104,7 +104,7 @@ object Node {
Value.ContractInstance(templateId, arg, agreementText)

def versionedCoinst: Value.VersionedContractInstance =
Value.VersionedContractInstance(version, templateId, arg, agreementText)
Versioned(version, coinst)

def versionedKey: Option[KeyWithMaintainers[Value.VersionedValue]] =
key.map(_.map(versionValue))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ object TransactionCoder {
coinst: Value.VersionedContractInstance,
): Either[EncodeError, TransactionOuterClass.ContractInstance] =
ValueCoder
.encodeVersionedValue(encodeCid, coinst.version, coinst.arg)
.encodeVersionedValue(encodeCid, coinst.version, coinst.unversioned.arg)
.map(
TransactionOuterClass.ContractInstance
.newBuilder()
.setTemplateId(ValueCoder.encodeIdentifier(coinst.template))
.setTemplateId(ValueCoder.encodeIdentifier(coinst.unversioned.template))
.setArgVersioned(_)
.setAgreement(coinst.agreementText)
.setAgreement(coinst.unversioned.agreementText)
.build()
)

Expand Down Expand Up @@ -173,11 +173,12 @@ object TransactionCoder {
for {
id <- ValueCoder.decodeIdentifier(protoCoinst.getTemplateId)
value <- ValueCoder.decodeVersionedValue(decodeCid, protoCoinst.getArgVersioned)
} yield Value.VersionedContractInstance(
value.version,
id,
value.value,
protoCoinst.getAgreement,
} yield value.map(
Value.ContractInstance(
id,
_,
protoCoinst.getAgreement,
)
)

private[this] def encodeKeyWithMaintainers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ object Util {
def normalizeVersionedValue(
value: VersionedValue
): Either[String, VersionedValue] =
normalizeValue(value.value, value.version).map(normalized => value.copy(value = normalized))
normalizeValue(value.unversioned, value.version).map(normalized => value.map(_ => normalized))

def normalizeContract(
contract: VersionedContractInstance
): Either[String, VersionedContractInstance] =
normalizeValue(contract.arg, contract.version)
.map(normalized => contract.copy(arg = normalized))
normalizeValue(contract.unversioned.arg, contract.version)
.map(normalized => contract.map(_.copy(arg = normalized)))

def normalizeKey(
key: Node.KeyWithMaintainers[Value],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.lf
package transaction

case class Versioned[+X](version: TransactionVersion, unversioned: X) {

def map[Y](f: X => Y): Versioned[Y] = Versioned(version, f(unversioned))

def setVersion(version: TransactionVersion): Versioned[X] = Versioned(version, unversioned)

}
Loading

0 comments on commit 9218116

Please sign in to comment.