Skip to content

Commit

Permalink
LF: remove type parameter from ContractInstance (#11419)
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da committed Oct 27, 2021
1 parent e474b2d commit d678a40
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ object ResultError {
ResultError(Error.Validation(validationError))
}

/** Intermediate result indicating that a [[ContractInst]] is required to complete the computation.
/** Intermediate result indicating that a [[ContractInstance]] is required to complete the computation.
* To resume the computation, the caller must invoke `resume` with the following argument:
* <ul>
* <li>`Some(contractInstance)`, if the caller can dereference `acoid` to `contractInstance`</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ final class ValueEnricher(
} yield versionedValue.copy(value = value)

def enrichContract(
contract: Value.ContractInst[Value]
): Result[Value.ContractInst[Value]] =
contract: Value.ContractInstance
): Result[Value.ContractInstance] =
for {
arg <- enrichContract(contract.template, contract.arg)
} yield contract.copy(arg = arg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class EngineTest
val BasicTests_WithKey = Identifier(basicTestsPkgId, withKeyTemplate)
val withKeyContractInst: VersionedContractInstance =
assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(basicTestsPkgId, withKeyTemplate),
ValueRecord(
Some(BasicTests_WithKey),
Expand All @@ -92,7 +92,7 @@ class EngineTest
Map(
toContractId("BasicTests:Simple:1") ->
assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(basicTestsPkgId, "BasicTests:Simple"),
ValueRecord(
Some(Identifier(basicTestsPkgId, "BasicTests:Simple")),
Expand All @@ -103,7 +103,7 @@ class EngineTest
),
toContractId("BasicTests:CallablePayout:1") ->
assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(basicTestsPkgId, "BasicTests:CallablePayout"),
ValueRecord(
Some(Identifier(basicTestsPkgId, "BasicTests:CallablePayout")),
Expand Down Expand Up @@ -1171,7 +1171,7 @@ class EngineTest
targs: ImmArray[(Option[Name], Value)],
) =
assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(basicTestsPkgId, tid),
ValueRecord(Some(Identifier(basicTestsPkgId, tid)), targs),
"",
Expand Down Expand Up @@ -1289,7 +1289,7 @@ class EngineTest

val fetchedContract =
assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(basicTestsPkgId, fetchedStrTid),
ValueRecord(
Some(Identifier(basicTestsPkgId, fetchedStrTid)),
Expand Down Expand Up @@ -1346,7 +1346,7 @@ class EngineTest
val lookerUpCid = toContractId("2")
val lookerUpInst =
assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(basicTestsPkgId, lookerUpTemplate),
ValueRecord(Some(lookerUpTemplateId), ImmArray((Some[Name]("p"), ValueParty(alice)))),
"",
Expand Down Expand Up @@ -1594,7 +1594,7 @@ class EngineTest
val fetcherTemplateId = Identifier(basicTestsPkgId, fetcherTemplate)
val fetcherCid = toContractId("2")
val fetcherInst = assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(basicTestsPkgId, fetcherTemplate),
ValueRecord(Some(fetcherTemplateId), ImmArray((Some[Name]("p"), ValueParty(alice)))),
"",
Expand Down Expand Up @@ -1671,7 +1671,7 @@ class EngineTest
val cid = toContractId("BasicTests:WithKey:1")
val fetcherCid = toContractId("42")
val fetcherInst = assertAsVersionedContract(
ContractInst(
ContractInstance(
fetcherId,
ValueRecord(
None,
Expand Down Expand Up @@ -1959,7 +1959,7 @@ class EngineTest
val cid1 = toContractId("1")
val cid2 = toContractId("2")
val keyedInst = assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(multiKeysPkgId, "MultiKeys:Keyed"),
ValueRecord(None, ImmArray((None, ValueParty(party)))),
"",
Expand Down Expand Up @@ -2085,7 +2085,7 @@ class EngineTest
val cid = toContractId("1")
val contracts = Map(
cid -> assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(exceptionsPkgId, "Exceptions:K"),
ValueRecord(None, ImmArray((None, ValueParty(party)), (None, ValueInt64(0)))),
"",
Expand Down Expand Up @@ -2195,7 +2195,7 @@ class EngineTest
val cid = toContractId("1")
val contracts = Map(
cid -> assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(exceptionsPkgId, "Exceptions:K"),
ValueRecord(None, ImmArray((None, ValueParty(party)), (None, ValueInt64(0)))),
"",
Expand Down Expand Up @@ -2272,7 +2272,7 @@ class EngineTest
val cid = toContractId("1")
val contracts = Map(
cid -> assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(exceptionsPkgId, "Exceptions:K"),
ValueRecord(None, ImmArray((None, ValueParty(party)), (None, ValueInt64(0)))),
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ReinterpretTest
Map(
toContractId("ReinterpretTests:MySimple:1") ->
assertAsVersionedContract(
ContractInst(
ContractInstance(
TypeConName(miniTestsPkgId, "ReinterpretTests:MySimple"),
ValueRecord(
Some(Identifier(miniTestsPkgId, "ReinterpretTests:MySimple")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,10 @@ private[lf] object Pretty {
def prettyEventId(n: EventId): Doc =
text(n.toLedgerString)

def prettyContractInst(coinst: ContractInst[Value]): Doc =
def prettyContractInst(coinst: ContractInstance): Doc =
(prettyIdentifier(coinst.template) / text("with:") &
prettyValue(false)(coinst.arg)).nested(4)

def prettyVersionedContractInst(coinst: ContractInst[Tx.Value]): Doc =
(prettyIdentifier(coinst.template) / text("with:") &
prettyValue(false)(coinst.arg.value)).nested(4)

def prettyTypeConName(tycon: TypeConName): Doc =
text(tycon.qualifiedName.toString) + char('@') + prettyPackageId(tycon.packageId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.daml.lf.data._
import com.daml.lf.language.LanguageVersion
import com.daml.lf.transaction.{Transaction => Tx}
import com.daml.lf.value.Value
import com.daml.lf.value.Value.{ContractId, ContractInst, VersionedContractInstance}
import com.daml.lf.value.Value.{ContractId, ContractInstance, VersionedContractInstance}

import scala.Ordering.Implicits.infixOrderingOps
import scala.annotation.tailrec
Expand Down Expand Up @@ -77,7 +77,7 @@ final class TransactionBuilder(pkgTxVersion: Ref.PackageId => TransactionVersion

def newCid: ContractId = TransactionBuilder.newV1Cid

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

def create(
Expand Down Expand Up @@ -320,14 +320,14 @@ object TransactionBuilder {
data.assertRight(asVersionedValue(value, supportedVersions))

def asVersionedContract(
contract: ContractInst[Value],
contract: ContractInstance,
supportedVersions: VersionRange[TransactionVersion] = TransactionVersion.DevVersions,
): Either[String, VersionedContractInstance] =
assignVersion(contract.arg, supportedVersions)
.map(VersionedContractInstance(_, contract))

def assertAsVersionedContract(
contract: ContractInst[Value],
contract: ContractInstance,
supportedVersions: VersionRange[TransactionVersion] = TransactionVersion.DevVersions,
): VersionedContractInstance =
data.assertRight(asVersionedContract(contract, supportedVersions))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ object ValueGenerators {

val genNonEmptyParties: Gen[Set[Party]] = ^(party, genMaybeEmptyParties)((hd, tl) => tl + hd)

val contractInstanceGen: Gen[ContractInst[Value]] = {
val contractInstanceGen: Gen[ContractInstance] = {
for {
template <- idGen
arg <- valueGen
agreement <- Arbitrary.arbitrary[String]
} yield ContractInst(template, arg, agreement)
} yield ContractInstance(template, arg, agreement)
}

val versionedContractInstanceGen: Gen[Value.VersionedContractInstance] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ object Node {

def versionedArg: VersionedValue = versionValue(arg)

def coinst: Value.ContractInst[Value] =
Value.ContractInst(templateId, arg, agreementText)
def coinst: Value.ContractInstance =
Value.ContractInstance(templateId, arg, agreementText)

def versionedCoinst: Value.VersionedContractInstance =
Value.VersionedContractInstance(version, templateId, arg, agreementText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ object Transaction {
type Value = Value.VersionedValue

@deprecated("use com.daml.value.Value.VersionedContractInstance", since = "1.8.0")
type ContractInst = Value.VersionedContractInstance
type ContractInstance = Value.VersionedContractInstance

/** Transaction nodes */
type Node = Node.GenNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,21 @@ object TransactionCoder {
def decodeContractInstance(
decodeCid: ValueCoder.DecodeCid,
protoCoinst: TransactionOuterClass.ContractInstance,
): Either[DecodeError, Value.ContractInst[Value]] =
): Either[DecodeError, Value.ContractInstance] =
for {
id <- ValueCoder.decodeIdentifier(protoCoinst.getTemplateId)
value <- ValueCoder.decodeValue(decodeCid, protoCoinst.getArgVersioned)
} yield Value.ContractInst(id, value, (protoCoinst.getAgreement))
} yield Value.ContractInstance(id, value, (protoCoinst.getAgreement))

