Skip to content

Commit

Permalink
NODE-2609 Fix generating balance restoration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mashonskii committed Sep 30, 2023
1 parent 6788996 commit 7bed2d4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1000,11 +1000,9 @@ class RocksDBWriter(
}

override def balanceSnapshots(address: Address, from: Int, to: Option[BlockId]): Seq[BalanceSnapshot] = readOnly { db =>
addressId(address).fold(Seq(BalanceSnapshot(1, 0, 0, 0, isBanned = false))) { addressId =>
addressId(address).fold(Seq(BalanceSnapshot(1, 0, 0, 0))) { addressId =>
val toHeight = to.flatMap(this.heightOf).getOrElse(this.height)

val banHeights = effectiveBalanceBanHeights(address).toSet

val lastBalance = balancesCache.get((address, Asset.Waves))
val lastLeaseBalance = leaseBalanceCache.get(address)

Expand Down Expand Up @@ -1035,9 +1033,8 @@ class RocksDBWriter(
wb = balanceAtHeightCache.get((wh, addressId), () => db.get(Keys.wavesBalanceAt(addressId, Height(wh))))
lb = leaseBalanceAtHeightCache.get((lh, addressId), () => db.get(Keys.leaseBalanceAt(addressId, Height(lh))))
} yield {
val height = wh.max(lh)
val isBanned = banHeights.contains(height)
BalanceSnapshot(height, wb.balance, lb.in, lb.out, isBanned)
val height = wh.max(lh)
BalanceSnapshot(height, wb.balance, lb.in, lb.out)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.wavesplatform.state

case class BalanceSnapshot(height: Int, regularBalance: Long, leaseIn: Long, leaseOut: Long, isBanned: Boolean) {
lazy val effectiveBalance = if (!isBanned) regularBalance + leaseIn - leaseOut else 0L
case class BalanceSnapshot(height: Int, regularBalance: Long, leaseIn: Long, leaseOut: Long) {
lazy val effectiveBalance = regularBalance + leaseIn - leaseOut
}

object BalanceSnapshot {
def apply(height: Int, p: Portfolio, isBanned: Boolean): BalanceSnapshot =
BalanceSnapshot(height, p.balance, p.lease.in, p.lease.out, isBanned)
def apply(height: Int, p: Portfolio): BalanceSnapshot =
BalanceSnapshot(height, p.balance, p.lease.in, p.lease.out)
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ case class SnapshotBlockchain(
} else {
val balance = this.balance(address)
val lease = this.leaseBalance(address)
val bs = BalanceSnapshot(this.height, Portfolio(balance, lease), this.hasBannedEffectiveBalance(address, this.height))
val bs = BalanceSnapshot(this.height, Portfolio(balance, lease))
val height2Fix = this.height == 2 && inner.isFeatureActivated(RideV6) && from < this.height
if (inner.height > 0 && (from < this.height - 1 || height2Fix))
bs +: inner.balanceSnapshots(address, from, None) // to == this liquid block, so no need to pass block id to inner blockchain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class MiningFailuresSuite extends FlatSpec with PathMockFactory with WithNewDBFo
Right(Applied(Nil, 0))
}
.once()
(blockchainUpdater.balanceSnapshots _).when(*, *, *).returning(Seq(BalanceSnapshot(1, ENOUGH_AMT, 0, 0, isBanned = false)))
(blockchainUpdater.balanceSnapshots _).when(*, *, *).returning(Seq(BalanceSnapshot(1, ENOUGH_AMT, 0, 0)))

val account = accountGen.sample.get
val generateBlock = generateBlockTask(miner)(account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1742,16 +1742,19 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
}

val challengedMiner = TxHelpers.signer(1)
withDomain(settings, balances = AddrWithBalance.enoughBalances(TxHelpers.defaultSigner)) { d =>
val sender = TxHelpers.signer(2)
withDomain(settings, balances = AddrWithBalance.enoughBalances(sender)) { d =>
val challengingMiner = d.wallet.generateNewAccount().get
val challengedMinerBalance = 2000.waves
d.appendBlock(
TxHelpers.transfer(TxHelpers.defaultSigner, challengingMiner.toAddress, 1000.waves),
TxHelpers.transfer(TxHelpers.defaultSigner, challengedMiner.toAddress, challengedMinerBalance)
TxHelpers.transfer(sender, challengingMiner.toAddress, 1000.waves),
TxHelpers.transfer(sender, challengedMiner.toAddress, challengedMinerBalance)
)
(1 to 999).foreach(_ => d.appendBlock())
val transferAmount = 1.waves
val txs = Seq(TxHelpers.transfer(sender, challengedMiner.toAddress, transferAmount))
val originalBlock =
d.createBlock(Block.ProtoBlockVersion, Seq.empty, strictTime = true, generator = challengedMiner, stateHash = Some(Some(invalidStateHash)))
d.createBlock(Block.ProtoBlockVersion, txs, strictTime = true, generator = challengedMiner, stateHash = Some(Some(invalidStateHash)))
val challengingBlock = d.createChallengingBlock(challengingMiner, originalBlock)

val appender = createBlockAppender(d)
Expand All @@ -1767,7 +1770,7 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
tryToAppendBlock(d, challengedMiner, appender) should produce(genBalanceError)
}
d.appendBlock()
d.accountsApi.balanceDetails(challengedMiner.toAddress).explicitGet().generating shouldBe challengedMinerBalance
d.accountsApi.balanceDetails(challengedMiner.toAddress).explicitGet().generating shouldBe challengedMinerBalance + transferAmount
tryToAppendBlock(d, challengedMiner, appender) should beRight
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,53 +61,53 @@ class StateReaderEffectiveBalancePropertyTest extends PropSpec with WithDomain {
withDomain(settings) { d =>
d.appendBlock()
d.blockchain.balanceSnapshots(defaultAddress, 1, None) shouldBe List(
BalanceSnapshot(1, 600000000, 0, 0, false)
BalanceSnapshot(1, 600000000, 0, 0)
)

d.appendMicroBlock(transfer(amount = 1))
d.appendKeyBlock()
d.blockchain.balanceSnapshots(defaultAddress, 1, None) shouldBe (
if (fixed)
List(
BalanceSnapshot(2, 1199999999, 0, 0, false),
BalanceSnapshot(1, 599399999, 0, 0, false)
BalanceSnapshot(2, 1199999999, 0, 0),
BalanceSnapshot(1, 599399999, 0, 0)
)
else
List(BalanceSnapshot(2, 1199999999, 0, 0, false))
List(BalanceSnapshot(2, 1199999999, 0, 0))
)
d.blockchain.balanceSnapshots(defaultAddress, 2, None) shouldBe List(
BalanceSnapshot(2, 1199999999, 0, 0, false)
BalanceSnapshot(2, 1199999999, 0, 0)
)

d.appendMicroBlock(transfer(amount = 1))
d.appendKeyBlock()
d.blockchain.balanceSnapshots(defaultAddress, 1, None) shouldBe List(
BalanceSnapshot(3, 1799999998, 0, 0, false),
BalanceSnapshot(2, 1199399998, 0, 0, false),
BalanceSnapshot(1, 599399999, 0, 0, false)
BalanceSnapshot(3, 1799999998, 0, 0),
BalanceSnapshot(2, 1199399998, 0, 0),
BalanceSnapshot(1, 599399999, 0, 0)
)
d.blockchain.balanceSnapshots(defaultAddress, 2, None) shouldBe List(
BalanceSnapshot(3, 1799999998, 0, 0, false)
BalanceSnapshot(3, 1799999998, 0, 0)
)
d.blockchain.balanceSnapshots(defaultAddress, 3, None) shouldBe List(
BalanceSnapshot(3, 1799999998, 0, 0, false)
BalanceSnapshot(3, 1799999998, 0, 0)
)

d.appendMicroBlock(transfer(amount = 1))
d.appendKeyBlock()
d.blockchain.balanceSnapshots(defaultAddress, 1, None) shouldBe List(
BalanceSnapshot(4, 2399999997L, 0, 0, false),
BalanceSnapshot(3, 1799399997, 0, 0, false),
BalanceSnapshot(2, 1199399998, 0, 0, false),
BalanceSnapshot(1, 599399999, 0, 0, false)
BalanceSnapshot(4, 2399999997L, 0, 0),
BalanceSnapshot(3, 1799399997, 0, 0),
BalanceSnapshot(2, 1199399998, 0, 0),
BalanceSnapshot(1, 599399999, 0, 0)
)
d.blockchain.balanceSnapshots(defaultAddress, 2, None) shouldBe List(
BalanceSnapshot(4, 2399999997L, 0, 0, false),
BalanceSnapshot(3, 1799399997, 0, 0, false),
BalanceSnapshot(2, 1199399998, 0, 0, false)
BalanceSnapshot(4, 2399999997L, 0, 0),
BalanceSnapshot(3, 1799399997, 0, 0),
BalanceSnapshot(2, 1199399998, 0, 0)
)
d.blockchain.balanceSnapshots(defaultAddress, 3, None) shouldBe List(
BalanceSnapshot(4, 2399999997L, 0, 0, false)
BalanceSnapshot(4, 2399999997L, 0, 0)
)
}

Expand Down

0 comments on commit 7bed2d4

Please sign in to comment.