Skip to content

Commit

Permalink
allow upgrading keys during re-interpretation
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbrauner-da committed Aug 16, 2024
1 parent a7ea9d5 commit 4c3e95e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,7 @@ private[lf] final class CommandPreprocessor(
if (strict || !enableUpgrading) ValueTranslator.Config.Strict
else ValueTranslator.Config.Upgradeable

private[this] def translateUpgradableArg(
typ: Ast.Type,
value: Value,
strict: Boolean,
enableUpgrading: Boolean,
) =
valueTranslator.unsafeTranslateValue(
ty = typ,
value = value,
config = valueTranslatorConfig(strict, enableUpgrading),
)

private[this] def translateNonUpgradableArg(
private[this] def translateArg(
typ: Ast.Type,
value: Value,
strict: Boolean,
Expand Down Expand Up @@ -86,7 +74,7 @@ private[lf] final class CommandPreprocessor(
}
// TODO: https://github.com/digital-asset/daml/issues/17082
// for now we need the package of the disclosed contract
val arg = translateUpgradableArg(
val arg = translateArg(
Ast.TTyCon(disc.templateId),
disc.argument,
strict = true,
Expand All @@ -108,7 +96,7 @@ private[lf] final class CommandPreprocessor(
strict: Boolean,
): speedy.Command.Create = {
discard(handleLookup(pkgInterface.lookupTemplate(templateId)))
val arg = translateUpgradableArg(
val arg = translateArg(
Ast.TTyCon(templateId),
argument,
strict = strict,
Expand Down Expand Up @@ -143,7 +131,7 @@ private[lf] final class CommandPreprocessor(
templateId = templateId,
contractId = valueTranslator.unsafeTranslateCid(contractId),
choiceId = choiceId,
argument = translateUpgradableArg(
argument = translateArg(
choice.argBinder._2,
argument,
strict = strict,
Expand All @@ -164,7 +152,7 @@ private[lf] final class CommandPreprocessor(
interfaceId = ifaceId,
contractId = valueTranslator.unsafeTranslateCid(contractId),
choiceId = choiceId,
argument = translateNonUpgradableArg(
argument = translateArg(
choice.argBinder._2,
argument,
strict = strict,
Expand All @@ -179,22 +167,23 @@ private[lf] final class CommandPreprocessor(
contractKey: Value,
choiceId: Ref.ChoiceName,
argument: Value,
strict: Boolean,
strictArgument: Boolean,
strictKey: Boolean,
): speedy.Command.ExerciseByKey = {
val choiceArgType = handleLookup(
pkgInterface.lookupTemplateChoice(templateId, choiceId)
).argBinder._2
val ckTtype = handleLookup(pkgInterface.lookupTemplateKey(templateId)).typ
val arg = translateUpgradableArg(
val arg = translateArg(
choiceArgType,
argument,
strict = strict,
strict = strictArgument,
isUpgradable(templateId.packageId),
)
val key = translateNonUpgradableArg(
val key = translateArg(
ckTtype,
contractKey,
strict = strict,
strict = strictKey,
isUpgradable(templateId.packageId),
)
speedy.Command.ExerciseByKey(templateId, key, choiceId, arg)
Expand All @@ -208,7 +197,7 @@ private[lf] final class CommandPreprocessor(
choiceArgument: Value,
strict: Boolean,
): speedy.Command.CreateAndExercise = {
val createArg = translateUpgradableArg(
val createArg = translateArg(
Ast.TTyCon(templateId),
createArgument,
strict = strict,
Expand All @@ -217,7 +206,7 @@ private[lf] final class CommandPreprocessor(
val choiceArgType = handleLookup(
pkgInterface.lookupTemplateChoice(templateId, choiceId)
).argBinder._2
val choiceArg = translateUpgradableArg(
val choiceArg = translateArg(
choiceArgType,
choiceArgument,
strict = strict,
Expand All @@ -239,7 +228,7 @@ private[lf] final class CommandPreprocessor(
strict: Boolean,
): speedy.Command.LookupByKey = {
val ckTtype = handleLookup(pkgInterface.lookupTemplateKey(templateId)).typ
val key = translateNonUpgradableArg(
val key = translateArg(
ckTtype,
contractKey,
strict = strict,
Expand Down Expand Up @@ -293,7 +282,14 @@ private[lf] final class CommandPreprocessor(
templateRef,
language.Reference.Template(templateRef),
)
unsafePreprocessExerciseByKey(templateId, contractKey, choiceId, argument, strict = false)
unsafePreprocessExerciseByKey(
templateId,
contractKey,
choiceId,
argument,
strictArgument = false,
strictKey = false,
)
case command.ApiCommand.CreateAndExercise(
templateRef,
createArgument,
Expand Down Expand Up @@ -336,7 +332,19 @@ private[lf] final class CommandPreprocessor(
choiceId,
argument,
) =>
unsafePreprocessExerciseByKey(templateId, contractKey, choiceId, argument, strict = true)
unsafePreprocessExerciseByKey(
templateId,
contractKey,
choiceId,
argument,
strictArgument = true,
// When reconstructing an exercise-by-key node from a view, we pull the key from the corresponding input
// contract. That input contract may use a different package version for the type of its input key than
// the version that was used for constructing the node in the submitting participant, which needs to be the
// same when re-interpreting the node. Therefore, we need to allow upgrading keys when re-interpreting
// exercise-by-key nodes.
strictKey = false,
)
case command.ReplayCommand.Fetch(typeId, coid) =>
val cid = valueTranslator.unsafeTranslateCid(coid)
handleLookup(pkgInterface.lookupTemplateOrInterface(typeId)) match {
Expand All @@ -347,7 +355,7 @@ private[lf] final class CommandPreprocessor(
}
case command.ReplayCommand.FetchByKey(templateId, key) =>
val ckTtype = handleLookup(pkgInterface.lookupTemplateKey(templateId)).typ
val sKey = translateNonUpgradableArg(
val sKey = translateArg(
ckTtype,
key,
strict = true,
Expand All @@ -356,7 +364,7 @@ private[lf] final class CommandPreprocessor(
speedy.Command.FetchByKey(templateId, sKey)
case command.ReplayCommand.LookupByKey(templateId, key) =>
val ckTtype = handleLookup(pkgInterface.lookupTemplateKey(templateId)).typ
val sKey = translateNonUpgradableArg(
val sKey = translateArg(
ckTtype,
key,
strict = true,
Expand Down Expand Up @@ -402,7 +410,7 @@ private[lf] final class CommandPreprocessor(
discard(handleLookup(pkgInterface.lookupInterface(interfaceId)))
discard(handleLookup(pkgInterface.lookupInterfaceInstance(interfaceId, templateId)))

val arg = translateNonUpgradableArg(
val arg = translateArg(
Ast.TTyCon(templateId),
argument,
strict = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ private[preprocessing] final class TransactionPreprocessor(
key.globalKey.key,
exe.choiceId,
exe.chosenValue,
strict = true,
strictArgument = true,
strictKey = true,
)
case _ =>
commandPreprocessor.unsafePreprocessExerciseTemplate(
Expand Down
3 changes: 1 addition & 2 deletions sdk/daml-script/test/daml/upgrades/ContractKeys.daml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ main = tests
, ("Fetching an unchanged old key for a new contract", fetchKeyUnchanged)
, ("ExerciseByKey in Update an unchanged old key for a new contract", exerciseUpdateKeyUnchanged)
, ("Query an Upgraded old key for a new contract", queryKeyUpgraded)
-- TODO(https://github.com/digital-asset/daml/issues/19782): re-enable this test once the issue is fixed
-- , ("ExerciseByKey command an Upgraded old key for a new contract", exerciseCmdKeyUpgraded)
, ("ExerciseByKey command an Upgraded old key for a new contract", exerciseCmdKeyUpgraded)
, ("Fetching an Upgraded old key for a new contract", fetchKeyUpgraded)
, ("ExerciseByKey in Update an Upgraded old key for a new contract", exerciseUpdateKeyUpgraded)
]
Expand Down

0 comments on commit 4c3e95e

Please sign in to comment.