Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-state): in-memory storage for last N blocks and transactions #2492

Merged
merged 13 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions __tests__/integration/core-p2p/mocks/core-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ jest.mock("@arkecosystem/core-container", () => {

if (name === "state") {
return {
getLastBlock: () => genesisBlock,
cacheTransactions: jest.fn().mockImplementation(txs => ({ notAdded: txs, added: [] })),
removeCachedTransactionIds: jest.fn().mockReturnValue(null),
getStore: () => ({
getLastBlock: () => genesisBlock,
getLastHeight: () => genesisBlock.data.height,
cacheTransactions: jest.fn().mockImplementation(txs => ({ notAdded: txs, added: [] })),
removeCachedTransactionIds: jest.fn().mockReturnValue(null),
}),
};
}

Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/core-blockchain/mocks/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const container = {
if (name === "state") {
stateStorageStub.blockchain = blockchainMachine.initialState;

return stateStorageStub;
return { getStore: () => stateStorageStub };
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-blockchain/stubs/state-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { State } from "@arkecosystem/core-interfaces";
import { Blocks, Interfaces } from "@arkecosystem/crypto";

export class StateStorageStub implements State.IStateStorage {
export class StateStoreStub implements State.IStateStore {
public blockchain: any;
public lastDownloadedBlock: Interfaces.IBlock | null;
public blockPing: any;
Expand Down Expand Up @@ -76,4 +76,4 @@ export class StateStorageStub implements State.IStateStorage {
}
}

export const stateStorageStub = new StateStorageStub();
export const stateStorageStub = new StateStoreStub();
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { State } from "@arkecosystem/core-interfaces";
import { Blocks, Interfaces } from "@arkecosystem/crypto";

export class StateStorageStub implements State.IStateStorage {
export class StateStoreStub implements State.IStateStore {
public blockchain: any;
public lastDownloadedBlock: Interfaces.IBlock | null;
public blockPing: any;
Expand Down Expand Up @@ -65,4 +65,4 @@ export class StateStorageStub implements State.IStateStorage {
public setLastBlock(block: Blocks.Block): void {}
}

export const stateStorageStub = new StateStorageStub();
export const stateStorageStub = new StateStoreStub();
2 changes: 1 addition & 1 deletion __tests__/unit/core-database/mocks/core-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jest.mock("@arkecosystem/core-container", () => {
}

if (name === "state") {
return stateStorageStub;
return { getStore: () => stateStorageStub };
}

return {};
Expand Down
6 changes: 4 additions & 2 deletions __tests__/unit/core-p2p/mocks/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { genesisBlock } from "../../../utils/fixtures/unitnet/block-model";

export const state = {
getLastBlock: () => genesisBlock,
forkedBlock: null,
getStore: () => ({
getLastBlock: () => genesisBlock,
forkedBlock: null,
}),
};
7 changes: 6 additions & 1 deletion __tests__/unit/core-p2p/network-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,18 @@ describe("NetworkMonitor", () => {

const spySuspend = jest.spyOn(processor, "suspend");

state.forkedBlock = { ip: "1.1.1.1" };
const spyStateStore = jest.spyOn(state, "getStore").mockReturnValueOnce({
...state.getStore(),
...{ forkedBlock: { ip: "1.1.1.1" } },
});

await monitor.refreshPeersAfterFork();

expect(monitor.resetSuspendedPeers).toHaveBeenCalled();
expect(spySuspend).toHaveBeenCalledWith("1.1.1.1");
expect(connector.disconnect).toHaveBeenCalled();

spyStateStore.mockRestore();
});
});

Expand Down
7 changes: 0 additions & 7 deletions __tests__/unit/core-state/mocks/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ export const container = {
}),
};
},
resolve: name => {
if (name === "state") {
return {};
}

return {};
},
resolvePlugin: name => {
if (name === "logger") {
return logger;
Expand Down
43 changes: 43 additions & 0 deletions __tests__/unit/core-state/stores/blocks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Blocks, Interfaces, Managers } from "@arkecosystem/crypto";
import { BlockStore } from "../../../../packages/core-state/src/stores/blocks";
import { genesisBlock as GB } from "../../../utils/config/testnet/genesisBlock";

Managers.configManager.setFromPreset("testnet");

describe("BlockStore", () => {
it("should push and get a block", () => {
const genesisBlock: Interfaces.IBlock = Blocks.BlockFactory.fromData(GB);

const store = new BlockStore(100);
store.set(genesisBlock);

expect(store.count()).toBe(1);
expect(store.get(genesisBlock.data.id)).toEqual(genesisBlock.data);
expect(store.get(genesisBlock.data.height)).toEqual(genesisBlock.data);
});

it("should fail to push a block if its height is not 1 and there is no last block", () => {
const store = new BlockStore(2);

expect(() => store.set({ data: { height: 3 } } as Interfaces.IBlock)).toThrow();
});

it("should fail to push a block if it isn't chained", () => {
const store = new BlockStore(2);
store.set({ data: { height: 1 } } as Interfaces.IBlock);

expect(() => store.set({ data: { height: 3 } } as Interfaces.IBlock)).toThrow();
});

it("should return all ids and heights in the order they were inserted", () => {
const store = new BlockStore(4);

for (let i = 1; i < 5; i++) {
store.set({ data: { id: i.toString(), height: i } } as Interfaces.IBlock);
}

expect(store.count()).toBe(4);
expect(store.getIds()).toEqual(["1", "2", "3", "4"]);
expect(store.getHeights()).toEqual([1, 2, 3, 4]);
});
});
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import "./mocks/";
import { container } from "./mocks/container";
import { logger } from "./mocks/logger";
import "../mocks/";
import { container } from "../mocks/container";
import { logger } from "../mocks/logger";

import { Blocks as cBlocks, Interfaces } from "@arkecosystem/crypto";
import delay from "delay";
import { defaults } from "../../../packages/core-state/src/defaults";
import { StateStorage } from "../../../packages/core-state/src/state-storage";
import "../../utils";
import { blocks101to155 } from "../../utils/fixtures/testnet/blocks101to155";
import { blocks2to100 } from "../../utils/fixtures/testnet/blocks2to100";
import { defaults } from "../../../../packages/core-state/src/defaults";
import { StateStore } from "../../../../packages/core-state/src/stores/state";
import "../../../utils";
import { blocks101to155 } from "../../../utils/fixtures/testnet/blocks101to155";
import { blocks2to100 } from "../../../utils/fixtures/testnet/blocks2to100";

const { Block, BlockFactory } = cBlocks;
const blocks = blocks2to100.concat(blocks101to155).map(block => BlockFactory.fromData(block));

let stateStorage;
beforeAll(async () => {
stateStorage = new StateStorage();
stateStorage = new StateStore();
});

beforeEach(() => {
Expand Down
14 changes: 14 additions & 0 deletions __tests__/unit/core-state/stores/transactions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { TransactionStore } from "../../../../packages/core-state/src/stores/transactions";
import { genesisBlock } from "../../../utils/config/testnet/genesisBlock";

describe("TransactionStore", () => {
it("should push and get a transaction", () => {
const transaction = genesisBlock.transactions[0];

const store = new TransactionStore(100);
store.push(transaction);

expect(store.count()).toBe(1);
expect(store.get(transaction.id)).toEqual(transaction);
});
});
15 changes: 12 additions & 3 deletions __tests__/unit/core-transaction-pool/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ describe("Connection", () => {
const heightAtStart = 42;

jest.spyOn(container.app, "has").mockReturnValue(true);
jest.spyOn(state, "getLastBlock").mockReturnValue({ data: { height: heightAtStart } });
jest.spyOn(state, "getStore").mockReturnValue({
...state.getStore(),
...{ getLastHeight: () => heightAtStart },
});

expect(connection.getPoolSize()).toBe(0);

Expand Down Expand Up @@ -247,11 +250,17 @@ describe("Connection", () => {

expect(connection.getPoolSize()).toBe(4);

jest.spyOn(state, "getLastBlock").mockReturnValue({ data: { height: expiration - 1 } });
jest.spyOn(state, "getStore").mockReturnValue({
...state.getStore(),
...{ getLastHeight: () => expiration - 1 },
});

expect(connection.getPoolSize()).toBe(4);

jest.spyOn(state, "getLastBlock").mockReturnValue({ data: { height: expiration } });
jest.spyOn(state, "getStore").mockReturnValue({
...state.getStore(),
...{ getLastHeight: () => expiration },
});

expect(connection.getPoolSize()).toBe(2);

Expand Down
9 changes: 6 additions & 3 deletions __tests__/unit/core-transaction-pool/mocks/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export const state = {
cacheTransactions: () => null,
getLastBlock: () => ({ data: { height: 0 } }),
removeCachedTransactionIds: () => null,
getStore: () => ({
cacheTransactions: () => null,
getLastBlock: () => ({ data: { height: 0 } }),
getLastHeight: () => 1,
removeCachedTransactionIds: () => null,
}),
};
Loading