Skip to content

Commit

Permalink
Merge branch 'version-1.5.x' into ride-generating-balance-test
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirlogachev authored Sep 23, 2024
2 parents 01cc8e0 + b09948e commit 0417cd7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
gpg-private-key: ${{ secrets.OSSRH_GPG_KEY }}
gpg-passphrase: ${{ secrets.OSSRH_GPG_PASSPHRASE }}
- name: Check PR
run: sbt --mem 6144 --batch ";checkPR;completeQaseRun"
run: sbt --mem 10240 --batch ";checkPR;completeQaseRun"
env:
QASE_ENABLE: true
QASE_RUN_NAME: checkPR
Expand Down
32 changes: 10 additions & 22 deletions node/src/main/scala/com/wavesplatform/database/RocksDBWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.EitherExt2
import com.wavesplatform.database
import com.wavesplatform.database.patch.DisableHijackedAliases
import com.wavesplatform.database.protobuf.{StaticAssetInfo, TransactionMeta, BlockMeta as PBBlockMeta, BlockMetaExt}
import com.wavesplatform.database.protobuf.{BlockMetaExt, StaticAssetInfo, TransactionMeta, BlockMeta as PBBlockMeta}
import com.wavesplatform.features.BlockchainFeatures
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.protobuf.block.PBBlocks
Expand Down Expand Up @@ -1315,28 +1315,16 @@ class RocksDBWriter(
.build[(Int, AddressId), LeaseBalanceNode]()

override def balanceAtHeight(address: Address, height: Int, assetId: Asset = Waves): Option[(Int, Long)] = readOnly { db =>
@tailrec
def getBalanceAtHeight(h: Height, key: Height => Key[BalanceNode]): (Int, Long) = {
val balance = db.get(key(h))
if (h <= height) {
h -> balance.balance
} else {
getBalanceAtHeight(balance.prevHeight, key)
}
}

db.get(Keys.addressId(address)).map { aid =>
val (balance, balanceNodeKey) =
assetId match {
case Waves => (db.get(Keys.wavesBalance(aid)), Keys.wavesBalanceAt(aid, _))
case asset @ IssuedAsset(_) => (db.get(Keys.assetBalance(aid, asset)), Keys.assetBalanceAt(aid, asset, _))
}

if (balance.height > height) {
getBalanceAtHeight(balance.prevHeight, balanceNodeKey)
} else {
balance.height -> balance.balance
db.get(Keys.addressId(address)).flatMap { aid =>
val key = assetId match {
case Waves => Keys.wavesBalanceAt(aid, Height(height))
case asset: IssuedAsset => Keys.assetBalanceAt(aid, asset, Height(height))
}
Using(db.newIterator) { iter =>
iter.seekForPrev(key.keyBytes)
require(iter.isValid && iter.key().startsWith(key.keyBytes.dropRight(Ints.BYTES)))
Ints.fromByteArray(iter.key().takeRight(Ints.BYTES)) -> key.parse(iter.value()).balance
}.toOption
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ case class SnapshotBlockchain(
snapshot.orderFills.getOrElse(orderId, inner.filledVolumeAndFee(orderId))

override def balanceAtHeight(address: Address, h: Int, assetId: Asset = Waves): Option[(Int, Long)] =
if (maybeSnapshot.isEmpty || h < this.height) {
if (maybeSnapshot.forall(!_.balances.contains(address -> assetId)) || h < this.height) {
inner.balanceAtHeight(address, h, assetId)
} else {
val balance = this.balance(address, assetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,46 @@ class RocksDBWriterSpec extends FreeSpec with WithDomain {
nonHistoricalKeys should contain theSameElementsInOrderAs nonHistoricalKeysWithoutCleanup
}
}

"balanceAtHeight returns correct values" in {
val richAccount = TxHelpers.signer(1001)
val account1 = TxHelpers.signer(1002)
val account2 = TxHelpers.signer(1003)
withDomain(DomainPresets.TransactionStateSnapshot, Seq(AddrWithBalance(richAccount.toAddress, 10_000.waves))) { d =>
val issueTx = TxHelpers.issue(richAccount, amount = 10000, decimals = 2.toByte, name = "IA01")
d.appendBlock(issueTx)
(1 to 3).foreach(_ => d.appendBlock())
d.blockchain.height shouldBe 5

d.appendBlock(TxHelpers.transfer(richAccount, account1.toAddress, 10.waves))
d.appendBlock(TxHelpers.transfer(richAccount, account1.toAddress, 100, asset = issueTx.asset))
d.appendBlock()
d.appendBlock(TxHelpers.transfer(richAccount, account1.toAddress, 1.waves))
d.appendBlock(TxHelpers.transfer(richAccount, account1.toAddress, 500, asset = issueTx.asset))
d.blockchain.height shouldBe 10

d.blockchain.balanceAtHeight(account1.toAddress, 10) shouldBe Some(9 -> 11.waves)
d.blockchain.balanceAtHeight(account1.toAddress, 9) shouldBe Some(9 -> 11.waves)
d.blockchain.balanceAtHeight(account1.toAddress, 8) shouldBe Some(6 -> 10.waves)
d.blockchain.balanceAtHeight(account1.toAddress, 6) shouldBe Some(6 -> 10.waves)
d.blockchain.balanceAtHeight(account1.toAddress, 5) shouldBe None

d.blockchain.balanceAtHeight(account1.toAddress, 10, issueTx.asset) shouldBe Some(10 -> 600)
d.blockchain.balanceAtHeight(account1.toAddress, 9, issueTx.asset) shouldBe Some(7 -> 100)
d.blockchain.balanceAtHeight(account1.toAddress, 8, issueTx.asset) shouldBe Some(7 -> 100)
d.blockchain.balanceAtHeight(account1.toAddress, 6, issueTx.asset) shouldBe None

d.appendBlock(TxHelpers.transfer(richAccount, account2.toAddress, 20.waves))
d.appendBlock(TxHelpers.transfer(richAccount, account2.toAddress, 700, issueTx.asset))

d.blockchain.balanceAtHeight(account2.toAddress, 12) shouldBe Some(11 -> 20.waves)
d.blockchain.balanceAtHeight(account2.toAddress, 11) shouldBe Some(11 -> 20.waves)
d.blockchain.balanceAtHeight(account2.toAddress, 10) shouldBe None

d.blockchain.balanceAtHeight(account2.toAddress, 12, issueTx.asset) shouldBe Some(12 -> 700)
d.blockchain.balanceAtHeight(account2.toAddress, 11, issueTx.asset) shouldBe None
}
}
}

private val HistoricalKeyTags = Seq(
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object Dependencies {
akkaModule("slf4j") % Runtime
)

private val rocksdb = "org.rocksdb" % "rocksdbjni" % "9.4.0"
private val rocksdb = "org.rocksdb" % "rocksdbjni" % "9.6.1"

lazy val node = Def.setting(
Seq(
Expand Down

0 comments on commit 0417cd7

Please sign in to comment.