DAML-LF: utility to compute duplicated keys in a transaction. (#7409)

The Engine does not verify key duplication when construction a
transaction, this task is currently let to the ledger implementation.
This PR provides an utility function to compute the duplicated keys
in a transaction.

CHANGELOG_BEGIN
CHANGELOG_END
This commit is contained in:
Remy 2020-09-21 09:18:47 +02:00 committed by GitHub
parent 65f1a247d0
commit e1f8d9c781
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -480,6 +480,42 @@ object GenTransaction extends value.CidContainer3[GenTransaction] {
Node.GenNode.foreach3(f1, f2, f3)(node) Node.GenNode.foreach3(f1, f2, f3)(node)
} }
} }
// crashes if transaction's keys contain contract Ids.
@throws[IllegalArgumentException]
def duplicatedContractKeys(tx: VersionedTransaction[NodeId, Value.ContractId]): Set[GlobalKey] = {
import GlobalKey.{assertBuild => globalKey}
case class State(active: Set[GlobalKey], duplicates: Set[GlobalKey]) {
def created(key: GlobalKey): State =
if (active(key)) copy(duplicates = duplicates + key) else copy(active = active + key)
def consumed(key: GlobalKey): State =
copy(active = active - key)
def referenced(key: GlobalKey): State =
copy(active = active + key)
}
tx.fold(State(Set.empty, Set.empty)) {
case (state, (_, node)) =>
node match {
case Node.NodeCreate(_, c, _, _, _, Some(key)) =>
state.created(globalKey(c.template, key.key.value))
case Node.NodeExercises(_, tmplId, _, _, true, _, _, _, _, _, _, _, Some(key)) =>
state.consumed(globalKey(tmplId, key.key.value))
case Node.NodeExercises(_, tmplId, _, _, false, _, _, _, _, _, _, _, Some(key)) =>
state.referenced(globalKey(tmplId, key.key.value))
case Node.NodeFetch(_, tmplId, _, _, _, _, Some(key)) =>
state.referenced(globalKey(tmplId, key.key.value))
case Node.NodeLookupByKey(tmplId, _, key, Some(_)) =>
state.referenced(globalKey(tmplId, key.key.value))
case _ =>
state
}
}
.duplicates
}
} }
object Transaction { object Transaction {