import crypto from 'crypto'; import Debug from 'debug'; import _ from 'lodash'; import type { Address } from 'viem'; import { ActionKinds, RawChainDefinition, checkConfig } from './actions'; import { ChainBuilderRuntime } from './runtime'; import { chainDefinitionSchema } from './schemas'; import { ChainBuilderContext } from './types'; import { template } from './utils/template'; import { PackageReference } from './package-reference'; import { ZodIssue } from 'zod'; import { AccessRecorderEngine } from './access-recorder'; const debug = Debug('cannon:builder:definition'); const debugVerbose = Debug('cannon:verbose:builder:definition'); type OutputClashCheckResult = { actions: string[]; output: string; }; export type ChainDefinitionProblems = { invalidSchema: ZodIssue[] | null; missing: { action: string; dependency: string }[]; cycles: string[][]; extraneousDeps: { node: string; extraneous: string; inDep: string }[]; outputClashes: OutputClashCheckResult[]; }; export type StateLayers = { [key: string]: { actions: string[]; depends: string[]; }; }; export function validatePackageName(n: string) { if (n.length < 3) { throw new Error('package name must be at least 3 characters long'); } if (n.length > 31) { throw new Error('package name must be at most 31 characters long'); } if (_.last(n) == '-' || _.first(n) == '-') { throw new Error('first and last character of package name must not be dash (-)'); } if (!n.match(/^[0-9a-z-]*$/)) { throw new Error('cannon packages can only have names connecting lowercase, alphanumeric characters, and dashes'); } } export function validatePackageVersion(v: string) { if (v.length > 31) { throw new Error('package version must be at most 31 characters long'); } } export class ChainDefinition { private raw: RawChainDefinition; private sensitiveDependencies: boolean; readonly allActionNames: string[]; private _roots: Set = new Set(); private _leaves: Set = new Set(); private cachedLayers: StateLayers | null = null; readonly cachedActionDepths = new Map(); readonly dependencyFor = new Map(); readonly resolvedDependencies = new Map(); accessRecorderEngine: AccessRecorderEngine | null = null; readonly danglingDependencies = new Set<`${string}:${string}`>(); constructor(def: RawChainDefinition, sensitiveDependencies = false) { debug('begin chain def init'); this.raw = def; this.sensitiveDependencies = sensitiveDependencies; // best way to get a list of actions is just to iterate over the entire def, and filter out anything // that are not an actions (because those are known) const actionsDef = _.omit(def, 'name', 'version', 'preset', 'description', 'keywords', 'deployers'); // Used to validate that there are not 2 steps with the same name const actionNames: string[] = []; const actions = []; for (const [action, data] of Object.entries(actionsDef)) { for (const name of Object.keys(data as any)) { // TODO: remove this? if (actionNames.includes(name)) { throw new Error(`Duplicated operation name found "${name}"`); } const fullActionName = `${action}.${name}`; // backwards-compatibility: We dont store setting or var names as they can have duplicate names if (action !== 'setting' && action !== 'var') { actionNames.push(name); } actions.push(fullActionName); } } // do some preindexing this.allActionNames = _.sortBy(actions, _.identity); debug('finished chain def init'); } getName(ctx: ChainBuilderContext) { const n = template(this.raw.name, ctx); validatePackageName(n); return n; } getVersion(ctx: ChainBuilderContext) { const v = template(this.raw.version, ctx); validatePackageVersion(v); return v; } getPreset(ctx: ChainBuilderContext) { return template(this.raw.preset || PackageReference.DEFAULT_PRESET, ctx); } getPackageRef(ctx: ChainBuilderContext) { return PackageReference.from(this.getName(ctx), this.getVersion(ctx), this.getPreset(ctx)); } getDeployers() { return (this.raw.deployers as Address[]) || []; } isPublicSourceCode() { return !this.raw.privateSourceCode; } getConfig(n: string, ctx: ChainBuilderContext) { if (_.sortedIndexOf(this.allActionNames, n) === -1) { throw new Error(`getConfig operation name not found: ${n}`); } const kind = n.split('.')[0] as keyof typeof ActionKinds; const action = ActionKinds[kind]; if (!action) { throw new Error( `action kind plugin not installed: "${kind}" (for action: "${n}"). please install the plugin necessary to build this package.` ); } return action.configInject({ ...ctx }, _.get(this.raw, n), { ref: this.getPackageRef(ctx), currentLabel: n, }); } /** * Used to determine if a state needs to be re-run or not * @param n action name * @param ctx context used to generate configuration for the action * @returns string representing the current state of the action */ async getState( n: string, runtime: ChainBuilderRuntime, ctx: ChainBuilderContext, tainted: boolean ): Promise { const kind = n.split('.')[0] as keyof typeof ActionKinds; if (!ActionKinds[kind]) { debug('action plugin not installed for state eval:', kind); if (tainted) { debug('state is tainted for custom plugin. cant recompute state. issuing invalid state.'); return []; } else { // no dependencies have changed, though it is possible that a setting inject means this need to be rebuilt return null; } } const objs = await ActionKinds[kind].getState(runtime, { ...ctx }, this.getConfig(n, ctx) as any, { ref: this.getPackageRef(ctx), currentLabel: n, }); if (!objs) { return null; } else { if (debugVerbose.enabled) { debugVerbose('creating hash of', objs.map(JSON.stringify as any)); } return objs.map((o) => crypto.createHash('md5').update(JSON.stringify(o)).digest('hex')); } } /** * Returns a list of imported cannon packages which would be needed to successfully build this package * @param ctx context used for interpolation * @returns list of required imported cannon charts */ getRequiredImports(ctx: ChainBuilderContext) { if (!this.raw.import) return []; // we have to apply templating here, only to the `source` // it would be best if the dep was downloaded when it was discovered to be needed, but there is not a lot we // can do about this right now return _.uniq( Object.values(this.raw.import).map((d) => ({ source: template(d.source, ctx), chainId: d.chainId || ctx.chainId, preset: d.preset ? template(d.preset, ctx) : 'main', })) ); } /** * Get direct dependencies for a given node * @param node action to get dependencies for * @returns direct dependencies for the specified node */ computeDependencies(node: string) { if (!this.accessRecorderEngine) { this.computePossibleNames(); } if (!_.get(this.raw, node)) { const stepName = node.split('.')[0]; const possibleSteps = _.get(this.raw, stepName); if (!possibleSteps) { throw new Error(`invalid dependency: ${node}`); } throw new Error(`invalid dependency: ${node}. Available "${stepName}" operations: ${Object.keys(possibleSteps) .map((dep) => `\n - ${stepName}.${dep}`) .join('')} `); } const deps = _.clone(_.get(this.raw, node)!.depends || []) as string[]; const n = node.split('.')[0]; if (!ActionKinds[n]) { throw new Error(`Unrecognized action type ${n} at [${node}]`); } if (ActionKinds[n].getInputs) { const accessComputationResults = ActionKinds[n].getInputs!(_.get(this.raw, node), this.accessRecorderEngine!, { ref: null, currentLabel: node, }); // Only throw this error if the user hasn't explicitly defined dependencies if (this.sensitiveDependencies && accessComputationResults.unableToCompute && !_.get(this.raw, node).depends) { throw new Error( `Unable to compute dependencies for [${node}] because of advanced logic in template strings. Specify dependencies manually, like "depends = ['${_.uniq( _.uniq(accessComputationResults.accesses).map((a) => `${this.dependencyFor.get(a)}`) ).join("', '")}']"` ); } for (const input of accessComputationResults.accesses) { debug(`deps: ${node} consumes ${input}`); if (this.dependencyFor.has(input)) { deps.push(this.dependencyFor.get(input)!); } else { debug(`WARNING: dependency ${input} not found for operation ${node}`); this.danglingDependencies.add(`${input}:${node}`); } } } debug(`resolved dependencies for ${node}: ${deps}`); return _.uniq(deps); } getDependencies(node: string) { return this.resolvedDependencies.get(node) || []; } /** * @note deps returned in topological order * @returns all dependencies reachable from the specfied node, and the depth of the iteration required */ getDependencyTree: (node: string) => [string[], number] = _.memoize((node) => { const deps = this.getDependencies(node); const allDeps = []; let maxDepth = 0; for (const dep of deps) { const [subDeps, subDepth] = this.getDependencyTree(dep); allDeps.push(...subDeps); allDeps.push(dep); maxDepth = Math.max(maxDepth, subDepth + 1); } this.cachedActionDepths.set(node, maxDepth); return [_.uniq(allDeps), maxDepth]; }); initializeComputedDependencies = _.memoize(() => { const computeDepsDebug = Debug('cannon:builder:dependencies'); computeDepsDebug('start compute dependencies'); // checking for output clashes will also fill out the dependency map const clashes = this.checkOutputClash(); computeDepsDebug('finished checking clashes'); if (clashes.length) { throw new Error(`cannot generate dependency tree: output clashes exist: ${JSON.stringify(clashes)}`); } // get all dependencies, and filter out the extraneous for (const action of this.allActionNames) { debug(`compute dependencies for ${action}`); const deps = this.computeDependencies(action); this.resolvedDependencies.set(action, deps); } computeDepsDebug('finished compute dependencies'); const cycles = this.checkCycles(); if (cycles?.length) { throw new Error(`cannot generate dependency tree: dependency cycle found: ${cycles.join(' => ')}`); } this._roots = new Set(this.allActionNames.filter((n) => !this.getDependencies(n).length)); if (computeDepsDebug.enabled) { computeDepsDebug(`computed roots: ${Array.from(this._roots.values()).join(', ')}`); } this._leaves = new Set( _.difference( this.allActionNames, _.chain(this.allActionNames) .map((n) => this.getDependencies(n)) .flatten() .uniq() .value() ) ); if (computeDepsDebug.enabled) { computeDepsDebug(`computed leaves: ${Array.from(this._leaves.values()).join(', ')}`); } if (debug.enabled) { debug('computed depends dump:'); for (const action of this.allActionNames) { debug(`${action} has depends`, this.resolvedDependencies.get(action)); } } debug('finish compute dependencies'); }); get topologicalActions() { this.initializeComputedDependencies(); for (const leaf of this.leaves) { const [, maxDepth] = this.getDependencyTree(leaf); this.cachedActionDepths.set(leaf, maxDepth); } return _.sortBy(this.allActionNames, (n) => this.cachedActionDepths.get(n)); } // actions which have no dependencies get roots() { this.initializeComputedDependencies(); return this._roots; } // actions which are not depended on by anything get leaves() { this.initializeComputedDependencies(); return this._leaves; } checkAll(): ChainDefinitionProblems | null { const invalidSchema = checkConfig(chainDefinitionSchema, this.raw); const missing = this.checkMissing(); const cycle = missing.length ? [] : this.checkCycles(); // TODO: this check seems to be a bit too sensitive atm const extraneousDeps: { node: string; extraneous: string; inDep: string }[] = []; // this.checkExtraneousDependencies(); const outputClashes = this.checkOutputClash(); for (const extDep of extraneousDeps) { const deps = this.resolvedDependencies.get(extDep.node)!; const depIdx = deps.indexOf(extDep.extraneous); if (depIdx !== -1) { deps.splice(depIdx, 1); } } if (!invalidSchema && !missing.length && !cycle && !extraneousDeps.length && !outputClashes.length) { return null; } return { invalidSchema, missing, cycles: cycle && cycle.length ? [cycle] : [], extraneousDeps, outputClashes, }; } checkOutputClash(actions = this.allActionNames): OutputClashCheckResult[] { const outputClashes: OutputClashCheckResult[] = []; for (const fullActionName of actions) { const [actionType] = fullActionName.split('.'); if (ActionKinds[actionType] && ActionKinds[actionType].getOutputs) { const actionOutputs = ActionKinds[actionType].getOutputs!(_.get(this.raw, fullActionName), { ref: null, currentLabel: fullActionName, }); for (const output of actionOutputs) { debug(`deps: ${fullActionName} provides ${output}`); if (!this.dependencyFor.has(output) || actionType === 'setting' || actionType === 'var') { this.dependencyFor.set(output, fullActionName); } else if (this.dependencyFor.get(output) !== fullActionName) { outputClashes.push({ output, actions: [this.dependencyFor.get(output)!, fullActionName] }); } } } } this.computePossibleNames(); return outputClashes; } computePossibleNames() { if (this.allActionNames.length > 0 && this.dependencyFor.size == 0) { this.checkOutputClash(); } const possibleNames = []; for (const k of this.dependencyFor.keys()) { const baseName = k.split('.')[0]; if ( baseName !== 'contracts' && baseName !== 'imports' && baseName !== 'settings' && baseName !== 'extras' && baseName !== 'txns' ) { possibleNames.push(baseName); } } this.accessRecorderEngine = new AccessRecorderEngine(possibleNames); } /** * Checks for any dependency actions which aren't defined among the given list of dependencies to check * @param actions which actions to check for missing dependencies * @returns a list of missing dependencies */ checkMissing(actions = this.allActionNames) { this.initializeComputedDependencies(); const missing: { action: string; dependency: string }[] = []; for (const n of actions) { for (const dep of this.getDependencies(n)) { if (_.sortedIndexOf(this.allActionNames, dep) === -1) { missing.push({ action: n, dependency: dep }); } } } return missing; } /** * Determines if the nodes reachable from the given list of actions have any cycles. * * If any cycles exist, an array of all the nodes involved in the cycle will be returned * * If more than one cycle exists, returns only the first one encountered. */ checkCycles( actions = this.allActionNames, seenNodes = new Set(), currentPath = new Set() ): string[] | null { // resolved dependencies gets set during dependency computation for (const n of actions) { if (seenNodes.has(n)) { return null; } if (currentPath.has(n)) { // we have just hit a cycle return [n]; } currentPath.add(n); const cycle = this.checkCycles(this.resolvedDependencies.get(n), seenNodes, currentPath); if (cycle) { if (this.resolvedDependencies.get(cycle[cycle.length - 1])?.indexOf(cycle[0]) === -1) { cycle.unshift(n); } return cycle; } // if we make it this far we know that this node is not involved in a cycle currentPath.delete(n); seenNodes.add(n); } return null; } /** * Runs `checkExtraneousdependenciesOnNode` for all actions, effectively checking the whole * chain definition for any extraneous dependencies. * @note according to wikipidea, this is allegedly the fastest algorithm to verify a transitive reduction * @returns list of extraneous dependencies found */ checkExtraneousDependencies() { const extraneous = []; for (const action of this.topologicalActions) { extraneous.push(...this.checkExtraneousDependenciesOnNode(action)); } return extraneous; } /** * Determines if the given node has any extraneous dependencies (that is, a dependency which is also contained in one of its children). * * If any extraneous dependencies are found, they are returned. * * @param node the node to start verifying from * @returns list of extraneous dependencies found */ checkExtraneousDependenciesOnNode(node: string): { node: string; extraneous: string; inDep: string }[] { const deps = this.getDependencies(node); const extraneous = []; // extraneous dependency is defined as a direct dependency // on this node which is also a deeper dependency of another // dependency of this node for (const dep of deps) { for (const d of deps) { const [childDeps] = this.getDependencyTree(dep); if (childDeps.includes(d)) { extraneous.push({ node, extraneous: d, inDep: dep }); } } } return extraneous; } getLayerDependencyTree(n: string, layers: StateLayers): string[] { const deps = []; if (!layers[n]) { debug('WARN: layer dependency tree not computable for operation because not found:', n); return []; } for (const dep of layers[n].depends) { deps.push(...this.getLayerDependencyTree(dep, layers)); } deps.push(...layers[n].actions); return deps; } // on local nodes, steps depending on the same base need to be merged into "layers" to prevent state collisions // returns an array of layers which can be deployed as a unit in topological order getStateLayers(): StateLayers { if (this.cachedLayers) { return this.cachedLayers; } const actions = this.topologicalActions; const layers: { [key: string]: { actions: string[]; depends: string[] } } = {}; const layerOfActions = new Map(); const layerDependingOn = new Map(); debug('start compute state layers', actions); for (const n of actions) { if (layerOfActions.has(n)) { continue; } const action = _.get(this.raw, n); if (!action) { throw new Error(`action not found: ${n}`); } // starts in its own layer debug('layer up', n); layers[n] = { actions: [n], // attached to all the layers of the dependencies depends: [], }; let attachingLayer: string = n; let deps = this.getDependencies(n); debug('layer dependencies before', deps); // first. filter any deps which are extraneous. This is a dependency which is a subdepenendency of an assigned layer for a dependency. // @note this is the slowest part of cannon atm. Improvements here would be most important. for (const dep of deps) { if (!layers[dep]) { debug('WARN: unknown dependency recorded in cannonfile:', dep); continue; } for (const depdep of layers[dep].depends) { const depTree = this.getLayerDependencyTree(depdep, layers); deps = deps.filter((d) => depTree.indexOf(d) === -1); } } deps = _.sortBy(deps, (d) => -this.cachedActionDepths.get(d)!); debug('layer dependencies after', deps); for (const dep of deps) { // layer is guarenteed to exist here because topological sort const depLayer = layerOfActions.get(dep)!; let dependingLayer = layerDependingOn.get(depLayer); if (dependingLayer && dependingLayer !== attachingLayer) { while (layerDependingOn.has(dependingLayer!)) { debug(`stepping up dep ${dependingLayer} because its deep already`); dependingLayer = layerDependingOn.get(dependingLayer!); } if (attachingLayer == dependingLayer) { // dependency is already handled continue; } // "merge" this entire layer into the other one debug(`merge from ${attachingLayer} into layer`, dependingLayer); layers[dependingLayer!].actions.push(...layers[attachingLayer].actions); layers[dependingLayer!].depends = _.uniq([...layers[dependingLayer!].depends, ...layers[attachingLayer].depends]); // ensure the other nodes are now pointing to this structure for (const a of layers[attachingLayer].actions) { layers[a] = layers[dependingLayer!]; } attachingLayer = dependingLayer!; } else if (layers[attachingLayer].depends.indexOf(depLayer) === -1) { // "extend" this layer to encapsulate this debug(`extend the layer ${attachingLayer} with dep`, depLayer); layers[attachingLayer].depends.push(depLayer); } } // ensure layer is set for the node we are on layers[n] = layers[attachingLayer]; for (const n of layers[attachingLayer].actions) { layerOfActions.set(n, attachingLayer); } for (const d of layers[attachingLayer].depends) { layerDependingOn.set(d, attachingLayer); } layers[attachingLayer].actions.sort(); layers[attachingLayer].depends.sort(); } debug('end compute state layer', actions); this.cachedLayers = layers; return layers; } private getPrintLinesUsed(n: string, layers = this.getStateLayers()): number { if (!layers[n]) { debug('WARN: cannot calculate print lines used for layer becuase undefined:', n); return 0; } return Math.max( layers[n].actions.length + 2, _.sumBy(layers[n].depends, (d) => this.getPrintLinesUsed(d, layers)) ); } printPartialTopology(n: string, layers = this.getStateLayers(), line = 0): string { const layer = layers[n]; let output = ''; // print myself const width = _.maxBy(layer.actions, 'length')!.length; if (line === 0) { output = '┌─' + '─'.repeat(width) + '─┐'; } else if (line <= layer.actions.length) { output = '│ ' + layer.actions[line - 1] + ' '.repeat(width - layer.actions[line - 1].length) + ' │'; } else if (line === layer.actions.length + 1) { output = '└─' + '─'.repeat(width) + '─┘'; } // print the layers we depend on if (layer.depends.length) { // get current dependency printing info let depIndex = 0; let depUsedLines = 0; for (; depIndex < layer.depends.length; depIndex++) { const lineCount = this.getPrintLinesUsed(layer.depends[depIndex], layers); if (depUsedLines + lineCount > line) { break; } else { depUsedLines += lineCount; } } if (depIndex < layer.depends.length) { if (!output) { output += ' '.repeat(width + 4); } if (line === 0) { output += ' '; } else if (line === 1) { output += layer.depends.length > 1 ? '──┬──' : '─────'; } else if (line - depUsedLines === 1) { output += depIndex < layer.depends.length - 1 ? ' ├──' : ' └──'; } else if (depIndex < layer.depends.length - 1 || line - depUsedLines < 1) { output += ' │ '; } else { output += ' '; } output += this.printPartialTopology(layer.depends[depIndex], layers, line - depUsedLines); } } return output; } /** * returns human-readable representation of the dag dependency graph of this chain definition * @returns array of lines to print */ printTopology(nodes: string[] = []): string[] { const layers = this.getStateLayers(); if (!nodes.length) { // get leaf layers const layerArray = _.uniq(Object.values(layers)); layerSearch: for (const layer of layerArray) { for (const action of layer.actions) { if (layerArray.find((l) => l.depends.indexOf(action) !== -1)) { // this isnt a leaf continue layerSearch; } } nodes.push(layer.actions[0]); } } debug('begin print topology', nodes); // for each leaf layer, print it until there is empty output const completeLines = []; for (const n of nodes) { const lines = []; let line; while ((line = this.printPartialTopology(n, layers, lines.length)) !== '') { lines.push(line); } completeLines.push(...lines); } debug('end print topology', nodes); return completeLines; } toJson(): RawChainDefinition { return _.cloneDeep(this.raw); } }