Skip to content

refactor(core-state): fire wallet events #4076

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

Merged
merged 10 commits into from
Oct 8, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const setUp = async (): Promise<Application> => {
const app = new Application(new Container.Container());

const triggersServiceProvider = app.resolve(Services.Triggers.ServiceProvider);
const eventsServiceProvider = app.resolve(Services.Events.ServiceProvider);
const searchServiceProvider = app.resolve(Services.Search.ServiceProvider);
const stateServiceProvider = app.resolve(StateServiceProvider);
const transactionsServiceProvider = app.resolve(TransactionsServiceProvider);
Expand All @@ -22,6 +23,7 @@ export const setUp = async (): Promise<Application> => {
);

await triggersServiceProvider.register();
await eventsServiceProvider.register();
await searchServiceProvider.register();
await stateServiceProvider.register();
await transactionsServiceProvider.register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const setUp = async (): Promise<Application> => {
const app = new Application(new Container.Container());

const triggersServiceProvider = app.resolve(Services.Triggers.ServiceProvider);
const eventsServiceProvider = app.resolve(Services.Events.ServiceProvider);
const searchServiceProvider = app.resolve(Services.Search.ServiceProvider);
const stateServiceProvider = app.resolve(StateServiceProvider);
const transactionsServiceProvider = app.resolve(TransactionsServiceProvider);
Expand All @@ -26,6 +27,7 @@ export const setUp = async (): Promise<Application> => {
);

await triggersServiceProvider.register();
await eventsServiceProvider.register();
await searchServiceProvider.register();
await stateServiceProvider.register();
await transactionsServiceProvider.register();
Expand Down
18 changes: 0 additions & 18 deletions __tests__/unit/core-manager/service-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Application, Container, Providers } from "@packages/core-kernel";
import { defaults } from "@packages/core-manager/src/defaults";
import { Identifiers } from "@packages/core-manager/src/ioc";
import { ServiceProvider } from "@packages/core-manager/src/service-provider";
import { WatcherWallet } from "@packages/core-manager/src/watcher-wallet";
import { cloneDeep } from "lodash";
import path from "path";
import { dirSync, setGracefulCleanup } from "tmp";
Expand Down Expand Up @@ -171,21 +170,4 @@ describe("ServiceProvider", () => {
it("should not be required", async () => {
await expect(serviceProvider.required()).resolves.toBeFalse();
});

it("should create wallet", async () => {
const usedDefaults = cloneDeep(defaults);

usedDefaults.watcher.enabled = true;
usedDefaults.watcher.watch.wallets = true;

setPluginConfiguration(app, serviceProvider, usedDefaults);

await expect(serviceProvider.register()).toResolve();

// @ts-ignore
const wallet = app.get(Container.Identifiers.WalletFactory)("123");
expect(wallet).toBeInstanceOf(WatcherWallet);

await expect(serviceProvider.dispose()).toResolve();
});
});
84 changes: 0 additions & 84 deletions __tests__/unit/core-manager/watcher-wallet.test.ts

This file was deleted.

107 changes: 69 additions & 38 deletions __tests__/unit/core-state/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { StateBuilder } from "@packages/core-state/src/state-builder";
import { StateStore } from "@packages/core-state/src/stores/state";
import { TransactionValidator } from "@packages/core-state/src/transaction-validator";
import { WalletRepository, WalletRepositoryClone, WalletRepositoryCopyOnWrite } from "@packages/core-state/src/wallets";
import { registerFactories, registerIndexers } from "@packages/core-state/src/wallets/indexers";
import { registerIndexers } from "@packages/core-state/src/wallets/indexers";
import { Sandbox } from "@packages/core-test-framework/src";
import { Factories, FactoryBuilder } from "@packages/core-test-framework/src/factories";
import { Managers, Utils } from "@packages/crypto/src";
import { walletFactory } from "@arkecosystem/core-state/src/wallets/wallet-factory";

export interface Spies {
applySpy: jest.SpyInstance;
Expand All @@ -31,6 +32,7 @@ export interface Spies {
getSentTransactionSpy: jest.SpyInstance;
getRegisteredHandlersSpy: jest.SpyInstance;
dispatchSpy: jest.SpyInstance;
dispatchSyncSpy: jest.SpyInstance;
}

export interface Setup {
Expand Down Expand Up @@ -145,7 +147,6 @@ export const setUp = async (setUpOptions = setUpDefaults, skipBoot = false): Pro
// });

registerIndexers(sandbox.app);
registerFactories(sandbox.app);

sandbox.app.bind(Container.Identifiers.PluginConfiguration).to(Providers.PluginConfiguration).inSingletonScope();

Expand All @@ -166,42 +167,6 @@ export const setUp = async (setUpOptions = setUpDefaults, skipBoot = false): Pro

const stateStore: StateStore = sandbox.app.get(Container.Identifiers.StateStore);

sandbox.app
.bind(Container.Identifiers.WalletRepository)
.to(WalletRepository)
.inSingletonScope()
.when(Container.Selectors.anyAncestorOrTargetTaggedFirst("state", "blockchain"));

sandbox.app
.bind(Container.Identifiers.WalletRepository)
.to(WalletRepositoryClone)
.inRequestScope()
.when(Container.Selectors.anyAncestorOrTargetTaggedFirst("state", "clone"));

sandbox.app
.bind(Container.Identifiers.WalletRepository)
.to(WalletRepositoryCopyOnWrite)
.inRequestScope()
.when(Container.Selectors.anyAncestorOrTargetTaggedFirst("state", "copy-on-write"));

const walletRepoClone: WalletRepositoryClone = sandbox.app.getTagged(
Container.Identifiers.WalletRepository,
"state",
"clone",
);

const walletRepo: WalletRepository = sandbox.app.getTagged(
Container.Identifiers.WalletRepository,
"state",
"blockchain",
);

const walletRepoCopyOnWrite: WalletRepositoryCopyOnWrite = sandbox.app.getTagged(
Container.Identifiers.WalletRepository,
"state",
"copy-on-write",
);

const applySpy: jest.SpyInstance = jest.fn();
const revertSpy: jest.SpyInstance = jest.fn();

Expand Down Expand Up @@ -244,18 +209,83 @@ export const setUp = async (setUpOptions = setUpDefaults, skipBoot = false): Pro
}

const dispatchSpy = jest.fn();
const dispatchSyncSpy = jest.fn();

@Container.injectable()
class MockEventDispatcher {
public dispatch(data) {
return dispatchSpy(data);
}

public dispatchSync(data) {
return dispatchSyncSpy(data);
}
}

sandbox.app.container.bind(Container.Identifiers.DatabaseBlockRepository).to(MockBlockRepository);
sandbox.app.container.bind(Container.Identifiers.DatabaseTransactionRepository).to(MockTransactionRepository);
sandbox.app.container.bind(Container.Identifiers.EventDispatcherService).to(MockEventDispatcher);

sandbox.app
.bind(Container.Identifiers.WalletRepository)
.to(WalletRepository)
.inSingletonScope()
.when(Container.Selectors.anyAncestorOrTargetTaggedFirst("state", "blockchain"));

sandbox.app
.bind(Container.Identifiers.WalletFactory)
.toFactory(({ container }) => {
return walletFactory(
container.get(Container.Identifiers.WalletAttributes),
container.get(Container.Identifiers.EventDispatcherService),
);
})
.when(Container.Selectors.anyAncestorOrTargetTaggedFirst("state", "blockchain"));

sandbox.app
.bind(Container.Identifiers.WalletRepository)
.to(WalletRepositoryClone)
.inRequestScope()
.when(Container.Selectors.anyAncestorOrTargetTaggedFirst("state", "clone"));

sandbox.app
.bind(Container.Identifiers.WalletFactory)
.toFactory(({ container }) => {
return walletFactory(container.get(Container.Identifiers.WalletAttributes));
})
.when(Container.Selectors.anyAncestorOrTargetTaggedFirst("state", "clone"));

sandbox.app
.bind(Container.Identifiers.WalletRepository)
.to(WalletRepositoryCopyOnWrite)
.inRequestScope()
.when(Container.Selectors.anyAncestorOrTargetTaggedFirst("state", "copy-on-write"));

sandbox.app
.bind(Container.Identifiers.WalletFactory)
.toFactory(({ container }) => {
return walletFactory(container.get(Container.Identifiers.WalletAttributes));
})
.when(Container.Selectors.anyAncestorOrTargetTaggedFirst("state", "copy-on-write"));

const walletRepoClone: WalletRepositoryClone = sandbox.app.getTagged(
Container.Identifiers.WalletRepository,
"state",
"clone",
);

const walletRepo: WalletRepository = sandbox.app.getTagged(
Container.Identifiers.WalletRepository,
"state",
"blockchain",
);

const walletRepoCopyOnWrite: WalletRepositoryCopyOnWrite = sandbox.app.getTagged(
Container.Identifiers.WalletRepository,
"state",
"copy-on-write",
);

sandbox.app.bind(Container.Identifiers.BlockState).to(BlockState);

sandbox.app
Expand Down Expand Up @@ -323,6 +353,7 @@ export const setUp = async (setUpOptions = setUpDefaults, skipBoot = false): Pro
getSentTransactionSpy,
getRegisteredHandlersSpy,
dispatchSpy,
dispatchSyncSpy,
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,11 @@ describe("Wallet Repository Copy On Write", () => {
const wallet = walletRepo.createWallet("abc");
wallet.setAttribute("delegate", { username: "test" });
walletRepo.index(wallet);
const clone = walletRepoCopyOnWrite.findByIndex(Contracts.State.WalletIndexes.Usernames, "test");

expect(walletRepoCopyOnWrite.findByIndex(Contracts.State.WalletIndexes.Usernames, "test")).toEqual(wallet);
expect(clone).not.toBe(wallet);
expect(clone.address).toEqual(wallet.address);
expect(clone.getAttribute("delegate.username")).toEqual(wallet.getAttribute("delegate.username"));
});
});

Expand Down
Loading