From 206d14a8c408c253ac8cfbcfbcfdc362cdc2ec89 Mon Sep 17 00:00:00 2001 From: Sergey Nazarov Date: Mon, 25 Jun 2018 17:51:04 +0300 Subject: [PATCH] NODE-864: Aliases in liquid blocks --- .../wavesplatform/database/LevelDBWriter.scala | 18 +++++++----------- .../com/wavesplatform/state/Blockchain.scala | 4 ++-- .../state/BlockchainUpdaterImpl.scala | 6 +++--- .../com/wavesplatform/state/package.scala | 7 ++++++- .../state/reader/CompositeBlockchain.scala | 14 +++++++------- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/scala/com/wavesplatform/database/LevelDBWriter.scala b/src/main/scala/com/wavesplatform/database/LevelDBWriter.scala index 8908bb6d867..073a1bcc562 100644 --- a/src/main/scala/com/wavesplatform/database/LevelDBWriter.scala +++ b/src/main/scala/com/wavesplatform/database/LevelDBWriter.scala @@ -8,7 +8,7 @@ import com.wavesplatform.settings.FunctionalitySettings import com.wavesplatform.state._ import com.wavesplatform.state.reader.LeaseDetails import org.iq80.leveldb.{DB, ReadOptions} -import scorex.account.{Address, AddressOrAlias, Alias} +import scorex.account.{Address, Alias} import scorex.block.{Block, BlockHeader} import scorex.transaction.Transaction.Type import scorex.transaction.ValidationError.{AliasDoesNotExist, AliasIsDisabled} @@ -513,16 +513,12 @@ class LevelDBWriter(writableDB: DB, fs: FunctionalitySettings, val maxCacheSize: } } - override def resolveAlias(a: AddressOrAlias): Either[ValidationError, Address] = a match { - case addr: Address => Right(addr) - case alias: Alias => - readOnly { db => - if (db.get(Keys.aliasIsDisabled(alias))) Left(AliasIsDisabled(alias)) - else - db.get(Keys.addressIdOfAlias(alias)) - .map(addressId => db.get(Keys.idToAddress(addressId))) - .toRight(AliasDoesNotExist(alias)) - } + override def resolveAlias(alias: Alias): Either[ValidationError, Address] = readOnly { db => + if (db.get(Keys.aliasIsDisabled(alias))) Left(AliasIsDisabled(alias)) + else + db.get(Keys.addressIdOfAlias(alias)) + .map(addressId => db.get(Keys.idToAddress(addressId))) + .toRight(AliasDoesNotExist(alias)) } override def leaseDetails(leaseId: ByteStr): Option[LeaseDetails] = readOnly { db => diff --git a/src/main/scala/com/wavesplatform/state/Blockchain.scala b/src/main/scala/com/wavesplatform/state/Blockchain.scala index 2adcea7f4fd..90f8efa5147 100644 --- a/src/main/scala/com/wavesplatform/state/Blockchain.scala +++ b/src/main/scala/com/wavesplatform/state/Blockchain.scala @@ -1,7 +1,7 @@ package com.wavesplatform.state import com.wavesplatform.state.reader.LeaseDetails -import scorex.account.{Address, AddressOrAlias} +import scorex.account.{Address, AddressOrAlias, Alias} import scorex.block.{Block, BlockHeader} import scorex.transaction.lease.LeaseTransaction import scorex.transaction.smart.script.Script @@ -47,7 +47,7 @@ trait Blockchain { def assetDescription(id: ByteStr): Option[AssetDescription] - def resolveAlias(a: AddressOrAlias): Either[ValidationError, Address] + def resolveAlias(a: Alias): Either[ValidationError, Address] def leaseDetails(leaseId: ByteStr): Option[LeaseDetails] diff --git a/src/main/scala/com/wavesplatform/state/BlockchainUpdaterImpl.scala b/src/main/scala/com/wavesplatform/state/BlockchainUpdaterImpl.scala index 9928305a88b..44b89bece68 100644 --- a/src/main/scala/com/wavesplatform/state/BlockchainUpdaterImpl.scala +++ b/src/main/scala/com/wavesplatform/state/BlockchainUpdaterImpl.scala @@ -12,7 +12,7 @@ import com.wavesplatform.utils.{UnsupportedFeature, forceStopApplication} import kamon.Kamon import monix.reactive.Observable import monix.reactive.subjects.ConcurrentSubject -import scorex.account.{Address, AddressOrAlias} +import scorex.account.{Address, Alias} import scorex.block.Block.BlockId import scorex.block.{Block, BlockHeader, MicroBlock} import scorex.transaction.Transaction.Type @@ -416,8 +416,8 @@ class BlockchainUpdaterImpl(blockchain: Blockchain, settings: WavesSettings, tim CompositeBlockchain.composite(blockchain, diff).assetDescription(id) } - override def resolveAlias(a: AddressOrAlias): Either[ValidationError, Address] = ngState.fold(blockchain.resolveAlias(a)) { ng => - CompositeBlockchain.composite(blockchain, ng.bestLiquidDiff).resolveAlias(a) + override def resolveAlias(alias: Alias): Either[ValidationError, Address] = ngState.fold(blockchain.resolveAlias(alias)) { ng => + CompositeBlockchain.composite(blockchain, ng.bestLiquidDiff).resolveAlias(alias) } override def leaseDetails(leaseId: AssetId): Option[LeaseDetails] = ngState match { diff --git a/src/main/scala/com/wavesplatform/state/package.scala b/src/main/scala/com/wavesplatform/state/package.scala index 6e170dfdac9..eec368d481c 100644 --- a/src/main/scala/com/wavesplatform/state/package.scala +++ b/src/main/scala/com/wavesplatform/state/package.scala @@ -1,6 +1,6 @@ package com.wavesplatform -import scorex.account.{Address, Alias} +import scorex.account.{Address, AddressOrAlias, Alias} import scorex.block.Block import scorex.transaction.ValidationError.{AliasDoesNotExist, GenericError} import scorex.transaction._ @@ -53,6 +53,11 @@ package object state { def genesis: Block = blockchain.blockAt(1).get + def resolveAlias(a: AddressOrAlias): Either[ValidationError, Address] = a match { + case addr: Address => Right(addr) + case alias: Alias => blockchain.resolveAlias(alias) + } + def canCreateAlias(alias: Alias): Boolean = blockchain.resolveAlias(alias) match { case Left(AliasDoesNotExist(_)) => true case _ => false diff --git a/src/main/scala/com/wavesplatform/state/reader/CompositeBlockchain.scala b/src/main/scala/com/wavesplatform/state/reader/CompositeBlockchain.scala index 3554822ece0..7c91670ade5 100644 --- a/src/main/scala/com/wavesplatform/state/reader/CompositeBlockchain.scala +++ b/src/main/scala/com/wavesplatform/state/reader/CompositeBlockchain.scala @@ -3,10 +3,10 @@ package com.wavesplatform.state.reader import cats.implicits._ import cats.kernel.Monoid import com.wavesplatform.state._ -import scorex.account.{Address, AddressOrAlias} +import scorex.account.{Address, AddressOrAlias, Alias} import scorex.block.{Block, BlockHeader} import scorex.transaction.Transaction.Type -import scorex.transaction.ValidationError.AliasDoesNotExist +import scorex.transaction.ValidationError.{AliasDoesNotExist, AliasIsDisabled} import scorex.transaction.assets.IssueTransaction import scorex.transaction.lease.LeaseTransaction import scorex.transaction.smart.script.Script @@ -92,11 +92,11 @@ class CompositeBlockchain(inner: Blockchain, maybeDiff: => Option[Diff]) extends } } - override def resolveAlias(a: AddressOrAlias): Either[ValidationError, Address] = - inner.resolveAlias(a).left.flatMap { - case adne @ AliasDoesNotExist(alias) => diff.aliases.get(alias).toRight(adne) - case other => Left(other) - } + override def resolveAlias(alias: Alias): Either[ValidationError, Address] = inner.resolveAlias(alias) match { + case l @ Left(AliasIsDisabled(_)) => l + case Right(addr) => Right(diff.aliases.getOrElse(alias, addr)) + case Left(adne: AliasDoesNotExist) => diff.aliases.get(alias).toRight(adne) + } override def allActiveLeases: Set[LeaseTransaction] = { val (active, canceled) = diff.leaseState.partition(_._2)