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

LF: builtins to create, signatory, and observer on interface payload. #11120

Merged
merged 1 commit into from
Oct 5, 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
2 changes: 1 addition & 1 deletion compiler/damlc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ daml_doc_test(
flags = ["--no-dflags-check"],
ignored_srcs = [
"LibraryModules.daml",
"DA/Experimental/Example.daml",
"DA/Experimental/*.daml",
"DA/Time/Types.daml",
],
)
21 changes: 21 additions & 0 deletions compiler/damlc/daml-stdlib-src/DA/Experimental/Interface.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0

{-# LANGUAGE CPP #-}

module DA.Experimental.Interface(
interfaceCreate,
interfaceSignatory,
interfaceObserver,
) where

import GHC.Types (primitive)

interfaceCreate: t -> Update (ContractId t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than adding these as top-level functions, I think it would be nice to generate HasCreate, HasSignatory and HasObserver functions. What do you think @sofiafaro-da @remyhaemmerle-da?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is just do the minimal in the Daml side. @sofiafaro-da should probably work to clean up integration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, we should definitely add HasCreate/etc instances.

interfaceCreate = primitive @"$INTERFACE_CREATE"

interfaceSignatory: t -> [Party]
interfaceSignatory = primitive @"$INTERFACE_SIGNATORIES"

interfaceObserver: t -> [Party]
interfaceObserver = primitive @"$INTERFACE_OBSERVERS"
1 change: 1 addition & 0 deletions compiler/damlc/daml-stdlib-src/LibraryModules.daml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import DA.Validation

#ifdef DAML_EXPERIMENTAL
import DA.Experimental.Example
import DA.Experimental.Interface
#endif

import Prelude
74 changes: 74 additions & 0 deletions compiler/damlc/tests/daml-test-files/InterfaceExperimental.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-- Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0

-- @SINCE-LF-FEATURE DAML_INTERFACE

-- TODO https://github.com/digital-asset/daml/issues/10810
-- DROP when we have more meaningful test for create, signatories, observers
module InterfaceExperimental where

import DA.Assert ((===))
import DA.Experimental.Interface
import GHC.Types (primitive)

interface Token where
getAmount : Int

choice Split : (ContractId Token, ContractId Token)
with
splitAmount : Int

choice Transfer : ContractId Token
with
newOwner : Party

nonconsuming choice Noop : ()
with
nothing : ()

template Asset
with
issuer : Party
owner : Party
amount : Int
where
signatory issuer
observer issuer, owner
implements Token where
let getAmount = amount

choice Split : (ContractId Token, ContractId Token)
with
splitAmount : Int
controller owner
do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toTokenContractId cid1, toTokenContractId cid2)

choice Transfer : ContractId Token
with
newOwner : Party
controller owner, newOwner
do
cid <- create this with owner = newOwner
pure (toTokenContractId cid)

nonconsuming choice Noop : ()
with
nothing : ()
controller owner
do
pure ()

main = scenario do
alice <- getParty "Alice"
bob <- getParty "Bob"
let asset = Asset alice bob 15
let token = toToken asset
submit alice do
interfaceCreate token
interfaceSignatory token === [alice]
interfaceObserver token === [bob, alice]
pure ()
Original file line number Diff line number Diff line change
Expand Up @@ -1702,14 +1702,31 @@ private[lf] object SBuiltin {
}
}

def apply(name: String): SExpr =
mapping.getOrElse(name, SBError(SEValue(SText(s"experimental $name not supported."))))
private final class SBExperimentalInterfaceDef(
toSDefRef: Identifier => SDefinitionRef,
name: String,
arity: Int,
) extends SBExperimental(name, arity) {
override private[speedy] def execute(
args: util.ArrayList[SValue],
machine: Machine,
): Unit = {
val tmplId = getSRecord(args, 0).id
machine.ctrl = SEApp(SEVal(toSDefRef(tmplId)), args.asScala.view.map(SEValue(_)).toArray)
}
}

private val mapping: Map[String, SEBuiltin] =
List[SBExperimental](
SBExperimentalAnswer
SBExperimentalAnswer,
new SBExperimentalInterfaceDef(CreateDefRef, "INTERFACE_CREATE", 2),
new SBExperimentalInterfaceDef(SignatoriesDefRef, "INTERFACE_SIGNATORIES", 1),
new SBExperimentalInterfaceDef(ObserversDefRef, "INTERFACE_OBSERVERS", 1),
).map(x => x.name -> SEBuiltin(x)).toMap

def apply(name: String): SExpr =
mapping.getOrElse(name, SBError(SEValue(SText(s"experimental $name not supported."))))

}

// Helpers
Expand Down
4 changes: 2 additions & 2 deletions daml-lf/spec/experimental.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ archive Protobuf definitions, compilers, type checkers, and archive
(de/en)coders.

In addition of development speed, those hooks also adds some
confidence non-development feature are not impacted when prototyping
new Daml feature as few part of the stack had to be modified.
confidence non-development features are not impacted when prototyping
new Daml feature as few parts of the stack have to be modified.

Daml-LF Experimental Specification
----------------------------------
Expand Down