Skip to content

Commit

Permalink
refactor(core-state): wallet repositories (#3501)
Browse files Browse the repository at this point in the history
  • Loading branch information
rainydio authored Feb 14, 2020
1 parent c558ddd commit b87899a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 112 deletions.
4 changes: 2 additions & 2 deletions packages/core-state/src/dpos/dpos-previous-round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { Interfaces } from "@arkecosystem/crypto";
@Container.injectable()
export class DposPreviousRoundState implements Contracts.State.DposPreviousRoundState {
@Container.inject(Container.Identifiers.BlockState)
@Container.tagged("state", "temp")
@Container.tagged("state", "clone")
private readonly blockState!: Contracts.State.BlockState;

@Container.inject(Container.Identifiers.DposState)
@Container.tagged("state", "temp")
@Container.tagged("state", "clone")
private readonly dposState!: Contracts.State.DposState;

public async revert(blocks: Interfaces.IBlock[], roundInfo: Contracts.Shared.RoundInfo): Promise<void> {
Expand Down
12 changes: 9 additions & 3 deletions packages/core-state/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BlockStore } from "./stores/blocks";
import { StateStore } from "./stores/state";
import { TransactionStore } from "./stores/transactions";
import { TransactionValidator } from "./transaction-validator";
import { TempWalletRepository, Wallet, WalletRepository } from "./wallets";
import { Wallet, WalletRepository, WalletRepositoryClone, WalletRepositoryCopyOnWrite } from "./wallets";
import {
addressesIndexer,
ipfsIndexer,
Expand Down Expand Up @@ -42,9 +42,15 @@ export class ServiceProvider extends Providers.ServiceProvider {

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

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

this.app.bind(Container.Identifiers.DposState).to(DposState);
this.app.bind(Container.Identifiers.BlockState).to(BlockState);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-state/src/transaction-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { strictEqual } from "assert";
@Container.injectable()
export class TransactionValidator implements Contracts.State.TransactionValidator {
@Container.inject(Container.Identifiers.TransactionHandlerRegistry)
@Container.tagged("state", "temp")
@Container.tagged("state", "clone")
private readonly handlerRegistry!: Handlers.Registry;

public async validate(transaction: Interfaces.ITransaction): Promise<void> {
Expand Down
3 changes: 2 additions & 1 deletion packages/core-state/src/wallets/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./temp-wallet-repository";
export * from "./wallet-repository";
export * from "./wallet-repository-clone";
export * from "./wallet-repository-copy-on-write";
export * from "./wallet";
93 changes: 0 additions & 93 deletions packages/core-state/src/wallets/temp-wallet-repository.ts

This file was deleted.

21 changes: 21 additions & 0 deletions packages/core-state/src/wallets/wallet-repository-clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Container, Contracts } from "@arkecosystem/core-kernel";

import { WalletRepository } from "./wallet-repository";

@Container.injectable()
export class WalletRepositoryClone extends WalletRepository {
@Container.inject(Container.Identifiers.WalletRepository)
@Container.tagged("state", "blockchain")
private readonly blockchainWalletRepository!: Contracts.State.WalletRepository;

@Container.postConstruct()
public initialize(): void {
for (const index of this.blockchainWalletRepository.getIndexNames()) {
this.indexes[index] = this.blockchainWalletRepository.getIndex(index).clone();
}
}

public reindex(wallet: Contracts.State.Wallet): void {
super.reindex(wallet.clone());
}
}
43 changes: 43 additions & 0 deletions packages/core-state/src/wallets/wallet-repository-copy-on-write.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Container, Contracts } from "@arkecosystem/core-kernel";

import { WalletRepository } from "./wallet-repository";

// ! This isn't copy-on-write, but copy-on-read and with many asterisks.
// ! It only covers current pool use-cases.
// ! It should be replaced with proper implementation eventually.

@Container.injectable()
export class WalletRepositoryCopyOnWrite extends WalletRepository {
@Container.inject(Container.Identifiers.WalletRepository)
@Container.tagged("state", "blockchain")
private readonly blockchainWalletRepository!: Contracts.State.WalletRepository;

public findByAddress(address: string): Contracts.State.Wallet {
if (address && !this.hasByAddress(address)) {
const walletClone = this.blockchainWalletRepository.findByAddress(address).clone();
this.reindex(walletClone);
}
return this.findByIndex(Contracts.State.WalletIndexes.Addresses, address)!;
}

public hasByIndex(index: string, key: string): boolean {
if (super.hasByIndex(index, key)) {
return true;
}
if (this.blockchainWalletRepository.hasByIndex(index, key) === false) {
return false;
}
const walletClone = this.blockchainWalletRepository.findByIndex(index, key).clone();
this.reindex(walletClone);
return true;
}

public allByUsername(): ReadonlyArray<Contracts.State.Wallet> {
for (const wallet of this.blockchainWalletRepository.allByUsername()) {
if (super.hasByAddress(wallet.address) === false) {
this.reindex(wallet.clone());
}
}
return super.allByUsername();
}
}
18 changes: 6 additions & 12 deletions packages/core-transaction-pool/src/connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Container, Contracts, Enums as AppEnums, Providers, Utils as AppUtils } from "@arkecosystem/core-kernel";
import { Wallets } from "@arkecosystem/core-state";
import { Handlers } from "@arkecosystem/core-transactions";
import { Enums, Interfaces, Transactions, Utils } from "@arkecosystem/crypto";
import { strictEqual } from "assert";
Expand Down Expand Up @@ -533,10 +532,7 @@ export class Connection implements Contracts.TransactionPool.Connection {
* @returns {Promise<Interfaces.ITransaction[]>}
* @memberof Connection
*/
private async validateTransactions(
transactions: Interfaces.ITransaction[],
walletRepository?: Wallets.TempWalletRepository,
): Promise<Interfaces.ITransaction[]> {
private async validateTransactions(transactions: Interfaces.ITransaction[]): Promise<Interfaces.ITransaction[]> {
const validTransactions: Interfaces.ITransaction[] = [];
const forgedIds: string[] = await this.cleaner.removeForgedTransactions(transactions);

Expand All @@ -546,13 +542,11 @@ export class Connection implements Contracts.TransactionPool.Connection {
(t, forgedId) => t.id === forgedId,
);

if (walletRepository === undefined) {
walletRepository = this.app.getTagged<Wallets.TempWalletRepository>(
Container.Identifiers.WalletRepository,
"state",
"temp",
);
}
const walletRepository = this.app.getTagged<Contracts.State.WalletRepository>(
Container.Identifiers.WalletRepository,
"state",
"clone",
);

for (const transaction of unforgedTransactions) {
try {
Expand Down

0 comments on commit b87899a

Please sign in to comment.