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 a035a196a16d..9b10032a20f5 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 @@ -343,7 +343,7 @@ class Engine(val config: EngineConfig = new EngineConfig(LanguageVersion.StableV return ResultNeedKey( gk, result => - if (cb(SKeyLookupResult(result))) + if (cb(result)) interpretLoop(machine, time) else ResultError(Error.Interpretation.ContractKeyNotFound(gk.globalKey)), diff --git a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala index 73c62a132291..2f82a4b7735e 100644 --- a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala +++ b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala @@ -1188,12 +1188,13 @@ private[lf] object SBuiltin { private[speedy] sealed abstract class SBUKeyBuiltin(operation: KeyOperation) extends OnLedgerBuiltin(1) { - private def cacheGlobalLookup(onLedger: OnLedger, gkey: GlobalKey, result: SKeyLookupResult) = { - import PartialTransaction.{KeyActive, KeyInactive} - val keyMapping = result match { - case SKeyLookupResult.Found(cid) => KeyActive(cid) - case SKeyLookupResult.NotFound | SKeyLookupResult.NotVisible => KeyInactive - } + private def cacheGlobalLookup( + onLedger: OnLedger, + gkey: GlobalKey, + result: Option[V.ContractId], + ) = { + import PartialTransaction.{KeyActive, KeyInactive, KeyMapping} + val keyMapping: KeyMapping = result.fold[KeyMapping](KeyInactive)(KeyActive(_)) onLedger.ptx = onLedger.ptx.copy( globalKeyInputs = onLedger.ptx.globalKeyInputs.updated(gkey, keyMapping) ) @@ -1247,14 +1248,13 @@ private[lf] object SBuiltin { // modify keys if the archive was for a key // already brought into scope. val activeResult = result match { - case SKeyLookupResult.Found(cid) if onLedger.ptx.consumedBy.contains(cid) => - SKeyLookupResult.NotFound - case SKeyLookupResult.Found(_) | SKeyLookupResult.NotFound | - SKeyLookupResult.NotVisible => + case Some(cid) if onLedger.ptx.consumedBy.contains(cid) => + None + case _ => result } activeResult match { - case SKeyLookupResult.Found(cid) => + case Some(cid) => onLedger.ptx = onLedger.ptx.copy( keys = onLedger.ptx.keys.updated(gkey, KeyActive(cid)) ) @@ -1263,12 +1263,11 @@ private[lf] object SBuiltin { // We delegate to CtrlImportValue the task to check cid. operation.handleInputKeyFound(machine, cid) true - case SKeyLookupResult.NotFound => + case None => onLedger.ptx = onLedger.ptx.copy( keys = onLedger.ptx.keys.updated(gkey, KeyInactive) ) operation.handleInputKeyNotFound(machine) - case SKeyLookupResult.NotVisible => false } }, ) diff --git a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SResult.scala b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SResult.scala index fa1c5b035774..be64844987cb 100644 --- a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SResult.scala +++ b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SResult.scala @@ -74,7 +74,7 @@ object SResult { committers: Set[Party], // Callback. // returns true if machine can continue with the given result. - cb: SKeyLookupResult => Boolean, + cb: Option[ContractId] => Boolean, ) extends SResult final case class SResultNeedLocalKeyVisible( @@ -105,15 +105,4 @@ object SResult { } } } - - sealed abstract class SKeyLookupResult - object SKeyLookupResult { - final case class Found(coid: ContractId) extends SKeyLookupResult - final case object NotFound extends SKeyLookupResult - final case object NotVisible extends SKeyLookupResult - - def apply(coid: Option[ContractId]): SKeyLookupResult = - coid.fold[SKeyLookupResult](NotFound)(Found) - } - } diff --git a/daml-lf/scenario-interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/ScenarioRunner.scala b/daml-lf/scenario-interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/ScenarioRunner.scala index 042a7a0ed963..87e8aa1d602c 100644 --- a/daml-lf/scenario-interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/ScenarioRunner.scala +++ b/daml-lf/scenario-interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/ScenarioRunner.scala @@ -240,7 +240,7 @@ object ScenarioRunner { gk: GlobalKey, actAs: Set[Party], readAs: Set[Party], - canContinue: SKeyLookupResult => Boolean, + canContinue: Option[ContractId] => Boolean, ): Either[SError, Unit] def currentTime: Time.Timestamp def commit( @@ -300,7 +300,7 @@ object ScenarioRunner { gk: GlobalKey, actAs: Set[Party], readAs: Set[Party], - canContinue: SKeyLookupResult => Boolean, + canContinue: Option[ContractId] => Boolean, ): Either[SError, Unit] = handleUnsafe(lookupKeyUnsafe(gk, actAs, readAs, canContinue)) @@ -308,17 +308,13 @@ object ScenarioRunner { gk: GlobalKey, actAs: Set[Party], readAs: Set[Party], - canContinue: SKeyLookupResult => Boolean, + canContinue: Option[ContractId] => Boolean, ): Unit = { val effectiveAt = ledger.currentTime val readers = actAs union readAs def missingWith(err: SError) = - if (!canContinue(SKeyLookupResult.NotFound)) - throw SRunnerException(err) - - def notVisibleWith(err: SError) = - if (!canContinue(SKeyLookupResult.NotVisible)) + if (!canContinue(None)) throw SRunnerException(err) ledger.ledgerData.activeKeys.get(gk) match { @@ -332,11 +328,11 @@ object ScenarioRunner { ) match { case ScenarioLedger.LookupOk(_, _, stakeholders) => if (!readers.intersect(stakeholders).isEmpty) - // We should always be able to continue with a SKeyLookupResult.Found. + // We should always be able to continue with a Some(_). // Run to get side effects and assert result. - assert(canContinue(SKeyLookupResult.Found(acoid))) + assert(canContinue(Some(acoid))) else - notVisibleWith( + throw SRunnerException( ScenarioErrorContractKeyNotVisible(acoid, gk, actAs, readAs, stakeholders) ) case ScenarioLedger.LookupContractNotFound(coid) => @@ -351,7 +347,7 @@ object ScenarioRunner { observers @ _, stakeholders, ) => - notVisibleWith( + throw SRunnerException( ScenarioErrorContractKeyNotVisible(coid, gk, actAs, readAs, stakeholders) ) } diff --git a/daml-lf/scenario-interpreter/src/perf/benches/scala/com/digitalasset/daml/lf/speedy/perf/CollectAuthority.scala b/daml-lf/scenario-interpreter/src/perf/benches/scala/com/digitalasset/daml/lf/speedy/perf/CollectAuthority.scala index d87c547496c5..e8a520743c15 100644 --- a/daml-lf/scenario-interpreter/src/perf/benches/scala/com/digitalasset/daml/lf/speedy/perf/CollectAuthority.scala +++ b/daml-lf/scenario-interpreter/src/perf/benches/scala/com/digitalasset/daml/lf/speedy/perf/CollectAuthority.scala @@ -198,7 +198,7 @@ class CannedLedgerApi( gk: GlobalKey, actAs: Set[Party], readAs: Set[Party], - canContinue: SKeyLookupResult => Boolean, + canContinue: Option[ContractId] => Boolean, ) = throw new RuntimeException("Keys are not supported in the benchmark") override def currentTime = throw new RuntimeException("getTime is not supported in the benchmark")