Skip to content

Commit

Permalink
LF: builtins to create, signatory, and obersver on interface payload.
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da committed Oct 5, 2021
1 parent b9a5a83 commit 9bfb529
Showing 6 changed files with 119 additions and 6 deletions.
2 changes: 1 addition & 1 deletion compiler/damlc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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)
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
@@ -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
@@ -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
4 changes: 2 additions & 2 deletions daml-lf/spec/experimental.rst
Original file line number Diff line number Diff line change
@@ -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
----------------------------------

0 comments on commit 9bfb529

Please sign in to comment.