Skip to content

Commit

Permalink
refactor(core-state): split DatabaseInteractions (#4353)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner committed Apr 7, 2021
1 parent 4e3925c commit bed6bef
Show file tree
Hide file tree
Showing 25 changed files with 1,487 additions and 996 deletions.
52 changes: 15 additions & 37 deletions __tests__/functional/transaction-forging/__support__/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import "jest-extended";

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 { Managers } from "@arkecosystem/crypto";
import delay from "delay";

jest.setTimeout(1200000);

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

Expand All @@ -27,6 +24,7 @@ export const setUp = async (): Promise<Contracts.Kernel.Application> => {
list: [{ ip: "127.0.0.1", port: 4000 }],
},
});

await sandbox.boot(async ({ app }) => {
await app.bootstrap({
flags: {
Expand Down Expand Up @@ -62,35 +60,6 @@ export const setUp = async (): Promise<Contracts.Kernel.Application> => {

Managers.configManager.getMilestone().aip11 = true;
Managers.configManager.getMilestone().htlcEnabled = true;

const databaseService = app.get<DatabaseService>(Container.Identifiers.DatabaseService);
const walletRepository = app.getTagged<Contracts.State.WalletRepository>(
Container.Identifiers.WalletRepository,
"state",
"blockchain",
);

await databaseService.saveRound(
secrets.map((secret, i) => {
const wallet = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase(secret));

wallet.setAttribute("delegate", {
username: `genesis_${i + 1}`,
voteBalance: Utils.BigNumber.make("300000000000000"),
forgedFees: Utils.BigNumber.ZERO,
forgedRewards: Utils.BigNumber.ZERO,
producedBlocks: 0,
round: 1,
rank: undefined,
});

return wallet;
}),
);

const databaseInteraction = app.get<DatabaseInteraction>(Container.Identifiers.DatabaseInteraction);

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

return sandbox.app;
Expand All @@ -116,18 +85,27 @@ export const tearDown = async (): Promise<void> => {
balance: wallet.balance,
nonce: wallet.nonce,
attributes: walletAttributes,
}
};
};
const sortWallets = (a: Contracts.State.Wallet, b: Contracts.State.Wallet) => a.publicKey!.localeCompare(b.publicKey!);
const sortWallets = (a: Contracts.State.Wallet, b: Contracts.State.Wallet) =>
a.publicKey!.localeCompare(b.publicKey!);

const allByPublicKey = walletRepository.allByPublicKey().map(w => w.clone()).sort(sortWallets).map(mapWallets);
const allByPublicKey = walletRepository
.allByPublicKey()
.map((w) => w.clone())
.sort(sortWallets)
.map(mapWallets);

walletRepository.reset();

await sandbox.app.resolve<StateBuilder>(StateBuilder).run();
await delay(2000); // if there is an issue with state builder, we wait a bit to be sure to catch it in the logs

const allByPublicKeyBootstrapped = walletRepository.allByPublicKey().map(w => w.clone()).sort(sortWallets).map(mapWallets);
const allByPublicKeyBootstrapped = walletRepository
.allByPublicKey()
.map((w) => w.clone())
.sort(sortWallets)
.map(mapWallets);
expect(allByPublicKeyBootstrapped).toEqual(allByPublicKey);
};

Expand Down
12 changes: 2 additions & 10 deletions __tests__/integration/core-api/handlers/rounds.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,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 { Contracts } from "@arkecosystem/core-kernel";
import { ApiHelpers } from "@packages/core-test-framework/src";

import { calculateRanks, setUp, tearDown } from "../__support__/setup";

Expand All @@ -13,12 +11,6 @@ beforeAll(async () => {
app = await setUp();
api = new ApiHelpers(app);

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

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

await calculateRanks();
});

Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-blockchain/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("Blockchain", () => {

databaseInteractions.getTopBlocks = jest.fn();
databaseInteractions.getLastBlock = jest.fn();
databaseInteractions.loadBlocksFromCurrentRound = jest.fn();
databaseInteractions.restoreCurrentRound = jest.fn();
databaseInteractions.revertBlock = jest.fn();
databaseInteractions.deleteRound = jest.fn();
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValue([]);
Expand Down Expand Up @@ -707,7 +707,7 @@ describe("Blockchain", () => {
await blockchain.removeTopBlocks(numberOfBlocks);

expect(blockRepository.deleteTopBlocks).toHaveBeenLastCalledWith(numberOfBlocks);
expect(databaseInteractions.loadBlocksFromCurrentRound).toHaveBeenCalled();
expect(databaseInteractions.restoreCurrentRound).toHaveBeenCalled();
},
);
});
Expand Down
12 changes: 6 additions & 6 deletions __tests__/unit/core-blockchain/process-blocks-job.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ProcessBlockAction } from "@packages/core-blockchain/src/actions";
import { Container, Services } from "@packages/core-kernel";
import { Crypto, Interfaces, Networks } from "@packages/crypto";
import { ProcessBlocksJob } from "@packages/core-blockchain/src/process-blocks-job";
import { BlockProcessorResult } from "@packages/core-blockchain/src/processor";
import { Container, Services } from "@packages/core-kernel";
import { Sandbox } from "@packages/core-test-framework";
import { Crypto, Interfaces, Networks } from "@packages/crypto";

import { Blocks } from "./__fixtures__";

Expand Down Expand Up @@ -200,7 +200,7 @@ describe("Blockchain", () => {

blockchainService.clearQueue = jest.fn();
blockchainService.resetLastDownloadedBlock = jest.fn();
databaseInteraction.loadBlocksFromCurrentRound = jest.fn();
databaseInteraction.restoreCurrentRound = jest.fn();
databaseService.deleteRound = jest.fn();

stateStore.setLastBlock = jest.fn();
Expand All @@ -210,7 +210,7 @@ describe("Blockchain", () => {

expect(blockchainService.clearQueue).toBeCalledTimes(1);
expect(blockchainService.resetLastDownloadedBlock).toBeCalledTimes(1);
expect(databaseInteraction.loadBlocksFromCurrentRound).toBeCalledTimes(1);
expect(databaseInteraction.restoreCurrentRound).toBeCalledTimes(1);
expect(databaseService.deleteRound).toBeCalledTimes(1);
});

Expand All @@ -229,7 +229,7 @@ describe("Blockchain", () => {

blockchainService.clearQueue = jest.fn();
blockchainService.resetLastDownloadedBlock = jest.fn();
databaseInteraction.loadBlocksFromCurrentRound = jest.fn();
databaseInteraction.restoreCurrentRound = jest.fn();
databaseService.deleteRound = jest.fn();

stateStore.setLastBlock = jest.fn();
Expand All @@ -239,7 +239,7 @@ describe("Blockchain", () => {

expect(blockchainService.clearQueue).toBeCalledTimes(1);
expect(blockchainService.resetLastDownloadedBlock).toBeCalledTimes(1);
expect(databaseInteraction.loadBlocksFromCurrentRound).toBeCalledTimes(1);
expect(databaseInteraction.restoreCurrentRound).toBeCalledTimes(1);
expect(databaseService.deleteRound).toBeCalledTimes(1);

expect(process.exit).toHaveBeenCalled();
Expand Down
17 changes: 10 additions & 7 deletions __tests__/unit/core-blockchain/processor/block-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ describe("BlockProcessor", () => {
},
getTopBlocks: jest.fn(),
getLastBlock: jest.fn(),
loadBlocksFromCurrentRound: jest.fn(),
restoreCurrentRound: jest.fn(),
revertBlock: jest.fn(),
deleteRound: jest.fn(),
};
const roundState = {
getActiveDelegates: jest.fn().mockReturnValue([]),
};

Expand All @@ -58,6 +60,7 @@ describe("BlockProcessor", () => {
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.RoundState).toConstantValue(roundState);
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 @@ -229,7 +232,7 @@ describe("BlockProcessor", () => {
};

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

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

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

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

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

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

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

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

Expand All @@ -359,7 +362,7 @@ describe("BlockProcessor", () => {
...chainedBlock,
transactions: [{ data: transactionData, id: transactionData.id } as Interfaces.ITransaction],
};
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
roundState.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
blockchain.getLastBlock = jest.fn().mockReturnValueOnce(baseBlock);
transactionRepository.getForgedTransactionsIds = jest.fn().mockReturnValueOnce([transactionData.id]);
walletRepository.getNonce = jest.fn().mockReturnValueOnce(Utils.BigNumber.ONE);
Expand All @@ -379,7 +382,7 @@ describe("BlockProcessor", () => {
const block = {
...chainedBlock,
};
databaseInteractions.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
roundState.getActiveDelegates = jest.fn().mockReturnValueOnce([]);
blockchain.getLastBlock = jest.fn().mockReturnValueOnce(baseBlock);
transactionRepository.getForgedTransactionsIds = jest.fn().mockReturnValueOnce([]);
const generatorWallet = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const databaseInteractions = {
loadBlocksFromCurrentRound: jest.fn(),
revertBlock: jest.fn(),
deleteRound: jest.fn(),
};

const roundState = {
getActiveDelegates: jest.fn().mockReturnValue([]),
};

Expand All @@ -39,6 +42,7 @@ beforeEach(() => {
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.RoundState).toConstantValue(roundState);

sandbox.app.bind(Container.Identifiers.TriggerService).to(Services.Triggers.Triggers).inSingletonScope();
sandbox.app
Expand Down Expand Up @@ -67,7 +71,7 @@ describe("UnchainedHandler", () => {
},
};
blockchain.getLastBlock = jest.fn().mockReturnValueOnce(lastBlock);
databaseInteractions.getActiveDelegates = jest
roundState.getActiveDelegates = jest
.fn()
.mockResolvedValueOnce(
[
Expand Down Expand Up @@ -96,7 +100,7 @@ describe("UnchainedHandler", () => {
},
};
blockchain.getLastBlock = jest.fn().mockReturnValueOnce(lastBlock);
databaseInteractions.getActiveDelegates = jest
roundState.getActiveDelegates = jest
.fn()
.mockResolvedValueOnce(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe("Initialize", () => {

expect(stateStore.setLastBlock).toHaveBeenCalledTimes(1);
expect(databaseService.deleteRound).toHaveBeenCalledTimes(1);
expect(databaseInteractions.restoreCurrentRound).toHaveBeenCalledTimes(0);
expect(databaseInteractions.restoreCurrentRound).toHaveBeenCalledTimes(1);
expect(transactionPool.readdTransactions).toHaveBeenCalledTimes(0);
expect(peerNetworkMonitor.boot).toHaveBeenCalledTimes(1);
expect(stateBuilder.run).toHaveBeenCalledTimes(1);
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-forger/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const setup = async (activeDelegates) => {
}

@Container.injectable()
class MockDatabaseInteraction {
class MockRoundState {
public async getActiveDelegates(): Promise<Wallet[]> {
return activeDelegates;
}
Expand All @@ -58,7 +58,7 @@ export const setup = async (activeDelegates) => {

sandbox.app.bind(Container.Identifiers.DatabaseService).to(MockDatabaseService);

sandbox.app.bind(Container.Identifiers.DatabaseInteraction).to(MockDatabaseInteraction);
sandbox.app.bind(Container.Identifiers.RoundState).to(MockRoundState);

sandbox.app.bind(Container.Identifiers.BlockchainService).to(MockBlockchainService);

Expand Down
Loading

0 comments on commit bed6bef

Please sign in to comment.