How to determine all packages used by a GenNode
Suppose I am given a GenTransaction tx that has been output by DAML engine and a node n which is part of tx.nodes.values.
Now suppose I feed the node n into Engine.reinterpret. How can I predict which packages my DAML engine will query, provided the package cache is initially empty?
I have already done some research and found/rejected some candidate answers:
-
com.daml.lf.transaction.Transaction.Metadatahas a field storing used packages. However, this field is on a transaction basis (rather than on a per node basis). Moreover, the field is optional and seems not to be populated right now. -
com.daml.ledger.participant.state.v1.TransactionMetaalso has a field storing used packages. However the same limitations as forMetadataseem to apply. - I imagine that I could traverse the subtree of
txrooted atnand collect all packageIds referenced by a templateId referenced by a node. Can you confirm that this would give me the complete set of used packages?
Candidate 3 is almost safe: you might also need the dependencies of the packages referenced by the template IDs you encounter. Otherwise I see no other way the Engine could ask for further packages, as it only evaluates the definitions used by the templates of the contracts in the transaction.
If you give a Node to the Engine#reinterpret method provided the package cache is empty, the engine will query exactly the package of the template ID of your node and all its dependencies. The reason is that packages are type-checked when loaded, and package dependencies are needed to perform this task.
So to predict the packages that will be query in the case you are describing, you just need to get all the dependencies of the package of the template of the node you are considering. You can use the method ConcurrentCompiledPackages#getPackageDependencies for this purpose. Note, you do not need to traverse the subtree as the packages you would find there are necessarily dependencies of the package of the root node.