Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NODE-2618 Fixed GRPC getNFTList #3904

Merged
merged 8 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.wavesplatform.api.grpc.test
import com.google.protobuf.ByteString
import com.wavesplatform.account.KeyPair
import com.wavesplatform.api.grpc.{AssetInfoResponse, AssetsApiGrpcImpl, NFTRequest, NFTResponse}
import com.wavesplatform.block.Block.ProtoBlockVersion
import com.wavesplatform.db.WithDomain
import com.wavesplatform.db.WithState.AddrWithBalance
import com.wavesplatform.features.BlockchainFeatures
Expand Down Expand Up @@ -55,6 +56,49 @@ class AssetsApiGrpcSpec extends FreeSpec with BeforeAndAfterAll with DiffMatcher
}
}

"NODE-999. GetNftList limit should work properly" in withDomain(
RideV6.addFeatures(BlockchainFeatures.ReduceNFTFee),
AddrWithBalance.enoughBalances(sender)
) { d =>
val nftIssues = (1 to 5).map(idx => TxHelpers.issue(sender, 1, name = s"nft$idx", reissuable = false))
val limit = 2
val afterId = 1 // second element

d.appendBlock()
val mb1 = d.appendMicroBlock(nftIssues.take(afterId + 1)*)
d.appendMicroBlock(nftIssues.drop(afterId + 1)*)

// full liquid
d.rocksDBWriter.containsTransaction(nftIssues(afterId)) shouldBe false
d.rocksDBWriter.containsTransaction(nftIssues(afterId + 1)) shouldBe false
check()

// liquid afterId
d.appendBlock(d.createBlock(ProtoBlockVersion, nftIssues.drop(afterId + 1), Some(mb1)))
d.rocksDBWriter.containsTransaction(nftIssues(afterId)) shouldBe true
d.rocksDBWriter.containsTransaction(nftIssues(afterId + 1)) shouldBe false
check()

// full solid
d.appendBlock()
d.rocksDBWriter.containsTransaction(nftIssues(afterId)) shouldBe true
d.rocksDBWriter.containsTransaction(nftIssues(afterId + 1)) shouldBe true
check()

def check() = {
val (observer, result) = createObserver[NFTResponse]
val request = NFTRequest.of(
ByteString.copyFrom(sender.toAddress.bytes),
limit,
afterAssetId = ByteString.copyFrom(nftIssues(afterId).asset.id.arr)
)
getGrpcApi(d).getNFTList(request, observer)
val response = result.runSyncUnsafe()
response.size shouldBe limit
response.map(_.assetInfo.get.name) shouldBe nftIssues.slice(afterId + 1, afterId + limit + 1).map(_.name.toStringUtf8)
}
}

private def getGrpcApi(d: Domain) =
new AssetsApiGrpcImpl(d.assetsApi, d.accountsApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.utils.ScorexLogging

import java.nio.ByteBuffer
import scala.collection.immutable.VectorMap
import scala.collection.mutable.ArrayBuffer
import scala.jdk.CollectionConverters.*

Expand All @@ -29,9 +30,8 @@ class NFTIterator(addressId: AddressId, maybeAfter: Option[IssuedAsset], resourc
def skipEntry(key: Array[Byte]): Boolean =
!key.endsWith(after.id.arr)

while (dbIterator.isValid && skipEntry(dbIterator.key())) {
while (dbIterator.isValid && skipEntry(dbIterator.key()))
dbIterator.next()
}
if (dbIterator.isValid && !skipEntry(dbIterator.key()))
dbIterator.next()
}
Expand Down Expand Up @@ -99,19 +99,14 @@ class WavesBalanceIterator(addressId: AddressId, resource: DBResource) extends A
class BalanceIterator(
address: Address,
underlying: Iterator[Seq[(IssuedAsset, Long)]],
includeAsset: IssuedAsset => Boolean,
private var pendingOverrides: Map[(Address, Asset), Long]
private var pendingOverrides: VectorMap[(Address, Asset), Long]
) extends AbstractIterator[Seq[(IssuedAsset, Long)]] {

private def nextOverride(): Seq[(IssuedAsset, Long)] =
if (pendingOverrides.isEmpty) endOfData()
else {
val balances = pendingOverrides.collect {
case ((`address`, asset: IssuedAsset), balance) if includeAsset(asset) =>
asset -> balance
}.toSeq
pendingOverrides = Map.empty
balances
val assetsWithBalances = pendingOverrides.collect { case ((`address`, asset: IssuedAsset), balance) => asset -> balance }.toSeq
pendingOverrides = VectorMap.empty
assetsWithBalances
}

override def computeNext(): Seq[(IssuedAsset, Long)] =
Expand All @@ -138,11 +133,15 @@ object AddressPortfolio {
resource
.get(Keys.addressId(address))
.fold(Iterator.empty[Seq[(IssuedAsset, Long)]])(addressId => new NFTIterator(addressId, maybeAfter, resource).asScala),
asset => loadAssetDescription(asset).exists(_.nft),
snapshot.balances
).asScala
.map(_.collect { case (asset, balance) if balance > 0 => asset }
.flatMap(a => loadAssetDescription(a).map(a -> _)))
.map { assets =>
maybeAfter
.filter(after => assets.exists(_._1 == after))
.fold(assets)(after => assets.dropWhile(_._1 != after).drop(1))
.collect { case (asset, balance) if balance > 0 => asset }
.flatMap(asset => loadAssetDescription(asset).collect { case description if description.nft => asset -> description })
}

def assetBalanceIterator(
resource: DBResource,
Expand All @@ -155,10 +154,9 @@ object AddressPortfolio {
resource
.get(Keys.addressId(address))
.fold(Iterator.empty[Seq[(IssuedAsset, Long)]])(addressId => new AssetBalanceIterator(addressId, resource).asScala),
includeAsset,
snapshot.balances
).asScala
.map(_.filter { case (asset, balance) =>
includeAsset(asset) && balance > 0
})
.map(
_.filter { case (asset, balance) => includeAsset(asset) && balance > 0 }
)
}
Loading