Wrong result returned by nft_get_total_supply() #399

Closed
opened 2022-07-13 16:37:53 +00:00 by christophersanborn · 8 comments
christophersanborn commented 2022-07-13 16:37:53 +00:00 (Migrated from gitlab.com)

Database API function nft_get_total_supply() (database_api.cpp#L2977) is returning the chain-wide total of nft_metadata objects instead of the number of NFT objects issued under a particular metadata object.

According to description in database_api.hpp#L985, this function returns the number of minted NFT objects issued under metadata object nft_metadata_id:

   /**
    * @brief Returns total number of NFTs assigned to NFT metadata
    * @param nft_metadata_id NFT metadata ID
    * @return Total number of NFTs assigned to NFT metadata
    */
   uint64_t nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const;

But this is not what it is returning:

uint64_t database_api_impl::nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const {
   const auto &idx_nft_md = _db.get_index_type<nft_metadata_index>().indices().get<by_id>();
   return idx_nft_md.size();
}

Steps to reproduce:

Can be observed by checking metadata object 1.30.8 on the Irona test net via wscat. This metadata object has issued four NFTs, and thus should have a "total supply" of four.

$ wscat -c "wss://irona.peerplays.download/api"
Connected (press CTRL+C to quit)
> {"id":1, "method":"call", "params":[0,"nft_get_total_supply",["1.30.8"]]}
< {"id":1,"jsonrpc":"2.0","result":58}

Expected result:

It is expected that this should return 4, since four NFTs (1.31.x objects) have been issued under 1.30.8:

< {"id":1,"jsonrpc":"2.0","result":4}

Encountered result:

Instead, this is returning 58. 58 happens to be the total number of NFT metadata objects issued on the chain so far (as of this moment), rather that the number of minted NFT objects belonging to that metadata.

< {"id":1,"jsonrpc":"2.0","result":58}
Database API function `nft_get_total_supply()` ([database_api.cpp#L2977](https://gitlab.com/PBSA/peerplays/-/blob/master/libraries/app/database_api.cpp#L2977)) is returning the chain-wide total of nft_metadata objects instead of the number of NFT objects issued under a particular metadata object. According to description in [database_api.hpp#L985](https://gitlab.com/PBSA/peerplays/-/blob/master/libraries/app/include/graphene/app/database_api.hpp#L985), this function returns the number of minted NFT objects issued under metadata object `nft_metadata_id`: ```cpp /** * @brief Returns total number of NFTs assigned to NFT metadata * @param nft_metadata_id NFT metadata ID * @return Total number of NFTs assigned to NFT metadata */ uint64_t nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const; ``` But this is not what it is returning: ```cpp uint64_t database_api_impl::nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const { const auto &idx_nft_md = _db.get_index_type<nft_metadata_index>().indices().get<by_id>(); return idx_nft_md.size(); } ``` ### Steps to reproduce: Can be observed by checking metadata object 1.30.8 on the Irona test net via wscat. This metadata object has issued four NFTs, and thus should have a "total supply" of four. ``` $ wscat -c "wss://irona.peerplays.download/api" Connected (press CTRL+C to quit) > {"id":1, "method":"call", "params":[0,"nft_get_total_supply",["1.30.8"]]} < {"id":1,"jsonrpc":"2.0","result":58} ``` #### Expected result: It is expected that this should return `4`, since four NFTs (1.31.x objects) have been issued under 1.30.8: ``` < {"id":1,"jsonrpc":"2.0","result":4} ``` #### Encountered result: Instead, this is returning `58`. 58 happens to be the total number of NFT metadata objects issued on the chain so far (as of this moment), rather that the number of minted NFT objects belonging to that metadata. ``` < {"id":1,"jsonrpc":"2.0","result":58} ```
christophersanborn commented 2022-07-13 17:58:00 +00:00 (Migrated from gitlab.com)

mentioned in issue PBSA/dapps/trade-hands#18

mentioned in issue PBSA/dapps/trade-hands#18
serkixenos commented 2022-07-20 18:54:01 +00:00 (Migrated from gitlab.com)

Looks like we should replace this:

uint64_t database_api_impl::nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const {
   const auto &idx_nft_md = _db.get_index_type<nft_metadata_index>().indices().get<by_id>();
   return idx_nft_md.size();
}

with this

uint64_t database_api_impl::nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const {
   const auto &idx_nft = _db.get_index_type<nft_index>().indices().get<by_metadata>();
   return idx_nft.count(nft_metadata_id);
}
Looks like we should replace this: ``` uint64_t database_api_impl::nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const { const auto &idx_nft_md = _db.get_index_type<nft_metadata_index>().indices().get<by_id>(); return idx_nft_md.size(); } ``` with this ``` uint64_t database_api_impl::nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const { const auto &idx_nft = _db.get_index_type<nft_index>().indices().get<by_metadata>(); return idx_nft.count(nft_metadata_id); } ```
serkixenos commented 2022-07-21 18:52:06 +00:00 (Migrated from gitlab.com)

mentioned in commit 662139ca22

mentioned in commit 662139ca22c0f683309b0449cd4ea1f57db3174a
serkixenos commented 2022-07-21 19:55:20 +00:00 (Migrated from gitlab.com)

Test steps:

# Create NFT metadata (cli wallet)
nft_metadata_create account01 "NFT" "NFT" "http://url.com" null null false false null null null true

# Create some NFTs (cli wallet)
nft_create account01 1.30.0 account01 account02 "http://url.com" true
nft_create account01 1.30.0 account01 account02 "http://url.com" true
nft_create account01 1.30.0 account01 account02 "http://url.com" true

# Query the count (terminal)
curl --silent --data '{"jsonrpc": "2.0", "method": "nft_get_total_supply", "params": ["1.30.0"], "id": 1}' http://localhost:8090/rpc | jq

# Output should show 3 NFTs
{
  "id": 1,
  "jsonrpc": "2.0",
  "result": 3
}

# Create some NFTs (cli wallet)
nft_create account01 1.30.0 account01 account02 "http://url.com" true
nft_create account01 1.30.0 account01 account02 "http://url.com" true

# Query the count (terminal)
curl --silent --data '{"jsonrpc": "2.0", "method": "nft_get_total_supply", "params": ["1.30.0"], "id": 1}' http://localhost:8090/rpc | jq

# Output should show 5 NFTs
{
  "id": 1,
  "jsonrpc": "2.0",
  "result": 5
}

# Create another metadata (cli wallet)
nft_metadata_create account01 "NFT2" "NFT2" "http://url2.com" null null false false null null null true

# Query the count (terminal)
curl --silent --data '{"jsonrpc": "2.0", "method": "nft_get_total_supply", "params": ["1.30.1"], "id": 1}' http://localhost:8090/rpc | jq

# Output should show 0 NFTs
{
  "id": 1,
  "jsonrpc": "2.0",
  "result": 0
}

# Create some NFTs (cli wallet)
nft_create account01 1.30.1 account01 account02 "http://url.com" true
nft_create account01 1.30.1 account01 account02 "http://url.com" true
nft_create account01 1.30.1 account01 account02 "http://url.com" true
nft_create account01 1.30.1 account01 account02 "http://url.com" true
nft_create account01 1.30.1 account01 account02 "http://url.com" true
nft_create account01 1.30.1 account01 account02 "http://url.com" true
nft_create account01 1.30.1 account01 account02 "http://url.com" true

# Query the count (terminal)
curl --silent --data '{"jsonrpc": "2.0", "method": "nft_get_total_supply", "params": ["1.30.1"], "id": 1}' http://localhost:8090/rpc | jq

# Output should show 7 NFTs
{
  "id": 1,
  "jsonrpc": "2.0",
  "result": 7
}
Test steps: ``` # Create NFT metadata (cli wallet) nft_metadata_create account01 "NFT" "NFT" "http://url.com" null null false false null null null true # Create some NFTs (cli wallet) nft_create account01 1.30.0 account01 account02 "http://url.com" true nft_create account01 1.30.0 account01 account02 "http://url.com" true nft_create account01 1.30.0 account01 account02 "http://url.com" true # Query the count (terminal) curl --silent --data '{"jsonrpc": "2.0", "method": "nft_get_total_supply", "params": ["1.30.0"], "id": 1}' http://localhost:8090/rpc | jq # Output should show 3 NFTs { "id": 1, "jsonrpc": "2.0", "result": 3 } # Create some NFTs (cli wallet) nft_create account01 1.30.0 account01 account02 "http://url.com" true nft_create account01 1.30.0 account01 account02 "http://url.com" true # Query the count (terminal) curl --silent --data '{"jsonrpc": "2.0", "method": "nft_get_total_supply", "params": ["1.30.0"], "id": 1}' http://localhost:8090/rpc | jq # Output should show 5 NFTs { "id": 1, "jsonrpc": "2.0", "result": 5 } # Create another metadata (cli wallet) nft_metadata_create account01 "NFT2" "NFT2" "http://url2.com" null null false false null null null true # Query the count (terminal) curl --silent --data '{"jsonrpc": "2.0", "method": "nft_get_total_supply", "params": ["1.30.1"], "id": 1}' http://localhost:8090/rpc | jq # Output should show 0 NFTs { "id": 1, "jsonrpc": "2.0", "result": 0 } # Create some NFTs (cli wallet) nft_create account01 1.30.1 account01 account02 "http://url.com" true nft_create account01 1.30.1 account01 account02 "http://url.com" true nft_create account01 1.30.1 account01 account02 "http://url.com" true nft_create account01 1.30.1 account01 account02 "http://url.com" true nft_create account01 1.30.1 account01 account02 "http://url.com" true nft_create account01 1.30.1 account01 account02 "http://url.com" true nft_create account01 1.30.1 account01 account02 "http://url.com" true # Query the count (terminal) curl --silent --data '{"jsonrpc": "2.0", "method": "nft_get_total_supply", "params": ["1.30.1"], "id": 1}' http://localhost:8090/rpc | jq # Output should show 7 NFTs { "id": 1, "jsonrpc": "2.0", "result": 7 } ```
serkixenos commented 2022-07-21 19:56:08 +00:00 (Migrated from gitlab.com)

assigned to @serkixenos

assigned to @serkixenos
serkixenos commented 2022-07-21 19:56:21 +00:00 (Migrated from gitlab.com)

assigned to @prandnum

assigned to @prandnum
christophersanborn commented 2022-07-24 20:23:14 +00:00 (Migrated from gitlab.com)

mentioned in issue PBSA/dapps/trade-hands#30

mentioned in issue PBSA/dapps/trade-hands#30
prandnum commented 2022-07-25 19:26:10 +00:00 (Migrated from gitlab.com)

Verified the steps mentioned above.

Verified the steps mentioned above.
prandnum (Migrated from gitlab.com) closed this issue 2022-07-25 19:26:16 +00:00
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: Peerplays_Blockchain/peerplays_migrated#399
No description provided.