Skip to content

Commit

Permalink
NODE-2354: Remove pessimistic portfolio (UTX pool) (wavesplatform#3558)
Browse files Browse the repository at this point in the history
  • Loading branch information
Karasiq authored Oct 18, 2021
1 parent 3ca899f commit 88d076d
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 489 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -933,11 +933,6 @@ object AsyncHttpApi extends Assertions {
implicit val leaseBalanceFormat: Reads[LeaseBalance] = Json.reads[LeaseBalance]
implicit val portfolioFormat: Reads[Portfolio] = Json.reads[Portfolio]

def debugPortfoliosFor(address: String, considerUnspent: Boolean, amountsAsStrings: Boolean = false): Future[Portfolio] = {
get(s"/debug/portfolios/$address?considerUnspent=$considerUnspent", withApiKey = true, amountsAsStrings = amountsAsStrings)
.as[Portfolio](amountsAsStrings)
}

def debugMinerInfo(): Future[Seq[State]] = getWithApiKey(s"/debug/minerInfo").as[Seq[State]]

def transactionSerializer(body: JsObject): Future[TransactionSerialize] =
Expand All @@ -958,7 +953,7 @@ object AsyncHttpApi extends Assertions {

def assertBalances(acc: String, balance: Long, effectiveBalance: Long)(implicit pos: Position): Future[Unit] =
for {
newBalance <- balanceDetails(acc)
newBalance <- balanceDetails(acc)
} yield {
withClue(s"effective balance of $acc") {
newBalance.effective shouldBe effectiveBalance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@ object SyncHttpApi extends Assertions with matchers.should.Matchers {
def assetDistribution(asset: String): AssetDistribution =
sync(async(n).assetDistribution(asset))

def debugPortfoliosFor(address: String, considerUnspent: Boolean, amountsAsStrings: Boolean = false): Portfolio =
sync(async(n).debugPortfoliosFor(address, considerUnspent, amountsAsStrings))

def broadcastIssue(
source: KeyPair,
name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,6 @@ class AmountAsStringSuite extends BaseTransactionSuite with OverflowBlock {

test("amount as string in debug api") {
val firstBalance = sender.balanceDetails(firstAddress).available
val portfolio = sender.debugPortfoliosFor(firstAddress, considerUnspent = false, amountsAsStrings = true)
portfolio.balance shouldBe firstBalance
portfolio.lease.in shouldBe 0
portfolio.lease.out shouldBe 0

sender.debugBalanceHistory(firstAddress, amountsAsStrings = true).head.balance shouldBe firstBalance

Expand Down

This file was deleted.

57 changes: 0 additions & 57 deletions node/src/main/resources/swagger-ui/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2579,63 +2579,6 @@ paths:
security:
- APIKey: []
x-codegen-request-body-name: address
'/debug/portfolios/{address}':
get:
tags:
- debug
summary: Portfolio
description: >-
Get current portfolio considering pessimistic transactions in the UTX
pool
operationId: getPortfolios
parameters:
- $ref: '#/components/parameters/address'
- name: considerUnspent
in: query
description: Taking into account pessimistic transactions from UTX pool
schema:
type: boolean
default: true
responses:
'200':
description: Json portfolio
content:
application/json:
schema:
required:
- assets
- balance
- lease
type: object
properties:
balance:
type: integer
format: int64
lease:
required:
- in
- out
type: object
properties:
in:
type: integer
format: int64
out:
type: integer
format: int64
assets:
type: object
additionalProperties:
type: integer
format: int64
description: map of assetId <-> balance
example:
assetId1: 0
assetId2: 100
'403':
$ref: '#/components/responses/ApiKeyNotValid'
security:
- APIKey: []
'/debug/rollback-to/{id}':
delete:
tags:
Expand Down
2 changes: 1 addition & 1 deletion node/src/main/scala/com/wavesplatform/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Application(val actorSystem: ActorSystem, val settings: WavesSettings, con
val establishedConnections = new ConcurrentHashMap[Channel, PeerInfo]
val allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE)
val utxStorage =
new UtxPoolImpl(time, blockchainUpdater, spendableBalanceChanged, settings.utxSettings, utxEvents.onNext)
new UtxPoolImpl(time, blockchainUpdater, settings.utxSettings, utxEvents.onNext)
maybeUtx = Some(utxStorage)

val timer = new HashedWheelTimer()
Expand Down
17 changes: 8 additions & 9 deletions node/src/main/scala/com/wavesplatform/Importer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package com.wavesplatform
import java.io._
import java.net.{MalformedURLException, URL}

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}

import akka.actor.ActorSystem
import com.google.common.io.ByteStreams
import com.google.common.primitives.Ints
Expand All @@ -12,7 +16,7 @@ import com.wavesplatform.api.common.{CommonAccountsApi, CommonAssetsApi, CommonB
import com.wavesplatform.block.{Block, BlockHeader}
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.consensus.PoSSelector
import com.wavesplatform.database.{DBExt, KeyTags, openDB}
import com.wavesplatform.database.{openDB, DBExt, KeyTags}
import com.wavesplatform.events.{BlockchainUpdateTriggers, UtxEvent}
import com.wavesplatform.extensions.{Context, Extension}
import com.wavesplatform.features.BlockchainFeatures
Expand All @@ -21,26 +25,21 @@ import com.wavesplatform.lang.ValidationError
import com.wavesplatform.mining.Miner
import com.wavesplatform.protobuf.block.PBBlocks
import com.wavesplatform.settings.WavesSettings
import com.wavesplatform.state.appender.BlockAppender
import com.wavesplatform.state.{Blockchain, BlockchainUpdaterImpl, Diff, Height}
import com.wavesplatform.state.appender.BlockAppender
import com.wavesplatform.transaction.{Asset, DiscardedBlocks, Transaction}
import com.wavesplatform.transaction.TxValidationError.GenericError
import com.wavesplatform.transaction.smart.script.trace.TracedResult
import com.wavesplatform.transaction.{Asset, DiscardedBlocks, Transaction}
import com.wavesplatform.utils._
import com.wavesplatform.utx.{UtxPool, UtxPoolImpl}
import com.wavesplatform.wallet.Wallet
import kamon.Kamon
import monix.eval.Task
import monix.execution.Scheduler
import monix.reactive.subjects.PublishSubject
import monix.reactive.{Observable, Observer}
import org.iq80.leveldb.DB
import scopt.OParser

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.util.{Failure, Success, Try}

object Importer extends ScorexLogging {
import monix.execution.Scheduler.Implicits.global

Expand Down Expand Up @@ -280,7 +279,7 @@ object Importer extends ScorexLogging {
val db = openDB(settings.dbSettings.directory)
val (blockchainUpdater, levelDb) =
StorageFactory(settings, db, time, Observer.empty, BlockchainUpdateTriggers.combined(triggers))
val utxPool = new UtxPoolImpl(time, blockchainUpdater, PublishSubject(), settings.utxSettings)
val utxPool = new UtxPoolImpl(time, blockchainUpdater, settings.utxSettings)
val pos = PoSSelector(blockchainUpdater, settings.synchronizationSettings.maxBaseTarget)
val extAppender = BlockAppender(blockchainUpdater, time, utxPool, pos, scheduler, importOptions.verify) _

Expand Down
57 changes: 24 additions & 33 deletions node/src/main/scala/com/wavesplatform/api/http/DebugApiRoute.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package com.wavesplatform.api.http
import java.net.{InetAddress, InetSocketAddress, URI}
import java.util.concurrent.ConcurrentMap

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._
import scala.util.{Failure, Success}
import scala.util.control.NonFatal

import akka.http.scaladsl.common.{EntityStreamingSupport, JsonEntityStreamingSupport}
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.headers.Accept
import akka.http.scaladsl.server.Route
import akka.stream.scaladsl.Source
import cats.kernel.Monoid
import cats.syntax.either._
import com.typesafe.config.{ConfigObject, ConfigRenderOptions}
import com.wavesplatform.account.Address
Expand All @@ -20,12 +24,12 @@ import com.wavesplatform.lang.ValidationError
import com.wavesplatform.mining.{Miner, MinerDebugInfo}
import com.wavesplatform.network.{PeerDatabase, PeerInfo, _}
import com.wavesplatform.settings.{RestAPISettings, WavesSettings}
import com.wavesplatform.state.{Blockchain, Height, LeaseBalance, NG, Portfolio, StateHash}
import com.wavesplatform.state.diffs.TransactionDiffer
import com.wavesplatform.state.reader.CompositeBlockchain
import com.wavesplatform.state.{Blockchain, Height, LeaseBalance, NG, Portfolio, StateHash}
import com.wavesplatform.transaction._
import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.transaction.TxValidationError.{GenericError, InvalidRequestSignature}
import com.wavesplatform.transaction._
import com.wavesplatform.transaction.smart.script.trace.{InvokeScriptTrace, TracedResult}
import com.wavesplatform.transaction.smart.InvokeScriptTransaction
import com.wavesplatform.utils.{ScorexLogging, Time}
Expand All @@ -34,13 +38,8 @@ import com.wavesplatform.wallet.Wallet
import io.netty.channel.Channel
import monix.eval.{Coeval, Task}
import monix.execution.Scheduler
import play.api.libs.json.Json.JsValueWrapper
import play.api.libs.json._

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal
import scala.util.{Failure, Success}
import play.api.libs.json.Json.JsValueWrapper

case class DebugApiRoute(
ws: WavesSettings,
Expand Down Expand Up @@ -80,7 +79,7 @@ case class DebugApiRoute(

override lazy val route: Route = pathPrefix("debug") {
stateChanges ~ balanceHistory ~ stateHash ~ validate ~ withAuth {
state ~ info ~ stateWaves ~ rollback ~ rollbackTo ~ blacklist ~ portfolios ~ minerInfo ~ configInfo ~ print
state ~ info ~ stateWaves ~ rollback ~ rollbackTo ~ blacklist ~ minerInfo ~ configInfo ~ print
}
}

Expand All @@ -90,18 +89,6 @@ case class DebugApiRoute(
""
})

def portfolios: Route = path("portfolios" / AddrSegment) { address =>
(get & parameter("considerUnspent".as[Boolean].?)) { considerUnspent =>
extractScheduler { implicit s =>
complete(accountsApi.portfolio(address).toListL.runToFuture.map { assetList =>
val bd = accountsApi.balanceDetails(address)
val base = Portfolio(bd.regular, LeaseBalance(bd.leaseIn, bd.leaseOut), assetList.toMap)
if (considerUnspent.getOrElse(true)) Monoid.combine(base, utxStorage.pessimisticPortfolio(address)) else base
})
}
}
}

def balanceHistory: Route = (path("balances" / "history" / AddrSegment) & get) { address =>
complete(Json.toJson(loadBalanceHistory(address).map {
case (h, b) => Json.obj("height" -> h, "balance" -> b)
Expand Down Expand Up @@ -246,21 +233,25 @@ case class DebugApiRoute(
val transactionJson = parsedTransaction.fold(_ => jsv, _.json())

val serializer = tracedDiff.resultE
.fold(_ => this.serializer, { case (_, diff) =>
val compositeBlockchain = CompositeBlockchain(blockchain, diff)
this.serializer.copy(blockchain = compositeBlockchain)
.fold(_ => this.serializer, {
case (_, diff) =>
val compositeBlockchain = CompositeBlockchain(blockchain, diff)
this.serializer.copy(blockchain = compositeBlockchain)
})

val extendedJson = tracedDiff.resultE
.fold(_ => jsv, { case (tx, diff) =>
val meta = tx match {
case ist: InvokeScriptTransaction =>
val result = diff.scriptResults.get(ist.id())
TransactionMeta.Invoke(Height(blockchain.height), ist, succeeded = true, result)
case tx => TransactionMeta.Default(Height(blockchain.height), tx, succeeded = true)
.fold(
_ => jsv, {
case (tx, diff) =>
val meta = tx match {
case ist: InvokeScriptTransaction =>
val result = diff.scriptResults.get(ist.id())
TransactionMeta.Invoke(Height(blockchain.height), ist, succeeded = true, result)
case tx => TransactionMeta.Default(Height(blockchain.height), tx, succeeded = true)
}
serializer.transactionWithMetaJson(meta)
}
serializer.transactionWithMetaJson(meta)
})
)

val response = Json.obj(
"valid" -> error.isEmpty,
Expand Down
8 changes: 2 additions & 6 deletions node/src/main/scala/com/wavesplatform/utx/UtxPool.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package com.wavesplatform.utx

import com.wavesplatform.account.Address
import scala.concurrent.duration.FiniteDuration

import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.mining.MultiDimensionalMiningConstraint
import com.wavesplatform.state.Portfolio
import com.wavesplatform.transaction._
import com.wavesplatform.transaction.smart.script.trace.TracedResult
import com.wavesplatform.utx.UtxPool.PackStrategy

import scala.concurrent.duration.FiniteDuration

trait UtxPool extends AutoCloseable {
def putIfNew(tx: Transaction, forceValidate: Boolean = false): TracedResult[ValidationError, Boolean]
def removeAll(txs: Iterable[Transaction]): Unit
def spendableBalance(addr: Address, assetId: Asset): Long
def pessimisticPortfolio(addr: Address): Portfolio
def all: Seq[Transaction]
def size: Int
def transactionById(transactionId: ByteStr): Option[Transaction]
Expand Down
Loading

0 comments on commit 88d076d

Please sign in to comment.