Skip to content

Commit

Permalink
fix(core-database): decouple database and state packages (#3936)
Browse files Browse the repository at this point in the history
  • Loading branch information
bertiespell committed Aug 20, 2020
1 parent 4fc57db commit af5e542
Show file tree
Hide file tree
Showing 39 changed files with 1,898 additions and 1,491 deletions.
9 changes: 6 additions & 3 deletions __tests__/functional/transaction-forging/__support__/index.ts
@@ -1,15 +1,16 @@
import "jest-extended";

import delay from "delay";
import { Container, Contracts } from "@arkecosystem/core-kernel";
import { Identities, Managers, Utils } from "@arkecosystem/crypto";
import secrets from "@packages/core-test-framework/src/internal/passphrases.json";
import delay from "delay";

jest.setTimeout(1200000);

import { DatabaseService } from "@arkecosystem/core-database";
import { Sandbox } from "@packages/core-test-framework/src";
import { DatabaseInteraction } from "@arkecosystem/core-state";
import { StateBuilder } from "@arkecosystem/core-state/src/state-builder";
import { Sandbox } from "@packages/core-test-framework/src";

const sandbox: Sandbox = new Sandbox();

Expand Down Expand Up @@ -87,7 +88,9 @@ export const setUp = async (): Promise<Contracts.Kernel.Application> => {
}),
);

await (databaseService as any).initializeActiveDelegates(1);
const databaseInteraction = app.get<DatabaseInteraction>(Container.Identifiers.DatabaseInteraction);

await (databaseInteraction as any).initializeActiveDelegates(1);
});

return sandbox.app;
Expand Down
5 changes: 4 additions & 1 deletion __tests__/integration/core-api/handlers/rounds.test.ts
Expand Up @@ -2,6 +2,7 @@ import "@packages/core-test-framework/src/matchers";

import { DatabaseService } from "@arkecosystem/core-database";
import { Container, Contracts } from "@arkecosystem/core-kernel";
import { DatabaseInteraction } from "@arkecosystem/core-state";
import { ApiHelpers, Factories } from "@packages/core-test-framework/src";

