Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LF: Drop typeparameter from KeyWithMaintainers #11625

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -547,12 +547,12 @@ final class Conversions(
}

def convertKeyWithMaintainers(
key: Node.KeyWithMaintainers[V.VersionedValue]
key: Node.VersionedKeyWithMaintainers
): proto.KeyWithMaintainers = {
proto.KeyWithMaintainers
.newBuilder()
.setKey(convertVersionedValue(key.key))
.addAllMaintainers(key.maintainers.map(convertParty).asJava)
.setKey(convertVersionedValue(key.map(_.key)))
.addAllMaintainers(key.unversioned.maintainers.map(convertParty).asJava)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ sealed trait Event extends Product with Serializable {
final case class CreateEvent(
contractId: ContractId,
templateId: Identifier,
contractKey: Option[KeyWithMaintainers[Value]],
contractKey: Option[KeyWithMaintainers],
argument: Value,
agreementText: String,
signatories: Set[Party],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,18 @@ final class ValueEnricher(
handleLookup(interface.lookupTemplateKey(tyCon))
.flatMap(key => enrichValue(key.typ, value))

def enrichVersionedContractKey(
tyCon: Identifier,
value: VersionedValue,
): Result[VersionedValue] =
handleLookup(interface.lookupTemplateKey(tyCon))
.flatMap(key => enrichVersionedValue(key.typ, value))

private val ResultNone = ResultDone(None)

def enrichContractKey(
tyCon: Identifier,
key: Node.KeyWithMaintainers[Value],
): Result[Node.KeyWithMaintainers[Value]] =
key: Node.KeyWithMaintainers,
): Result[Node.KeyWithMaintainers] =
enrichContractKey(tyCon, key.key).map(normalizedKey => key.copy(key = normalizedKey))

def enrichContractKey(
tyCon: Identifier,
key: Option[Node.KeyWithMaintainers[Value]],
): Result[Option[Node.KeyWithMaintainers[Value]]] =
key: Option[Node.KeyWithMaintainers],
): Result[Option[Node.KeyWithMaintainers]] =
key match {
case Some(k) =>
enrichContractKey(tyCon, k).map(Some(_))
Expand All @@ -129,14 +122,14 @@ final class ValueEnricher(

def enrichVersionedContractKey(
tyCon: Identifier,
key: Node.KeyWithMaintainers[VersionedValue],
): Result[Node.KeyWithMaintainers[VersionedValue]] =
enrichVersionedContractKey(tyCon, key.key).map(normalizedKey => key.copy(key = normalizedKey))
key: Node.VersionedKeyWithMaintainers,
): Result[Node.VersionedKeyWithMaintainers] =
enrichContractKey(tyCon, key.unversioned).map(normalizedValue => key.map(_ => normalizedValue))

def enrichVersionedContractKey(
tyCon: Identifier,
key: Option[Node.KeyWithMaintainers[VersionedValue]],
): Result[Option[Node.KeyWithMaintainers[VersionedValue]]] =
key: Option[Node.VersionedKeyWithMaintainers],
): Result[Option[Node.VersionedKeyWithMaintainers]] =
key match {
case Some(k) =>
enrichVersionedContractKey(tyCon, k).map(Some(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ private[lf] object Pretty {
prettyLoc(amf.optLocation)
}

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

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

def prettyEventInfo(l: ScenarioLedger, txId: TransactionId)(nodeId: NodeId): Doc = {
def arrowRight(d: Doc) = text("└─>") & d
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ private[lf] object SBuiltin {
templateId: TypeConName,
location: String,
v: SValue,
): Node.KeyWithMaintainers[V] =
): Node.KeyWithMaintainers =
v match {
case SStruct(_, vals) =>
val key = onLedger.ptx.normValue(templateId, vals.get(keyIdx))
Expand All @@ -1782,7 +1782,7 @@ private[lf] object SBuiltin {
templateId: TypeConName,
where: String,
optKey: SValue,
): Option[Node.KeyWithMaintainers[V]] =
): Option[Node.KeyWithMaintainers] =
optKey match {
case SOptional(mbKey) => mbKey.map(extractKeyWithMaintainers(onLedger, templateId, where, _))
case v => throw SErrorCrash(where, s"Expected optional key with maintainers, got: $v")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private[lf] object Speedy {
value: SValue,
signatories: Set[Party],
observers: Set[Party],
key: Option[Node.KeyWithMaintainers[V]],
key: Option[Node.KeyWithMaintainers],
) {
private[lf] val stakeholders: Set[Party] = signatories union observers;
}
Expand Down Expand Up @@ -355,7 +355,7 @@ private[lf] object Speedy {
arg: SValue,
signatories: Set[Party],
observers: Set[Party],
key: Option[Node.KeyWithMaintainers[V]],
key: Option[Node.KeyWithMaintainers],
) =
withOnLedger("addLocalContract") { onLedger =>
onLedger.cachedContracts = onLedger.cachedContracts.updated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private[lf] object PartialTransaction {
final case class ExercisesContextInfo(
targetId: Value.ContractId,
templateId: TypeConName,
contractKey: Option[Node.KeyWithMaintainers[Value]],
contractKey: Option[Node.KeyWithMaintainers],
choiceId: ChoiceName,
consuming: Boolean,
actingParties: Set[Party],
Expand Down Expand Up @@ -407,7 +407,7 @@ private[speedy] case class PartialTransaction(
optLocation: Option[Location],
signatories: Set[Party],
stakeholders: Set[Party],
key: Option[Node.KeyWithMaintainers[Value]],
key: Option[Node.KeyWithMaintainers],
byInterface: Option[TypeConName],
): (Value.ContractId, PartialTransaction) = {
val actionNodeSeed = context.nextActionChildSeed
Expand Down Expand Up @@ -500,7 +500,7 @@ private[speedy] case class PartialTransaction(
actingParties: Set[Party],
signatories: Set[Party],
stakeholders: Set[Party],
key: Option[Node.KeyWithMaintainers[Value]],
key: Option[Node.KeyWithMaintainers],
byKey: Boolean,
byInterface: Option[TypeConName],
): PartialTransaction = {
Expand Down Expand Up @@ -528,7 +528,7 @@ private[speedy] case class PartialTransaction(
auth: Authorize,
templateId: TypeConName,
optLocation: Option[Location],
key: Node.KeyWithMaintainers[Value],
key: Node.KeyWithMaintainers,
result: Option[Value.ContractId],
): PartialTransaction = {
val nid = NodeId(nextNodeIdx)
Expand Down Expand Up @@ -557,7 +557,7 @@ private[speedy] case class PartialTransaction(
signatories: Set[Party],
stakeholders: Set[Party],
choiceObservers: Set[Party],
mbKey: Option[Node.KeyWithMaintainers[Value]],
mbKey: Option[Node.KeyWithMaintainers],
byKey: Boolean,
chosenValue: Value,
byInterface: Option[TypeConName],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ object TransactionBuilder {

type TxValue = value.Value.VersionedValue

type KeyWithMaintainers = Node.KeyWithMaintainers[Value]
type TxKeyWithMaintainers = Node.KeyWithMaintainers[TxValue]
type KeyWithMaintainers = Node.KeyWithMaintainers
type TxKeyWithMaintainers = Node.VersionedKeyWithMaintainers

def apply(
pkgLangVersion: Ref.PackageId => LanguageVersion = _ => LanguageVersion.StableVersions.max
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ object ValueGenerators {
agreement <- Arbitrary.arbitrary[String]
} yield arg.map(Value.ContractInstance(template, _, agreement))

val keyWithMaintainersGen: Gen[Node.KeyWithMaintainers[Value]] = {
val keyWithMaintainersGen: Gen[Node.KeyWithMaintainers] = {
for {
key <- valueGen
maintainers <- genNonEmptyParties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ package com.daml.lf
package transaction

import com.daml.lf.data.Ref._
import com.daml.lf.data.{ImmArray, ScalazEqual}
import com.daml.lf.value.Value.{ContractId, VersionedValue}
import com.daml.lf.data.ImmArray
import com.daml.lf.value.Value.ContractId
import com.daml.lf.value._
import scalaz.Equal
import scalaz.syntax.equal._

/** Generic transaction node type for both update transactions and the
* transaction graph.
Expand Down Expand Up @@ -58,8 +56,7 @@ object Node {

def byInterface: Option[TypeConName]

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

@deprecated("use Node.LeafOnlyAction", since = "1.18.0")
Expand All @@ -83,7 +80,7 @@ object Node {
agreementText: String,
signatories: Set[Party],
stakeholders: Set[Party],
key: Option[KeyWithMaintainers[Value]],
key: Option[KeyWithMaintainers],
override val byInterface: Option[TypeConName],
// For the sake of consistency between types with a version field, keep this field the last.
override val version: TransactionVersion,
Expand All @@ -98,16 +95,14 @@ object Node {
override def mapCid(f: ContractId => ContractId): Node.Create =
copy(coid = f(coid), arg = arg.mapCid(f), key = key.map(_.mapCid(f)))

def versionedArg: VersionedValue = versionValue(arg)
def versionedArg: Value.VersionedValue = versioned(arg)

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

def versionedCoinst: Value.VersionedContractInstance =
Versioned(version, coinst)
def versionedCoinst: Value.VersionedContractInstance = versioned(coinst)

def versionedKey: Option[KeyWithMaintainers[Value.VersionedValue]] =
key.map(_.map(versionValue))
def versionedKey: Option[VersionedKeyWithMaintainers] = key.map(versioned)
}

@deprecated("use Node.Fetch", since = "1.18.0")
Expand All @@ -122,7 +117,7 @@ object Node {
actingParties: Set[Party],
signatories: Set[Party],
stakeholders: Set[Party],
key: Option[KeyWithMaintainers[Value]],
key: Option[KeyWithMaintainers],
override val byKey: Boolean, // invariant (!byKey || exerciseResult.isDefined)
override val byInterface: Option[TypeConName],
// For the sake of consistency between types with a version field, keep this field the last.
Expand All @@ -136,8 +131,7 @@ object Node {
override def mapCid(f: ContractId => ContractId): Node.Fetch =
copy(coid = f(coid), key = key.map(_.mapCid(f)))

def versionedKey: Option[KeyWithMaintainers[Value.VersionedValue]] =
key.map(_.map(versionValue))
def versionedKey: Option[VersionedKeyWithMaintainers] = key.map(versioned)
}

@deprecated("use Node.Exercise", since = "1.18.0")
Expand All @@ -162,7 +156,7 @@ object Node {
choiceObservers: Set[Party],
children: ImmArray[NodeId],
exerciseResult: Option[Value],
key: Option[KeyWithMaintainers[Value]],
key: Option[KeyWithMaintainers],
override val byKey: Boolean, // invariant (!byKey || exerciseResult.isDefined)
override val byInterface: Option[TypeConName],
// For the sake of consistency between types with a version field, keep this field the last.
Expand All @@ -185,14 +179,11 @@ object Node {
override def mapNodeId(f: NodeId => NodeId): Node.Exercise =
copy(children = children.map(f))

def versionedChosenValue: Value.VersionedValue =
versionValue(chosenValue)
def versionedChosenValue: Value.VersionedValue = versioned(chosenValue)

def versionedExerciseResult: Option[Value.VersionedValue] =
exerciseResult.map(versionValue)
def versionedExerciseResult: Option[Value.VersionedValue] = exerciseResult.map(versioned)

def versionedKey: Option[KeyWithMaintainers[Value.VersionedValue]] =
key.map(_.map(versionValue))
def versionedKey: Option[VersionedKeyWithMaintainers] = key.map(versioned)
}

@deprecated("use Node.LookupByKey", since = "1.18.0")
Expand All @@ -202,7 +193,7 @@ object Node {

final case class LookupByKey(
override val templateId: TypeConName,
key: KeyWithMaintainers[Value],
key: KeyWithMaintainers,
result: Option[ContractId],
// For the sake of consistency between types with a version field, keep this field the last.
override val version: TransactionVersion,
Expand All @@ -220,32 +211,23 @@ object Node {
override private[lf] def updateVersion(version: TransactionVersion): Node.LookupByKey =
copy(version = version)

def versionedKey: KeyWithMaintainers[Value.VersionedValue] =
key.map(versionValue)
def versionedKey: VersionedKeyWithMaintainers = versioned(key)
}

final case class KeyWithMaintainers[+Val](key: Val, maintainers: Set[Party]) {
final case class KeyWithMaintainers(key: Value, maintainers: Set[Party])
extends CidContainer[KeyWithMaintainers] {

def map[Val2](f: Val => Val2): KeyWithMaintainers[Val2] =
def map(f: Value => Value): KeyWithMaintainers =
copy(key = f(key))
}

object KeyWithMaintainers {
implicit class CidContainerInstance[Val <: CidContainer[Val]](key: KeyWithMaintainers[Val])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

extends CidContainer[KeyWithMaintainers[Val]] {
override def self = key
final override def mapCid(f: ContractId => ContractId): KeyWithMaintainers[Val] =
self.copy(key = self.key.mapCid(f))
}

implicit def equalInstance[Val: Equal]: Equal[KeyWithMaintainers[Val]] =
ScalazEqual.withNatural(Equal[Val].equalIsNatural) { (a, b) =>
import a._
val KeyWithMaintainers(bKey, bMaintainers) = b
key === bKey && maintainers == bMaintainers
}
override protected def self: this.type = this

override def mapCid(f: ContractId => ContractId): KeyWithMaintainers =
copy(key = key.mapCid(f))
}

type VersionedKeyWithMaintainers = Versioned[KeyWithMaintainers]

@deprecated("use Node.Rollback", since = "1.18.0")
type NodeRollback = Rollback
@deprecated("use Node.Rollback", since = "1.18.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Normalization {
* longer need this separate normalization pass.
*/

private type KWM = Node.KeyWithMaintainers[Val]
private type KWM = Node.KeyWithMaintainers
private type VTX = VersionedTransaction

def normalizeTx(vtx: VTX): VTX = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ sealed abstract class HasTxNodes {
def assertKeyMapping(
templateId: Identifier,
cid: Value.ContractId,
optKey: Option[Node.KeyWithMaintainers[Value]],
optKey: Option[Node.KeyWithMaintainers],
): Either[KeyInputError, State] =
optKey.fold[Either[KeyInputError, State]](Right(this)) { key =>
val gk = GlobalKey.assertBuild(templateId, key.key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ object TransactionCoder {
private[this] def encodeKeyWithMaintainers(
encodeCid: ValueCoder.EncodeCid,
version: TransactionVersion,
key: Node.KeyWithMaintainers[Value],
key: Node.KeyWithMaintainers,
): Either[EncodeError, TransactionOuterClass.KeyWithMaintainers] = {
val builder =
TransactionOuterClass.KeyWithMaintainers
Expand All @@ -204,7 +204,7 @@ object TransactionCoder {
private[this] def encodeAndSetContractKey(
encodeCid: ValueCoder.EncodeCid,
version: TransactionVersion,
key: Option[Node.KeyWithMaintainers[Value]],
key: Option[Node.KeyWithMaintainers],
setKey: TransactionOuterClass.KeyWithMaintainers => GeneratedMessageV3.Builder[_],
) = {
key match {
Expand Down Expand Up @@ -420,7 +420,7 @@ object TransactionCoder {
decodeCid: ValueCoder.DecodeCid,
version: TransactionVersion,
keyWithMaintainers: TransactionOuterClass.KeyWithMaintainers,
): Either[DecodeError, Node.KeyWithMaintainers[Value]] = {
): Either[DecodeError, Node.KeyWithMaintainers] = {
for {
maintainers <- toPartySet(keyWithMaintainers.getMaintainersList)
key <- decodeValue(
Expand All @@ -438,7 +438,7 @@ object TransactionCoder {
decodeCid: ValueCoder.DecodeCid,
version: TransactionVersion,
keyWithMaintainers: TransactionOuterClass.KeyWithMaintainers,
): Either[DecodeError, Option[Node.KeyWithMaintainers[Value]]] = {
): Either[DecodeError, Option[Node.KeyWithMaintainers]] = {
if (keyWithMaintainers == TransactionOuterClass.KeyWithMaintainers.getDefaultInstance) {
RightNone
} else {
Expand Down
Loading