From f82f98b28e0af5f55423843343fc9b23ef5291f0 Mon Sep 17 00:00:00 2001 From: Remy Date: Fri, 3 Apr 2020 14:49:28 +0200 Subject: [PATCH] Engine: simplify commandPreprocessor output (#5415) * Engine: simplify commandPreprocessor output * changelog CHANGELOG_BEGIN CHANGELOG_END --- .../daml/lf/engine/CommandPreprocessor.scala | 55 ++++++++++--------- .../digitalasset/daml/lf/engine/Engine.scala | 32 +++++------ 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/daml-lf/engine/src/main/scala/com/digitalasset/daml/lf/engine/CommandPreprocessor.scala b/daml-lf/engine/src/main/scala/com/digitalasset/daml/lf/engine/CommandPreprocessor.scala index 173d6b22057..3fda28c8b92 100644 --- a/daml-lf/engine/src/main/scala/com/digitalasset/daml/lf/engine/CommandPreprocessor.scala +++ b/daml-lf/engine/src/main/scala/com/digitalasset/daml/lf/engine/CommandPreprocessor.scala @@ -18,7 +18,8 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka private[engine] def translateValue( ty0: Type, - v0: Value[Value.AbsoluteContractId]): Result[SValue] = { + v0: Value[Value.AbsoluteContractId], + ): Result[SValue] = { valueTranslator.translateValue(ty0, v0) match { case ResultNeedPackage(pkgId, resume) => @@ -36,7 +37,8 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka private[engine] def preprocessCreate( templateId: Identifier, - argument: Value[Value.AbsoluteContractId]): Result[(Type, SpeedyCommand)] = + argument: Value[Value.AbsoluteContractId], + ): Result[SpeedyCommand] = Result.needDataType( compiledPackages, templateId, @@ -47,15 +49,16 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka ResultError(Error( s"Unexpected type parameters ${dataType.params} for template $templateId. Template datatypes should never have parameters.")) } else { - val typ = TTyCon(templateId) - translateValue(typ, argument).map(typ -> SpeedyCommand.Create(templateId, _)) + translateValue(TTyCon(templateId), argument) + .map(SpeedyCommand.Create(templateId, _)) } } ) private[engine] def preprocessFetch( templateId: Identifier, - coid: Value.AbsoluteContractId): Result[(Type, SpeedyCommand)] = + coid: Value.AbsoluteContractId, + ): Result[SpeedyCommand] = Result.needDataType( compiledPackages, templateId, @@ -66,8 +69,7 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka ResultError(Error( s"Unexpected type parameters ${dataType.params} for template $templateId. Template datatypes should never have parameters.")) } else { - val typ = TTyCon(templateId) - ResultDone(typ -> SpeedyCommand.Fetch(templateId, SValue.SContractId(coid))) + ResultDone(SpeedyCommand.Fetch(templateId, SValue.SContractId(coid))) } } ) @@ -76,7 +78,8 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka templateId: Identifier, contractId: Value.ContractId, choiceId: ChoiceName, - argument: Value[Value.AbsoluteContractId]): Result[(Type, SpeedyCommand)] = + argument: Value[Value.AbsoluteContractId], + ): Result[SpeedyCommand] = Result.needTemplate( compiledPackages, templateId, @@ -89,8 +92,7 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka case Some(choice) => val choiceTyp = choice.argBinder._2 translateValue(choiceTyp, argument).map( - choiceTyp -> SpeedyCommand - .Exercise(templateId, SValue.SContractId(contractId), choiceId, _)) + SpeedyCommand.Exercise(templateId, SValue.SContractId(contractId), choiceId, _)) } } ) @@ -99,7 +101,8 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka templateId: Identifier, contractKey: Value[Value.AbsoluteContractId], choiceId: ChoiceName, - argument: Value[Value.AbsoluteContractId]): Result[(Type, SpeedyCommand)] = + argument: Value[Value.AbsoluteContractId], + ): Result[SpeedyCommand] = Result.needTemplate( compiledPackages, templateId, @@ -117,9 +120,7 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka for { arg <- translateValue(choiceType, argument) key <- translateValue(ck.typ, contractKey) - } yield - choiceType -> SpeedyCommand - .ExerciseByKey(templateId, key, choiceId, arg) + } yield SpeedyCommand.ExerciseByKey(templateId, key, choiceId, arg) } } ) @@ -128,8 +129,8 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka templateId: ValueRef, createArgument: Value[Value.AbsoluteContractId], choiceId: ChoiceName, - choiceArgument: Value[Value.AbsoluteContractId] - ): Result[(Type, SpeedyCommand)] = { + choiceArgument: Value[Value.AbsoluteContractId], + ): Result[SpeedyCommand] = { Result.needDataType( compiledPackages, templateId, @@ -155,7 +156,7 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka case Some(choice) => val choiceTyp = choice.argBinder._2 translateValue(choiceTyp, choiceArgument).map( - choiceTyp -> SpeedyCommand + SpeedyCommand .CreateAndExercise(templateId, createValue, choiceId, _)) } } @@ -168,8 +169,8 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka private[engine] def preprocessLookupByKey( templateId: ValueRef, - contractKey: Value[Nothing] - ): Result[(Type, SpeedyCommand)] = { + contractKey: Value[Nothing], + ): Result[SpeedyCommand] = { Result.needTemplate( compiledPackages, templateId, @@ -181,15 +182,13 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka case Some(ck) => for { key <- translateValue(ck.typ, contractKey) - } yield - TTyCon(templateId) -> SpeedyCommand - .LookupByKey(templateId, key) + } yield SpeedyCommand.LookupByKey(templateId, key) } } ) } - private[engine] def preprocessCommand(cmd: Command): Result[(Type, SpeedyCommand)] = + private[engine] def preprocessCommand(cmd: Command): Result[SpeedyCommand] = cmd match { case CreateCommand(templateId, argument) => preprocessCreate(templateId, argument) @@ -217,7 +216,8 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka } private[engine] def preprocessCommands( - cmds0: Commands): Result[ImmArray[(Type, SpeedyCommand)]] = { + cmds0: Commands, + ): Result[ImmArray[SpeedyCommand]] = { // before, we had // // ``` @@ -230,8 +230,9 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka // after the first command which demands it. @tailrec def go( - processed: BackStack[(Type, SpeedyCommand)], - toProcess: ImmArray[Command]): Result[ImmArray[(Type, SpeedyCommand)]] = { + processed: BackStack[SpeedyCommand], + toProcess: ImmArray[Command], + ): Result[ImmArray[SpeedyCommand]] = { toProcess match { case ImmArray() => ResultDone(processed.toImmArray) case ImmArrayCons(cmd, cmds) => @@ -257,7 +258,7 @@ private[engine] class CommandPreprocessor(compiledPackages: MutableCompiledPacka } } - def goResume(processed: BackStack[(Type, SpeedyCommand)], toProcess: ImmArray[Command]) = + def goResume(processed: BackStack[SpeedyCommand], toProcess: ImmArray[Command]) = go(processed, toProcess) go(BackStack.empty, cmds0.commands) diff --git a/daml-lf/engine/src/main/scala/com/digitalasset/daml/lf/engine/Engine.scala b/daml-lf/engine/src/main/scala/com/digitalasset/daml/lf/engine/Engine.scala index b7e699fd73e..ec2c7b1c53d 100644 --- a/daml-lf/engine/src/main/scala/com/digitalasset/daml/lf/engine/Engine.scala +++ b/daml-lf/engine/src/main/scala/com/digitalasset/daml/lf/engine/Engine.scala @@ -105,15 +105,14 @@ final class Engine { // all commands are actions on a contract template, with a fully typed // argument, we only need to consider the templates mentioned in the command // to compute the full dependencies. - val deps = processedCmds.foldLeft(Set.empty[PackageId]) { - case (pkgIds, (_, cmd)) => - val pkgId = cmd.templateId.packageId - val transitiveDeps = - _compiledPackages - .getPackageDependencies(pkgId) - .getOrElse( - sys.error(s"INTERNAL ERROR: Missing dependencies of package $pkgId")) - (pkgIds + pkgId) union transitiveDeps + val deps = processedCmds.foldLeft(Set.empty[PackageId]) { (pkgIds, cmd) => + val pkgId = cmd.templateId.packageId + val transitiveDeps = + _compiledPackages + .getPackageDependencies(pkgId) + .getOrElse( + sys.error(s"INTERNAL ERROR: Missing dependencies of package $pkgId")) + (pkgIds + pkgId) union transitiveDeps } tx -> Transaction.Metadata( submissionTime = submissionTime, @@ -164,7 +163,7 @@ final class Engine { commands <- Result.sequence(ImmArray(nodes).map(translateNode(commandTranslation))) checkSubmitterInMaintainers <- ShouldCheckSubmitterInMaintainers( _compiledPackages, - commands.map(_._2.templateId)) + commands.map(_.templateId)) // reinterpret is never used for submission, only for validation. result <- interpretCommands( validating = true, @@ -232,7 +231,7 @@ final class Engine { commands <- translateTransactionRoots(commandTranslation, tx) checkSubmitterInMaintainers <- ShouldCheckSubmitterInMaintainers( _compiledPackages, - commands.map(_._2._2.templateId)) + commands.map(_._2.templateId)) result <- interpretCommands( validating = true, checkSubmitterInMaintainers = checkSubmitterInMaintainers, @@ -280,7 +279,8 @@ final class Engine { // Translate a GenNode into an expression re-interpretable by the interpreter private[this] def translateNode[Cid <: Value.ContractId]( commandPreprocessor: CommandPreprocessor)( - node: GenNode.WithTxValue[Transaction.NodeId, Cid]): Result[(Type, SpeedyCommand)] = { + node: GenNode.WithTxValue[Transaction.NodeId, Cid], + ): Result[SpeedyCommand] = { node match { case NodeCreate(nodeSeed @ _, coid @ _, coinst, optLoc @ _, sigs @ _, stks @ _, key @ _) => @@ -324,8 +324,8 @@ final class Engine { private[this] def translateTransactionRoots[Cid <: Value.ContractId]( commandPreprocessor: CommandPreprocessor, - tx: GenTransaction.WithTxValue[Transaction.NodeId, Cid] - ): Result[ImmArray[(Transaction.NodeId, (Type, SpeedyCommand))]] = { + tx: GenTransaction.WithTxValue[Transaction.NodeId, Cid], + ): Result[ImmArray[(Transaction.NodeId, SpeedyCommand)]] = { Result.sequence(tx.roots.map(id => tx.nodes.get(id) match { case None => @@ -357,14 +357,14 @@ final class Engine { /* See documentation for `Speedy.Machine` for the meaning of this field */ checkSubmitterInMaintainers: Boolean, submitters: Set[Party], - commands: ImmArray[(Type, SpeedyCommand)], + commands: ImmArray[SpeedyCommand], ledgerTime: Time.Timestamp, transactionSeedAndSubmissionTime: Option[(crypto.Hash, Time.Timestamp)] ): Result[(Transaction.Transaction, Boolean)] = { val machine = Machine .build( checkSubmitterInMaintainers = checkSubmitterInMaintainers, - sexpr = Compiler(compiledPackages.packages).compile(commands.map(_._2)), + sexpr = Compiler(compiledPackages.packages).compile(commands), compiledPackages = _compiledPackages, transactionSeedAndSubmissionTime = transactionSeedAndSubmissionTime, )