@midnight-ntwrk/compact-runtime v0.7.0 • Readme | API
@midnight-ntwrk/compact-runtime v0.7.0 / contractDependencies
Function: contractDependencies()
contractDependencies(contractReferenceLocations, state): string[]
Given a StateValue representing the current ledger state of a contract, uses the ContractReferenceLocations object produced by the Compact compiler to extract the current contract addresses present in the given ledger state. The produced contract addresses represent the contracts on which the root contract depends. The dependencies are used in a multi-contract setting to fetch the ledger states of all contracts on which the root contract depends prior to execution.
NOTE: The given ContractReferenceLocations must be from the contract executable containing the ledger state constructor that produced the given StateValue.
Parameters
• contractReferenceLocations: ContractReferenceLocations
A data structure pointing to contract references in the ledger state of the root contract.
• state: StateValue
The current ledger state of the root contract.
Returns
string
[]
A list of all contract addresses (references) present in the given ledger state.
Remarks
The algorithm has three main stages:
- It unwraps the PublicLedgerSegments in the given ContractReferenceLocations until a SparseCompactADT is reached. Each time a PublicLedgerSegments is unwrapped, it casts the current state value to a state value array and proceeds recursively with each of the state values and unwrapped ledger segments.
- It unwraps each SparseCompactADT in the current PublicLedgerSegments until a SparseCompactType is reached. Each time a SparseCompactADT is unwrapped, it casts the current state value to a state representation indicated by the SparseCompactADT.
- Once the current state can no longer be reduced, it must represent a Compact contract address somewhere inside the state, and that contract address is added to the dependency set.