Skip to content

Commit 9ead4d4

Browse files
authored
NODE-2519 BU Correct updated waves amount in response (#3768)
1 parent 40af2a7 commit 9ead4d4

4 files changed

Lines changed: 78 additions & 15 deletions

File tree

grpc-server/src/main/scala/com/wavesplatform/events/Repo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Repo(db: DB, blocksApi: CommonBlocksApi)(implicit s: Scheduler) extends Bl
7373
db.put(keyForHeight(ls.keyBlock.height), ls.solidify().protobuf.update(_.append.block.optionalBlock := None).toByteArray)
7474
)
7575

76-
val ba = BlockAppended.from(block, diff, blockchainBeforeWithMinerReward)
76+
val ba = BlockAppended.from(block, diff, blockchainBeforeWithMinerReward, minerReward)
7777
liquidState = Some(LiquidState(ba, Seq.empty))
7878
handlers.forEach(_.handleUpdate(ba))
7979
}

grpc-server/src/main/scala/com/wavesplatform/events/events.scala

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import com.wavesplatform.transaction.assets.exchange.ExchangeTransaction
2424
import com.wavesplatform.transaction.lease.LeaseTransaction
2525
import com.wavesplatform.transaction.smart.InvokeScriptTransaction
2626
import com.wavesplatform.transaction.transfer.{MassTransferTransaction, TransferTransaction}
27-
import com.wavesplatform.transaction.{Asset, Authorized, EthereumTransaction, GenesisTransaction}
27+
import com.wavesplatform.transaction.{Asset, Authorized, EthereumTransaction}
2828

2929
import scala.collection.mutable
3030
import scala.collection.mutable.ArrayBuffer
@@ -557,21 +557,18 @@ final case class BlockAppended(
557557
}
558558

