Skip to content

Commit

Permalink
GRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
xrtm000 committed Dec 15, 2023
1 parent 4f363b0 commit a5bddfe
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.wavesplatform.api.grpc

import scala.concurrent.Future
import com.wavesplatform.account.AddressScheme
import com.wavesplatform.api.common.{CommonTransactionsApi, TransactionMeta}
import com.wavesplatform.api.grpc.TransactionsApiGrpcImpl.applicationStatusFromTxStatus
import com.wavesplatform.protobuf.*
import com.wavesplatform.protobuf.transaction.*
import com.wavesplatform.protobuf.utils.PBImplicitConversions.PBRecipientImplicitConversionOps
import com.wavesplatform.state.{Blockchain, TxMeta, InvokeScriptResult as VISR}
import com.wavesplatform.transaction.{Authorized, EthereumTransaction}
import com.wavesplatform.transaction.TxValidationError.GenericError
import io.grpc.{Status, StatusRuntimeException}
import com.wavesplatform.transaction.{Authorized, EthereumTransaction}
import io.grpc.stub.StreamObserver
import io.grpc.{Status, StatusRuntimeException}
import monix.execution.Scheduler
import monix.reactive.Observable

import scala.concurrent.Future

class TransactionsApiGrpcImpl(blockchain: Blockchain, commonApi: CommonTransactionsApi)(implicit sc: Scheduler)
extends TransactionsApiGrpc.TransactionsApi {

Expand Down Expand Up @@ -64,6 +65,20 @@ class TransactionsApiGrpcImpl(blockchain: Blockchain, commonApi: CommonTransacti
)
}

override def getTransactionSnapshots(
request: TransactionSnapshotsRequest,
responseObserver: StreamObserver[TransactionSnapshotResponse]
): Unit =
responseObserver.interceptErrors {
val snapshots =
for {
id <- request.transactionIds
(snapshot, status) <- blockchain.transactionSnapshot(id.toByteStr)
pbSnapshot = PBSnapshots.toProtobuf(snapshot, status)
} yield TransactionSnapshotResponse(id, Some(pbSnapshot))
responseObserver.completeWith(Observable.fromIterable(snapshots))
}

