Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e7fa156
refactor: refactor core-database to use interfaces, first pass.
paroxysm Jan 28, 2019
54286c7
refactor: remove dep on core-database-postgres and use interface type…
paroxysm Jan 29, 2019
4bfac6d
refactor: have postgres repos implement their respective interfaces. …
paroxysm Jan 30, 2019
2df6216
refactor: use event-emitter to fire some database life-cycle events
paroxysm Jan 31, 2019
d79db8c
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Jan 31, 2019
dccea07
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 1, 2019
01fdf5e
fix: wallet-manager tests
paroxysm Feb 3, 2019
3241b51
fix: delegates and wallets business repo tests.
paroxysm Feb 3, 2019
53da8e4
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 3, 2019
8453533
fix: missed a method rename here.
paroxysm Feb 3, 2019
27b9d75
revert: changes to business repo tests asserting count.
paroxysm Feb 3, 2019
bd5c1db
fix: failing tests in core-transaction-pool
paroxysm Feb 3, 2019
705b4ec
fix: failing tests in core-graphql
paroxysm Feb 3, 2019
fdcdd87
refactor: rename these files.
paroxysm Feb 3, 2019
32ef185
test: re-enable this test until it's re-written.
paroxysm Feb 3, 2019
95b1c01
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 3, 2019
543cfaf
Merge branch 'develop' into feat/refactor-core-database
faustbrian Feb 4, 2019
faca4e0
Merge branch 'develop' into feat/refactor-core-database
faustbrian Feb 4, 2019
6a02a43
fix: startup issue in core-snapshots-cli(apparently it supports booti…
paroxysm Feb 5, 2019
0a091e1
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 5, 2019
b7bc01e
fix: compilation failure here.
paroxysm Feb 5, 2019
2af2cbe
Merge branch 'develop' into feat/refactor-core-database
paroxysm Feb 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/core-api/__tests__/__support__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { Database } from "@arkecosystem/core-interfaces";
import delay from "delay";
import { registerWithContainer, setUpContainer } from "../../../core-test-utils/src/helpers/container";
import { plugin } from "../../src/plugin";
Expand Down Expand Up @@ -31,11 +31,11 @@ async function setUp() {
],
});

const connection = app.resolvePlugin<PostgresConnection>("database");
await connection.db.rounds.truncate();
await connection.buildWallets(1);
await connection.saveWallets(true);
await connection.saveRound(round);
const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");
await databaseService.connection.roundsRepository.truncate();
await databaseService.buildWallets(1);
await databaseService.saveWallets(true);
await databaseService.saveRound(round);

await registerWithContainer(plugin, options);
await delay(1000); // give some more time for api server to be up
Expand All @@ -48,16 +48,16 @@ async function tearDown() {
}

