Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NODE-2626 Corrected snapshot hash of LeaseState #3909

Merged
merged 11 commits into from
Nov 17, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@ class AccountsApiGrpcImpl(commonApi: CommonAccountsApi)(implicit sc: Scheduler)
responseObserver.completeWith(responseStream)
}

override def getScript(request: AccountRequest): Future[ScriptData] = Future {
override def getScript(request: AccountRequest): Future[ScriptResponse] = Future {
commonApi.script(request.address.toAddress) match {
case Some(desc) => ScriptData(
case Some(desc) =>
ScriptResponse(
PBTransactions.toPBScript(Some(desc.script)),
desc.script.expr.toString,
desc.verifierComplexity
desc.verifierComplexity,
desc.publicKey.toByteString
)
case None =>
ScriptData()
ScriptResponse()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ object SyncGrpcApi extends Assertions {
maybeWaitForTransaction(sync(async(n).setScript(sender, script, fee, timestamp, version)), waitForTx)
}

def scriptInfo(address: ByteString): ScriptData = {
def scriptInfo(address: ByteString): ScriptResponse = {
accounts.getScript(AccountRequest.of(address))
}

Expand Down
64 changes: 30 additions & 34 deletions node/src/main/scala/com/wavesplatform/state/StateSnapshot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import cats.data.Ior
import cats.implicits.{catsSyntaxEitherId, catsSyntaxSemigroup, toBifunctorOps, toTraverseOps}
import cats.kernel.Monoid
import com.google.protobuf.ByteString
import com.wavesplatform.account.{Address, AddressScheme, Alias, PublicKey}
import com.wavesplatform.account.{Address, Alias, PublicKey}
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.EitherExt2
import com.wavesplatform.crypto.KeyLength
import com.wavesplatform.database.protobuf.EthereumTransactionMeta
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.lang.script.ScriptReader
import com.wavesplatform.protobuf.snapshot.TransactionStateSnapshot
import com.wavesplatform.protobuf.snapshot.TransactionStateSnapshot.AssetStatic
import com.wavesplatform.protobuf.transaction.{PBAmounts, PBRecipients, PBTransactions}
import com.wavesplatform.protobuf.transaction.{PBAmounts, PBTransactions}
import com.wavesplatform.protobuf.{AddressExt, ByteStrExt, ByteStringExt}
import com.wavesplatform.state.reader.LeaseDetails.Status
import com.wavesplatform.state.reader.{LeaseDetails, SnapshotBlockchain}
Expand Down Expand Up @@ -63,24 +64,14 @@ case class StateSnapshot(
orderFills.map { case (orderId, VolumeAndFee(volume, fee)) =>
S.OrderFill(orderId.toByteString, volume, fee)
}.toSeq,
leaseStates.map { case (leaseId, LeaseDetails(sender, recipient, amount, status, sourceId, height)) =>
leaseStates.map { case (leaseId, LeaseDetails(sender, recipient, amount, status, _, _)) =>
val pbStatus = status match {
case Status.Active =>
S.LeaseState.Status.Active(S.LeaseState.Active())
case Status.Cancelled(cancelHeight, txId) =>
S.LeaseState.Status.Cancelled(S.LeaseState.Cancelled(cancelHeight, txId.fold(ByteString.EMPTY)(_.toByteString)))
case Status.Expired(expiredHeight) =>
S.LeaseState.Status.Cancelled(S.LeaseState.Cancelled(expiredHeight))
S.LeaseState.Status.Active(S.LeaseState.Active(amount, sender.toByteString, recipient.asInstanceOf[Address].toByteString))
case _: Status.Cancelled | _: Status.Expired =>
S.LeaseState.Status.Cancelled(S.LeaseState.Cancelled())
}
S.LeaseState(
leaseId.toByteString,
pbStatus,
amount,
sender.toByteString,
ByteString.copyFrom(recipient.asInstanceOf[Address].bytes),
sourceId.toByteString,
height
)
S.LeaseState(leaseId.toByteString, pbStatus)
}.toSeq,
accountScripts.map { case (publicKey, scriptOpt) =>
scriptOpt.fold(
Expand Down Expand Up @@ -172,23 +163,28 @@ object StateSnapshot {
.toMap

val leaseStates: Map[ByteStr, LeaseDetails] =
pbSnapshot.leaseStates
.map(ls =>
ls.leaseId.toByteStr -> LeaseDetails(
ls.sender.toPublicKey,
PBRecipients.toAddress(ls.recipient.toByteArray, AddressScheme.current.chainId).explicitGet(),
ls.amount,
ls.status match {
case TransactionStateSnapshot.LeaseState.Status.Cancelled(c) =>
LeaseDetails.Status.Cancelled(c.height, if (c.transactionId.isEmpty) None else Some(c.transactionId.toByteStr))
case _ =>
LeaseDetails.Status.Active
},
ls.originTransactionId.toByteStr,
ls.height
)
)
.toMap
pbSnapshot.leaseStates.map { ls =>
ls.status match {
case TransactionStateSnapshot.LeaseState.Status.Active(value) =>
ls.leaseId.toByteStr -> LeaseDetails(
value.sender.toPublicKey,
value.recipient.toAddress,
value.amount,
LeaseDetails.Status.Active,
sourceId = ByteStr.empty,
height = 0
)
case _: TransactionStateSnapshot.LeaseState.Status.Cancelled | TransactionStateSnapshot.LeaseState.Status.Empty =>
ls.leaseId.toByteStr -> LeaseDetails(
PublicKey(ByteStr.fill(KeyLength)(0)),
Address(Array.fill(Address.HashLength)(0)),
0,
LeaseDetails.Status.Cancelled(0, None),
sourceId = ByteStr.empty,
height = 0
)
}
}.toMap

val aliases: Map[Alias, Address] =
pbSnapshot.aliases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ object TxStateSnapshotHashBuilder {
} addEntry(KeyType.AssetScript, asset.id.arr)(scriptInfo.script.bytes().arr)

snapshot.leaseStates.foreach { case (leaseId, details) =>
addEntry(KeyType.LeaseStatus, leaseId.arr)(booleanToBytes(details.isActive))
if (details.isActive)
addEntry(KeyType.LeaseStatus, leaseId.arr)(
booleanToBytes(true),
details.sender.arr,
details.recipient.bytes,
Longs.toByteArray(details.amount)
)
else
addEntry(KeyType.LeaseStatus, leaseId.arr)(
booleanToBytes(false)
)
}

snapshot.sponsorships.foreach { case (asset, sponsorship) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.wavesplatform.state.snapshot

import com.google.protobuf.ByteString
import com.wavesplatform.account.Alias
import com.wavesplatform.account.{Address, Alias, PublicKey}
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.EitherExt2
import com.wavesplatform.crypto.KeyLength
import com.wavesplatform.lang.directives.values.V6
import com.wavesplatform.lang.v1.compiler.TestCompiler
import com.wavesplatform.protobuf.snapshot.TransactionStateSnapshot.AssetStatic
Expand Down Expand Up @@ -60,14 +61,14 @@ class StateSnapshotProtoTest extends PropSpec {
IssuedAsset(ByteStr.fromBytes(2, 2, 2)) -> SponsorshipValue(0)
),
Map(
ByteStr.fromBytes(4, 5, 6) -> LeaseDetails(defaultSigner.publicKey, secondAddress, 123, Status.Active, ByteStr.fromBytes(1, 2, 3), 4),
ByteStr.fromBytes(4, 5, 6) -> LeaseDetails(defaultSigner.publicKey, secondAddress, 123, Status.Active, ByteStr.empty, 0),
ByteStr.fromBytes(7, 8, 9) -> LeaseDetails(
secondSigner.publicKey,
defaultAddress,
PublicKey(ByteStr.fill(KeyLength)(0)),
Address(Array.fill(Address.HashLength)(0)),
0,
Status.Cancelled(2, Some(ByteStr.fromBytes(5, 5, 5))),
ByteStr.fromBytes(1, 2, 3),
777777777
Status.Cancelled(0, None),
ByteStr.empty,
0
)
),
Map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.wavesplatform.TestValues.fee
import com.wavesplatform.account.{Address, Alias, PublicKey}
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.EitherExt2
import com.wavesplatform.crypto.KeyLength
import com.wavesplatform.db.WithDomain
import com.wavesplatform.lang.directives.values.V6
import com.wavesplatform.lang.v1.compiler.TestCompiler
Expand Down Expand Up @@ -175,7 +176,7 @@ class StateSnapshotStorageTest extends PropSpec with WithDomain {
recipient -> LeaseBalance(leaseTx.amount.value, 0)
),
leaseStates = Map(
leaseTx.id() -> LeaseDetails(sender.publicKey, recipient, leaseTx.amount.value, Active, leaseTx.id(), d.solidStateHeight + 2)
leaseTx.id() -> LeaseDetails(sender.publicKey, recipient, leaseTx.amount.value, Active, ByteStr.empty, 0)
)
)
)
Expand All @@ -194,12 +195,12 @@ class StateSnapshotStorageTest extends PropSpec with WithDomain {
),
leaseStates = Map(
leaseTx.id() -> LeaseDetails(
sender.publicKey,
recipient,
leaseTx.amount.value,
Cancelled(d.solidStateHeight + 2, Some(leaseCancelTx.id())),
leaseTx.id(),
d.solidStateHeight
PublicKey(ByteStr.fill(KeyLength)(0)),
Address(Array.fill(Address.HashLength)(0)),
0,
Cancelled(0, None),
ByteStr.empty,
0
)
)
)
Expand Down Expand Up @@ -333,7 +334,7 @@ class StateSnapshotStorageTest extends PropSpec with WithDomain {
dAppAssetId -> AssetInfo("name", "description", Height(height))
),
leaseStates = Map(
leaseId -> LeaseDetails(dAppPk, senderAddress, 123, Active, invokeId, height)
leaseId -> LeaseDetails(dAppPk, senderAddress, 123, Active, ByteStr.empty, 0)
),
accountData = Map(
dAppPk.toAddress -> Map("key" -> StringDataEntry("key", "abc"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ class TxStateSnapshotHashSpec extends PropSpec with WithDomain {
private val orderId = ByteStr.fill(DigestLength)(5)
private val volumeAndFee = VolumeAndFee(11, 2)

private val leaseId = ByteStr.fill(DigestLength)(6)
private val leaseDetails = LeaseDetails(TxHelpers.signer(1).publicKey, address2, 1.waves, LeaseDetails.Status.Active, leaseId, 2)
private val leaseId1 = ByteStr.fill(DigestLength)(6)
private val leaseId2 = ByteStr.fill(DigestLength)(7)
private val leaseDetails1 = LeaseDetails(TxHelpers.signer(1).publicKey, address2, 1.waves, LeaseDetails.Status.Active, leaseId1, 2)
private val leaseDetails2 = LeaseDetails(TxHelpers.signer(1).publicKey, address2, 1.waves, LeaseDetails.Status.Cancelled(1, None), leaseId2, 2)

private val addr1Balance = 10.waves
private val addr2Balance = 20.waves
Expand Down Expand Up @@ -100,7 +102,7 @@ class TxStateSnapshotHashSpec extends PropSpec with WithDomain {
),
aliases = Map(addr1Alias1 -> address1, addr2Alias -> address2, addr1Alias2 -> address1),
orderFills = Map(orderId -> volumeAndFee),
leaseState = Map(leaseId -> leaseDetails),
leaseState = Map(leaseId1 -> leaseDetails1, leaseId2 -> leaseDetails2),
scripts = Map(TxHelpers.signer(2).publicKey -> Some(accountScriptInfo)),
assetScripts = Map(assetId1 -> Some(assetScriptInfo)),
accountData = Map(address1 -> Map(dataEntry.key -> dataEntry)),
Expand Down Expand Up @@ -132,7 +134,15 @@ class TxStateSnapshotHashSpec extends PropSpec with WithDomain {
Array(KeyType.LeaseBalance.id.toByte) ++ address1.bytes ++ Longs.toByteArray(addr1PortfolioDiff.lease.in) ++ Longs.toByteArray(
addr1PortfolioDiff.lease.out
),
Array(KeyType.LeaseStatus.id.toByte) ++ leaseId.arr ++ (if (leaseDetails.isActive) Array(1: Byte) else Array(0: Byte)),
Array(KeyType.LeaseStatus.id.toByte)
++ leaseId1.arr
++ Array(1: Byte)
++ leaseDetails1.sender.arr
++ leaseDetails1.recipient.bytes
++ Longs.toByteArray(leaseDetails1.amount),
Array(KeyType.LeaseStatus.id.toByte)
++ leaseId2.arr
++ Array(0: Byte),
Array(KeyType.Sponsorship.id.toByte) ++ assetId1.id.arr ++ Longs.toByteArray(sponsorship.minFee),
Array(KeyType.Alias.id.toByte) ++ address1.bytes ++ addr1Alias1.name.getBytes(StandardCharsets.UTF_8),
Array(KeyType.Alias.id.toByte) ++ address1.bytes ++ addr1Alias2.name.getBytes(StandardCharsets.UTF_8),
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 sbt.{Def, _}
object Dependencies {
// Node protobuf schemas
private[this] val protoSchemasLib =
"com.wavesplatform" % "protobuf-schemas" % "1.5.0" classifier "protobuf-src" intransitive ()
"com.wavesplatform" % "protobuf-schemas" % "1.5.1-84-SNAPSHOT" classifier "protobuf-src" intransitive ()

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

Expand Down