diff --git a/node/src/main/scala/com/wavesplatform/database/Caches.scala b/node/src/main/scala/com/wavesplatform/database/Caches.scala index 2abfc164c7..3a579244b5 100644 --- a/node/src/main/scala/com/wavesplatform/database/Caches.scala +++ b/node/src/main/scala/com/wavesplatform/database/Caches.scala @@ -318,7 +318,8 @@ abstract class Caches extends Blockchain with Storage { for (((address, _), (entry, _)) <- updatedDataWithNodes) stateHash.addDataEntry(address, entry.entry) for ((address, lease) <- leaseBalances) stateHash.addLeaseBalance(address, lease.in, lease.out) for ((address, script) <- snapshot.accountScriptsByAddress) stateHash.addAccountScript(address, script.map(_.script)) - for ((asset, script) <- snapshot.assetScripts) stateHash.addAssetScript(asset, script.map(_.script)) + for ((asset, script) <- snapshot.assetScripts) stateHash.addAssetScript(asset, Some(script.script)) + for ((asset, _) <- snapshot.assetStatics) if (!snapshot.assetScripts.contains(asset)) stateHash.addAssetScript(asset, None) for ((leaseId, lease) <- snapshot.leaseStates) stateHash.addLeaseStatus(leaseId, lease.isActive) for ((assetId, sponsorship) <- snapshot.sponsorships) stateHash.addSponsorship(assetId, sponsorship.minFee) for ((alias, address) <- snapshot.aliases) stateHash.addAlias(address, alias.name) @@ -352,7 +353,7 @@ abstract class Caches extends Blockchain with Storage { for ((alias, address) <- snapshot.aliases) aliasCache.put(Alias.create(alias.name).explicitGet(), Some(address)) leaseBalanceCache.putAll(leaseBalances.asJava) scriptCache.putAll(snapshot.accountScriptsByAddress.asJava) - assetScriptCache.putAll(snapshot.assetScripts.asJava) + assetScriptCache.putAll(snapshot.assetScripts.view.mapValues(Some(_)).toMap.asJava) accountDataCache.putAll(updatedDataWithNodes.map { case (key, (value, _)) => (key, value) }.asJava) } diff --git a/node/src/main/scala/com/wavesplatform/database/RocksDBWriter.scala b/node/src/main/scala/com/wavesplatform/database/RocksDBWriter.scala index b7c3c5693c..33579e15c5 100644 --- a/node/src/main/scala/com/wavesplatform/database/RocksDBWriter.scala +++ b/node/src/main/scala/com/wavesplatform/database/RocksDBWriter.scala @@ -497,7 +497,7 @@ class RocksDBWriter( for ((asset, script) <- snapshot.assetScripts) { expiredKeys ++= updateHistory(rw, Keys.assetScriptHistory(asset), threshold, Keys.assetScript(asset)) - if (script.isDefined) rw.put(Keys.assetScript(asset)(height), script) + rw.put(Keys.assetScript(asset)(height), Some(script)) } if (height % BlockStep == 1) { diff --git a/node/src/main/scala/com/wavesplatform/state/StateSnapshot.scala b/node/src/main/scala/com/wavesplatform/state/StateSnapshot.scala index e34d0f63b1..bbf9912715 100644 --- a/node/src/main/scala/com/wavesplatform/state/StateSnapshot.scala +++ b/node/src/main/scala/com/wavesplatform/state/StateSnapshot.scala @@ -28,7 +28,7 @@ case class StateSnapshot( assetStatics: VectorMap[IssuedAsset, AssetStatic] = VectorMap(), assetVolumes: Map[IssuedAsset, AssetVolumeInfo] = Map(), assetNamesAndDescriptions: Map[IssuedAsset, AssetInfo] = Map(), - assetScripts: Map[IssuedAsset, Option[AssetScriptInfo]] = Map(), + assetScripts: Map[IssuedAsset, AssetScriptInfo] = Map(), sponsorships: Map[IssuedAsset, SponsorshipValue] = Map(), leaseStates: Map[ByteStr, LeaseDetails] = Map(), aliases: Map[Alias, Address] = Map(), @@ -57,7 +57,7 @@ case class StateSnapshot( S.AssetNameAndDescription(asset.id.toByteString, info.name.toStringUtf8, info.description.toStringUtf8, info.lastUpdatedAt) }.toSeq, assetScripts.map { case (asset, script) => - S.AssetScript(asset.id.toByteString, script.fold(ByteString.EMPTY)(_.script.bytes().toByteString)) + S.AssetScript(asset.id.toByteString, script.script.bytes().toByteString) }.toSeq, aliases.map { case (alias, address) => S.Alias(address.toByteString, alias.name) }.toSeq, orderFills.map { case (orderId, VolumeAndFee(volume, fee)) => @@ -148,14 +148,9 @@ object StateSnapshot { .map(b => b.address.toAddress -> LeaseBalance(b.in, b.out)) .toMap - val assetScripts: Map[IssuedAsset, Option[AssetScriptInfo]] = + val assetScripts: Map[IssuedAsset, AssetScriptInfo] = pbSnapshot.assetScripts.map { s => - val info = - if (s.script.isEmpty) - None - else - Some(AssetScriptInfo(ScriptReader.fromBytes(s.script.toByteArray).explicitGet(), 0)) - s.assetId.toIssuedAssetId -> info + s.assetId.toIssuedAssetId -> AssetScriptInfo(ScriptReader.fromBytes(s.script.toByteArray).explicitGet(), 0) }.toMap val assetStatics: VectorMap[IssuedAsset, AssetStatic] = @@ -257,7 +252,7 @@ object StateSnapshot { orderFills: Map[ByteStr, VolumeAndFee] = Map(), issuedAssets: VectorMap[IssuedAsset, NewAssetInfo] = VectorMap(), updatedAssets: Map[IssuedAsset, Ior[AssetInfo, AssetVolumeInfo]] = Map(), - assetScripts: Map[IssuedAsset, Option[AssetScriptInfo]] = Map(), + assetScripts: Map[IssuedAsset, AssetScriptInfo] = Map(), sponsorships: Map[IssuedAsset, Sponsorship] = Map(), leaseStates: Map[ByteStr, LeaseDetails] = Map(), aliases: Map[Alias, Address] = Map(), diff --git a/node/src/main/scala/com/wavesplatform/state/TxStateSnapshotHashBuilder.scala b/node/src/main/scala/com/wavesplatform/state/TxStateSnapshotHashBuilder.scala index 92de0b975f..c5c60a8bbe 100644 --- a/node/src/main/scala/com/wavesplatform/state/TxStateSnapshotHashBuilder.scala +++ b/node/src/main/scala/com/wavesplatform/state/TxStateSnapshotHashBuilder.scala @@ -71,9 +71,8 @@ object TxStateSnapshotHashBuilder { } for { - (asset, sv) <- snapshot.assetScripts - script = sv.map(_.script) - } addEntry(KeyType.AssetScript, asset.id.arr)(script.fold(Array.emptyByteArray)(_.bytes().arr)) + (asset, scriptInfo) <- snapshot.assetScripts + } addEntry(KeyType.AssetScript, asset.id.arr)(scriptInfo.script.bytes().arr) snapshot.leaseStates.foreach { case (leaseId, details) => addEntry(KeyType.LeaseStatus, leaseId.arr)(booleanToBytes(details.isActive)) diff --git a/node/src/main/scala/com/wavesplatform/state/diffs/AssetTransactionsDiffs.scala b/node/src/main/scala/com/wavesplatform/state/diffs/AssetTransactionsDiffs.scala index 4a494c5be1..e2fca02202 100644 --- a/node/src/main/scala/com/wavesplatform/state/diffs/AssetTransactionsDiffs.scala +++ b/node/src/main/scala/com/wavesplatform/state/diffs/AssetTransactionsDiffs.scala @@ -79,7 +79,9 @@ object AssetTransactionsDiffs { _ <- checkSize(blockchain, tx.script) snapshot <- StateSnapshot.build( blockchain, - assetScripts = Map(tx.asset -> script.map(AssetScriptInfo.tupled)), + assetScripts = script.fold(Map[IssuedAsset, AssetScriptInfo]()) { case (script, complexity) => + Map(tx.asset -> AssetScriptInfo(script, complexity)) + }, portfolios = Map(tx.sender.toAddress -> Portfolio(-tx.fee.value)) ) } yield snapshot @@ -120,7 +122,9 @@ object AssetTransactionsDiffs { _ <- checkEstimationOverflow(blockchain, script) snapshot <- StateSnapshot.build( blockchain, - assetScripts = Map(asset -> script.map(AssetScriptInfo.tupled)), + assetScripts = script.fold(Map[IssuedAsset, AssetScriptInfo]()) { case (script, complexity) => + Map(asset -> AssetScriptInfo(script, complexity)) + }, issuedAssets = VectorMap(asset -> NewAssetInfo(assetStatic, assetInfo, assetVolume)), portfolios = VectorMap(tx.sender.toAddress -> Portfolio.build(-tx.fee.value, asset, tx.quantity.value)) ) diff --git a/node/src/main/scala/com/wavesplatform/state/diffs/invoke/InvokeDiffsCommon.scala b/node/src/main/scala/com/wavesplatform/state/diffs/invoke/InvokeDiffsCommon.scala index 82144dacff..c8cba5acea 100644 --- a/node/src/main/scala/com/wavesplatform/state/diffs/invoke/InvokeDiffsCommon.scala +++ b/node/src/main/scala/com/wavesplatform/state/diffs/invoke/InvokeDiffsCommon.scala @@ -589,8 +589,7 @@ object InvokeDiffsCommon { StateSnapshot.build( blockchain, portfolios = Map(pk.toAddress -> Portfolio(assets = VectorMap(asset -> issue.quantity))), - issuedAssets = VectorMap(asset -> NewAssetInfo(staticInfo, info, volumeInfo)), - assetScripts = Map(asset -> None) + issuedAssets = VectorMap(asset -> NewAssetInfo(staticInfo, info, volumeInfo)) ) } } diff --git a/node/src/main/scala/com/wavesplatform/state/reader/SnapshotBlockchain.scala b/node/src/main/scala/com/wavesplatform/state/reader/SnapshotBlockchain.scala index 7aaa02e57a..76fa0a2ccf 100644 --- a/node/src/main/scala/com/wavesplatform/state/reader/SnapshotBlockchain.scala +++ b/node/src/main/scala/com/wavesplatform/state/reader/SnapshotBlockchain.scala @@ -79,7 +79,7 @@ case class SnapshotBlockchain( override def assetScript(asset: IssuedAsset): Option[AssetScriptInfo] = maybeSnapshot .flatMap(_.assetScripts.get(asset)) - .getOrElse(inner.assetScript(asset)) + .orElse(inner.assetScript(asset)) override def assetDescription(asset: IssuedAsset): Option[AssetDescription] = SnapshotBlockchain.assetDescription(asset, snapshot, height, inner) @@ -267,7 +267,7 @@ object SnapshotBlockchain { volume.get.isReissuable, volume.get.volume, info.get.lastUpdatedAt, - script.flatten, + script, sponsorship.getOrElse(0), static.nft, assetNum, @@ -285,7 +285,7 @@ object SnapshotBlockchain { description = info.map(_.description).getOrElse(d.description), lastUpdatedAt = info.map(_.lastUpdatedAt).getOrElse(d.lastUpdatedAt), sponsorship = sponsorship.getOrElse(d.sponsorship), - script = script.getOrElse(d.script) + script = script.orElse(d.script) ) ) ) diff --git a/node/src/test/scala/com/wavesplatform/history/SnapshotOps.scala b/node/src/test/scala/com/wavesplatform/history/SnapshotOps.scala index 3a78de1e58..d1d6195c51 100644 --- a/node/src/test/scala/com/wavesplatform/history/SnapshotOps.scala +++ b/node/src/test/scala/com/wavesplatform/history/SnapshotOps.scala @@ -23,7 +23,7 @@ object SnapshotOps { orderFills(blockchain), s.leaseStates, s.accountScripts, - s.assetScripts, + s.assetScripts.view.mapValues(Some(_)).toMap, s.accountData, s.sponsorships, scriptsRun = 0, @@ -94,7 +94,7 @@ object SnapshotOps { diff.orderFills, diff.issuedAssets, diff.updatedAssets, - diff.assetScripts, + diff.assetScripts.collect { case (asset, Some(info)) => (asset, info) }, diff.sponsorship, diff.leaseState, diff.aliases, diff --git a/node/src/test/scala/com/wavesplatform/state/snapshot/StateSnapshotProtoTest.scala b/node/src/test/scala/com/wavesplatform/state/snapshot/StateSnapshotProtoTest.scala index def8a6c916..7f2d5ae0a1 100644 --- a/node/src/test/scala/com/wavesplatform/state/snapshot/StateSnapshotProtoTest.scala +++ b/node/src/test/scala/com/wavesplatform/state/snapshot/StateSnapshotProtoTest.scala @@ -53,8 +53,7 @@ class StateSnapshotProtoTest extends PropSpec { IssuedAsset(ByteStr.fromBytes(2, 2, 2)) -> AssetInfo("name2", "desc2", Height @@ 999) ), Map( - IssuedAsset(ByteStr.fromBytes(1, 1, 1)) -> None, - IssuedAsset(ByteStr.fromBytes(2, 2, 2)) -> Some(AssetScriptInfo(TestCompiler(V6).compileAsset("this != this"), 0)) + IssuedAsset(ByteStr.fromBytes(2, 2, 2)) -> AssetScriptInfo(TestCompiler(V6).compileAsset("this != this"), 0) ), Map( IssuedAsset(ByteStr.fromBytes(1, 1, 1)) -> SponsorshipValue(99999), diff --git a/node/src/test/scala/com/wavesplatform/state/snapshot/StateSnapshotStorageTest.scala b/node/src/test/scala/com/wavesplatform/state/snapshot/StateSnapshotStorageTest.scala index bab0dacaef..34a4c6c90a 100644 --- a/node/src/test/scala/com/wavesplatform/state/snapshot/StateSnapshotStorageTest.scala +++ b/node/src/test/scala/com/wavesplatform/state/snapshot/StateSnapshotStorageTest.scala @@ -104,7 +104,7 @@ class StateSnapshotStorageTest extends PropSpec with WithDomain { asset -> AssetInfo(issueTx.name, issueTx.description, Height(d.solidStateHeight + 2)) ), assetScripts = Map( - asset -> Some(AssetScriptInfo(script, 0)) + asset -> AssetScriptInfo(script, 0) ) ) ) @@ -288,7 +288,7 @@ class StateSnapshotStorageTest extends PropSpec with WithDomain { (senderAddress, Waves) -> (d.balance(senderAddress) - 1.waves) ), assetScripts = Map( - asset -> Some(AssetScriptInfo(script, 0)) + asset -> AssetScriptInfo(script, 0) ) ) ) @@ -332,9 +332,6 @@ class StateSnapshotStorageTest extends PropSpec with WithDomain { assetNamesAndDescriptions = Map( dAppAssetId -> AssetInfo("name", "description", Height(height)) ), - assetScripts = Map( - dAppAssetId -> None - ), leaseStates = Map( leaseId -> LeaseDetails(dAppPk, senderAddress, 123, Active, invokeId, height) ),