Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

fix: check nft existence in db #347

Open
wants to merge 1 commit into
base: release-5.6.x
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions plugins/nft/nft-transactions/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export class NftsManager {
return nftRepository();
}

public async exists(tokenId: string) {
public async exists(tokenId: string): Promise<boolean> {
const token = await this.repository.findById(tokenId);
return token && token !== null;
return !!token;
}

public async insert(id: string, ownerId: string) {
Expand Down
14 changes: 3 additions & 11 deletions plugins/nft/nft-transactions/src/transactions/handlers/nft-mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Database, State, TransactionPool } from "@arkecosystem/core-interfaces"
import { Handlers, TransactionReader } from "@arkecosystem/core-transactions";
import { Interfaces, Managers, Transactions } from "@arkecosystem/crypto";
import { getCurrentNftAsset, Transactions as NftTransactions } from "@uns/core-nft-crypto";
import { nftRepository } from "../..";
import { NftOwnedError } from "../../errors";
import { INftWalletAttributes } from "../../interfaces";
import { NftsManager } from "../../manager";
import { addNftToWallet, applyNftMintDb, checkAssetPropertiesSize, removeNftFromWallet } from "./helpers";

Expand Down Expand Up @@ -44,22 +44,14 @@ export class NftMintTransactionHandler extends Handlers.TransactionHandler {
walletManager: State.IWalletManager,
): Promise<void> {
const { tokenId, properties } = getCurrentNftAsset(transaction.data.asset);

const nftManager = app.resolvePlugin<NftsManager>("core-nft");
// check if token is already owned
if (
!!walletManager.allByAddress().find(wallet => {
if (wallet.hasAttribute("tokens")) {
return Object.keys(wallet.getAttribute<INftWalletAttributes>("tokens")).includes(tokenId);
}
return false;
})
) {
if (await nftManager.exists(tokenId)) {
Copy link
Contributor

@dlecan dlecan Sep 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the performance impacts?
Especially when starting a node from scratch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

benched on my local machine:
old version (parse all wallets in ram): 21.223ms
new version (db query) : 4.088ms

throw new NftOwnedError(tokenId);
}

checkAssetPropertiesSize(properties);

const nftManager = app.resolvePlugin<NftsManager>("core-nft");
nftManager.constraints.applyGenesisPropertyConstraint(transaction.data);
await nftManager.constraints.applyConstraints(transaction.data);
return super.throwIfCannotBeApplied(transaction, wallet, walletManager);
Expand Down