Skip to content

Commit

Permalink
fix: get the basics running again
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Aug 22, 2019
1 parent e8a2d6b commit 53e4469
Show file tree
Hide file tree
Showing 47 changed files with 327 additions and 257 deletions.
12 changes: 6 additions & 6 deletions packages/core-blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { BlockProcessor, BlockProcessorResult } from "./processor";
import { stateMachine } from "./state-machine";

const logger = app.resolve<Contracts.Kernel.ILogger>("logger");
const emitter = app.resolve<Contracts.Kernel.IEventDispatcher>("event-emitter");
const emitter = app.resolve<Contracts.Kernel.IEventDispatcher>("event-dispatcher");
const { BlockFactory } = Blocks;

export class Blockchain implements Contracts.Blockchain.IBlockchain {
Expand Down Expand Up @@ -95,23 +95,23 @@ export class Blockchain implements Contracts.Blockchain.IBlockchain {
public dispatch(event): void {
const nextState = stateMachine.transition(this.state.blockchain, event);

if (nextState.State.actions.length > 0) {
if (nextState.actions.length > 0) {
logger.debug(
`event '${event}': ${JSON.stringify(this.state.blockchain.value)} -> ${JSON.stringify(
nextState.State.value,
)} -> actions: [${nextState.State.actions.map(a => a.type).join(", ")}]`,
nextState.value,
)} -> actions: [${nextState.actions.map(a => a.type).join(", ")}]`,
);
} else {
logger.debug(
`event '${event}': ${JSON.stringify(this.state.blockchain.value)} -> ${JSON.stringify(
nextState.State.value,
nextState.value,
)}`,
);
}

this.state.blockchain = nextState;

for (const actionKey of nextState.State.actions) {
for (const actionKey of nextState.actions) {
const action = this.actions[actionKey];

if (action) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-blockchain/src/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { blockchainMachine } from "./machines/blockchain";
import { Blockchain } from "./blockchain";

const { BlockFactory } = Blocks;
const emitter = app.resolve<Contracts.Kernel.IEventDispatcher>("event-emitter");
const emitter = app.resolve<Contracts.Kernel.IEventDispatcher>("event-dispatcher");
const logger = app.resolve<Contracts.Kernel.ILogger>("logger");
const stateStorage = app.resolve<Contracts.State.IStateService>("state").getStore();

Expand Down
2 changes: 1 addition & 1 deletion packages/core-database-postgres/src/postgres-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class PostgresConnection implements Contracts.Database.IConnection {
public pgp: IMain;
private readonly logger: Contracts.Kernel.ILogger = app.resolve<Contracts.Kernel.ILogger>("logger");
private readonly emitter: Contracts.Kernel.IEventDispatcher = app.resolve<Contracts.Kernel.IEventDispatcher>(
"event-emitter",
"event-dispatcher",
);
private migrationsRepository: MigrationsRepository;
private cache: Map<any, any>;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-database-postgres/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ServiceProvider extends Support.AbstractServiceProvider {
const connectionManager = this.app.resolve<ConnectionManager>("database-manager");
const connection = await connectionManager.createConnection(new PostgresConnection(this.opts, walletManager));

this.app.bind("database", databaseServiceFactory(this.opts, walletManager, connection));
this.app.bind("database", await databaseServiceFactory(this.opts, walletManager, connection));
}

public async dispose(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-database-postgres/src/state-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Interfaces, Managers, Utils } from "@arkecosystem/crypto";
export class StateBuilder {
private readonly logger: Contracts.Kernel.ILogger = app.resolve<Contracts.Kernel.ILogger>("logger");
private readonly emitter: Contracts.Kernel.IEventDispatcher = app.resolve<Contracts.Kernel.IEventDispatcher>(
"event-emitter",
"event-dispatcher",
);

constructor(
Expand Down
2 changes: 1 addition & 1 deletion packages/core-database/src/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class DatabaseService implements Contracts.Database.IDatabaseService {
public connection: Contracts.Database.IConnection;
public walletManager: Contracts.State.IWalletManager;
public logger = app.resolve<Contracts.Kernel.ILogger>("logger");
public emitter = app.resolve<Contracts.Kernel.IEventDispatcher>("event-emitter");
public emitter = app.resolve<Contracts.Kernel.IEventDispatcher>("event-dispatcher");
public options: any;
public wallets: Contracts.Database.IWalletsBusinessRepository;
public delegates: Contracts.Database.IDelegatesBusinessRepository;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-elasticsearch/src/indices/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { storage } from "../storage";

export abstract class Index {
protected readonly emitter: Contracts.Kernel.IEventDispatcher = app.resolve<Contracts.Kernel.IEventDispatcher>(
"event-emitter",
"event-dispatcher",
);
protected readonly logger: Contracts.Kernel.ILogger = app.resolve<Contracts.Kernel.ILogger>("logger");
protected readonly database: Contracts.Database.IDatabaseService = app.resolve<Contracts.Database.IDatabaseService>(
Expand Down
2 changes: 1 addition & 1 deletion packages/core-forger/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class ForgerManager {
private initialized: boolean;

constructor(options) {
this.secrets = app.config("delegates.secrets");
this.secrets = app.config("delegates").secrets;
this.network = Managers.configManager.get("network");
this.client = new Client(options.hosts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const transactionPayload = {
}

const transactionsCount = request.payload.transactions.length;
const maxTransactionsPerRequest = transactionPool.maxTransactionsPerRequest;
const { maxTransactionsPerRequest } = transactionPool;

if (transactionsCount > maxTransactionsPerRequest) {
return Boom.entityTooLarge(
Expand Down
1 change: 1 addition & 0 deletions packages/core-kernel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@arkecosystem/crypto": "^3.0.0-next.0",
"awilix": "^4.2.2",
"cosmiconfig": "^5.2.1",
"cron": "^1.7.1",
"dayjs": "^1.8.15",
"delay": "^4.3.0",
Expand Down
98 changes: 65 additions & 33 deletions packages/core-kernel/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { set } from "dottie";
import envPaths from "env-paths";
import expandHomeDir from "expand-home-dir";
import { existsSync, removeSync, writeFileSync } from "fs-extra";
import camelCase from "lodash/camelCase";
import logProcessErrors from "log-process-errors";
import { join } from "path";
// import logProcessErrors from "log-process-errors";
import { join, resolve } from "path";
import { JsonObject } from "type-fest";
import * as Bootstrappers from "./bootstrap";
import { ConfigFactory, ConfigRepository } from "./config";
Expand Down Expand Up @@ -47,24 +50,25 @@ export class Application extends Container implements Kernel.IApplication {

/**
* @param {JsonObject} config
* @param {JsonObject} [flags={}]
* @returns {Promise<void>}
* @memberof Application
*/
public async bootstrap(config: JsonObject, flags: JsonObject = {}): Promise<void> {
public async bootstrap(config: JsonObject): Promise<void> {
this.registerErrorHandler();

this.bindConfiguration(config);
await this.bindConfiguration(config);

this.bindPathsInContainer();
this.registerBindings();

await this.registerCoreFactories();
this.registerNamespace();

await this.registerCoreServices();
this.registerPaths();

this.registerBindings();
await this.registerFactories();

this.registerNamespace();
await this.registerServices();

await this.boot();
}

/**
Expand Down Expand Up @@ -115,11 +119,11 @@ export class Application extends Container implements Kernel.IApplication {

/**
* @param {AbstractServiceProvider} provider
* @param {Record<string, any>} opts
* @param {JsonObject} opts
* @returns {AbstractServiceProvider}
* @memberof Application
*/
public makeProvider(provider: AbstractServiceProvider, opts: Record<string, any>): AbstractServiceProvider {
public makeProvider(provider: AbstractServiceProvider, opts: JsonObject): AbstractServiceProvider {
return this.providers.make(provider, opts);
}

Expand Down Expand Up @@ -165,6 +169,14 @@ export class Application extends Container implements Kernel.IApplication {
return this.resolve("config").get(key);
}

/**
* @returns {string}
* @memberof Application
*/
public dirPrefix(): string {
return this.resolve("app.dirPrefix");
}

/**
* @returns {string}
* @memberof Application
Expand Down Expand Up @@ -400,11 +412,11 @@ export class Application extends Container implements Kernel.IApplication {

/**
* @readonly
* @type {Kernel.ILogger}
* @type {Contracts.Kernel.ILogger}
* @memberof Application
*/
public get log(): Kernel.ILogger {
return this.resolve<Kernel.ILogger>("log");
public get log(): Contracts.Kernel.ILogger {
return this.resolve<Contracts.Kernel.ILogger>("log");
}

/**
Expand Down Expand Up @@ -436,30 +448,33 @@ export class Application extends Container implements Kernel.IApplication {

/**
* @readonly
* @type {Kernel.IEventDispatcher}
* @type {Contracts.Kernel.IEventDispatcher}
* @memberof Application
*/
public get events(): Kernel.IEventDispatcher {
return this.resolve<Kernel.IEventDispatcher>("events");
public get events(): Contracts.Kernel.IEventDispatcher {
return this.resolve<Contracts.Kernel.IEventDispatcher>("event-dispatcher");
}

/**
* @private
* @memberof Application
*/
private registerErrorHandler(): void {
// @TODO: implement passing in of options
logProcessErrors();
// @TODO: implement passing in of options and ensure handling of critical exceptions
// logProcessErrors({ exitOn: [] });
}

/**
* @private
* @param {Record<string, any>} config
* @param {JsonObject} config
* @memberof Application
*/
private bindConfiguration(config: Record<string, any>): void {
this.bind("configLoader", ConfigFactory.make(this, "local")); // @TODO
private async bindConfiguration(config: JsonObject): Promise<void> {
// @TODO: pass in what config provider should be used
this.bind("configLoader", ConfigFactory.make(this, (config.configLoader || "local") as string));
this.bind("config", new ConfigRepository(config));

this.resolve("config").set("options", config.options);
}

/**
Expand All @@ -471,6 +486,13 @@ export class Application extends Container implements Kernel.IApplication {
this.bind("app.token", this.config("token"));
this.bind("app.network", this.config("network"));
this.bind("app.version", this.config("version"));

// @TODO: implement a getter/setter that sets vars locally and in the process.env variables
process.env.CORE_ENV = this.config("env");
process.env.NODE_ENV = process.env.CORE_ENV;
process.env.CORE_TOKEN = this.config("token");
process.env.CORE_NETWORK_NAME = this.config("network");
process.env.CORE_VERSION = this.config("version");
}

/**
Expand All @@ -494,20 +516,20 @@ export class Application extends Container implements Kernel.IApplication {
* @returns {Promise<void>}
* @memberof Application
*/
private async registerCoreServices(): Promise<void> {
this.bind("events", new EventDispatcher());
this.bind("log", await this.resolve("factoryLogger").make(new ConsoleLogger()));
private async registerFactories(): Promise<void> {
this.bind("factoryLogger", new LoggerFactory(this));
this.bind("factoryCache", new CacheFactory(this));
this.bind("factoryQueue", new QueueFactory(this));
}

/**
* @private
* @returns {Promise<void>}
* @memberof Application
*/
private async registerCoreFactories(): Promise<void> {
this.bind("factoryLogger", new LoggerFactory(this));
this.bind("factoryCache", new CacheFactory(this));
this.bind("factoryQueue", new QueueFactory(this));
private async registerServices(): Promise<void> {
this.bind("event-dispatcher", new EventDispatcher());
this.bind("log", await this.resolve("factoryLogger").make(new ConsoleLogger()));
}

/**
Expand Down Expand Up @@ -542,8 +564,18 @@ export class Application extends Container implements Kernel.IApplication {
* @private
* @memberof Application
*/
private bindPathsInContainer(): void {
for (const [type, path] of Object.entries(this.config("paths"))) {
private registerPaths(): void {
const paths: Array<[string, string]> = Object.entries(envPaths(this.token(), { suffix: "core" }));

for (let [type, path] of paths) {
const processPath: string | null = process.env[`CORE_PATH_${type.toUpperCase()}`];

if (processPath) {
path = resolve(expandHomeDir(processPath));
}

set(process.env, `CORE_PATH_${type.toUpperCase()}`, path);

this[camelCase(`use_${type}_path`)](path);

this.bind(`path.${type}`, path);
Expand All @@ -557,7 +589,7 @@ export class Application extends Container implements Kernel.IApplication {
* @memberof Application
*/
private getPath(type: string): string {
const path = this.resolve(`path.${type}`);
const path: string = this.resolve<string>(`path.${type}`);

if (!existsSync(path)) {
throw new DirectoryNotFound(path);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-kernel/src/bootstrap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from "./load-environment-variables";
export * from "./load-configuration";
export * from "./load-cryptography";
export * from "./register-providers";
export * from "./boot-providers";
// export * from "./boot-providers";
13 changes: 0 additions & 13 deletions packages/core-kernel/src/bootstrap/load-environment-variables.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import { set } from "dottie";
import envPaths from "env-paths";
import expandHomeDir from "expand-home-dir";
import { ensureDirSync } from "fs-extra";
import { resolve } from "path";
import { Kernel } from "../contracts";

/**
Expand All @@ -17,13 +12,5 @@ export class LoadEnvironmentVariables {
*/
public async bootstrap(app: Kernel.IApplication): Promise<void> {
await app.resolve("configLoader").loadEnvironmentVariables();

for (const [key, value] of Object.entries(envPaths(app.token(), { suffix: "core" }))) {
const path: string = resolve(`${expandHomeDir(value)}/${app.network()}`);

ensureDirSync(path);

set(process.env, `PATH_${key.toUpperCase()}`, path);
}
}
}
5 changes: 3 additions & 2 deletions packages/core-kernel/src/bootstrap/register-providers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import semver from "semver";
import { JsonObject } from "type-fest";
import { Kernel } from "../contracts";
import { FailedDependencySatisfaction, FailedServiceProviderRegistration } from "../errors";
import { AbstractServiceProvider } from "../support/service-provider";
Expand All @@ -14,12 +15,12 @@ export class RegisterProviders {
* @memberof RegisterProviders
*/
public async bootstrap(app: Kernel.IApplication): Promise<void> {
const providers = app.config("providers");
const providers: JsonObject = app.config("providers");

for (const [pkg, opts] of Object.entries(providers)) {
const { ServiceProvider } = require(pkg);

const serviceProvider: AbstractServiceProvider = app.makeProvider(ServiceProvider, opts);
const serviceProvider: AbstractServiceProvider = app.makeProvider(ServiceProvider, opts as JsonObject);

if (this.satisfiesDependencies(app, serviceProvider)) {
await app.registerProvider(serviceProvider);
Expand Down
Loading

0 comments on commit 53e4469

Please sign in to comment.