Skip to content

Commit

Permalink
refactor: use ioc for wallet repository indexes
Browse files Browse the repository at this point in the history
Instead of registering magistrate wallet repository indexes in boot method they are registered through dependency injection and injected through multiInject. Default indexes are nothing special now, and are also registered through dependency injection.
  • Loading branch information
rainydio authored and Brian Faust committed Jan 24, 2020
1 parent d4acdfe commit 2eb295d
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 321 deletions.
12 changes: 5 additions & 7 deletions packages/core-kernel/src/contracts/state/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface WalletIndex {

export type WalletIndexer = (index: WalletIndex, wallet: Wallet) => void;

export type WalletIndexerIndex = { name: string; indexer: WalletIndexer };

export enum WalletIndexes {
Addresses = "addresses",
PublicKeys = "publicKeys",
Expand Down Expand Up @@ -148,10 +150,6 @@ export interface WalletRepository {

reset(): void;

registerIndex(name: string, indexer: WalletIndexer): void;

unregisterIndex(name: string): void;

getIndex(name: string): WalletIndex;

allByAddress(): ReadonlyArray<Wallet>;
Expand All @@ -160,8 +158,6 @@ export interface WalletRepository {

allByUsername(): ReadonlyArray<Wallet>;

findById(id: string): Wallet;

findByAddress(address: string): Wallet;

has(key: string): boolean;
Expand All @@ -174,7 +170,9 @@ export interface WalletRepository {

findByUsername(username: string): Wallet;

findByIndex(index: string | string[], key: string): Wallet;
findByIndex(index: string, key: string): Wallet;

findByIndexes(indexes: string[], key: string): Wallet;

getNonce(publicKey: string): Utils.BigNumber;

Expand Down
1 change: 1 addition & 0 deletions packages/core-kernel/src/ioc/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const Identifiers = {
DatabaseConnection: Symbol.for("Database<Connection>"),
// Repositories
WalletRepository: Symbol.for("Repository<Wallet>"),
WalletRepositoryIndexerIndex: Symbol.for("IndexerIndex<Repository<Wallet>>"),
// Database - @todo add database prefix?
BlockRepository: Symbol.for("Database<BlockRepository>"),
RoundRepository: Symbol.for("Database<RoundRepository>"),
Expand Down
17 changes: 9 additions & 8 deletions packages/core-magistrate-transactions/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { bridgechainIndexer, businessIndexer, MagistrateIndex } from "./wallet-i

export class ServiceProvider extends Providers.ServiceProvider {
public async register(): Promise<void> {
this.registerIndexers();

this.app.bind(Container.Identifiers.TransactionHandler).to(BusinessRegistrationTransactionHandler);
this.app.bind(Container.Identifiers.TransactionHandler).to(BusinessResignationTransactionHandler);
this.app.bind(Container.Identifiers.TransactionHandler).to(BusinessUpdateTransactionHandler);
Expand All @@ -20,14 +22,13 @@ export class ServiceProvider extends Providers.ServiceProvider {
this.app.bind(Container.Identifiers.TransactionHandler).to(BridgechainUpdateTransactionHandler);
}

public async boot(): Promise<void> {
const walletRepository = this.app.getTagged<Contracts.State.WalletRepository>(
Container.Identifiers.WalletRepository,
"state",
"blockchain",
);
private registerIndexers(): void {
this.app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: MagistrateIndex.Businesses, indexer: businessIndexer });

walletRepository.registerIndex(MagistrateIndex.Businesses, businessIndexer);
walletRepository.registerIndex(MagistrateIndex.Bridgechains, bridgechainIndexer);
this.app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: MagistrateIndex.Bridgechains, indexer: bridgechainIndexer });
}
}
35 changes: 35 additions & 0 deletions packages/core-state/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import { StateStore } from "./stores/state";
import { TransactionStore } from "./stores/transactions";
import { TransactionValidator } from "./transaction-validator";
import { TempWalletRepository, Wallet, WalletRepository } from "./wallets";
import {
addressesIndexer,
ipfsIndexer,
locksIndexer,
publicKeysIndexer,
resignationsIndexer,
usernamesIndexer,
} from "./wallets/wallet-indexes";

const dposPreviousRoundStateProvider = (context: Container.interfaces.Context) => {
return async (
Expand All @@ -24,6 +32,7 @@ const dposPreviousRoundStateProvider = (context: Container.interfaces.Context) =
export class ServiceProvider extends Providers.ServiceProvider {
public async register(): Promise<void> {
this.registerFactories();
this.registerIndexers();

this.app
.bind(Container.Identifiers.WalletRepository)
Expand Down Expand Up @@ -79,4 +88,30 @@ export class ServiceProvider extends Providers.ServiceProvider {
),
);
}

private registerIndexers(): void {
this.app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.Addresses, indexer: addressesIndexer });

this.app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.PublicKeys, indexer: publicKeysIndexer });

this.app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.Usernames, indexer: usernamesIndexer });

this.app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.Resignations, indexer: resignationsIndexer });

this.app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.Locks, indexer: locksIndexer });

this.app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.Ipfs, indexer: ipfsIndexer });
}
}
45 changes: 45 additions & 0 deletions packages/core-state/src/wallets/wallet-indexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Contracts } from "@arkecosystem/core-kernel";

export const addressesIndexer = (index: Contracts.State.WalletIndex, wallet: Contracts.State.Wallet) => {
if (wallet.address) {
index.set(wallet.address, wallet);
}
};

export const publicKeysIndexer = (index: Contracts.State.WalletIndex, wallet: Contracts.State.Wallet) => {
if (wallet.publicKey) {
index.set(wallet.publicKey, wallet);
}
};

export const usernamesIndexer = (index: Contracts.State.WalletIndex, wallet: Contracts.State.Wallet) => {
if (wallet.isDelegate()) {
index.set(wallet.getAttribute("delegate.username"), wallet);
}
};

export const resignationsIndexer = (index: Contracts.State.WalletIndex, wallet: Contracts.State.Wallet) => {
if (wallet.isDelegate() && wallet.hasAttribute("delegate.resigned")) {
index.set(wallet.getAttribute("delegate.username"), wallet);
}
};

export const locksIndexer = (index: Contracts.State.WalletIndex, wallet: Contracts.State.Wallet) => {
if (wallet.hasAttribute("htlc.locks")) {
const locks: object = wallet.getAttribute("htlc.locks");

for (const lockId of Object.keys(locks)) {
index.set(lockId, wallet);
}
}
};

export const ipfsIndexer = (index: Contracts.State.WalletIndex, wallet: Contracts.State.Wallet) => {
if (wallet.hasAttribute("ipfs.hashes")) {
const hashes: object = wallet.getAttribute("ipfs.hashes");

for (const hash of Object.keys(hashes)) {
index.set(hash, wallet);
}
}
};
Loading

0 comments on commit 2eb295d

Please sign in to comment.