Skip to content

Commit

Permalink
Code review and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Jan 8, 2022
1 parent b7b1617 commit 1ddf0a8
Show file tree
Hide file tree
Showing 23 changed files with 365 additions and 314 deletions.
4 changes: 2 additions & 2 deletions config/eslint/eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ module.exports = {
},
],
"@typescript-eslint/unified-signatures": "error",
"@typescript-eslint/return-await": "error",
"constructor-super": "error",
eqeqeq: ["error", "always"],
"guard-for-in": "error",
Expand All @@ -162,7 +163,7 @@ module.exports = {
"sibling",
"index",
],
"newlines-between": "always"
"newlines-between": "always",
},
],
"no-bitwise": "error",
Expand All @@ -177,7 +178,6 @@ module.exports = {
"no-new-func": "error",
"no-new-wrappers": "error",
"no-return-await": "off",
"@typescript-eslint/return-await": "error",
"no-sequences": "error",
"no-sparse-arrays": "error",
"no-template-curly-in-string": "error",
Expand Down
6 changes: 1 addition & 5 deletions examples/simple-ts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import "@nomiclabs/hardhat-ignition"
export default {
solidity: "0.8.4",
networks: {
hardhat: {
// mining: {
// auto: false
// }
},
hardhat: {},
},
};
2 changes: 1 addition & 1 deletion examples/simple-ts/ignition/Timelock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class TimelockCallExecutor extends Executor<TimelockCallOptions, string> {
}

public getDescription() {
return "Deploy contract with multisig and timelock";
return "Deploy contract with timelock";
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/fs-extra": "^9.0.13",
"@types/mocha": "^9.0.0",
"@types/node": "12.20.25",
"@types/react": "^17.0.35",
"@types/sinon": "^10.0.6",
"@types/tmp": "^0.2.2",
"@typescript-eslint/eslint-plugin": "4.31.2",
Expand All @@ -37,7 +38,6 @@
"typescript": "^4.4.3"
},
"dependencies": {
"@types/react": "^17.0.35",
"debug": "^4.3.2",
"ethers": "^5.4.7",
"fs-extra": "^10.0.0",
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ export function deserializeBindingOutput(x: any) {
}

export type ModuleResult = Record<string, BindingOutput>;
export type SerializedModuleResult = Record<
string,
ReturnType<typeof serializeBindingOutput>
>;
export type SerializedModuleResult = Record<string, SerializedBindingResult>;
export type SerializedBindingResult = ReturnType<typeof serializeBindingOutput>;

export type SerializedDeploymentResult = Record<string, SerializedModuleResult>;

Expand Down
32 changes: 15 additions & 17 deletions packages/core/src/execution-engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import debug from "debug";
import setupDebug from "debug";

import {
deserializeBindingOutput,
Expand All @@ -21,18 +21,15 @@ import {
import { TxSender } from "./tx-sender";
import { sleep } from "./utils";

export type GetModuleResult = (
moduleId: string
) => Promise<SerializedModuleResult | undefined>;

export type SaveModuleResult = (
moduleId: string,
moduleResult: SerializedModuleResult
) => Promise<void>;
export interface IgnitionModulesResults {
load: (moduleId: string) => Promise<SerializedModuleResult | undefined>;
save: (
moduleId: string,
moduleResult: SerializedModuleResult
) => Promise<void>;
}

export interface ExecutionEngineOptions {
saveModuleResult: SaveModuleResult;
getModuleResult: GetModuleResult;
parallelizationLevel: number;
loggingEnabled: boolean;
txPollingInterval: number;
Expand All @@ -46,18 +43,18 @@ type ModulePlan = "already-deployed" | ExecutorPlan[];
export type DeploymentPlan = Record<string, ModulePlan>;

export class ExecutionEngine {
private _debug = debug("ignition:execution-engine");
private _debug = setupDebug("ignition:execution-engine");

public static async buildPlan(
executionGraph: ExecutionGraph,
{ getModuleResult }: { getModuleResult: GetModuleResult }
modulesResults: IgnitionModulesResults
): Promise<DeploymentPlan> {
const plan: DeploymentPlan = {};

const ignitionModules = executionGraph.getSortedModules();

for (const ignitionModule of ignitionModules) {
const moduleResult = await getModuleResult(ignitionModule.id);
const moduleResult = await modulesResults.load(ignitionModule.id);
if (moduleResult !== undefined) {
plan[ignitionModule.id] = "already-deployed";
continue;
Expand All @@ -80,6 +77,7 @@ export class ExecutionEngine {
constructor(
private _providers: Providers,
private _journal: Journal,
private _modulesResults: IgnitionModulesResults,
private _options: ExecutionEngineOptions
) {}

Expand Down Expand Up @@ -128,7 +126,7 @@ export class ExecutionEngine {

// execute each module sequentially
for (const executionModule of executionModules) {
const serializedModuleResult = await this._options.getModuleResult(
const serializedModuleResult = await this._modulesResults.load(
executionModule.id
);

Expand Down Expand Up @@ -217,7 +215,7 @@ export class ExecutionEngine {
) {
if (moduleState.isSuccess()) {
const moduleResult = moduleState.toModuleResult();
await this._options.saveModuleResult(ignitionModule.id, moduleResult);
await this._modulesResults.save(ignitionModule.id, moduleResult);
await this._journal.delete(ignitionModule.id);
}

Expand Down Expand Up @@ -315,7 +313,7 @@ export type DeploymentResult =
| { _kind: "success"; result: SerializedDeploymentResult };

export class ExecutionManager {
private _debug = debug("ignition:execution-manager");
private _debug = setupDebug("ignition:execution-manager");
constructor(
private _engine: ExecutionEngine,
private _tickInterval: number
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/executors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import debug, { IDebugger } from "debug";
import setupDebug, { IDebugger } from "debug";
import { ethers } from "ethers";

import {
Expand Down Expand Up @@ -34,7 +34,7 @@ export abstract class Executor<I = unknown, O extends BindingOutput = any> {
constructor(public readonly binding: InternalBinding<I, O>) {
const moduleId = binding.moduleId;
const bindingId = binding.id;
this._debug = debug(`ignition:executor:${moduleId}:${bindingId}`);
this._debug = setupDebug(`ignition:executor:${moduleId}:${bindingId}`);
}

abstract execute(input: Resolved<I>, services: Services): Promise<O>;
Expand Down
65 changes: 29 additions & 36 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import debug from "debug";
import setupDebug from "debug";

import {
AddressLike,
BindingOutput,
ContractBinding,
ContractOptions,
InternalBinding,
InternalContractBinding,
SerializedDeploymentResult,
SerializedModuleResult,
SerializedBindingResult,
} from "./bindings";
import { BindingState, ModuleState } from "./deployment-state";
import {
DeploymentPlan,
DeploymentResult,
ExecutionEngine,
ExecutionManager,
GetModuleResult,
SaveModuleResult,
IgnitionModulesResults,
} from "./execution-engine";
import { Executor, Hold } from "./executors";
import { FileJournal, InMemoryJournal } from "./journal";
Expand All @@ -31,8 +29,6 @@ export { DeploymentPlan } from "./execution-engine";
export { buildModule } from "./modules";
export {
AddressLike,
BindingOutput,
BindingState,
Contract,
ContractBinding,
ContractOptions,
Expand All @@ -42,45 +38,43 @@ export {
InternalBinding,
InternalContractBinding,
ModuleBuilder,
ModuleState,
Providers,
Services,
UserModule,
SerializedBindingResult,
SerializedModuleResult,
SerializedDeploymentResult,
};

const log = debug("ignition:main");
const log = setupDebug("ignition:main");

export interface IgnitionDeployOptions {
getModuleResult: GetModuleResult;
saveModuleResult: SaveModuleResult;
pathToJournal: string | undefined;
txPollingInterval: number;
}

type ModulesOutputs = Record<string, any>;

export class Ignition {
constructor(private _providers: Providers) {}
constructor(
private _providers: Providers,
private _modulesResults: IgnitionModulesResults
) {}

public async deploy(
userModules: Array<UserModule<any>>,
{
getModuleResult,
saveModuleResult,
pathToJournal,
txPollingInterval,
}: IgnitionDeployOptions
) {
{ pathToJournal, txPollingInterval }: IgnitionDeployOptions
): Promise<[DeploymentResult, ModulesOutputs]> {
log(`Start deploy, '${userModules.length}' modules`);

const m = new ModuleBuilderImpl();

const moduleOutputs: Record<string, any> = {};
const modulesOutputs: ModulesOutputs = {};

for (const userModule of userModules) {
log("Load module '%s'", userModule.id);
const moduleOutput = m.useModule(userModule) ?? {};
moduleOutputs[userModule.id] = moduleOutput;
modulesOutputs[userModule.id] = moduleOutput;
}

log("Build execution graph");
Expand All @@ -92,13 +86,16 @@ export class Ignition {
? new FileJournal(pathToJournal)
: new InMemoryJournal();

const engine = new ExecutionEngine(this._providers, journal, {
parallelizationLevel: 2,
loggingEnabled: pathToJournal !== undefined,
txPollingInterval,
getModuleResult,
saveModuleResult,
});
const engine = new ExecutionEngine(
this._providers,
journal,
this._modulesResults,
{
parallelizationLevel: 2,
loggingEnabled: pathToJournal !== undefined,
txPollingInterval,
}
);

const executionManager = new ExecutionManager(
engine,
Expand All @@ -108,28 +105,24 @@ export class Ignition {
log("Execute deployment");
const deploymentResult = await executionManager.execute(executionGraph);

return [deploymentResult, moduleOutputs] as const;
return [deploymentResult, modulesOutputs];
}

public async buildPlan(
userModules: Array<UserModule<any>>,
{ getModuleResult }: { getModuleResult: GetModuleResult }
userModules: Array<UserModule<any>>
): Promise<DeploymentPlan> {
log(`Start building plan, '${userModules.length}' modules`);

const m = new ModuleBuilderImpl();

const moduleOutputs: any[] = [];

for (const userModule of userModules) {
log("Load module '%s'", userModule.id);
const moduleOutput = m.useModule(userModule);
moduleOutputs.push(moduleOutput);
m.useModule(userModule);
}

log("Build ExecutionGraph");
const executionGraph = m.buildExecutionGraph();

return ExecutionEngine.buildPlan(executionGraph, { getModuleResult });
return ExecutionEngine.buildPlan(executionGraph, this._modulesResults);
}
}
8 changes: 5 additions & 3 deletions packages/core/src/journal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import debug from "debug";
import setupDebug, { IDebugger } from "debug";
import fsExtra from "fs-extra";

export interface JournalEntry {
Expand Down Expand Up @@ -32,7 +32,7 @@ export interface Journal {
}

export class FileJournal implements Journal {
private _log: debug.IDebugger = debug("ignition:journal:file-journal");
private _log: IDebugger = setupDebug("ignition:journal:file-journal");

constructor(private _path: string) {}

Expand Down Expand Up @@ -123,7 +123,9 @@ export class FileJournal implements Journal {
}

export class InMemoryJournal implements Journal {
private _log: debug.IDebugger = debug("ignition:journal:in-memory-journal");
private _log: debug.IDebugger = setupDebug(
"ignition:journal:in-memory-journal"
);
private _journal: Map<string, Map<string, JournalEntry[]>> = new Map();

public async addEntry(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import debug from "debug";
import setupDebug from "debug";
import { ethers, Contract, ContractFactory } from "ethers";

import { IgnitionSigner, Providers } from "./providers";
Expand All @@ -15,7 +15,7 @@ interface TransactionOptions {
}

export class ContractsService {
private _debug = debug("ignition:services:contracts-service");
private _debug = setupDebug("ignition:services:contracts-service");
private _ethersProvider: ethers.providers.Web3Provider;

constructor(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/tx-sender.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import debug, { IDebugger } from "debug";
import setupDebug, { IDebugger } from "debug";
import { ethers } from "ethers";

import { Journal } from "./journal";
Expand All @@ -22,7 +22,7 @@ export class TxSender {
private _gasProvider: GasProvider,
private _journal: Journal
) {
this._debug = debug(`ignition:tx-sender:${_moduleId}:${_executorId}`);
this._debug = setupDebug(`ignition:tx-sender:${_moduleId}:${_executorId}`);
}

/**
Expand Down
Loading

0 comments on commit 1ddf0a8

Please sign in to comment.