override def getUnconfirmed(request: TransactionsRequest, responseObserver: StreamObserver[TransactionResponse]): Unit =
responseObserver.interceptErrors {
val unconfirmedTransactions = if (!request.sender.isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ case class TransactionsApiRoute(
blockchain
.transactionSnapshot(id)
.toRight(TransactionDoesNotExist)
.map(StateSnapshotJson.fromSnapshot)
.map { case (snapshot, _) => StateSnapshotJson.fromSnapshot(snapshot) }
val single = (get & path(TransactionId))(id => complete(readSnapshot(id)))
val multiple = (pathEndOrSingleSlash & anyParam("id", limit = settings.transactionSnapshotsLimit))(rawIds =>
complete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.wavesplatform.protobuf.snapshot.TransactionStatus as PBStatus
import com.wavesplatform.protobuf.{ByteStrExt, ByteStringExt, PBSnapshots}
import com.wavesplatform.settings.{BlockchainSettings, DBSettings}
import com.wavesplatform.state.*
import com.wavesplatform.state.TxMeta.Status
import com.wavesplatform.transaction.*
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
import com.wavesplatform.transaction.EthereumTransaction.Transfer
Expand Down Expand Up @@ -970,11 +971,11 @@ class RocksDBWriter(
}
}

override def transactionSnapshot(id: ByteStr): Option[StateSnapshot] = readOnly { db =>
override def transactionSnapshot(id: ByteStr): Option[(StateSnapshot, Status)] = readOnly { db =>
for {
meta <- db.get(Keys.transactionMetaById(TransactionId(id), rdb.txMetaHandle))
snapshot <- db.get(Keys.transactionStateSnapshotAt(Height(meta.height), TxNum(meta.num.toShort), rdb.txSnapshotHandle))
} yield PBSnapshots.fromProtobuf(snapshot, id, meta.height)._1
} yield PBSnapshots.fromProtobuf(snapshot, id, meta.height)
}

override def resolveAlias(alias: Alias): Either[ValidationError, Address] =
Expand Down
3 changes: 2 additions & 1 deletion node/src/main/scala/com/wavesplatform/state/Blockchain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.wavesplatform.lang.script.ContractScript
import com.wavesplatform.lang.v1.ContractLimits
import com.wavesplatform.lang.v1.traits.domain.Issue
import com.wavesplatform.settings.BlockchainSettings
import com.wavesplatform.state.TxMeta.Status
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
import com.wavesplatform.transaction.TxValidationError.AliasDoesNotExist
import com.wavesplatform.transaction.assets.IssueTransaction
Expand Down Expand Up @@ -46,7 +47,7 @@ trait Blockchain {
def transactionInfo(id: ByteStr): Option[(TxMeta, Transaction)]
def transactionInfos(ids: Seq[ByteStr]): Seq[Option[(TxMeta, Transaction)]]
def transactionMeta(id: ByteStr): Option[TxMeta]
def transactionSnapshot(id: ByteStr): Option[StateSnapshot]
def transactionSnapshot(id: ByteStr): Option[(StateSnapshot, Status)]

def containsTransaction(tx: Transaction): Boolean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.wavesplatform.metrics.{TxsInBlockchainStats, *}
import com.wavesplatform.mining.{Miner, MiningConstraint, MiningConstraints}
import com.wavesplatform.settings.{BlockchainSettings, WavesSettings}
import com.wavesplatform.state.BlockchainUpdaterImpl.BlockApplyResult.{Applied, Ignored}
import com.wavesplatform.state.TxMeta.Status
import com.wavesplatform.state.diffs.BlockDiffer
import com.wavesplatform.transaction.*
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
Expand Down Expand Up @@ -753,7 +754,7 @@ class BlockchainUpdaterImpl(
snapshotBlockchain.transactionMeta(id)
}

override def transactionSnapshot(id: ByteStr): Option[StateSnapshot] = readLock {
override def transactionSnapshot(id: ByteStr): Option[(StateSnapshot, Status)] = readLock {
snapshotBlockchain.transactionSnapshot(id)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.features.BlockchainFeatures.RideV6
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.settings.BlockchainSettings
import com.wavesplatform.state.TxMeta.Status
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
import com.wavesplatform.transaction.TxValidationError.{AliasDoesNotExist, AliasIsDisabled}
import com.wavesplatform.transaction.transfer.{TransferTransaction, TransferTransactionLike}
Expand Down Expand Up @@ -119,10 +120,10 @@ case class SnapshotBlockchain(
.map(t => TxMeta(Height(this.height), t.status, t.spentComplexity))
.orElse(inner.transactionMeta(id))

override def transactionSnapshot(id: ByteStr): Option[StateSnapshot] =
override def transactionSnapshot(id: ByteStr): Option[(StateSnapshot, Status)] =
snapshot.transactions
.get(id)
.map(_.snapshot)
.map(tx => (tx.snapshot, tx.status))
.orElse(inner.transactionSnapshot(id))

override def height: Int = inner.height + blockMeta.fold(0)(_ => 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.wavesplatform.lang.v1.compiler.TestCompiler
import com.wavesplatform.lang.v1.traits.domain.Recipient
import com.wavesplatform.settings.BlockchainSettings
import com.wavesplatform.state.*
import com.wavesplatform.state.TxMeta.Status
import com.wavesplatform.test.PropSpec
import com.wavesplatform.transaction.Asset.Waves
import com.wavesplatform.transaction.smart.script.ScriptRunner
Expand Down Expand Up @@ -41,7 +42,7 @@ class MatcherBlockchainTest extends PropSpec with MockFactory with WithDomain {
override def transactionInfo(id: ByteStr): Option[(TxMeta, Transaction)] = ???
override def transactionInfos(ids: Seq[BlockId]): Seq[Option[(TxMeta, Transaction)]] = ???
override def transactionMeta(id: ByteStr): Option[TxMeta] = ???
override def transactionSnapshot(id: ByteStr): Option[StateSnapshot] = ???
override def transactionSnapshot(id: ByteStr): Option[(StateSnapshot, Status)] = ???
override def containsTransaction(tx: Transaction): Boolean = ???
override def assetDescription(id: Asset.IssuedAsset): Option[AssetDescription] = ???
override def resolveAlias(a: Alias): Either[ValidationError, Address] = ???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.settings.BlockchainSettings
import com.wavesplatform.state.*
import com.wavesplatform.state.TxMeta.Status
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
import com.wavesplatform.transaction.TxValidationError.GenericError
import com.wavesplatform.transaction.transfer.TransferTransactionLike
Expand Down Expand Up @@ -49,7 +50,7 @@ trait EmptyBlockchain extends Blockchain {

override def transactionMeta(id: ByteStr): Option[TxMeta] = None

override def transactionSnapshot(id: ByteStr): Option[StateSnapshot] = None
override def transactionSnapshot(id: ByteStr): Option[(StateSnapshot, Status)] = None

override def containsTransaction(tx: Transaction): Boolean = false

Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scalapb.compiler.Version.scalapbVersion
object Dependencies {
// Node protobuf schemas
private[this] val protoSchemasLib =
"com.wavesplatform" % "protobuf-schemas" % "1.5.1" classifier "protobuf-src" intransitive ()
"com.wavesplatform" % "protobuf-schemas" % "1.5.2-86-SNAPSHOT" classifier "protobuf-src" intransitive ()

private def akkaModule(module: String) = "com.typesafe.akka" %% s"akka-$module" % "2.6.21"

Expand Down

0 comments on commit a5bddfe

Please sign in to comment.