private[this] def decodeContractInstance(
decodeCid: ValueCoder.DecodeCid,
nodeVersion: TransactionVersion,
protoCoinst: TransactionOuterClass.ContractInstance,
): Either[DecodeError, Value.ContractInst[Value]] =
): Either[DecodeError, Value.ContractInstance] =
for {
id <- ValueCoder.decodeIdentifier(protoCoinst.getTemplateId)
value <- decodeValue(decodeCid, nodeVersion, protoCoinst.getArgVersioned)
} yield Value.ContractInst(id, value, protoCoinst.getAgreement)
} yield Value.ContractInstance(id, value, protoCoinst.getAgreement)

def decodeVersionedContractInstance(
decodeCid: ValueCoder.DecodeCid,
Expand Down Expand Up @@ -507,7 +507,7 @@ object TransactionCoder {
for {
tmplId <- ValueCoder.decodeIdentifier(protoCreate.getTemplateId)
arg <- ValueCoder.decodeValue(decodeCid, nodeVersion, protoCreate.getArgUnversioned)
} yield Value.ContractInst(tmplId, arg, protoCreate.getAgreement)
} yield Value.ContractInstance(tmplId, arg, protoCreate.getAgreement)
}
stakeholders <- toPartySet(protoCreate.getStakeholdersList)
signatories <- toPartySet(protoCreate.getSignatoriesList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,31 +198,17 @@ object Value {
new `Value Equal instance`

/** A contract instance is a value plus the template that originated it. */
final case class ContractInst[+Val](
final case class ContractInstance(
template: Identifier,
arg: Val,
arg: Value,
agreementText: String,
) {

def map[Val2](f: Val => Val2): ContractInst[Val2] =
def map(f: Value => Value): ContractInstance =
copy(arg = f(arg))
}

object ContractInst {

implicit class CidContainerInstance[Val <: value.CidContainer[Val]](inst: ContractInst[Val])
extends value.CidContainer[ContractInst[Val]] {
override protected def self = inst
final override def mapCid(f: ContractId => ContractId): ContractInst[Val] =
inst.copy(arg = inst.arg.mapCid(f))
}

implicit def equalInstance[Val: Equal]: Equal[ContractInst[Val]] =
ScalazEqual.withNatural(Equal[Val].equalIsNatural) { (a, b) =>
import a._
val ContractInst(bTemplate, bArg, bAgreementText) = b
template == bTemplate && arg === bArg && agreementText == bAgreementText
}
def mapCid(f: ContractId => ContractId): ContractInstance =
copy(arg = arg.mapCid(f))
}

final case class VersionedContractInstance(
Expand All @@ -231,12 +217,12 @@ object Value {
arg: Value,
agreementText: String,
) {
def coinst = ContractInst(template, arg, agreementText)
def coinst = ContractInstance(template, arg, agreementText)
def versionedArg = VersionedValue(version, arg)
}

object VersionedContractInstance {
def apply(version: TransactionVersion, coinst: ContractInst[Value]): VersionedContractInstance =
def apply(version: TransactionVersion, coinst: ContractInstance): VersionedContractInstance =
VersionedContractInstance(version, coinst.template, coinst.arg, coinst.agreementText)
def apply(
template: Identifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,13 @@ class ValueSpec

"VersionedValue" - {

val pkgId = Ref.PackageId.assertFromString("pkgId")
val tmplId = Ref.Identifier(pkgId, Ref.QualifiedName.assertFromString("Mod:Template"))

"does not bump version when" - {

"ensureNoCid is used " in {
val value = VersionedValue(TransactionVersion.minVersion, ValueUnit)
val contract = ContractInst(tmplId, value, "agreed")
value.ensureNoCid.map(_.version) shouldBe Right(TransactionVersion.minVersion)
contract.ensureNoCid.map(_.arg.version) shouldBe Right(TransactionVersion.minVersion)

}

}

}

"ContractID.V1.build" - {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.daml.lf.data.{FrontStack, ImmArray, Ref, Time}
import com.daml.lf.transaction.Node._
import com.daml.lf.transaction._
import com.daml.lf.transaction.test.TransactionBuilder
import com.daml.lf.value.Value.{ContractId, ContractInst, ValueText, VersionedContractInstance}
import com.daml.lf.value.Value.{ContractId, ContractInstance, ValueText, VersionedContractInstance}
import com.daml.lf.value.{Value => LfValue}
import com.daml.logging.LoggingContext
import com.daml.platform.store.appendonlydao.PersistenceResponse
Expand Down Expand Up @@ -149,7 +149,7 @@ private[dao] trait JdbcLedgerDaoSuite extends JdbcLedgerDaoBackend {
private[this] val txVersion = TransactionVersion.StableVersions.min
private[this] def newBuilder() = new TransactionBuilder(_ => txVersion)

protected final val someContractInstance = ContractInst(
protected final val someContractInstance = ContractInstance(
someTemplateId,
someContractArgument,
someAgreement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.daml.lf.data.ImmArray
import com.daml.lf.data.Time.Timestamp
import com.daml.lf.transaction.GlobalKey
import com.daml.lf.transaction.test.TransactionBuilder
import com.daml.lf.value.Value.{ContractInst, ValueRecord, ValueText}
import com.daml.lf.value.Value.{ContractInstance, ValueRecord, ValueText}
import com.daml.logging.LoggingContext
import com.daml.metrics.Metrics
import com.daml.platform.store.EventSequentialId
Expand Down Expand Up @@ -552,7 +552,7 @@ object MutableCacheBackedContractStoreSpec {
Some(templateId),
ImmArray.Empty,
)
val contractInstance = ContractInst(
val contractInstance = ContractInstance(
templateId,
contractArgument,
"some agreement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import com.daml.lf.data.Time.{Timestamp => LfTimestamp}
import com.daml.lf.engine.Error
import com.daml.lf.transaction.test.TransactionBuilder
import com.daml.lf.transaction.{BlindingInfo, NodeId, TransactionOuterClass, TransactionVersion}
import com.daml.lf.value.Value.{ContractId, ContractInst, ValueText}
import com.daml.lf.value.Value.{ContractId, ContractInstance, ValueText}
import com.daml.lf.value.ValueOuterClass
import com.daml.logging.{ContextualizedLogger, LoggingContext}
import com.fasterxml.jackson.databind.ObjectMapper
Expand Down Expand Up @@ -676,7 +676,7 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues {

private def lfContractInstance(discriminator: String) =
new TransactionBuilder(_ => txVersion).versionContract(
ContractInst(
ContractInstance(
Ref.Identifier.assertFromString("some:template:name"),
ValueText(discriminator),
"",
Expand Down

0 comments on commit d678a40

Please sign in to comment.