import { calculateRanks, setUp, tearDown } from "../__support__/setup";
Expand All @@ -14,7 +15,9 @@ beforeAll(async () => {

const databaseService = app.get<DatabaseService>(Container.Identifiers.DatabaseService);
await databaseService.saveRound(Factories.factory("Round").make());
await (databaseService as any).initializeActiveDelegates(1);
const databaseInteraction = app.get<DatabaseInteraction>(Container.Identifiers.DatabaseInteraction);

await (databaseInteraction as any).initializeActiveDelegates(1);

await calculateRanks();
});
Expand Down
25 changes: 15 additions & 10 deletions __tests__/unit/core-blockchain/blockchain.test.ts
Expand Up @@ -3,8 +3,8 @@ import "jest-extended";
import { ProcessBlockAction } from "@packages/core-blockchain/src/actions";
import { Blockchain } from "@packages/core-blockchain/src/blockchain";
import { BlockProcessorResult } from "@packages/core-blockchain/src/processor/block-processor";
import { GetActiveDelegatesAction } from "@packages/core-database/src/actions";
import { Container, Enums, Services, Utils as AppUtils } from "@packages/core-kernel";
import { GetActiveDelegatesAction } from "@packages/core-state/src/actions";
import { Sandbox } from "@packages/core-test-framework";
import { Crypto, Interfaces, Managers, Networks, Utils } from "@packages/crypto";
import delay from "delay";
Expand All @@ -22,13 +22,15 @@ describe("Blockchain", () => {
const peerNetworkMonitor: any = {};
const peerStorage: any = {};
const blockProcessor: any = {};
const databaseInteractions: any = {};

beforeAll(() => {
sandbox = new Sandbox();

sandbox.app.bind(Container.Identifiers.LogService).toConstantValue(logService);
sandbox.app.bind(Container.Identifiers.StateStore).toConstantValue(stateStore);
sandbox.app.bind(Container.Identifiers.DatabaseService).toConstantValue(databaseService);
sandbox.app.bind(Container.Identifiers.DatabaseInteraction).toConstantValue(databaseInteractions);
sandbox.app.bind(Container.Identifiers.DatabaseBlockRepository).toConstantValue(blockRepository);
sandbox.app.bind(Container.Identifiers.TransactionPoolService).toConstantValue(transactionPoolService);
sandbox.app.bind(Container.Identifiers.StateMachine).toConstantValue(stateMachine);
Expand Down Expand Up @@ -72,12 +74,15 @@ describe("Blockchain", () => {
stateStore.pushPingBlock = jest.fn();
stateStore.pingBlock = jest.fn();

databaseService.getTopBlocks = jest.fn();
databaseService.getLastBlock = jest.fn();
databaseService.loadBlocksFromCurrentRound = jest.fn();
databaseService.revertBlock = jest.fn();
databaseService.deleteRound = jest.fn();
databaseService.getActiveDelegates = jest.fn().mockReturnValue([]);
databaseService.revertBlock = jest.fn();

databaseInteractions.getTopBlocks = jest.fn();
databaseInteractions.getLastBlock = jest.fn();
databaseInteractions.loadBlocksFromCurrentRound = jest.fn();
databaseInteractions.revertBlock = jest.fn();
databaseInteractions.deleteRound = jest.fn();
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValue([]);

blockRepository.deleteBlocks = jest.fn();
blockRepository.deleteTopBlocks = jest.fn();
Expand Down Expand Up @@ -597,7 +602,7 @@ describe("Blockchain", () => {

await blockchain.removeBlocks(2);

expect(databaseService.revertBlock).toHaveBeenCalledTimes(2);
expect(databaseInteractions.revertBlock).toHaveBeenCalledTimes(2);
expect(stateStore.setLastBlock).toHaveBeenCalledTimes(2);
expect(blockRepository.deleteBlocks).toHaveBeenCalledTimes(1);
});
Expand All @@ -622,7 +627,7 @@ describe("Blockchain", () => {

stateStore.getLastBlock = jest.fn();

expect(databaseService.revertBlock).toHaveBeenCalledTimes(1);
expect(databaseInteractions.revertBlock).toHaveBeenCalledTimes(1);
expect(stateStore.setLastBlock).toHaveBeenCalledTimes(1);
expect(blockRepository.deleteBlocks).toHaveBeenCalledTimes(1);
});
Expand All @@ -637,7 +642,7 @@ describe("Blockchain", () => {
await blockchain.removeTopBlocks(numberOfBlocks);

expect(blockRepository.deleteTopBlocks).toHaveBeenLastCalledWith(numberOfBlocks);
expect(databaseService.loadBlocksFromCurrentRound).toHaveBeenCalled();
expect(databaseInteractions.loadBlocksFromCurrentRound).toHaveBeenCalled();
},
);
});
Expand Down Expand Up @@ -701,7 +706,7 @@ describe("Blockchain", () => {

expect(spyClearQueue).toBeCalledTimes(1);
expect(spyResetLastDownloadedBlock).toBeCalledTimes(1);
expect(databaseService.revertBlock).toBeCalledTimes(1);
expect(databaseInteractions.revertBlock).toBeCalledTimes(1);
});

it("should broadcast a block if (Crypto.Slots.getSlotNumber() * blocktime <= block.data.timestamp)", async () => {
Expand Down
31 changes: 19 additions & 12 deletions __tests__/unit/core-blockchain/processor/block-processor.test.ts
Expand Up @@ -9,8 +9,8 @@ import {
UnchainedHandler,
VerificationFailedHandler,
} from "@packages/core-blockchain/src/processor/handlers";
import { GetActiveDelegatesAction } from "@packages/core-database/src/actions";
import { Container, Services } from "@packages/core-kernel";
import { GetActiveDelegatesAction } from "@packages/core-state/src/actions";
import { Sandbox } from "@packages/core-test-framework";
import { Interfaces, Managers, Utils } from "@packages/crypto";

Expand All @@ -37,11 +37,17 @@ describe("BlockProcessor", () => {
const transactionHandlerRegistry = {
getActivatedHandlerForData: jest.fn(),
};
const databaseService = {
getActiveDelegates: jest.fn(),
const databaseService = {};
const databaseInteractions = {
walletRepository: {
getNonce: jest.fn(),
},
getTopBlocks: jest.fn(),
getLastBlock: jest.fn(),
loadBlocksFromCurrentRound: jest.fn(),
revertBlock: jest.fn(),
deleteRound: jest.fn(),
getActiveDelegates: jest.fn().mockReturnValue([]),
};

beforeAll(() => {
Expand All @@ -50,6 +56,7 @@ describe("BlockProcessor", () => {
sandbox.app.bind(Container.Identifiers.DatabaseTransactionRepository).toConstantValue(transactionRepository);
sandbox.app.bind(Container.Identifiers.WalletRepository).toConstantValue(walletRepository);
sandbox.app.bind(Container.Identifiers.DatabaseService).toConstantValue(databaseService);
sandbox.app.bind(Container.Identifiers.DatabaseInteraction).toConstantValue(databaseInteractions);
sandbox.app.bind(Container.Identifiers.TransactionHandlerRegistry).toConstantValue(transactionHandlerRegistry);
sandbox.app.bind(Container.Identifiers.StateStore).toConstantValue({});
sandbox.app.bind(Container.Identifiers.TransactionPoolService).toConstantValue({});
Expand Down Expand Up @@ -197,7 +204,7 @@ describe("BlockProcessor", () => {
],
};

databaseService.walletRepository.getNonce = jest.fn().mockReturnValueOnce(Utils.BigNumber.ONE);
databaseInteractions.walletRepository.getNonce = jest.fn().mockReturnValueOnce(Utils.BigNumber.ONE);

const blockProcessor = sandbox.app.resolve<BlockProcessor>(BlockProcessor);

Expand All @@ -220,8 +227,8 @@ describe("BlockProcessor", () => {
],
};

databaseService.walletRepository.getNonce = jest.fn().mockReturnValueOnce(Utils.BigNumber.ONE);
databaseService.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
databaseInteractions.walletRepository.getNonce = jest.fn().mockReturnValueOnce(Utils.BigNumber.ONE);
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
blockchain.getLastBlock = jest.fn().mockReturnValueOnce(baseBlock);
const generatorWallet = {
getAttribute: jest.fn().mockReturnValue("generatorusername"),
Expand All @@ -247,7 +254,7 @@ describe("BlockProcessor", () => {
};
walletRepository.findByPublicKey = jest.fn().mockReturnValueOnce(generatorWallet);
UnchainedHandler.prototype.initialize = jest.fn().mockReturnValueOnce(new UnchainedHandler());
databaseService.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValueOnce([]);

const blockProcessor = sandbox.app.resolve<BlockProcessor>(BlockProcessor);

Expand Down Expand Up @@ -303,7 +310,7 @@ describe("BlockProcessor", () => {
activeDelegatesWithoutGenerator.length = 51;
activeDelegatesWithoutGenerator.fill(notBlockGenerator, 0);

databaseService.getActiveDelegates = jest.fn().mockReturnValueOnce(activeDelegatesWithoutGenerator);
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValueOnce(activeDelegatesWithoutGenerator);

const blockProcessor = sandbox.app.resolve<BlockProcessor>(BlockProcessor);

Expand All @@ -330,7 +337,7 @@ describe("BlockProcessor", () => {
publicKey: "02ff171adaef486b7db9fc160b28433d20cf43163d56fd28fee72145f0d5219a4b",
};

databaseService.getActiveDelegates = jest.fn().mockReturnValueOnce([notBlockGenerator]);
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValueOnce([notBlockGenerator]);

const blockProcessor = sandbox.app.resolve<BlockProcessor>(BlockProcessor);

Expand All @@ -351,10 +358,10 @@ describe("BlockProcessor", () => {
...chainedBlock,
transactions: [{ data: transactionData, id: transactionData.id } as Interfaces.ITransaction],
};
databaseService.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
blockchain.getLastBlock = jest.fn().mockReturnValueOnce(baseBlock);
transactionRepository.getForgedTransactionsIds = jest.fn().mockReturnValueOnce([transactionData.id]);
databaseService.walletRepository.getNonce = jest.fn().mockReturnValueOnce(Utils.BigNumber.ONE);
databaseInteractions.walletRepository.getNonce = jest.fn().mockReturnValueOnce(Utils.BigNumber.ONE);
const generatorWallet = {
getAttribute: jest.fn().mockReturnValue("generatorusername"),
};
Expand All @@ -371,7 +378,7 @@ describe("BlockProcessor", () => {
const block = {
...chainedBlock,
};
databaseService.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
blockchain.getLastBlock = jest.fn().mockReturnValueOnce(baseBlock);
transactionRepository.getForgedTransactionsIds = jest.fn().mockReturnValueOnce([]);
const generatorWallet = {
Expand Down
@@ -1,8 +1,9 @@
import { Container } from "@arkecosystem/core-kernel";
import { AcceptBlockHandler } from "../../../../../packages/core-blockchain/src/processor/handlers/accept-block-handler";
import { BlockProcessorResult } from "../../../../../packages/core-blockchain/src/processor";
import { Interfaces } from "@arkecosystem/crypto";

import { BlockProcessorResult } from "../../../../../packages/core-blockchain/src/processor";
import { AcceptBlockHandler } from "../../../../../packages/core-blockchain/src/processor/handlers/accept-block-handler";

describe("AcceptBlockHandler", () => {
const container = new Container.Container();

Expand All @@ -14,9 +15,19 @@ describe("AcceptBlockHandler", () => {
setLastBlock: jest.fn(),
lastDownloadedBlock: undefined,
};
const database = { applyBlock: jest.fn() };
const transactionPool = { acceptForgedTransaction: jest.fn() };

const databaseInteractions = {
walletRepository: {
getNonce: jest.fn(),
},
applyBlock: jest.fn(),
getTopBlocks: jest.fn(),
getLastBlock: jest.fn(),
loadBlocksFromCurrentRound: jest.fn(),
revertBlock: jest.fn(),
deleteRound: jest.fn(),
getActiveDelegates: jest.fn().mockReturnValue([]),
};
const application = { get: jest.fn() };

beforeAll(() => {
Expand All @@ -25,7 +36,7 @@ describe("AcceptBlockHandler", () => {
container.bind(Container.Identifiers.LogService).toConstantValue(logger);
container.bind(Container.Identifiers.BlockchainService).toConstantValue(blockchain);
container.bind(Container.Identifiers.StateStore).toConstantValue(state);
container.bind(Container.Identifiers.DatabaseService).toConstantValue(database);
container.bind(Container.Identifiers.DatabaseInteraction).toConstantValue(databaseInteractions);
container.bind(Container.Identifiers.TransactionPoolService).toConstantValue(transactionPool);
});

Expand All @@ -47,8 +58,8 @@ describe("AcceptBlockHandler", () => {

expect(result).toBe(BlockProcessorResult.Accepted);

expect(database.applyBlock).toBeCalledTimes(1);
expect(database.applyBlock).toHaveBeenCalledWith(block);
expect(databaseInteractions.applyBlock).toBeCalledTimes(1);
expect(databaseInteractions.applyBlock).toHaveBeenCalledWith(block);

expect(blockchain.resetWakeUp).toBeCalledTimes(1);

Expand Down Expand Up @@ -85,7 +96,7 @@ describe("AcceptBlockHandler", () => {
it("should return Reject and resetLastDownloadedBlock when something throws", async () => {
const acceptBlockHandler = container.resolve<AcceptBlockHandler>(AcceptBlockHandler);

database.applyBlock = jest.fn().mockRejectedValueOnce(new Error("oops"));
databaseInteractions.applyBlock = jest.fn().mockRejectedValueOnce(new Error("oops"));
const result = await acceptBlockHandler.execute(block as Interfaces.IBlock);

expect(result).toBe(BlockProcessorResult.Rejected);
Expand Down
@@ -1,9 +1,9 @@
import { Container, Services } from "@arkecosystem/core-kernel";
import { UnchainedHandler } from "@packages/core-blockchain/src/processor/handlers/unchained-handler";
import { BlockProcessorResult } from "@packages/core-blockchain/src/processor";
import { Interfaces } from "@packages/crypto";
import { GetActiveDelegatesAction } from "@packages/core-database/src/actions";
import { UnchainedHandler } from "@packages/core-blockchain/src/processor/handlers/unchained-handler";
import { GetActiveDelegatesAction } from "@packages/core-state/src/actions";
import { Sandbox } from "@packages/core-test-framework";
import { Interfaces } from "@packages/crypto";

let sandbox: Sandbox;

Expand All @@ -15,7 +15,18 @@ const blockchain = {
queue: { length: jest.fn() },
};
const stateStore = { numberOfBlocksToRollback: undefined };
const database = { getActiveDelegates: jest.fn() };
const database = {};
const databaseInteractions = {
walletRepository: {
getNonce: jest.fn(),
},
getTopBlocks: jest.fn(),
getLastBlock: jest.fn(),
loadBlocksFromCurrentRound: jest.fn(),
revertBlock: jest.fn(),
deleteRound: jest.fn(),
getActiveDelegates: jest.fn().mockReturnValue([]),
};

beforeEach(() => {
sandbox = new Sandbox();
Expand All @@ -24,6 +35,7 @@ beforeEach(() => {
sandbox.app.bind(Container.Identifiers.BlockchainService).toConstantValue(blockchain);
sandbox.app.bind(Container.Identifiers.LogService).toConstantValue(logger);
sandbox.app.bind(Container.Identifiers.DatabaseService).toConstantValue(database);
sandbox.app.bind(Container.Identifiers.DatabaseInteraction).toConstantValue(databaseInteractions);

sandbox.app.bind(Container.Identifiers.TriggerService).to(Services.Triggers.Triggers).inSingletonScope();
sandbox.app
Expand Down Expand Up @@ -52,7 +64,7 @@ describe("UnchainedHandler", () => {
},
};
blockchain.getLastBlock = jest.fn().mockReturnValueOnce(lastBlock);
database.getActiveDelegates = jest
databaseInteractions.getActiveDelegates = jest
.fn()
.mockResolvedValueOnce(
[
Expand Down Expand Up @@ -81,7 +93,7 @@ describe("UnchainedHandler", () => {
},
};
blockchain.getLastBlock = jest.fn().mockReturnValueOnce(lastBlock);
database.getActiveDelegates = jest
databaseInteractions.getActiveDelegates = jest
.fn()
.mockResolvedValueOnce(
[
Expand Down
1 change: 1 addition & 0 deletions __tests__/unit/core-blockchain/service-provider.test.ts
Expand Up @@ -11,6 +11,7 @@ describe("ServiceProvider", () => {

app.bind(Container.Identifiers.StateStore).toConstantValue({ reset: jest.fn() });
app.bind(Container.Identifiers.DatabaseService).toConstantValue({});
app.bind(Container.Identifiers.DatabaseInteraction).toConstantValue({});
app.bind(Container.Identifiers.DatabaseBlockRepository).toConstantValue({});
app.bind(Container.Identifiers.TransactionPoolService).toConstantValue({});
app.bind(Container.Identifiers.LogService).toConstantValue({});
Expand Down

0 comments on commit af5e542

Please sign in to comment.