Skip to content

Commit

Permalink
allow ignition modules as a dependency for futures
Browse files Browse the repository at this point in the history
  • Loading branch information
zoeyTM committed Nov 1, 2024
1 parent 44ebf75 commit a62df74
Show file tree
Hide file tree
Showing 24 changed files with 269 additions and 76 deletions.
59 changes: 45 additions & 14 deletions packages/core/src/ignition-module-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
contractName: future.contractName,
constructorArgs: future.constructorArgs.map((arg) =>
Expand All @@ -161,7 +163,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
contractName: future.contractName,
artifact: future.artifact,
Expand All @@ -187,7 +191,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
contractName: future.contractName,
from: isRuntimeValue(future.from)
Expand All @@ -204,7 +210,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
contractName: future.contractName,
artifact: future.artifact,
Expand All @@ -222,7 +230,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
contract: this._convertFutureToFutureToken(future.contract),
functionName: future.functionName,
Expand All @@ -245,7 +255,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
contract: this._convertFutureToFutureToken(future.contract),
functionName: future.functionName,
Expand All @@ -264,7 +276,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
contract: this._convertFutureToFutureToken(future.contract),
functionName: future.functionName,
Expand All @@ -279,7 +293,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
contractName: future.contractName,
address: isFuture(future.address)
Expand All @@ -298,7 +314,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
contractName: future.contractName,
artifact: future.artifact,
Expand All @@ -318,7 +336,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
futureToReadFrom: this._convertFutureToFutureToken(
future.futureToReadFrom
Expand All @@ -336,7 +356,9 @@ export class IgnitionModuleSerializer {
moduleId: future.module.id,
type: future.type,
dependencies: Array.from(future.dependencies).map((d) =>
this._convertFutureToFutureToken(d)
isFuture(d)
? this._convertFutureToFutureToken(d)
: this._convertModuleToModuleToken(d)
),
to: isFuture(future.to)
? this._convertFutureToFutureToken(future.to)
Expand Down Expand Up @@ -475,7 +497,14 @@ export class IgnitionModuleDeserializer {
);

for (const dependencyId of serializedFuture.dependencies) {
const dependency = this._lookup(futuresLookup, dependencyId.futureId);
let dependency: Future | IgnitionModule;

if (dependencyId._kind === "FutureToken") {
dependency = this._lookup(futuresLookup, dependencyId.futureId);
} else {
dependency = this._lookup(modulesLookup, dependencyId.moduleId);
}

future.dependencies.add(dependency);
}

Expand Down Expand Up @@ -548,8 +577,10 @@ export class IgnitionModuleDeserializer {

for (const serializedFuture of serializedFutures) {
for (const dependencyToken of serializedFuture.dependencies) {
const dependency = serializedFuturesMap[dependencyToken.futureId];
graph.get(dependency)!.add(serializedFuture);
if (dependencyToken._kind === "FutureToken") {
const dependency = serializedFuturesMap[dependencyToken.futureId];
graph.get(dependency)!.add(serializedFuture);
}
}
}

Expand Down
23 changes: 22 additions & 1 deletion packages/core/src/internal/batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,28 @@ export class Batcher {
): boolean {
const dependencies = batchState.adjacencyList.getDependenciesFor(futureId);

return [...dependencies].every(
return [...dependencies].every((depId) => {
if (/#/.test(depId)) {
return batchState.visitState[depId] === VisitStatus.VISITED;
}

return this._checkModuleDependencyIsComplete(depId, batchState);
});
}

/**
* This is needed because moduleIds are not present in the visit state
* causing an infinite loop when checking whether a depenedency is visited if that dependency is a module.
*/
private static _checkModuleDependencyIsComplete(
moduleId: string,
batchState: BatchState
) {
const dependencies = Object.keys(batchState.visitState).filter((futureId) =>
futureId.startsWith(moduleId)
);

return dependencies.every(
(depId) => batchState.visitState[depId] === VisitStatus.VISITED
);
}
Expand Down
40 changes: 40 additions & 0 deletions packages/core/src/internal/module-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

for (const libraryFuture of Object.values(options.libraries)) {
Expand Down Expand Up @@ -356,6 +360,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

for (const libraryFuture of Object.values(options.libraries)) {
Expand Down Expand Up @@ -426,6 +434,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

for (const libraryFuture of Object.values(options.libraries)) {
Expand Down Expand Up @@ -470,6 +482,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

for (const libraryFuture of Object.values(options.libraries)) {
Expand Down Expand Up @@ -543,6 +559,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

this._module.futures.add(future);
Expand Down Expand Up @@ -607,6 +627,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

this._module.futures.add(future);
Expand Down Expand Up @@ -669,6 +693,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

this._module.futures.add(future);
Expand Down Expand Up @@ -774,6 +802,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

if (isFuture(address)) {
Expand Down Expand Up @@ -819,6 +851,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

if (isFuture(address)) {
Expand Down Expand Up @@ -953,6 +989,10 @@ class IgnitionModuleBuilderImplementation<

for (const afterFuture of options.after ?? []) {
future.dependencies.add(afterFuture);

if (!isFuture(afterFuture)) {
this._module.submodules.add(afterFuture);
}
}

this._module.futures.add(future);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/internal/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
const customInspectSymbol = Symbol.for("util.inspect.custom");

abstract class BaseFutureImplementation<FutureTypeT extends FutureType> {
public readonly dependencies: Set<Future> = new Set();
public readonly dependencies: Set<Future | IgnitionModule> = new Set();

constructor(
public readonly id: string,
Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/internal/utils/adjacency-list-converter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isFuture } from "../../type-guards";
import { Future } from "../../types/module";

import { AdjacencyList } from "./adjacency-list";
Expand All @@ -12,11 +13,13 @@ export class AdjacencyListConverter {
for (const dependency of future.dependencies) {
dependencyGraph.addDependency({ from: future.id, to: dependency.id });

this._optionallyAddDependenciesSubmoduleSiblings(
dependencyGraph,
future,
dependency
);
if (isFuture(dependency)) {
this._optionallyAddDependenciesSubmoduleSiblings(
dependencyGraph,
future,
dependency
);
}
}
}

Expand Down
Loading

0 comments on commit a62df74

Please sign in to comment.