async function calculateRanks() {
const connection = app.resolvePlugin<PostgresConnection>("database");
const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");

const rows = await connection.query.manyOrNone(queries.spv.delegatesRanks);
const rows = await (databaseService.connection as any).query.manyOrNone(queries.spv.delegatesRanks);

rows.forEach((delegate, i) => {
const wallet = connection.walletManager.findByPublicKey(delegate.publicKey);
const wallet = databaseService.walletManager.findByPublicKey(delegate.publicKey);
wallet.missedBlocks = +delegate.missedBlocks;
wallet.rate = i + 1;
(wallet as any).rate = i + 1;

connection.walletManager.reindex(wallet);
databaseService.walletManager.reindex(wallet);
});
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core-api/__tests__/v2/handlers/blocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { blocks2to100 } from "../../../../core-test-utils/src/fixtures";
import { resetBlockchain } from "../../../../core-test-utils/src/helpers";

import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { Database } from "@arkecosystem/core-interfaces";

const container = app;
const { Block } = models;
Expand Down Expand Up @@ -146,8 +146,8 @@ describe("API 2.0 - Blocks", () => {
it("should POST a search for blocks with the exact specified previousBlock", async () => {
// save a new block so that we can make the request with previousBlock
const block2 = new Block(blocks2to100[0]);
const database = container.resolvePlugin<PostgresConnection>("database");
await database.saveBlock(block2);
const databaseService = container.resolvePlugin<Database.IDatabaseService>("database");
await databaseService.saveBlock(block2);

const response = await utils[request]("POST", "blocks/search", {
id: blocks2to100[0].id,
Expand All @@ -163,7 +163,7 @@ describe("API 2.0 - Blocks", () => {
expect(block.id).toBe(blocks2to100[0].id);
expect(block.previous).toBe(blocks2to100[0].previousBlock);

await database.deleteBlock(block2); // reset to genesis block
await databaseService.deleteBlock(block2); // reset to genesis block
});
},
);
Expand Down
8 changes: 4 additions & 4 deletions packages/core-api/__tests__/v2/handlers/delegates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { models } from "@arkecosystem/crypto";
const { Block } = models;

import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { Database } from "@arkecosystem/core-interfaces";

const delegate = {
username: "genesis_9",
Expand Down Expand Up @@ -155,8 +155,8 @@ describe("API 2.0 - Delegates", () => {
it("should GET all blocks for a delegate by the given identifier", async () => {
// save a new block so that we can make the request with generatorPublicKey
const block2 = new Block(blocks2to100[0]);
const database = app.resolvePlugin<PostgresConnection>("database");
await database.saveBlock(block2);
const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");
await databaseService.saveBlock(block2);

const response = await utils[request](
"GET",
Expand All @@ -166,7 +166,7 @@ describe("API 2.0 - Delegates", () => {
expect(response.data.data).toBeArray();
utils.expectBlock(response.data.data[0]);

await database.deleteBlock(block2); // reset to genesis block
await databaseService.deleteBlock(block2); // reset to genesis block
});
},
);
Expand Down
1 change: 0 additions & 1 deletion packages/core-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"dependencies": {
"@arkecosystem/core-interfaces": "^2.1.0",
"@arkecosystem/core-container": "^2.1.0",
"@arkecosystem/core-database-postgres": "^2.1.0",
"@arkecosystem/core-http-utils": "^2.1.0",
"@arkecosystem/core-transaction-pool": "^2.1.0",
"@arkecosystem/core-utils": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/interfaces/repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface IRepository {
database: any;
databaseService: any;
cache: any;
model: any;
query: any;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/repositories/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class BlockRepository extends Repository implements IRepository {
}

public getModel(): any {
return this.database.models.block;
return (this.databaseService.connection as any).models.block;
}

public __orderBy(parameters): string[] {
Expand Down
13 changes: 6 additions & 7 deletions packages/core-api/src/repositories/repository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { TransactionPool } from "@arkecosystem/core-interfaces";
import { Database, TransactionPool } from "@arkecosystem/core-interfaces";
import snakeCase from "lodash/snakeCase";
import { IRepository } from "../interfaces";

export abstract class Repository implements IRepository {
public database = app.resolvePlugin<PostgresConnection>("database");
public cache = this.database.getCache();
public databaseService = app.resolvePlugin<Database.IDatabaseService>("database");
public cache = this.databaseService.cache;
public transactionPool = app.resolvePlugin<TransactionPool.ITransactionPool>("transactionPool");
public model = this.getModel();
public query = this.model.query();
Expand All @@ -20,11 +19,11 @@ export abstract class Repository implements IRepository {
public abstract getModel(): any;

public async _find(query): Promise<any> {
return this.database.query.oneOrNone(query.toQuery());
return (this.databaseService.connection as any).query.oneOrNone(query.toQuery());
}

public async _findMany(query): Promise<any> {
return this.database.query.manyOrNone(query.toQuery());
return (this.databaseService.connection as any).query.manyOrNone(query.toQuery());
}

public async _findManyWithCount(selectQuery, { limit, offset, orderBy }): Promise<any> {
Expand Down Expand Up @@ -61,7 +60,7 @@ export abstract class Repository implements IRepository {

let count = 0;
const explainSql = `EXPLAIN ${selectQuery.toString()}`;
for (const row of await this.database.query.manyOrNone(explainSql)) {
for (const row of await (this.databaseService.connection as any).query.manyOrNone(explainSql)) {
const line: any = Object.values(row)[0];
const match = line.match(/rows=([0-9]+)/);
if (match !== null) {
Expand Down
10 changes: 5 additions & 5 deletions packages/core-api/src/repositories/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class TransactionsRepository extends Repository implements IRepository {
}

if (parameters.ownerId) {
const owner = this.database.walletManager.findByAddress(parameters.ownerId);
const owner = this.databaseService.walletManager.findByAddress(parameters.ownerId);

selectQuery.and(this.query.sender_public_key.equals(owner.publicKey));
selectQuery.or(this.query.recipient_id.equals(owner.address));
Expand Down Expand Up @@ -394,7 +394,7 @@ export class TransactionsRepository extends Repository implements IRepository {
}

public getModel(): object {
return this.database.models.transaction;
return (this.databaseService.connection as any).models.transaction;
}

/**
Expand All @@ -403,7 +403,7 @@ export class TransactionsRepository extends Repository implements IRepository {
* @return {Object}
*/
public async __mapBlocksToTransactions(data): Promise<any> {
const blockQuery = this.database.models.block.query();
const blockQuery = (this.databaseService.connection as any).models.block.query();

// Array...
if (Array.isArray(data)) {
Expand Down Expand Up @@ -493,8 +493,8 @@ export class TransactionsRepository extends Repository implements IRepository {
* @return {String}
*/
public __publicKeyFromAddress(senderId): string {
if (this.database.walletManager.exists(senderId)) {
return this.database.walletManager.findByAddress(senderId).publicKey;
if (this.databaseService.walletManager.exists(senderId)) {
return this.databaseService.walletManager.findByAddress(senderId).publicKey;
}

return null;
Expand Down
10 changes: 5 additions & 5 deletions packages/core-api/src/versions/1/accounts/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class AccountsController extends Controller {
public async delegates(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
// @ts-ignore
const account = await this.database.wallets.findById(request.query.address);
const account = await this.databaseService.wallets.findById(request.query.address);

if (!account) {
return super.respondWith("Address not found.", true);
Expand All @@ -74,7 +74,7 @@ export class AccountsController extends Controller {
);
}

const delegate = await this.database.delegates.findById(account.vote);
const delegate = await this.databaseService.delegates.findById(account.vote);

return super.respondWith({
delegates: [super.toResource(request, delegate, "delegate")],
Expand All @@ -86,9 +86,9 @@ export class AccountsController extends Controller {

public async top(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
let accounts = this.database.wallets.top(super.paginate(request));
const wallets = this.databaseService.wallets.top(super.paginate(request));

accounts = accounts.rows.map(account => ({
const accounts = wallets.rows.map(account => ({
address: account.address,
balance: `${account.balance}`,
publicKey: account.publicKey,
Expand All @@ -102,7 +102,7 @@ export class AccountsController extends Controller {

public async count(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
const { count } = await this.database.wallets.findAll();
const { count } = await this.databaseService.wallets.findAll();

return super.respondWith({ count });
} catch (error) {
Expand Down
12 changes: 6 additions & 6 deletions packages/core-api/src/versions/1/accounts/methods.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { Database } from "@arkecosystem/core-interfaces";
import { ServerCache } from "../../../services";
import { paginate, respondWith, toCollection, toResource } from "../utils";

const database = app.resolvePlugin<PostgresConnection>("database");
const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");

const index = async request => {
const { rows } = await database.wallets.findAll({
const { rows } = await databaseService.wallets.findAll({
...request.query,
...paginate(request),
});
Expand All @@ -17,7 +17,7 @@ const index = async request => {
};

const show = async request => {
const account = await database.wallets.findById(request.query.address);
const account = await databaseService.wallets.findById(request.query.address);

if (!account) {
return respondWith("Account not found", true);
Expand All @@ -29,7 +29,7 @@ const show = async request => {
};

const balance = async request => {
const account = await database.wallets.findById(request.query.address);
const account = await databaseService.wallets.findById(request.query.address);

if (!account) {
return respondWith({ balance: "0", unconfirmedBalance: "0" });
Expand All @@ -42,7 +42,7 @@ const balance = async request => {
};

const publicKey = async request => {
const account = await database.wallets.findById(request.query.address);
const account = await databaseService.wallets.findById(request.query.address);

if (!account) {
return respondWith("Account not found", true);
Expand Down
4 changes: 2 additions & 2 deletions packages/core-api/src/versions/1/delegates/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class DelegatesController extends Controller {

public async forged(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
const wallet = this.database.walletManager.findByPublicKey(
const wallet = this.databaseService.walletManager.findByPublicKey(
// @ts-ignore
request.query.generatorPublicKey,
);
Expand All @@ -95,7 +95,7 @@ export class DelegatesController extends Controller {
const delegatesCount = this.config.getMilestone(lastBlock).activeDelegates;
const currentSlot = slots.getSlotNumber(lastBlock.data.timestamp);

let activeDelegates = await this.database.getActiveDelegates(lastBlock.data.height);
let activeDelegates = await this.databaseService.getActiveDelegates(lastBlock.data.height);
activeDelegates = activeDelegates.map(delegate => delegate.publicKey);

const nextForgers = [];
Expand Down
16 changes: 8 additions & 8 deletions packages/core-api/src/versions/1/delegates/methods.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { Database } from "@arkecosystem/core-interfaces";
import { ServerCache } from "../../../services";
import { paginate, respondWith, toCollection, toResource } from "../utils";

const database = app.resolvePlugin<PostgresConnection>("database");
const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");

const index = async request => {
const { count, rows } = await database.delegates.paginate({
const { count, rows } = await databaseService.delegates.findAll({
...request.query,
...{
offset: request.query.offset || 0,
Expand All @@ -25,7 +25,7 @@ const show = async request => {
return respondWith("Delegate not found", true);
}

const delegate = await database.delegates.findById(request.query.publicKey || request.query.username);
const delegate = await databaseService.delegates.findById(request.query.publicKey || request.query.username);

if (!delegate) {
return respondWith("Delegate not found", true);
Expand All @@ -37,13 +37,13 @@ const show = async request => {
};

const countDelegates = async request => {
const delegate = await database.delegates.findAll();
const delegate = await databaseService.delegates.findAll();

return respondWith({ count: delegate.count });
};

const search = async request => {
const { rows } = await database.delegates.search({
const { rows } = await databaseService.delegates.search({
...{ username: request.query.q },
...paginate(request),
});
Expand All @@ -54,15 +54,15 @@ const search = async request => {
};

const voters = async request => {
const delegate = await database.delegates.findById(request.query.publicKey);
const delegate = await databaseService.delegates.findById(request.query.publicKey);

if (!delegate) {
return respondWith({
accounts: [],
});
}

const accounts = await database.wallets.findAllByVote(delegate.publicKey);
const accounts = await databaseService.wallets.findAllByVote(delegate.publicKey);

return respondWith({
accounts: toCollection(request, accounts.rows, "voter"),
Expand Down
5 changes: 2 additions & 3 deletions packages/core-api/src/versions/1/shared/controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { Blockchain, Logger } from "@arkecosystem/core-interfaces";
import { Blockchain, Database, Logger } from "@arkecosystem/core-interfaces";
import Hapi from "hapi";
import { paginate, respondWith, respondWithCache, toCollection, toResource } from "../utils";

export class Controller {
protected config = app.getConfig();
protected blockchain = app.resolvePlugin<Blockchain.IBlockchain>("blockchain");
protected database = app.resolvePlugin<PostgresConnection>("database");
protected databaseService = app.resolvePlugin<Database.IDatabaseService>("database");
protected logger = app.resolvePlugin<Logger.ILogger>("logger");

protected paginate(request: Hapi.Request): any {
Expand Down
6 changes: 3 additions & 3 deletions packages/core-api/src/versions/2/blocks/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { Database } from "@arkecosystem/core-interfaces";
import { bignumify, formatTimestamp } from "@arkecosystem/core-utils";

export function transformBlock(model) {
const database = app.resolvePlugin<PostgresConnection>("database");
const generator = database.walletManager.findByPublicKey(model.generatorPublicKey);
const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");
const generator = databaseService.walletManager.findByPublicKey(model.generatorPublicKey);

model.reward = bignumify(model.reward);
model.totalFee = bignumify(model.totalFee);
Expand Down
Loading