Skip to content

Commit

Permalink
Handle fetchByKey callback correctly in scenario runner (#10980)
Browse files Browse the repository at this point in the history
fixes #10977

Turns out assertions are good unless they’re wrong …

This only affects scenarios, the engine never looks at the callback.

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Sep 22, 2021
1 parent 23e6a2d commit 99836d2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
36 changes: 36 additions & 0 deletions compiler/damlc/tests/daml-test-files/ConsumedContractKey.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- @ERROR range=23:1-23:32; no contract with that key was found
-- @ERROR range=33:1-33:29; consumed in same transaction
module ConsumedContractKey where

template Foo
with
signer: Party
where
signatory signer
key signer : Party
maintainer key
controller signer can
FetchKey : Foo
do
snd <$> fetchByKey @Foo signer
LookupKey : ()
do
None <- lookupByKey @Foo signer
pure ()
Fetch : Foo
do fetch self

testFetchKeyFromConsumingChoice = do
alice <- getParty "Alice"
fooId <- alice `submit` create Foo with signer = alice
alice `submit` exercise fooId FetchKey

testLookupKeyFromConsumingChoice = do
alice <- getParty "Alice"
fooId <- alice `submit` create Foo with signer = alice
alice `submit` exercise fooId LookupKey

testFetchFromConsumingChoice = do
alice <- getParty "Alice"
fooId <- alice `submit` create Foo with signer = alice
alice `submit` exercise fooId Fetch
1 change: 1 addition & 0 deletions daml-lf/scenario-interpreter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ da_scala_library(
"//daml-lf/language",
"//daml-lf/transaction",
"//libs-scala/nameof",
"//libs-scala/scala-utils",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.daml.lf.speedy.SResult._
import com.daml.lf.transaction.IncompleteTransaction
import com.daml.lf.value.Value
import com.daml.nameof.NameOf
import com.daml.scalautil.Statement.discard

import scala.annotation.tailrec
import scala.util.{Failure, Success, Try}
Expand Down Expand Up @@ -328,9 +329,12 @@ object ScenarioRunner {
) match {
case ScenarioLedger.LookupOk(_, _, stakeholders) =>
if (!readers.intersect(stakeholders).isEmpty)
// We should always be able to continue with a SKeyLookupResult.Found.
// Run to get side effects and assert result.
assert(callback(Some(acoid)))
// Note that even with a successful global lookup
// the callback can return false. This happens for a fetch-by-key
// if the contract got archived in the meantime.
// We discard the result here and rely on fetch-by-key
// setting up the state such that continuing interpretation fails.
discard(callback(Some(acoid)))
else
throw Error.ContractKeyNotVisible(acoid, gk, actAs, readAs, stakeholders)
case ScenarioLedger.LookupContractNotFound(coid) =>
Expand Down

0 comments on commit 99836d2

Please sign in to comment.