Skip to content

Commit

Permalink
feat(core-kernel): initial draft of new container
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Aug 26, 2019
1 parent 1a823ee commit 583f5a9
Show file tree
Hide file tree
Showing 164 changed files with 916 additions and 1,018 deletions.
9 changes: 6 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
"no-async-promise-executor": 0,
"no-empty": 0,
"no-prototype-builtins": 0,
"prefer-const": ["error", {
"destructuring": "all"
}],
"prefer-const": [
"error",
{
"destructuring": "all"
}
],
"require-atomic-updates": 0
}
}
6 changes: 3 additions & 3 deletions packages/core-api/src/handlers/blocks/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class BlocksController extends Controller {
public async first(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
return super.respondWithResource(
app
.resolve<Contracts.State.IStateService>("state")
app.ioc
.get<Contracts.State.IStateService>("state")
.getStore()
.getGenesisBlock().data,
"block",
Expand All @@ -33,7 +33,7 @@ export class BlocksController extends Controller {
public async last(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
return super.respondWithResource(
app.resolve<Contracts.Blockchain.IBlockchain>("blockchain").getLastBlock().data,
app.ioc.get<Contracts.Blockchain.IBlockchain>("blockchain").getLastBlock().data,
"block",
(request.query.transform as unknown) as boolean,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/handlers/blocks/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Boom from "@hapi/boom";
import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

const databaseService = app.resolve<Contracts.Database.IDatabaseService>("database");
const databaseService = app.ioc.get<Contracts.Database.IDatabaseService>("database");
const blocksRepository = databaseService.blocksBusinessRepository;
const transactionsRepository = databaseService.transactionsBusinessRepository;

Expand Down
4 changes: 2 additions & 2 deletions packages/core-api/src/handlers/blocks/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export const transformBlock = (model, transform) => {
return model;
}

const databaseService: Contracts.Database.IDatabaseService = app.resolve<Contracts.Database.IDatabaseService>(
const databaseService: Contracts.Database.IDatabaseService = app.ioc.get<Contracts.Database.IDatabaseService>(
"database",
);
const generator: Contracts.State.IWallet = databaseService.walletManager.findByPublicKey(model.generatorPublicKey);
const lastBlock: Interfaces.IBlock = app.resolve<Contracts.Blockchain.IBlockchain>("blockchain").getLastBlock();
const lastBlock: Interfaces.IBlock = app.ioc.get<Contracts.Blockchain.IBlockchain>("blockchain").getLastBlock();

model.reward = Utils.BigNumber.make(model.reward);
model.totalFee = Utils.BigNumber.make(model.totalFee);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/handlers/delegates/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Boom from "@hapi/boom";
import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

const databaseService = app.resolve<Contracts.Database.IDatabaseService>("database");
const databaseService = app.ioc.get<Contracts.Database.IDatabaseService>("database");
const blocksRepository = databaseService.blocksBusinessRepository;

const index = async request => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core-api/src/handlers/node/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class NodeController extends Controller {
public async configuration(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
const network = this.config.get("network");
const dynamicFees = app.resolve("transactionPool.options").dynamicFees;
const dynamicFees = app.ioc.get<any>("transactionPool.options").dynamicFees;

return {
data: {
Expand Down Expand Up @@ -81,7 +81,7 @@ export class NodeController extends Controller {
}

public async fees(request: Hapi.Request) {
const { transactionsBusinessRepository } = app.resolve<Contracts.Database.IDatabaseService>("database");
const { transactionsBusinessRepository } = app.ioc.get<Contracts.Database.IDatabaseService>("database");

// @ts-ignore
const results = await transactionsBusinessRepository.getFeeStatistics(request.query.days);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/handlers/peers/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { app } from "@arkecosystem/core-kernel";
export const transformPeer = model => {
return {
ip: model.ip,
port: +app.resolve("p2p.options").server.port,
port: +app.ioc.get<any>("p2p.options").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/rounds/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ServerCache } from "../../services";
import { respondWithCollection } from "../utils";

const delegates = async request => {
const databaseService = app.resolve<Contracts.Database.IDatabaseService>("database");
const databaseService = app.ioc.get<Contracts.Database.IDatabaseService>("database");
const roundsRepository = databaseService.connection.roundsRepository;

const delegates = await roundsRepository.findById(request.params.id);
Expand Down
4 changes: 2 additions & 2 deletions packages/core-api/src/handlers/shared/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {

export class Controller {
protected readonly config = Managers.configManager;
protected readonly blockchain = app.resolve<Contracts.Blockchain.IBlockchain>("blockchain");
protected readonly databaseService = app.resolve<Contracts.Database.IDatabaseService>("database");
protected readonly blockchain = app.ioc.get<Contracts.Blockchain.IBlockchain>("blockchain");
protected readonly databaseService = app.ioc.get<Contracts.Database.IDatabaseService>("database");

protected paginate(request: Hapi.Request): any {
// @ts-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export const pagination = {
limit: Joi.number()
.integer()
.min(1)
.max(app.resolve("api.options").pagination.limit),
.max(app.ioc.get<any>("api.options").pagination.limit),
};
5 changes: 3 additions & 2 deletions packages/core-api/src/handlers/transactions/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Hapi from "@hapi/hapi";
import { Controller } from "../shared/controller";

export class TransactionsController extends Controller {
private readonly transactionPool = app.resolve<Contracts.TransactionPool.IConnection>("transactionPool");
private readonly transactionPool = app.ioc.get<Contracts.TransactionPool.IConnection>("transactionPool");

public async index(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
Expand All @@ -24,7 +24,8 @@ export class TransactionsController extends Controller {
const result = await processor.validate((request.payload as any).transactions);

if (result.broadcast.length > 0) {
app.resolve<Contracts.P2P.IPeerService>("p2p")
app.ioc
.get<Contracts.P2P.IPeerService>("p2p")
.getMonitor()
.broadcastTransactions(processor.getBroadcastTransactions());
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/handlers/transactions/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Boom from "@hapi/boom";
import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

const transactionsRepository = app.resolve<Contracts.Database.IDatabaseService>("database")
const transactionsRepository = app.ioc.get<Contracts.Database.IDatabaseService>("database")
.transactionsBusinessRepository;

const index = async request => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/handlers/transactions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const store: object = {
transactions: {
$ref: "transactions",
minItems: 1,
maxItems: app.resolve("transactionPool.options").maxTransactionsPerRequest,
maxItems: app.ioc.get<any>("transactionPool.options").maxTransactionsPerRequest,
},
},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/core-api/src/handlers/transactions/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { formatTimestamp } from "@arkecosystem/core-utils";
import { Interfaces, Transactions } from "@arkecosystem/crypto";

export const transformTransaction = (model, transform) => {
const blockchain = app.resolve<Contracts.Blockchain.IBlockchain>("blockchain");
const databaseService = app.resolve<Contracts.Database.IDatabaseService>("database");
const blockchain = app.ioc.get<Contracts.Blockchain.IBlockchain>("blockchain");
const databaseService = app.ioc.get<Contracts.Database.IDatabaseService>("database");

const transaction: Interfaces.ITransaction = Transactions.TransactionFactory.fromBytesUnsafe(
model.serialized,
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/handlers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const respondWithCollection = (data, transformer, transform = true): obje
};

export const respondWithCache = (data, h): any => {
if (!app.resolve("api.options").cache.enabled) {
if (!app.ioc.get<any>("api.options").cache.enabled) {
return data;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/handlers/votes/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { paginate, respondWithResource, toPagination } from "../utils";

const { TransactionType } = Enums;

const databaseService = app.resolve<Contracts.Database.IDatabaseService>("database");
const databaseService = app.ioc.get<Contracts.Database.IDatabaseService>("database");
const transactionsRepository = databaseService.transactionsBusinessRepository;

const index = async request => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/handlers/wallets/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Boom from "@hapi/boom";
import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

const databaseService = app.resolve<Contracts.Database.IDatabaseService>("database");
const databaseService = app.ioc.get<Contracts.Database.IDatabaseService>("database");
const transactionsRepository = databaseService.transactionsBusinessRepository;

const index = async request => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Server {
*/
public constructor({ app }: { app: Contracts.Kernel.IApplication }) {
this.app = app;
this.config = app.resolve("api.options");
this.config = app.ioc.get<any>("api.options");
}

public async start(): Promise<void> {
Expand Down
20 changes: 10 additions & 10 deletions packages/core-api/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import { Server } from "./server";
export class ServiceProvider extends Support.AbstractServiceProvider {
public async register(): Promise<void> {
if (!this.config().get("enabled")) {
this.app.resolve<Contracts.Kernel.Log.ILogger>("log").info("Public API is disabled");
this.ioc.get<Contracts.Kernel.Log.ILogger>("log").info("Public API is disabled");
return;
}

this.app.bind("api.options", this.config().all());
this.app.singleton<Server>("api", Server);
this.ioc.bind("api.options").toConstantValue(this.config().all());

this.ioc
.bind<Server>("api")
.to(Server)
.inSingletonScope();
}

public async boot(): Promise<void> {
await this.app.resolve<Server>("api").start();
await this.ioc.get<Server>("api").start();
}

public async dispose(): Promise<void> {
if (this.config().get("enabled")) {
this.app.resolve<Contracts.Kernel.Log.ILogger>("log").info(`Stopping Public API`);
this.ioc.get<Contracts.Kernel.Log.ILogger>("log").info(`Stopping Public API`);

await this.app.resolve<Server>("api").stop();
await this.ioc.get<Server>("api").stop();
}
}

public provides(): string[] {
return ["api"];
}
}
2 changes: 1 addition & 1 deletion packages/core-api/src/services/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ServerCache {
}

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

return JSON.parse(generateTimeout);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core-blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Blockchain implements Contracts.Blockchain.IBlockchain {
* @return {IStateStore}
*/
get state(): Contracts.State.IStateStore {
return app.resolve<Contracts.State.IStateService>("state").getStore();
return app.ioc.get<Contracts.State.IStateService>("state").getStore();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core-blockchain/src/processor/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export enum BlockProcessorResult {
}

export class BlockProcessor {
private readonly logger: Contracts.Kernel.Log.ILogger = app.resolve<Contracts.Kernel.Log.ILogger>("log");
private readonly logger: Contracts.Kernel.Log.ILogger = app.ioc.get<Contracts.Kernel.Log.ILogger>("log");

public constructor(private readonly blockchain: Blockchain) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Blockchain } from "../../blockchain";
import { BlockProcessorResult } from "../block-processor";

export abstract class BlockHandler {
protected readonly logger: Contracts.Kernel.Log.ILogger = app.resolve<Contracts.Kernel.Log.ILogger>("log");
protected readonly logger: Contracts.Kernel.Log.ILogger = app.ioc.get<Contracts.Kernel.Log.ILogger>("log");

public constructor(protected readonly blockchain: Blockchain, protected readonly block: Interfaces.IBlock) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class UnchainedHandler extends BlockHandler {
switch (status) {
case UnchainedBlockStatus.DoubleForging: {
const roundInfo: Contracts.Shared.IRoundInfo = roundCalculator.calculateRound(this.block.data.height);
const delegates: Contracts.State.IWallet[] = await app
.resolve("database")
const delegates: Contracts.State.IWallet[] = await app.ioc
.get<any>("database")
.getActiveDelegates(roundInfo);

if (delegates.some(delegate => delegate.publicKey === this.block.data.generatorPublicKey)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core-blockchain/src/replay/replay-blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class ReplayBlockchain extends Blockchain {
this.walletManager = new Wallets.WalletManager();
this.memoryDatabase = new MemoryDatabaseService(this.walletManager);

this.logger = app.resolve<Contracts.Kernel.Log.ILogger>("log");
this.localDatabase = app.resolve<Contracts.Database.IDatabaseService>("database");
this.logger = app.ioc.get<Contracts.Kernel.Log.ILogger>("log");
this.localDatabase = app.ioc.get<Contracts.Database.IDatabaseService>("database");
this.localDatabase.walletManager = this.walletManager;

this.queue.kill();
Expand Down
14 changes: 5 additions & 9 deletions packages/core-blockchain/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,21 @@ export class ServiceProvider extends Support.AbstractServiceProvider {
? new ReplayBlockchain()
: new Blockchain(this.config().all());

this.app
.resolve<Contracts.State.IStateService>("state")
this.ioc
.get<Contracts.State.IStateService>("state")
.getStore()
.reset(blockchainMachine);

if (!process.env.CORE_SKIP_BLOCKCHAIN && !this.config().get("replay")) {
await blockchain.start();
}

this.app.bind("blockchain", blockchain);
this.app.bind("blockchain.options", this.config().all());
this.ioc.bind("blockchain").toConstantValue(blockchain);
this.ioc.bind("blockchain.options").toConstantValue(this.config().all());
}

public async dispose(): Promise<void> {
await this.app.resolve<Blockchain>("blockchain").stop();
}

public provides(): string[] {
return ["blockchain"];
await this.ioc.get<Blockchain>("blockchain").stop();
}

public async required(): Promise<boolean> {
Expand Down
8 changes: 4 additions & 4 deletions 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.Events.IEventDispatcher>("events");
const emitter = app.ioc.get<Contracts.Kernel.Events.IEventDispatcher>("events");

// defer initialisation to "init" due to this being resolved before the container kicks in
let logger;
Expand Down Expand Up @@ -117,8 +117,8 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
},

async init() {
logger = app.resolve<Contracts.Kernel.Log.ILogger>("log");
stateStorage = app.resolve<Contracts.State.IStateService>("state").getStore();
logger = app.ioc.get<Contracts.Kernel.Log.ILogger>("log");
stateStorage = app.ioc.get<Contracts.State.IStateService>("state").getStore();
blockchainMachine.state = stateStorage;

try {
Expand Down Expand Up @@ -285,7 +285,7 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
async rollbackDatabase() {
logger.info("Trying to restore database integrity");

const { maxBlockRewind, steps } = app.resolve("blockchain.options").databaseRollback;
const { maxBlockRewind, steps } = app.ioc.get<any>("blockchain.options").databaseRollback;

for (let i = maxBlockRewind; i >= 0; i -= steps) {
await blockchain.removeTopBlocks(steps);
Expand Down
10 changes: 5 additions & 5 deletions packages/core-blockchain/src/utils/tick-sync-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const tickSyncTracker = (blockCount, count): void => {
if (!tracker) {
tracker = {
start: new Date().getTime(),
networkHeight: app
.resolve<Contracts.P2P.IPeerService>("p2p")
networkHeight: app.ioc
.get<Contracts.P2P.IPeerService>("p2p")
.getMonitor()
.getNetworkHeight(),
blocksInitial: +count,
Expand Down Expand Up @@ -44,9 +44,9 @@ export const tickSyncTracker = (blockCount, count): void => {
secondsDecimalDigits: 0,
});

app.resolve<Contracts.Kernel.Log.ILogger>("log").info(
`Synchronising In Progress (${blocksDownloaded} of ${networkHeight} blocks - Est. ${timeLeft})`,
);
app.ioc
.get<Contracts.Kernel.Log.ILogger>("log")
.info(`Synchronising In Progress (${blocksDownloaded} of ${networkHeight} blocks - Est. ${timeLeft})`);
}

if (tracker.percent === 100) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core-blockchain/src/utils/validate-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { roundCalculator } from "@arkecosystem/core-utils";
import { Crypto, Interfaces } from "@arkecosystem/crypto";

export const validateGenerator = async (block: Interfaces.IBlock): Promise<boolean> => {
const database: Contracts.Database.IDatabaseService = app.resolve<Contracts.Database.IDatabaseService>("database");
const logger: Contracts.Kernel.Log.ILogger = app.resolve<Contracts.Kernel.Log.ILogger>("log");
const database: Contracts.Database.IDatabaseService = app.ioc.get<Contracts.Database.IDatabaseService>("database");
const logger: Contracts.Kernel.Log.ILogger = app.ioc.get<Contracts.Kernel.Log.ILogger>("log");

const roundInfo: Contracts.Shared.IRoundInfo = roundCalculator.calculateRound(block.data.height);
const delegates: Contracts.State.IWallet[] = await database.getActiveDelegates(roundInfo);
Expand Down
Loading

0 comments on commit 583f5a9

Please sign in to comment.