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

Fix conversion of TryContextInfo in scenario service #9731

Merged
merged 1 commit into from
May 19, 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
18 changes: 18 additions & 0 deletions compiler/damlc/tests/daml-test-files/ExceptionSemantics.daml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-- SPDX-License-Identifier: Apache-2.0

-- @SINCE-LF-FEATURE DAML_EXCEPTIONS
-- @ERROR range=145:1-145:11; Attempt to exercise a consumed contract
module ExceptionSemantics where

import DA.Exception (throw)
Expand Down Expand Up @@ -33,6 +34,14 @@ template T
catch
E -> pure ()

nonconsuming choice UncatchableTry : ()
with
cid : ContractId K
controller p
do try (() <$ fetch cid)
catch
E -> pure()

nonconsuming choice TransientDuplicate : ()
with
i : Int
Expand Down Expand Up @@ -132,3 +141,12 @@ divulgence = scenario do
submit p1 $ exercise divulger (RollbackFetch cid)
submit p2 $ exercise fetcher (Fetch cid)
pure ()

tryContext = scenario do
p <- getParty "p"
kCid <- submit p $ create (K p 0)
submit p $ archive kCid
c <- submit p $ create (T p)
-- This will result in a partial transaction with ptx.context.info
-- pointing to a TryContextInfo.
submit p $ exercise c (UncatchableTry kCid)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.daml.lf.transaction.{GlobalKey, Node => N, NodeId}
import com.daml.lf.ledger._
import com.daml.lf.value.{Value => V}

import scala.annotation.tailrec
import scala.jdk.CollectionConverters._

final class Conversions(
Expand Down Expand Up @@ -416,19 +417,23 @@ final class Conversions(
ptx.context.children.toImmArray.toSeq.sortBy(_.index).map(convertTxNodeId).asJava
)

ptx.context.info match {
case ctx: SPartialTransaction.ExercisesContextInfo =>
val ecBuilder = proto.ExerciseContext.newBuilder
.setTargetId(mkContractRef(ctx.targetId, ctx.templateId))
.setChoiceId(ctx.choiceId)
.setChosenValue(convertValue(ctx.chosenValue))
ctx.optLocation.map(loc => ecBuilder.setExerciseLocation(convertLocation(loc)))
builder.setExerciseContext(ecBuilder.build)
case _: SPartialTransaction.TryContextInfo =>
// TODO: https://github.com/digital-asset/daml/issues/8020
// handle catch context
sys.error("exception not supported")
case _: SPartialTransaction.RootContextInfo =>
@tailrec
def unwindToExercise(
contextInfo: SPartialTransaction.ContextInfo
): Option[SPartialTransaction.ExercisesContextInfo] = contextInfo match {
case ctx: SPartialTransaction.ExercisesContextInfo => Some(ctx)
case ctx: SPartialTransaction.TryContextInfo =>
unwindToExercise(ctx.parent.info)
case _: SPartialTransaction.RootContextInfo => None
}

unwindToExercise(ptx.context.info).foreach { ctx =>
val ecBuilder = proto.ExerciseContext.newBuilder
.setTargetId(mkContractRef(ctx.targetId, ctx.templateId))
.setChoiceId(ctx.choiceId)
.setChosenValue(convertValue(ctx.chosenValue))
ctx.optLocation.map(loc => ecBuilder.setExerciseLocation(convertLocation(loc)))
builder.setExerciseContext(ecBuilder.build)
}
builder.build
}
Expand Down