Skip to content

Commit

Permalink
fix(core-magistrate): fix entity bootstrap methods (#3851)
Browse files Browse the repository at this point in the history
  • Loading branch information
air1one authored Jun 30, 2020
1 parent fcde954 commit 89a7176
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Models } from "@arkecosystem/core-database";
import { Container, Contracts, Utils } from "@arkecosystem/core-kernel";
import { IEntityAsset } from "@arkecosystem/core-magistrate-crypto/dist/interfaces";
import { TransactionReader } from "@arkecosystem/core-transactions";
import { Interfaces } from "@arkecosystem/crypto";

import { EntityAlreadyRegisteredError, EntityNameAlreadyRegisteredError } from "../../errors";
Expand All @@ -14,18 +13,16 @@ import { MagistrateIndex } from "../../wallet-indexes";
export class EntityRegisterSubHandler {
public async bootstrap(
walletRepository: Contracts.State.WalletRepository,
reader: TransactionReader,
transactions: Models.Transaction[],
): Promise<void> {
const transactions: Models.Transaction[] = await reader.read();

for (const transaction of transactions) {
const wallet: Contracts.State.Wallet = walletRepository.findByPublicKey(transaction.senderPublicKey);
const entities: IEntitiesWallet = wallet.getAttribute<IEntitiesWallet>("entities", {});

entities[transaction.id] = {
type: entities[transaction.asset.registrationId].type,
subType: entities[transaction.asset.registrationId].subType,
data: entities[transaction.asset.registrationId].data,
type: transaction.asset.type,
subType: transaction.asset.subType,
data: transaction.asset.data,
};

wallet.setAttribute("entities", entities);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Models } from "@arkecosystem/core-database";
import { Container, Contracts, Utils } from "@arkecosystem/core-kernel";
import { TransactionReader } from "@arkecosystem/core-transactions";
import { Interfaces } from "@arkecosystem/crypto";

import {
Expand All @@ -17,10 +16,8 @@ import { IEntitiesWallet } from "../../interfaces";
export class EntityResignSubHandler {
public async bootstrap(
walletRepository: Contracts.State.WalletRepository,
reader: TransactionReader,
transactions: Models.Transaction[],
): Promise<void> {
const transactions: Models.Transaction[] = await reader.read();

for (const transaction of transactions) {
const wallet: Contracts.State.Wallet = walletRepository.findByPublicKey(transaction.senderPublicKey);
const entities: IEntitiesWallet = wallet.getAttribute<IEntitiesWallet>("entities");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Models } from "@arkecosystem/core-database";
import { Container, Contracts, Utils } from "@arkecosystem/core-kernel";
import { Enums } from "@arkecosystem/core-magistrate-crypto";
import { IEntityAsset, IEntityAssetData } from "@arkecosystem/core-magistrate-crypto/dist/interfaces";
import { TransactionReader } from "@arkecosystem/core-transactions";
import { Interfaces } from "@arkecosystem/crypto";

import {
Expand All @@ -22,10 +21,8 @@ export class EntityUpdateSubHandler {

public async bootstrap(
walletRepository: Contracts.State.WalletRepository,
reader: TransactionReader,
transactions: Models.Transaction[],
): Promise<void> {
const transactions: Models.Transaction[] = await reader.read();

for (const transaction of transactions) {
const wallet: Contracts.State.Wallet = walletRepository.findByPublicKey(transaction.senderPublicKey);
const entities: IEntitiesWallet = wallet.getAttribute<IEntitiesWallet>("entities");
Expand Down
29 changes: 27 additions & 2 deletions packages/core-magistrate-transactions/src/handlers/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Interfaces as MagistrateInterfaces,
Transactions as MagistrateTransactions,
} from "@arkecosystem/core-magistrate-crypto";
import { Handlers } from "@arkecosystem/core-transactions";
import { Handlers, TransactionReader } from "@arkecosystem/core-transactions";
import { Interfaces as CryptoInterfaces, Interfaces, Transactions, Utils, Managers } from "@arkecosystem/crypto";

import { EntityWrongSubTypeError, StaticFeeMismatchError } from "../errors";
Expand All @@ -14,6 +14,7 @@ import BusinessSubHandlers from "./entity-subhandlers/business";
import DeveloperSubHandlers from "./entity-subhandlers/developer";
import PluginCoreSubHandlers from "./entity-subhandlers/plugin-core";
import PluginDesktopSubHandlers from "./entity-subhandlers/plugin-desktop";
import { Models } from "@arkecosystem/core-database";

type SubHandlers = {
[Enums.EntityAction.Register]: EntityRegisterSubHandler;
Expand Down Expand Up @@ -63,7 +64,31 @@ export class EntityTransactionHandler extends Handlers.TransactionHandler {
return ["entities"];
}

public async bootstrap(): Promise<void> {}
public async bootstrap(): Promise<void> {
const reader: TransactionReader = this.getTransactionReader();
const transactions: Models.Transaction[] = await reader.read();

const registerTransactions: Models.Transaction[] = [];
const updateTransactions: Models.Transaction[] = [];
const resignTransactions: Models.Transaction[] = [];
for (const transaction of transactions) {
switch (transaction.asset.action) {
case Enums.EntityAction.Register:
registerTransactions.push(transaction);
break;
case Enums.EntityAction.Update:
updateTransactions.push(transaction);
break;
case Enums.EntityAction.Resign:
resignTransactions.push(transaction);
break;
}
}

await this.app.resolve(EntityRegisterSubHandler).bootstrap(this.walletRepository, registerTransactions);
await this.app.resolve(EntityUpdateSubHandler).bootstrap(this.walletRepository, updateTransactions);
await this.app.resolve(EntityResignSubHandler).bootstrap(this.walletRepository, resignTransactions);
}

public async throwIfCannotBeApplied(
transaction: CryptoInterfaces.ITransaction,
Expand Down

0 comments on commit 89a7176

Please sign in to comment.