Exception rollback

Hello all,

I was experimenting a bit with daml exceptions, and I came across a situation that I found unexpected and was hoping you could clarify me on this particular topic.
Given the following code:

template Foo
    with
        p : Party
    where
        signatory p

        controller p can
            nonconsuming DoFoo : ()
                with
                    barCid : ContractId Bar
                do
                    try
                       Foldable.mapA_ (exercise barCid . DoBar) [1..5]
                    catch error@(Qux msg) -> do
                            debug $ message error

template Bar
    with
        p : Party
    where
        signatory p

        controller p can
            nonconsuming DoBar : ()
                with
                    n : Int
                do
                    when (n % 5 == 0) do
                        throw Qux with msg = "Test message"
                    create Baz with p
                    return ()

template Baz
    with
        p : Party
    where
        signatory p

exception Qux
  with
    msg : Text
  where
    message "This is an error - " <> msg

testException : Script ()
testException = script do

    let Some p = partyFromText "Party"

    fooCid <- submit p do createCmd Foo with p
    barCid <- submit p do createCmd Bar with p

    submit p do exerciseCmd fooCid DoFoo with barCid

    return ()

I expected that no contract “Baz” would exists after the execution of the “DoFoo” choice, as I call the “DoBar” choice inside of the try… catch statement and as such when the execution throws an Exception all previous sub-transactions would have been rolled back.

Thank you in advance

2 Likes

Hi @David_Martins, good catch! You are completely right that these contracts are inactive. In fact, if you try to fetch them or if you query they’d already be inactive. The bug only affects display in Daml studio.

Fix activeness display in script service by cocreature · Pull Request #13059 · digital-asset/daml · GitHub fixes the bug and with that the table view for your example looks as expected:

Small side note: Use allocateParty instead of partyFromText. Using a party without allocating it will be disallowed in future releases.

Thanks @cocreature, indeed I did not attempt to query for these contracts after the fact, and relied only on the table view of the results. However if it has resolved an issue even better.
My usage of the partyFromText was really just a makeshift means of obtaining a Party to use for the example, but thank you for the notice and heads up.