Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
phearnot committed Nov 20, 2023
1 parent b672ff8 commit 5dc4185
Show file tree
Hide file tree
Showing 58 changed files with 228 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.wavesplatform.protobuf.transaction.InvokeScriptResult.Call.Argument
import com.wavesplatform.protobuf.transaction.{PBAmounts, PBTransactions, InvokeScriptResult as PBInvokeScriptResult}
import com.wavesplatform.state.*
import com.wavesplatform.state.diffs.invoke.InvokeScriptTransactionLike
import com.wavesplatform.state.reader.SnapshotBlockchain
import com.wavesplatform.state.SnapshotBlockchain
import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.transaction.assets.exchange.ExchangeTransaction
import com.wavesplatform.transaction.lease.LeaseTransaction
Expand Down
4 changes: 1 addition & 3 deletions node/src/main/protobuf/waves/database.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "waves/transaction.proto";
import "waves/amount.proto";
import "waves/recipient.proto";
import "waves/transaction_state_snapshot.proto";
import "google/protobuf/empty.proto";

message AssetDetails {
bytes name = 1;
Expand Down Expand Up @@ -112,8 +111,7 @@ message LeaseDetails {
bytes source_id = 4;
int32 height = 5;

oneof status {
google.protobuf.Empty active = 10;
oneof cancel_reason {
Cancelled cancelled = 11;
Expired expired = 12;
}
Expand Down
2 changes: 1 addition & 1 deletion node/src/main/scala/com/wavesplatform/Explorer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.wavesplatform.lang.script.ContractScript
import com.wavesplatform.lang.script.v1.ExprScript
import com.wavesplatform.settings.Constants
import com.wavesplatform.state.diffs.{DiffsCommon, SetScriptTransactionDiff}
import com.wavesplatform.state.reader.SnapshotBlockchain
import com.wavesplatform.state.SnapshotBlockchain
import com.wavesplatform.state.{Blockchain, Height, Portfolio, StateSnapshot, TransactionId}
import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.utils.ScorexLogging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import com.wavesplatform.features.BlockchainFeatures
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.protobuf.transaction.PBRecipients
import com.wavesplatform.state.patch.CancelLeasesToDisabledAliases
import com.wavesplatform.state.reader.LeaseDetails.Status
import com.wavesplatform.state.reader.SnapshotBlockchain
import com.wavesplatform.state.LeaseDetails.Status
import com.wavesplatform.state.SnapshotBlockchain
import com.wavesplatform.state.{AccountScriptInfo, AssetDescription, Blockchain, DataEntry, Height, InvokeScriptResult, TxMeta}
import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.transaction.EthereumTransaction.Invocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.wavesplatform.mining.{Miner, MinerDebugInfo}
import com.wavesplatform.network.{PeerDatabase, PeerInfo, *}
import com.wavesplatform.settings.{RestAPISettings, WavesSettings}
import com.wavesplatform.state.diffs.TransactionDiffer
import com.wavesplatform.state.reader.SnapshotBlockchain
import com.wavesplatform.state.SnapshotBlockchain
import com.wavesplatform.state.{Blockchain, Height, LeaseBalance, NG, Portfolio, StateHash, TxMeta}
import com.wavesplatform.transaction.*
import com.wavesplatform.transaction.Asset.IssuedAsset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.wavesplatform.lang.v1.compiler.Terms.{ARR, CONST_BOOLEAN, CONST_BYTES
import com.wavesplatform.lang.v1.serialization.SerdeV1
import com.wavesplatform.protobuf.transaction.PBAmounts
import com.wavesplatform.state.InvokeScriptResult.{AttachedPayment, Burn, Call, ErrorMessage, Invocation, Issue, Lease, LeaseCancel, Reissue, SponsorFee}
import com.wavesplatform.state.reader.LeaseDetails
import com.wavesplatform.state.LeaseDetails
import com.wavesplatform.state.{Blockchain, DataEntry, InvokeScriptResult, TxMeta}
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
import com.wavesplatform.transaction.lease.{LeaseCancelTransaction, LeaseTransaction}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,31 @@ import com.wavesplatform.lang.v1.parser.Parser.LibrariesOffset.NoLibraries
import com.wavesplatform.lang.v1.traits.domain.Recipient.Address as RideAddress
import com.wavesplatform.lang.{ValidationError, utils}
import com.wavesplatform.state.diffs.FeeValidation.{FeeConstants, FeeUnit}
import com.wavesplatform.state.{Blockchain, BlockchainOverrides, OverriddenBlockchain}
import com.wavesplatform.state.{Blockchain, BlockchainOverrides, SnapshotBlockchain, StateSnapshot}
import com.wavesplatform.transaction.TxValidationError.GenericError
import com.wavesplatform.transaction.smart.AttachedPaymentExtractor
import com.wavesplatform.transaction.smart.InvokeScriptTransaction.Payment
import com.wavesplatform.transaction.{TransactionType, smart}
import com.wavesplatform.transaction.{Asset, TransactionType, smart}
import play.api.libs.json.*

import scala.collection.immutable.VectorMap

sealed trait UtilsEvaluationRequest {
def state: Option[BlockchainOverrides]
def mkBlockchain(underlying: Blockchain): Blockchain = state.foldLeft(underlying)(new OverriddenBlockchain(_, _))
def mkBlockchain(underlying: Blockchain): Blockchain = {

state.fold(underlying) { ovs =>
SnapshotBlockchain(
underlying,
StateSnapshot(balances = VectorMap.from[(Address, Asset), Long](for {
(addr, ov) <- ovs.accounts
(id, balance) <- ov.assetBalances
} yield ((addr, id), balance.value)) ++ VectorMap.from(ovs.accounts.flatMap { case (addr, acc) =>
acc.regularBalance.map(v => ((addr, Asset.Waves), v.value))
}))
)
}
}
}

object UtilsEvaluationRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.wavesplatform.lang.{ValidationError, utils}
import com.wavesplatform.serialization.ScriptValuesJson
import com.wavesplatform.state.diffs.TransactionDiffer
import com.wavesplatform.state.diffs.invoke.{InvokeDiffsCommon, InvokeScriptTransactionLike, StructuredCallableActions}
import com.wavesplatform.state.reader.SnapshotBlockchain
import com.wavesplatform.state.SnapshotBlockchain
import com.wavesplatform.state.{AccountScriptInfo, Blockchain, InvokeScriptResult, Portfolio, StateSnapshot}
import com.wavesplatform.transaction.Asset.Waves
import com.wavesplatform.transaction.TransactionType.InvokeScript
Expand Down
9 changes: 5 additions & 4 deletions node/src/main/scala/com/wavesplatform/database/Caches.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.wavesplatform.transaction.{Asset, DiscardedBlocks, Transaction}
import com.wavesplatform.utils.ObservedLoadingCache
import monix.reactive.Observer

import java.{util, lang}
import java.{lang, util}
import scala.collection.immutable.VectorMap
import scala.collection.mutable
import scala.jdk.CollectionConverters.*
Expand Down Expand Up @@ -321,9 +321,10 @@ abstract class Caches extends Blockchain with Storage {
for ((address, script) <- snapshot.accountScriptsByAddress) stateHash.addAccountScript(address, 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)
for (leaseId <- snapshot.newLeases.keys) if (!snapshot.cancelledLeases.contains(leaseId)) stateHash.addLeaseStatus(leaseId, isActive = true)
for (leaseId <- snapshot.cancelledLeases.keys) stateHash.addLeaseStatus(leaseId, isActive = false)
for ((assetId, sponsorship) <- snapshot.sponsorships) stateHash.addSponsorship(assetId, sponsorship.minFee)
for ((alias, address) <- snapshot.aliases) stateHash.addAlias(address, alias.name)

doAppend(
newMeta,
Expand Down
1 change: 0 additions & 1 deletion node/src/main/scala/com/wavesplatform/database/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.wavesplatform.protobuf.snapshot.TransactionStateSnapshot
import com.wavesplatform.protobuf.transaction.PBRecipients
import com.wavesplatform.state
import com.wavesplatform.state.*
import com.wavesplatform.state.reader.LeaseDetails
import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.transaction.{ERC20Address, Transaction}
import com.wavesplatform.utils.*
Expand Down
13 changes: 10 additions & 3 deletions node/src/main/scala/com/wavesplatform/database/RocksDBWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.wavesplatform.protobuf.snapshot.{TransactionStateSnapshot, Transactio
import com.wavesplatform.protobuf.{ByteStrExt, ByteStringExt, PBSnapshots}
import com.wavesplatform.settings.{BlockchainSettings, DBSettings}
import com.wavesplatform.state.*
import com.wavesplatform.state.reader.LeaseDetails
import com.wavesplatform.transaction.*
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
import com.wavesplatform.transaction.EthereumTransaction.Transfer
Expand Down Expand Up @@ -484,8 +483,16 @@ class RocksDBWriter(
expiredKeys ++= updateHistory(rw, Keys.assetDetailsHistory(asset), threshold, Keys.assetDetails(asset))
}

for ((id, details) <- snapshot.leaseStates) {
rw.put(Keys.leaseDetails(id)(height), Some(details))
for ((id, li) <- snapshot.newLeases) {
rw.put(Keys.leaseDetails(id)(height), Some(LeaseDetails(li, snapshot.cancelledLeases.getOrElse(id, LeaseDetails.Status.Active))))
expiredKeys ++= updateHistory(rw, Keys.leaseDetailsHistory(id), threshold, Keys.leaseDetails(id))
}

for ((id, status) <- snapshot.cancelledLeases if !snapshot.newLeases.contains(id)) {
rw.fromHistory(Keys.leaseDetailsHistory(id), Keys.leaseDetails(id)).flatten.foreach { d =>
rw.put(Keys.leaseDetails(id)(height), Some(d.copy(status = status)))
}

expiredKeys ++= updateHistory(rw, Keys.leaseDetailsHistory(id), threshold, Keys.leaseDetails(id))
}

Expand Down
30 changes: 15 additions & 15 deletions node/src/main/scala/com/wavesplatform/database/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.wavesplatform.protobuf.transaction.{PBRecipients, PBTransactions}
import com.wavesplatform.protobuf.{ByteStringExt, PBSnapshots}
import com.wavesplatform.state.*
import com.wavesplatform.state.StateHash.SectionId
import com.wavesplatform.state.reader.LeaseDetails
import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.transaction.{
EthereumTransaction,
Expand Down Expand Up @@ -151,29 +150,30 @@ package object database {
ByteString.copyFrom(ld.sourceId.arr),
ld.height,
ld.status match {
case LeaseDetails.Status.Active => pb.LeaseDetails.Status.Active(com.google.protobuf.empty.Empty())
case LeaseDetails.Status.Active => pb.LeaseDetails.CancelReason.Empty
case LeaseDetails.Status.Cancelled(height, cancelTxId) =>
pb.LeaseDetails.Status
pb.LeaseDetails.CancelReason
.Cancelled(pb.LeaseDetails.Cancelled(height, cancelTxId.fold(ByteString.EMPTY)(id => ByteString.copyFrom(id.arr))))
case LeaseDetails.Status.Expired(height) => pb.LeaseDetails.Status.Expired(pb.LeaseDetails.Expired(height))
case LeaseDetails.Status.Expired(height) => pb.LeaseDetails.CancelReason.Expired(pb.LeaseDetails.Expired(height))
}
).toByteArray

def readLeaseDetails(data: Array[Byte]): LeaseDetails = {
val d = pb.LeaseDetails.parseFrom(data)
LeaseDetails(
d.senderPublicKey.toPublicKey,
PBRecipients.toAddress(d.recipient.get, AddressScheme.current.chainId).explicitGet(),
TxPositiveAmount.unsafeFrom(d.amount),
d.status match {
case pb.LeaseDetails.Status.Active(_) => LeaseDetails.Status.Active
case pb.LeaseDetails.Status.Expired(pb.LeaseDetails.Expired(height, _)) => LeaseDetails.Status.Expired(height)
case pb.LeaseDetails.Status.Cancelled(pb.LeaseDetails.Cancelled(height, transactionId, _)) =>
LeaseStaticInfo(
d.senderPublicKey.toPublicKey,
PBRecipients.toAddress(d.recipient.get, AddressScheme.current.chainId).explicitGet(),
TxPositiveAmount.unsafeFrom(d.amount),
d.sourceId.toByteStr,
d.height
),
d.cancelReason match {
case pb.LeaseDetails.CancelReason.Expired(pb.LeaseDetails.Expired(height, _)) => LeaseDetails.Status.Expired(height)
case pb.LeaseDetails.CancelReason.Cancelled(pb.LeaseDetails.Cancelled(height, transactionId, _)) =>
LeaseDetails.Status.Cancelled(height, Some(transactionId.toByteStr).filter(!_.isEmpty))
case pb.LeaseDetails.Status.Empty => ???
},
d.sourceId.toByteStr,
d.height
case pb.LeaseDetails.CancelReason.Empty => LeaseDetails.Status.Active
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.wavesplatform.state.BlockchainUpdaterImpl.BlockApplyResult
import com.wavesplatform.state.BlockchainUpdaterImpl.BlockApplyResult.Applied
import com.wavesplatform.state.appender.MaxTimeDrift
import com.wavesplatform.state.diffs.BlockDiffer
import com.wavesplatform.state.reader.SnapshotBlockchain
import com.wavesplatform.state.SnapshotBlockchain
import com.wavesplatform.state.{Blockchain, StateSnapshot, TxStateSnapshotHashBuilder}
import com.wavesplatform.transaction.TxValidationError.GenericError
import com.wavesplatform.transaction.{BlockchainUpdater, Transaction}
Expand Down
44 changes: 13 additions & 31 deletions node/src/main/scala/com/wavesplatform/protobuf/PBSnapshots.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.EitherExt2
import com.wavesplatform.lang.script.ScriptReader
import com.wavesplatform.protobuf.snapshot.TransactionStateSnapshot
import com.wavesplatform.protobuf.snapshot.TransactionStateSnapshot.AssetStatic
import com.wavesplatform.protobuf.snapshot.TransactionStateSnapshot.NewAsset
import com.wavesplatform.protobuf.transaction.{PBAmounts, PBTransactions}
import com.wavesplatform.state.*
import com.wavesplatform.state.reader.LeaseDetails
import com.wavesplatform.state.reader.LeaseDetails.Status
import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.transaction.{Asset, TxPositiveAmount}

Expand All @@ -29,8 +27,10 @@ object PBSnapshots {
leaseBalances.map { case (address, balance) =>
S.LeaseBalance(address.toByteString, balance.in, balance.out)
}.toSeq,
newLeases = Seq(),
cancelledLeases = Seq(),
assetStatics.map { case (id, st) =>
AssetStatic(id.id.toByteString, st.issuer.toByteString, st.decimals, st.nft)
NewAsset(id.id.toByteString, st.issuer.toByteString, st.decimals, st.nft)
}.toSeq,
assetVolumes.map { case (asset, info) =>
S.AssetVolume(asset.id.toByteString, info.isReissuable, ByteString.copyFrom(info.volume.toByteArray))
Expand All @@ -45,15 +45,6 @@ object PBSnapshots {
orderFills.map { case (orderId, VolumeAndFee(volume, fee)) =>
S.OrderFill(orderId.toByteString, volume, fee)
}.toSeq,
leaseStates.map { case (leaseId, ld) =>
val pbStatus = ld.status match {
case Status.Active =>
S.LeaseState.Status.Active(S.LeaseState.Active(ld.amount.value, ld.sender.toByteString, ld.recipientAddress.toByteString))
case _: Status.Cancelled | _: Status.Expired =>
S.LeaseState.Status.Cancelled(S.LeaseState.Cancelled())
}
S.LeaseState(leaseId.toByteString, pbStatus)
}.toSeq,
accountScripts.map { case (publicKey, scriptOpt) =>
scriptOpt.fold(
S.AccountScript(publicKey.toByteString)
Expand Down Expand Up @@ -115,22 +106,13 @@ object PBSnapshots {
.map(s => s.assetId.toIssuedAssetId -> SponsorshipValue(s.minFee))
.toMap

val leaseStates: Map[ByteStr, LeaseDetails] =
pbSnapshot.leaseStates.map { ls =>
ls.status match {
case TransactionStateSnapshot.LeaseState.Status.Active(value) =>
ls.leaseId.toByteStr -> LeaseDetails(
value.sender.toPublicKey,
value.recipient.toAddress(),
TxPositiveAmount.unsafeFrom(value.amount),
LeaseDetails.Status.Active,
txId,
height
)
case _: TransactionStateSnapshot.LeaseState.Status.Cancelled | TransactionStateSnapshot.LeaseState.Status.Empty =>
ls.leaseId.toByteStr -> ???
}
}.toMap
val newLeases = pbSnapshot.newLeases.map { l => l.leaseId.toByteStr ->
LeaseStaticInfo(l.senderPublicKey.toPublicKey, l.recipientAddress.toAddress(), TxPositiveAmount.unsafeFrom(l.amount), txId, height)
}.toMap

val cancelledLeases = pbSnapshot.cancelledLeases.map { cl =>
cl.leaseId.toByteStr -> LeaseDetails.Status.Cancelled(height, Some(txId))
}.toMap

val aliases: Map[Alias, Address] =
pbSnapshot.aliases
Expand Down Expand Up @@ -178,7 +160,8 @@ object PBSnapshots {
assetNamesAndDescriptions,
assetScripts,
sponsorships,
leaseStates,
newLeases,
cancelledLeases,
aliases,
orderFills,
accountScripts,
Expand All @@ -187,5 +170,4 @@ object PBSnapshots {
TxMeta.Status.fromProtobuf(pbSnapshot.transactionStatus)
)
}

}
Loading

0 comments on commit 5dc4185

Please sign in to comment.