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-transaction-pool): disaptch additional transaction-pool events #3766

Merged
merged 4 commits into from
Jun 3, 2020
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
14 changes: 9 additions & 5 deletions __tests__/unit/core-transaction-pool/sender-state.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Container, Contracts } from "@packages/core-kernel";
import { Crypto, Interfaces, Managers } from "@packages/crypto";

import { SenderState } from "@packages/core-transaction-pool/src/sender-state";
import { Sandbox } from "@packages/core-test-framework";
import { Container, Contracts, Enums } from "@packages/core-kernel";
import { Services } from "@packages/core-kernel/dist";
import { Sandbox } from "@packages/core-test-framework";
import {
ApplyTransactionAction,
RevertTransactionAction,
ThrowIfCannotEnterPoolAction,
VerifyTransactionAction,
} from "@packages/core-transaction-pool/src/actions";
import { SenderState } from "@packages/core-transaction-pool/src/sender-state";
import { Crypto, Interfaces, Managers } from "@packages/crypto";

jest.mock("@packages/crypto");

const configuration = { getRequired: jest.fn(), getOptional: jest.fn() };
const handler = { verify: jest.fn(), throwIfCannotEnterPool: jest.fn(), apply: jest.fn(), revert: jest.fn() };
const handlerRegistry = { getActivatedHandlerForData: jest.fn() };
const expirationService = { isExpired: jest.fn(), getExpirationHeight: jest.fn() };
const eventDispatcherService = { dispatch: jest.fn() };

let sandbox: Sandbox;

