Skip to content

Commit

Permalink
refactor: resolve plugin configurations from service provider repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Oct 14, 2019
1 parent bfa2c28 commit b13d52d
Show file tree
Hide file tree
Showing 87 changed files with 270 additions and 119 deletions.
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/bridgechains/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Boom from "@hapi/boom";
import Hapi from "@hapi/hapi";

import { Controller } from "../shared/controller";

export class BridgechainController extends Controller {
Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/bridgechains/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Hapi from "@hapi/hapi";

import { registerMethods } from "./methods";
import { registerRoutes } from "./routes";

Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/bridgechains/methods.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { app, Container, Contracts } from "@arkecosystem/core-kernel";
import Boom from "@hapi/boom";

import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/bridgechains/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Hapi from "@hapi/hapi";

import { BridgechainController } from "./controller";
import * as Schema from "./schema";

Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/bridgechains/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Joi from "@hapi/joi";

import { pagination } from "../shared/schemas/pagination";

export const index: object = {
Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/businesses/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Boom from "@hapi/boom";
import Hapi from "@hapi/hapi";

import { Controller } from "../shared/controller";

export class BusinessController extends Controller {
Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/businesses/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Hapi from "@hapi/hapi";

import { registerMethods } from "./methods";
import { registerRoutes } from "./routes";

Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/businesses/methods.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { app, Container, Contracts } from "@arkecosystem/core-kernel";
import Boom from "@hapi/boom";

import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/businesses/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Hapi from "@hapi/hapi";

import { BusinessController } from "./controller";
import * as Schema from "./schema";

Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/businesses/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Joi from "@hapi/joi";

import { pagination } from "../shared/schemas/pagination";

export const index: object = {
Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/locks/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Boom from "@hapi/boom";
import Hapi from "@hapi/hapi";

import { Controller } from "../shared/controller";

export class LocksController extends Controller {
Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/locks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Hapi from "@hapi/hapi";

import { registerMethods } from "./methods";
import { registerRoutes } from "./routes";

Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/locks/methods.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { app, Container, Contracts } from "@arkecosystem/core-kernel";
import Boom from "@hapi/boom";

import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/locks/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Hapi from "@hapi/hapi";

import { LocksController } from "./controller";
import * as Schema from "./schema";

Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/locks/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Joi from "@hapi/joi";

import { pagination } from "../shared/schemas/pagination";

export const index: object = {
Expand Down
9 changes: 7 additions & 2 deletions packages/core-api/src/handlers/node/controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { app, Container, Contracts } from "@arkecosystem/core-kernel";
import { app, Container, Contracts, Providers } from "@arkecosystem/core-kernel";
import { Crypto, Managers } from "@arkecosystem/crypto";
import Boom from "@hapi/boom";
import Hapi from "@hapi/hapi";
import { spawnSync } from "child_process";
import { existsSync } from "fs";

import { Controller } from "../shared/controller";

// todo: remove the abstract and use dependency injection if needed
Expand Down Expand Up @@ -53,7 +54,11 @@ export class NodeController extends Controller {
public async configuration(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
const network = this.config.get("network");
const dynamicFees = app.get<any>("transactionPool.options").dynamicFees;
const dynamicFees = app
.get<Providers.ServiceProviderRepository>(Container.Identifiers.ServiceProviderRepository)
.get("@arkecosystem/core-transaction-pool")
.config()
.get<{ enabled?: boolean }>("dynamicFees");

return {
data: {
Expand Down
8 changes: 6 additions & 2 deletions packages/core-api/src/handlers/peers/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { app } from "@arkecosystem/core-kernel";
import { app, Container, Providers } from "@arkecosystem/core-kernel";

// todo: review the implementation
export const transformPeer = model => {
return {
ip: model.ip,
port: +app.get<any>("p2p.options").server.port,
port: +app
.get<Providers.ServiceProviderRepository>(Container.Identifiers.ServiceProviderRepository)
.get("@arkecosystem/core-p2p")
.config()
.get("server.port"),
ports: model.ports,
version: model.version,
height: model.state ? model.state.height : model.height,
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/handlers/transactions/controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { app, Container, Contracts } from "@arkecosystem/core-kernel";
import { Enums, Interfaces } from "@arkecosystem/crypto";
import { Handlers } from "@arkecosystem/core-transactions";
import { Enums, Interfaces } from "@arkecosystem/crypto";
import Boom from "@hapi/boom";
import Hapi from "@hapi/hapi";

Expand Down
8 changes: 6 additions & 2 deletions packages/core-api/src/handlers/transactions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ export const store: object = {
$ref: "transactions",
minItems: 1,
maxItems: 40,
// @fixme: the container is not available at the time this file is loaded
// maxItems: app.get<any>("transactionPool.options").maxTransactionsPerRequest,
// todo: the container is not available at the time this file is loaded
// maxItems: app
// .get<Providers.ServiceProviderRepository>(Container.Identifiers.ServiceProviderRepository)
// .get("@arkecosystem/core-transaction-pool")
// .config()
// .get<number>("maxTransactionsPerRequest"),
},
},
};
Expand Down
10 changes: 8 additions & 2 deletions packages/core-api/src/handlers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app } from "@arkecosystem/core-kernel";
import { app, Container, Providers } from "@arkecosystem/core-kernel";
import Boom from "@hapi/boom";
import Hapi from "@hapi/hapi";

Expand Down Expand Up @@ -34,7 +34,13 @@ export const respondWithCollection = (data, transformer, transform = true): obje
};

export const respondWithCache = (data, h): any => {
if (!app.get<any>("api.options").get("plugins.cache.enabled")) {
if (
!app
.get<Providers.ServiceProviderRepository>(Container.Identifiers.ServiceProviderRepository)
.get("@arkecosystem/core-state")
.config()
.get<boolean>("plugins.cache.enabled")
) {
return data;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/plugins/pagination/ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class Ext {
const currentPage = query.page;
const currentLimit = query.limit;

const { totalCount } = !!source.totalCount ? source : request;
const { totalCount } = source.totalCount ? source : request;

let pageCount: number;
if (totalCount) {
Expand Down
2 changes: 0 additions & 2 deletions packages/core-api/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { Server } from "./server";

export class ServiceProvider extends Providers.ServiceProvider {
public async register(): Promise<void> {
this.app.bind("api.options").toConstantValue(this.config());

if (this.config().get("server.http.enabled")) {
await this.buildServer("http");
}
Expand Down
14 changes: 11 additions & 3 deletions packages/core-api/src/services/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app } from "@arkecosystem/core-kernel";
import { app, Container, Providers } from "@arkecosystem/core-kernel";
import { Crypto } from "@arkecosystem/crypto";
import Hapi, { ServerMethod } from "@hapi/hapi";

Expand All @@ -13,7 +13,7 @@ export class ServerCache {
public method(name: string, method: ServerMethod, expiresIn: number, argsCallback?: any): this {
let options = {};

if (app.get<any>("api.options").get("plugins.cache.enabled")) {
if (this.getConfig("plugins.cache.enabled")) {
options = {
cache: {
expiresIn: expiresIn * 1000,
Expand All @@ -34,8 +34,16 @@ export class ServerCache {
}

private getCacheTimeout(): number | boolean {
const { generateTimeout } = app.get<any>("api.options").get("plugins.cache");
const { generateTimeout } = this.getConfig("plugins.cache");

return JSON.parse(generateTimeout);
}

private getConfig<T>(key: string): T {
return app
.get<Providers.ServiceProviderRepository>(Container.Identifiers.ServiceProviderRepository)
.get("@arkecosystem/core-api")
.config()
.get(key);
}
}
6 changes: 2 additions & 4 deletions packages/core-blockchain/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { ReplayBlockchain } from "./replay";

export class ServiceProvider extends Providers.ServiceProvider {
public async register(): Promise<void> {
this.app.bind("blockchain.options").toConstantValue(this.config().all());

// blockchain
// Blockchain
const blockchain: Blockchain = this.config().get("replay")
? new ReplayBlockchain()
: this.app.resolve<Blockchain>(Blockchain);
Expand All @@ -17,7 +15,7 @@ export class ServiceProvider extends Providers.ServiceProvider {

blockchain.init(this.config().all());

// state
// State
this.app.get<Contracts.State.StateStore>(Container.Identifiers.StateStore).reset(blockchainMachine);

if (!process.env.CORE_SKIP_BLOCKCHAIN && !this.config().get("replay")) {
Expand Down
8 changes: 6 additions & 2 deletions packages/core-blockchain/src/state-machine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, Container, Contracts, Enums, Utils as AppUtils } from "@arkecosystem/core-kernel";
import { app, Container, Contracts, Enums, Providers, Utils as AppUtils } from "@arkecosystem/core-kernel";
import { Interfaces, Managers, Utils } from "@arkecosystem/crypto";

import { Blockchain } from "./blockchain";
Expand Down Expand Up @@ -277,7 +277,11 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
async rollbackDatabase() {
logger.info("Trying to restore database integrity");

const { maxBlockRewind, steps } = app.get<any>("blockchain.options").databaseRollback;
const { maxBlockRewind, steps } = app
.get<Providers.ServiceProviderRepository>(Container.Identifiers.ServiceProviderRepository)
.get("@arkecosystem/core-blockchain")
.config()
.get<Record<string, number>>("databaseRollback");

for (let i = maxBlockRewind; i >= 0; i -= steps) {
await blockchain.removeTopBlocks(steps);
Expand Down
5 changes: 3 additions & 2 deletions packages/core-database-postgres/src/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { loadQueryFile } from "../utils";
import { readdirSync } from "fs";

import { loadQueryFile } from "../utils";

export const migrations = readdirSync(__dirname)
.filter(name => name.substr(-4).toLowerCase() === '.sql')
.filter(name => name.substr(-4).toLowerCase() === ".sql")
.sort()
.map(name => loadQueryFile(__dirname, name));
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, Contracts } from "@arkecosystem/core-kernel";
import { app, Container, Contracts, Providers } from "@arkecosystem/core-kernel";
import { Enums, Interfaces } from "@arkecosystem/crypto";

import { SearchParameterConverter } from "./utils/search-parameter-converter";
Expand Down Expand Up @@ -83,7 +83,11 @@ export class TransactionsBusinessRepository implements Contracts.Database.Transa
> {
return this.databaseServiceProvider().connection.transactionsRepository.getFeeStatistics(
days,
app.get<any>("transactionPool.options").dynamicFees.minFeeBroadcast,
app
.get<Providers.ServiceProviderRepository>(Container.Identifiers.ServiceProviderRepository)
.get("@arkecosystem/core-state")
.config()
.get<number>("dynamicFees.minFeeBroadcast"),
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Contracts } from "@arkecosystem/core-kernel";

import { getProperty } from "./get-property";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Contracts, Utils } from "@arkecosystem/core-kernel";

import filterRows from "./filter-rows";
import limitRows from "./limit-rows";
import { sortEntries } from "./sort-entries";
Expand Down
15 changes: 5 additions & 10 deletions packages/core-kernel/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,19 @@ export class Application implements Contracts.Kernel.Application {
* @memberof Application
*/
public async bootstrap({ flags, plugins }: { flags: JsonObject; plugins?: JsonObject }): Promise<void> {
await this.registerEventDispatcher();

// todo: move into a bootstrapper
this.bind<ConfigRepository>(Identifiers.ConfigRepository)
.to(ConfigRepository)
.inSingletonScope();

// todo: move into a bootstrapper
this.bind<KeyValuePair>(Identifiers.ConfigFlags).toConstantValue(flags);

// todo: move into a bootstrapper
this.bind<KeyValuePair>(Identifiers.ConfigPlugins).toConstantValue(plugins);

// todo: move into a bootstrapper
this.bind<ServiceProviderRepository>(Identifiers.ServiceProviderRepository)
.to(ServiceProviderRepository)
.inSingletonScope();

this.bind<KeyValuePair>(Identifiers.ConfigFlags).toConstantValue(flags);
this.bind<KeyValuePair>(Identifiers.ConfigPlugins).toConstantValue(plugins);

await this.registerEventDispatcher();

await this.bootstrapWith("app");
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core-kernel/src/services/config/drivers/local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { dotenv, get, set } from "@arkecosystem/utils";
import { existsSync, readFileSync } from "fs";
import importFresh from "import-fresh";
import { extname } from "path";

import { Application } from "../../../contracts/kernel";
import { ConfigLoader } from "../../../contracts/kernel/config";
Expand All @@ -12,7 +13,6 @@ import {
import { Identifiers, inject, injectable } from "../../../ioc";
import { JsonObject, KeyValuePair, Primitive } from "../../../types";
import { ConfigRepository } from "../repository";
import { extname } from "path";

/**
* @export
Expand Down
2 changes: 1 addition & 1 deletion packages/core-kernel/src/utils/expiration-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ export const calculateTransactionExpiration = (
const createdAtHeight: number = context.currentHeight - createdBlocksAgo;

return createdAtHeight + context.maxTransactionAge;
}
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Interfaces, Transactions, Utils } from "@arkecosystem/crypto";

import { MagistrateTransactionGroup, MagistrateTransactionType } from "../enums";
import { IBridgechainRegistrationAsset } from "../interfaces";
import { BridgechainRegistrationTransaction } from "../transactions";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Interfaces, Transactions, Utils } from "@arkecosystem/crypto";

import { MagistrateTransactionGroup, MagistrateTransactionType } from "../enums";
import { BridgechainResignationTransaction } from "../transactions";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Interfaces, Transactions, Utils } from "@arkecosystem/crypto";

import { MagistrateTransactionGroup, MagistrateTransactionType } from "../enums";
import { IBridgechainUpdateAsset } from "../interfaces";
import { BridgechainUpdateTransaction } from "../transactions";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Interfaces, Transactions, Utils } from "@arkecosystem/crypto";

import { MagistrateTransactionGroup, MagistrateTransactionType } from "../enums";
import { IBusinessRegistrationAsset } from "../interfaces";
import { BusinessRegistrationTransaction } from "../transactions";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Interfaces, Transactions, Utils } from "@arkecosystem/crypto";

import { MagistrateTransactionGroup, MagistrateTransactionType } from "../enums";
import { BusinessResignationTransaction } from "../transactions";

Expand Down
Loading

0 comments on commit b13d52d

Please sign in to comment.