559559
object BlockAppended {
560-
def from(block: Block, diff: DetailedDiff, blockchainBeforeWithMinerReward: Blockchain): BlockAppended = {
560+
def from(block: Block, diff: DetailedDiff, blockchainBeforeWithMinerReward: Blockchain, minerReward: Option[Long]): BlockAppended = {
561+
val height = blockchainBeforeWithMinerReward.height
561562
val (blockStateUpdate, txsStateUpdates, txsMetadata, refAssets) =
562563
StateUpdate.container(blockchainBeforeWithMinerReward, diff, block.sender.toAddress)
563564

564565
// updatedWavesAmount can change as a result of either genesis transactions or miner rewards
565-
val updatedWavesAmount = blockchainBeforeWithMinerReward.height match {
566-
// genesis case
567-
case 0 => block.transactionData.collect { case GenesisTransaction(_, amount, _, _, _) => amount.value }.sum
568-
// miner reward case
569-
case height => blockchainBeforeWithMinerReward.wavesAmount(height).toLong
570-
}
566+
val wavesAmount = blockchainBeforeWithMinerReward.wavesAmount(height).toLong
567+
val updatedWavesAmount = wavesAmount + minerReward.filter(_ => height > 0).getOrElse(0L)
571568

572569
BlockAppended(
573570
block.id(),
574-
blockchainBeforeWithMinerReward.height + 1,
571+
height + 1,
575572
block,
576573
updatedWavesAmount,
577574
blockStateUpdate,

grpc-server/src/test/scala/com/wavesplatform/events/BlockchainUpdatesSpec.scala

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.wavesplatform.events.protobuf.BlockchainUpdated.Rollback.RollbackType
1414
import com.wavesplatform.events.protobuf.BlockchainUpdated.Update
1515
import com.wavesplatform.events.protobuf.serde.*
1616
import com.wavesplatform.events.protobuf.{TransactionMetadata, BlockchainUpdated as PBBlockchainUpdated}
17+
import com.wavesplatform.features.BlockchainFeatures.BlockReward
1718
import com.wavesplatform.history.Domain
1819
import com.wavesplatform.lang.directives.values.V5
1920
import com.wavesplatform.lang.v1.FunctionHeader
@@ -278,9 +279,74 @@ class BlockchainUpdatesSpec extends FreeSpec with WithBUDomain with ScalaFutures
278279
)
279280
}
280281

281-
"should include correct waves amount" in withNEmptyBlocksSubscription(settings = currentSettings) { result =>
282-
val balances = result.collect { case b if b.update.isAppend => b.getAppend.getBlock.updatedWavesAmount }
283-
balances shouldBe Seq(10000000000000000L, 10000000600000000L, 10000001200000000L)
282+
"should include correct waves amount" - {
283+
val totalWaves = 100_000_000_0000_0000L
284+
val reward = 6_0000_0000
285+
286+
"on preactivated block reward" in {
287+
val settings = currentSettings.setFeaturesHeight((BlockReward, 0))
288+
289+
withDomainAndRepo(settings) { case (d, repo) =>
290+
d.appendBlock()
291+
d.blockchain.wavesAmount(1) shouldBe totalWaves + reward
292+
repo.getBlockUpdate(1).getUpdate.vanillaAppend.updatedWavesAmount shouldBe totalWaves + reward
293+
294+
d.appendBlock()
295+
d.blockchain.wavesAmount(2) shouldBe totalWaves + reward * 2
296+
repo.getBlockUpdate(2).getUpdate.vanillaAppend.updatedWavesAmount shouldBe totalWaves + reward * 2
297+
}
298+
}
299+
300+
"on activation of block reward" in {
301+
val settings = currentSettings.setFeaturesHeight((BlockReward, 3))
302+
303+
withNEmptyBlocksSubscription(settings = settings, count = 3) { result =>
304+
val balances = result.collect { case b if b.update.isAppend => b.getAppend.getBlock.updatedWavesAmount }
305+
balances shouldBe Seq(totalWaves, totalWaves, totalWaves + reward, totalWaves + reward * 2)
306+
}
307+
308+
withDomainAndRepo(settings) { case (d, repo) =>
309+
d.appendBlock()
310+
d.blockchain.wavesAmount(1) shouldBe totalWaves
311+
repo.getBlockUpdate(1).getUpdate.vanillaAppend.updatedWavesAmount shouldBe totalWaves
312+
313+
d.appendBlock()
314+
d.blockchain.wavesAmount(2) shouldBe totalWaves
315+
repo.getBlockUpdate(2).getUpdate.vanillaAppend.updatedWavesAmount shouldBe totalWaves
316+
317+
d.appendBlock()
318+
d.blockchain.wavesAmount(3) shouldBe totalWaves + reward
319+
repo.getBlockUpdate(3).getUpdate.vanillaAppend.updatedWavesAmount shouldBe totalWaves + reward
320+
321+
d.appendBlock()
322+
d.blockchain.wavesAmount(4) shouldBe totalWaves + reward * 2
323+
repo.getBlockUpdate(4).getUpdate.vanillaAppend.updatedWavesAmount shouldBe totalWaves + reward * 2
324+
}
325+
}
326+
327+
"on rollbacks" in {
328+
withDomainAndRepo(currentSettings) { case (d, repo) =>
329+
d.appendBlock()
330+
331+
// block and micro append
332+
val block = d.appendBlock()
333+
block.sender shouldBe defaultSigner.publicKey
334+
335+
d.appendMicroBlock(TxHelpers.transfer(defaultSigner))
336+
d.blockchain.wavesAmount(2) shouldBe totalWaves + reward * 2
337+
repo.getBlockUpdate(2).getUpdate.vanillaAppend.updatedWavesAmount shouldBe totalWaves + reward * 2
338+
339+
// micro rollback
340+
d.appendKeyBlock(Some(block.id()))
341+
d.blockchain.wavesAmount(3) shouldBe totalWaves + reward * 3
342+
repo.getBlockUpdate(3).getUpdate.vanillaAppend.updatedWavesAmount shouldBe totalWaves + reward * 3
343+
344+
// block rollback
345+
d.rollbackTo(2)
346+
d.blockchain.wavesAmount(2) shouldBe totalWaves + reward * 2
347+
repo.getBlockUpdate(2).getUpdate.vanillaAppend.updatedWavesAmount shouldBe totalWaves + reward * 2
348+
}
349+
}
284350
}
285351

286352
"should include correct heights" in withNEmptyBlocksSubscription(settings = currentSettings) { result =>

node/src/main/scala/com/wavesplatform/state/BlockchainUpdaterImpl.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,11 @@ class BlockchainUpdaterImpl(
333333
block,
334334
hitSource,
335335
differResult.carry,
336-
reward
336+
None
337337
)
338338
miner.scheduleMining(Some(tempBlockchain))
339339

340-
blockchainUpdateTriggers.onProcessBlock(block, differResult.detailedDiff, reward, referencedBlockchain)
340+
blockchainUpdateTriggers.onProcessBlock(block, differResult.detailedDiff, reward, this)
341341

342342
leveldb.append(liquidDiffWithCancelledLeases, carry, totalFee, prevReward, prevHitSource, referencedForgedBlock)
343343
BlockStats.appended(referencedForgedBlock, referencedLiquidDiff.scriptsComplexity)

0 commit comments

Comments
 (0)