Expand All @@ -26,6 +26,7 @@ beforeEach(() => {
sandbox.app.bind(Container.Identifiers.PluginConfiguration).toConstantValue(configuration);
sandbox.app.bind(Container.Identifiers.TransactionHandlerRegistry).toConstantValue(handlerRegistry);
sandbox.app.bind(Container.Identifiers.TransactionPoolExpirationService).toConstantValue(expirationService);
sandbox.app.bind(Container.Identifiers.EventDispatcherService).toConstantValue(eventDispatcherService);
sandbox.app.bind(Container.Identifiers.TriggerService).to(Services.Triggers.Triggers).inSingletonScope();

sandbox.app
Expand Down Expand Up @@ -112,6 +113,9 @@ describe("SenderState.apply", () => {

await expect(promise).rejects.toBeInstanceOf(Contracts.TransactionPool.PoolError);
await expect(promise).rejects.toHaveProperty("type", "ERR_EXPIRED");

expect(eventDispatcherService.dispatch).toHaveBeenCalledTimes(1);
expect(eventDispatcherService.dispatch).toHaveBeenCalledWith(Enums.TransactionEvent.Expired, expect.anything());
});

it("should throw when transaction fails to verify", async () => {
Expand Down
103 changes: 70 additions & 33 deletions __tests__/unit/core-transaction-pool/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Container, Contracts } from "@arkecosystem/core-kernel";
import { Identities, Managers, Transactions } from "@arkecosystem/crypto";

import { Service } from "../../../packages/core-transaction-pool/src/service";
import { Container, Contracts, Enums } from "@packages/core-kernel";
import { Sandbox } from "@packages/core-test-framework";
import { Service } from "@packages/core-transaction-pool/src/service";
import { Identities, Managers, Transactions } from "@packages/crypto";

const logger = {
debug: jest.fn(),
Expand Down Expand Up @@ -32,14 +32,18 @@ const poolQuery = {
const expirationService = {
isExpired: jest.fn(),
};
const eventDispatcherService = {
dispatch: jest.fn(),
};

const container = new Container.Container();
container.bind(Container.Identifiers.LogService).toConstantValue(logger);
container.bind(Container.Identifiers.PluginConfiguration).toConstantValue(configuration);
container.bind(Container.Identifiers.TransactionPoolStorage).toConstantValue(storage);
container.bind(Container.Identifiers.TransactionPoolMempool).toConstantValue(mempool);
container.bind(Container.Identifiers.TransactionPoolQuery).toConstantValue(poolQuery);
container.bind(Container.Identifiers.TransactionPoolExpirationService).toConstantValue(expirationService);
const sandbox = new Sandbox();
sandbox.app.bind(Container.Identifiers.LogService).toConstantValue(logger);
sandbox.app.bind(Container.Identifiers.PluginConfiguration).toConstantValue(configuration);
sandbox.app.bind(Container.Identifiers.TransactionPoolStorage).toConstantValue(storage);
sandbox.app.bind(Container.Identifiers.TransactionPoolMempool).toConstantValue(mempool);
sandbox.app.bind(Container.Identifiers.TransactionPoolQuery).toConstantValue(poolQuery);
sandbox.app.bind(Container.Identifiers.TransactionPoolExpirationService).toConstantValue(expirationService);
sandbox.app.bind(Container.Identifiers.EventDispatcherService).toConstantValue(eventDispatcherService);

beforeEach(() => {
logger.debug.mockReset();
Expand Down Expand Up @@ -91,7 +95,7 @@ describe("Service.boot", () => {
process.env["CORE_RESET_DATABASE"] = "1";

try {
const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.boot();

expect(mempool.flush).toBeCalled();
Expand All @@ -104,7 +108,7 @@ describe("Service.boot", () => {
it("should readd stored transactions", async () => {
storage.getAllTransactions.mockReturnValueOnce([transaction1, transaction2]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.boot();

expect(mempool.flush).toBeCalled();
Expand All @@ -117,7 +121,7 @@ describe("Service.getPoolSize", () => {
it("should return value from mempool", () => {
mempool.getSize.mockReturnValueOnce(100);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
const size = service.getPoolSize();

expect(size).toBe(100);
Expand All @@ -129,7 +133,7 @@ describe("Service.addTransaction", () => {
it("should throw if transaction is already in storage", async () => {
storage.hasTransaction.mockReturnValueOnce(true);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
const promise = service.addTransaction(transaction1);

await expect(promise).rejects.toBeInstanceOf(Contracts.TransactionPool.PoolError);
Expand All @@ -140,24 +144,35 @@ describe("Service.addTransaction", () => {
configuration.getRequired.mockReturnValue(10); // maxTransactionsInPool
mempool.getSize.mockReturnValue(0);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.addTransaction(transaction1);

expect(storage.addTransaction).toBeCalledWith(transaction1);
expect(mempool.addTransaction).toBeCalledWith(transaction1);

expect(eventDispatcherService.dispatch).toHaveBeenCalledTimes(1);
expect(eventDispatcherService.dispatch).toHaveBeenCalledWith(
Enums.TransactionEvent.AddedToPool,
expect.anything(),
);
});

it("should remove transaction from storage that failed adding to mempool", async () => {
configuration.getRequired.mockReturnValue(10); // maxTransactionsInPool
mempool.getSize.mockReturnValue(0);
mempool.addTransaction.mockRejectedValueOnce(new Error("Nope"));

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
const promise = service.addTransaction(transaction1);

await expect(promise).rejects.toBeInstanceOf(Error);
expect(storage.addTransaction).toBeCalledWith(transaction1);
expect(storage.removeTransaction).toBeCalledWith(transaction1.id);

expect(eventDispatcherService.dispatch).toHaveBeenCalledWith(
Enums.TransactionEvent.RejectedByPool,
expect.anything(),
);
});

it("should remove expired transactions when pool is full", async () => {
Expand All @@ -168,12 +183,17 @@ describe("Service.addTransaction", () => {
mempool.removeTransaction.mockReturnValueOnce([transaction2]);
mempool.getSize.mockReturnValueOnce(0);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.addTransaction(transaction1);

expect(expirationService.isExpired).toBeCalledWith(transaction2);
expect(mempool.removeTransaction).toBeCalledWith(transaction2);
expect(storage.removeTransaction).toBeCalledWith(transaction2.id);

expect(eventDispatcherService.dispatch).toHaveBeenCalledWith(
Enums.TransactionEvent.RemovedFromPool,
expect.anything(),
);
});

it("should throw if fee isn't higher than lowest priority transaction when pool is full", async () => {
Expand All @@ -183,11 +203,16 @@ describe("Service.addTransaction", () => {
expirationService.isExpired.mockReturnValueOnce(false);
poolQuery.getFromLowestPriority.mockReturnValueOnce({ first: () => transaction2 });

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
const promise = service.addTransaction(transaction1);

await expect(promise).rejects.toBeInstanceOf(Contracts.TransactionPool.PoolError);
await expect(promise).rejects.toHaveProperty("type", "ERR_POOL_FULL");

expect(eventDispatcherService.dispatch).toHaveBeenCalledWith(
Enums.TransactionEvent.RejectedByPool,
expect.anything(),
);
});

it("should remove low priority transactions when pool is full", async () => {
Expand All @@ -207,22 +232,27 @@ describe("Service.addTransaction", () => {
poolQuery.getFromLowestPriority.mockReturnValueOnce({ first: () => transaction2 });
mempool.removeTransaction.mockReturnValueOnce([transaction2]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.addTransaction(transaction3);

expect(mempool.removeTransaction).toBeCalledWith(transaction1);
expect(storage.removeTransaction).toBeCalledWith(transaction1.id);

expect(mempool.removeTransaction).toBeCalledWith(transaction2);
expect(storage.removeTransaction).toBeCalledWith(transaction2.id);

expect(eventDispatcherService.dispatch).toHaveBeenCalledWith(
Enums.TransactionEvent.RemovedFromPool,
expect.anything(),
);
});
});

describe("Service.removeTransaction", () => {
it("should log error if transaction wasn't added previously", async () => {
storage.hasTransaction.mockReturnValueOnce(false);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.removeTransaction(transaction1);

expect(logger.error).toBeCalled();
Expand All @@ -232,19 +262,24 @@ describe("Service.removeTransaction", () => {
storage.hasTransaction.mockReturnValueOnce(true);
mempool.removeTransaction.mockReturnValueOnce([transaction1, transaction2]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.removeTransaction(transaction1);

expect(mempool.removeTransaction).toBeCalledWith(transaction1);
expect(storage.removeTransaction).toBeCalledWith(transaction1.id);
expect(storage.removeTransaction).toBeCalledWith(transaction2.id);

expect(eventDispatcherService.dispatch).toHaveBeenCalledWith(
Enums.TransactionEvent.RemovedFromPool,
expect.anything(),
);
});

it("should log error if transaction wasn't found in mempool", async () => {
storage.hasTransaction.mockReturnValueOnce(true);
mempool.removeTransaction.mockReturnValueOnce([transaction2]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.removeTransaction(transaction1);

expect(mempool.removeTransaction).toBeCalledWith(transaction1);
Expand All @@ -258,7 +293,7 @@ describe("Service.acceptForgedTransaction", () => {
it("should do nothing if transaction wasn't added previously", async () => {
storage.hasTransaction.mockReturnValueOnce(false);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.acceptForgedTransaction(transaction1);

expect(mempool.acceptForgedTransaction).not.toBeCalled();
Expand All @@ -268,7 +303,7 @@ describe("Service.acceptForgedTransaction", () => {
storage.hasTransaction.mockReturnValueOnce(true);
mempool.acceptForgedTransaction.mockReturnValueOnce([transaction1, transaction2]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.acceptForgedTransaction(transaction1);

expect(mempool.acceptForgedTransaction).toBeCalledWith(transaction1);
Expand All @@ -280,7 +315,7 @@ describe("Service.acceptForgedTransaction", () => {
storage.hasTransaction.mockReturnValueOnce(true);
mempool.acceptForgedTransaction.mockReturnValueOnce([transaction2]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.acceptForgedTransaction(transaction1);

expect(mempool.acceptForgedTransaction).toBeCalledWith(transaction1);
Expand All @@ -294,7 +329,7 @@ describe("Service.readdTransactions", () => {
it("should flush mempool", async () => {
storage.getAllTransactions.mockReturnValueOnce([]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.readdTransactions();

expect(mempool.flush).toBeCalled();
Expand All @@ -303,7 +338,7 @@ describe("Service.readdTransactions", () => {
it("should add all transactions from storage", async () => {
storage.getAllTransactions.mockReturnValueOnce([transaction1, transaction2, transaction3]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.readdTransactions();

expect(mempool.addTransaction).toBeCalledWith(transaction1);
Expand All @@ -315,7 +350,7 @@ describe("Service.readdTransactions", () => {
storage.getAllTransactions.mockReturnValueOnce([transaction1]);
mempool.addTransaction.mockRejectedValueOnce(new Error("Something wrong"));

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.readdTransactions();

expect(storage.removeTransaction).toBeCalledWith(transaction1.id);
Expand All @@ -324,7 +359,7 @@ describe("Service.readdTransactions", () => {
it("should first add transactions passed in argument", async () => {
storage.getAllTransactions.mockReturnValueOnce([transaction3]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.readdTransactions([transaction1, transaction2]);

expect(mempool.addTransaction).toBeCalledWith(transaction1);
Expand All @@ -336,7 +371,7 @@ describe("Service.readdTransactions", () => {
storage.getAllTransactions.mockReturnValueOnce([transaction3]);
mempool.addTransaction.mockRejectedValueOnce(new Error("Something wrong"));

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.readdTransactions([transaction1, transaction2]);

expect(mempool.addTransaction).toBeCalledWith(transaction2);
Expand All @@ -350,11 +385,13 @@ describe("Service.cleanUp", () => {
expirationService.isExpired.mockReturnValueOnce(true);
mempool.removeTransaction.mockReturnValueOnce([transaction2]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.cleanUp();

expect(mempool.removeTransaction).toBeCalledWith(transaction2);
expect(storage.removeTransaction).toBeCalledWith(transaction2.id);

expect(eventDispatcherService.dispatch).toHaveBeenCalledWith(Enums.TransactionEvent.Expired, expect.anything());
});

it("should remove lowest priority transactions", async () => {
Expand All @@ -368,7 +405,7 @@ describe("Service.cleanUp", () => {
poolQuery.getFromLowestPriority.mockReturnValueOnce({ first: () => transaction1 });
mempool.removeTransaction.mockReturnValueOnce([transaction1]);

const service = container.resolve(Service);
const service = sandbox.app.resolve(Service);
await service.addTransaction(transaction3);

expect(mempool.removeTransaction).toBeCalledWith(transaction1);
Expand Down
4 changes: 3 additions & 1 deletion packages/core-transaction-pool/src/sender-state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Container, Contracts, Providers, Services } from "@arkecosystem/core-kernel";
import { Container, Contracts, Enums, Providers, Services } from "@arkecosystem/core-kernel";
import { Handlers } from "@arkecosystem/core-transactions";
import { Crypto, Interfaces, Managers } from "@arkecosystem/crypto";

Expand Down Expand Up @@ -48,6 +48,8 @@ export class SenderState implements Contracts.TransactionPool.SenderState {
}

if (await this.expirationService.isExpired(transaction)) {
this.app.events.dispatch(Enums.TransactionEvent.Expired, transaction.data);

const expirationHeight: number = await this.expirationService.getExpirationHeight(transaction);
throw new TransactionHasExpiredError(transaction, expirationHeight);
}
Expand Down
Loading