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-2608 Removed Diff from tests #3903

Merged
merged 11 commits into from
Nov 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ class MicroblocksGenerationSuite extends BaseFreeSpec with TransferSending {
block <- miner.blockAt(2)
} yield {
block.transactions.size shouldBe maxTxs

val blockTxs = block.transactions.map(_.id)
val diff = uploadedTxs.map(_.id).toSet -- blockTxs
diff shouldBe empty
block.transactions.map(_.id) should contain theSameElementsAs uploadedTxs.map(_.id).toSet
},
3.minutes
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ class WideStateGenerationSuite extends BaseFreeSpec with WaitForHeight2 with Tra
} yield ()

val limit = GlobalTimer.instance.schedule(Future.failed(new TimeoutException("Time is out for test")), 18.minutes)
val testWithDumps = Future.firstCompletedOf(Seq(test, limit)).recoverWith {
case e =>
for {
_ <- dumpBalances()
dumps <- traverse(nodes)(dumpBlockChain)
} yield {
log.debug(dumps.mkString("Dumps:\n", "\n\n", "\n"))
throw e
}
val testWithDumps = Future.firstCompletedOf(Seq(test, limit)).recoverWith { case e =>
for {
_ <- dumpBalances()
dumps <- traverse(nodes)(dumpBlockChain)
} yield {
log.debug(dumps.mkString("Dumps:\n", "\n\n", "\n"))
throw e
}
}

Await.result(testWithDumps, 18.minutes)
Expand All @@ -84,13 +83,11 @@ class WideStateGenerationSuite extends BaseFreeSpec with WaitForHeight2 with Tra
for {
height <- node.height
blocks <- node.blockSeq(1, height)
} yield {
val txsInBlockchain = blocks.flatMap(_.transactions.map(_.id))
val diff = txIds -- txsInBlockchain
} yield
withClue(s"all transactions in node") {
diff shouldBe empty
val txsInBlockchain = blocks.flatMap(_.transactions.map(_.id))
txIds should contain theSameElementsAs txsInBlockchain
}
}
}

private def dumpBalances(): Future[Map[Config, Long]] = {
Expand All @@ -99,7 +96,7 @@ class WideStateGenerationSuite extends BaseFreeSpec with WaitForHeight2 with Tra
}.map(_.toMap)
.map { r =>
log.debug(s"""Balances:
|${r.map { case (config, balance) => s"${config.getString("address")} -> $balance" }.mkString("\n")}""".stripMargin)
|${r.map { case (config, balance) => s"${config.getString("address")} -> $balance" }.mkString("\n")}""".stripMargin)
r
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ object StateSnapshot {
}
.map(_.toMap)

private def assetStatics(issuedAssets: VectorMap[IssuedAsset, NewAssetInfo]): VectorMap[IssuedAsset, AssetStatic] =
def assetStatics(issuedAssets: VectorMap[IssuedAsset, NewAssetInfo]): VectorMap[IssuedAsset, AssetStatic] =
issuedAssets.map { case (asset, info) =>
asset ->
AssetStatic(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.wavesplatform.block.SignedBlockHeader
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.*
import com.wavesplatform.features.{BlockchainFeature, BlockchainFeatures}
import com.wavesplatform.history.SnapshotOps.*
import com.wavesplatform.lagonaki.mocks.TestBlock
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.lang.script.Script
Expand Down Expand Up @@ -148,12 +147,9 @@ trait BlockchainStubHelpers { self: MockFactoryBase =>
)
}

def transactionDiffer(time: Time = SystemTime, withFailed: Boolean = false)(tx: Transaction): TracedResult[ValidationError, Diff] = {
val snapshot =
if (withFailed) TransactionDiffer(Some(time.correctedTime()), time.correctedTime())(blockchain, tx)
else TransactionDiffer.forceValidate(Some(time.correctedTime()), time.correctedTime())(blockchain, tx)
snapshot.map(_.toDiff(blockchain))
}
def transactionDiffer(time: Time = SystemTime, withFailed: Boolean = false)(tx: Transaction): TracedResult[ValidationError, StateSnapshot] =
if (withFailed) TransactionDiffer(Some(time.correctedTime()), time.correctedTime())(blockchain, tx)
else TransactionDiffer.forceValidate(Some(time.correctedTime()), time.correctedTime())(blockchain, tx)

def transactionPublisher(time: Time = SystemTime): TransactionPublisher = (tx: Transaction, _: Option[Channel]) => {
val differ = transactionDiffer(time) _
Expand Down
60 changes: 32 additions & 28 deletions node/src/test/scala/com/wavesplatform/db/WithState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import com.wavesplatform.db.WithState.AddrWithBalance
import com.wavesplatform.events.BlockchainUpdateTriggers
import com.wavesplatform.features.BlockchainFeatures
import com.wavesplatform.features.BlockchainFeatures.LightNode
import com.wavesplatform.history.SnapshotOps.TransactionStateSnapshotExt
import com.wavesplatform.history.{Domain, SnapshotOps}
import com.wavesplatform.history.Domain
import com.wavesplatform.lagonaki.mocks.TestBlock
import com.wavesplatform.lagonaki.mocks.TestBlock.BlockWithSigner
import com.wavesplatform.lang.ValidationError
Expand All @@ -24,8 +23,10 @@ import com.wavesplatform.settings.{TestFunctionalitySettings as TFS, *}
import com.wavesplatform.state.diffs.{BlockDiffer, ENOUGH_AMT}
import com.wavesplatform.state.reader.SnapshotBlockchain
import com.wavesplatform.state.utils.TestRocksDB
import com.wavesplatform.state.{Blockchain, BlockchainUpdaterImpl, Diff, NgState, Portfolio, StateSnapshot, TxStateSnapshotHashBuilder}
import com.wavesplatform.state.{Blockchain, BlockchainUpdaterImpl, NgState, StateSnapshot, TxStateSnapshotHashBuilder}
import com.wavesplatform.test.*
import com.wavesplatform.transaction.Asset.Waves
import com.wavesplatform.transaction.TxHelpers.defaultAddress
import com.wavesplatform.transaction.smart.script.trace.TracedResult
import com.wavesplatform.transaction.{BlockchainUpdater, GenesisTransaction, Transaction, TxHelpers}
import com.wavesplatform.{NTPTime, TestHelpers}
Expand Down Expand Up @@ -112,7 +113,7 @@ trait WithState extends BeforeAndAfterAll with DBCacheSettings with Matchers wit
fs: FunctionalitySettings = TFS.Enabled,
enableExecutionLog: Boolean = false
)(
assertion: Either[ValidationError, Diff] => Unit
assertion: Either[ValidationError, StateSnapshot] => Unit
): Unit = withTestState(fs) { (bcu, state) =>
assertDiffEi(preconditions, block, bcu, state, enableExecutionLog)(assertion)
}
Expand All @@ -124,7 +125,7 @@ trait WithState extends BeforeAndAfterAll with DBCacheSettings with Matchers wit
state: RocksDBWriter,
enableExecutionLog: Boolean
)(
assertion: Either[ValidationError, Diff] => Unit
assertion: Either[ValidationError, StateSnapshot] => Unit
): Unit = {
def differ(blockchain: Blockchain, b: Block) =
BlockDiffer.fromBlock(
Expand All @@ -150,8 +151,11 @@ trait WithState extends BeforeAndAfterAll with DBCacheSettings with Matchers wit
preconditionBlock
)
}
val totalDiff1 = blockWithComputedStateHash(block.block, block.signer, bcu).resultE.flatMap(differ(state, _))
assertion(totalDiff1.map(_.snapshot.toDiff(state)))
val snapshot =
blockWithComputedStateHash(block.block, block.signer, bcu).resultE
.flatMap(differ(state, _))
.map(_.snapshot)
assertion(snapshot)
}

def assertDiffEiTraced(
Expand All @@ -160,7 +164,7 @@ trait WithState extends BeforeAndAfterAll with DBCacheSettings with Matchers wit
fs: FunctionalitySettings = TFS.Enabled,
enableExecutionLog: Boolean = false
)(
assertion: TracedResult[ValidationError, Diff] => Unit
assertion: TracedResult[ValidationError, StateSnapshot] => Unit
): Unit = withTestState(fs) { (bcu, state) =>
def getCompBlockchain(blockchain: Blockchain) = {
val reward = if (blockchain.height > 0) bcu.computeNextReward else None
Expand Down Expand Up @@ -196,17 +200,17 @@ trait WithState extends BeforeAndAfterAll with DBCacheSettings with Matchers wit
)
}

val totalDiff1 =
val snapshot1 =
(blockWithComputedStateHash(block.block, block.signer, bcu) match {
case right @ TracedResult(Right(_), _, _) => right.copy(trace = Nil)
case err => err
}).flatMap(differ(state, state.lastBlock, _))

assertion(totalDiff1.map(_.snapshot.toDiff(state)))
assertion(snapshot1.map(_.snapshot))
}

private def assertDiffAndState(preconditions: Seq[BlockWithSigner], block: BlockWithSigner, fs: FunctionalitySettings, withNg: Boolean)(
assertion: (Diff, Blockchain) => Unit
assertion: (StateSnapshot, Blockchain) => Unit
): Unit = withTestState(fs) { (bcu, state) =>
def getCompBlockchain(blockchain: Blockchain) =
if (withNg && fs.preActivatedFeatures.get(BlockchainFeatures.BlockReward.id).exists(_ <= blockchain.height)) {
Expand All @@ -226,8 +230,8 @@ trait WithState extends BeforeAndAfterAll with DBCacheSettings with Matchers wit

preconditions.foldLeft[Option[Block]](None) { (prevBlock, curBlock) =>
val preconditionBlock = blockWithComputedStateHash(curBlock.block, curBlock.signer, bcu).resultE.explicitGet()
val BlockDiffer.Result(diff, fees, totalFee, _, _, computedStateHash) = differ(state, prevBlock, preconditionBlock).explicitGet()
state.append(diff, fees, totalFee, None, preconditionBlock.header.generationSignature, computedStateHash, preconditionBlock)
val BlockDiffer.Result(snapshot, fees, totalFee, _, _, computedStateHash) = differ(state, prevBlock, preconditionBlock).explicitGet()
state.append(snapshot, fees, totalFee, None, preconditionBlock.header.generationSignature, computedStateHash, preconditionBlock)
Some(preconditionBlock)
}

Expand All @@ -245,22 +249,19 @@ trait WithState extends BeforeAndAfterAll with DBCacheSettings with Matchers wit
checkedBlock.header.generationSignature,
Map()
)
val cb = SnapshotBlockchain(state, ngState)
val diff = snapshot.toDiff(state)
assertion(diff, cb)

val cb = SnapshotBlockchain(state, ngState)
assertion(snapshot, cb)
state.append(snapshot, fees, totalFee, None, checkedBlock.header.generationSignature, computedStateHash, checkedBlock)

assertion(diff, state)
assertion(snapshot, state)
}

def assertNgDiffState(preconditions: Seq[BlockWithSigner], block: BlockWithSigner, fs: FunctionalitySettings = TFS.Enabled)(
assertion: (Diff, Blockchain) => Unit
assertion: (StateSnapshot, Blockchain) => Unit
): Unit =
assertDiffAndState(preconditions, block, fs, withNg = true)(assertion)

def assertDiffAndState(preconditions: Seq[BlockWithSigner], block: BlockWithSigner, fs: FunctionalitySettings = TFS.Enabled)(
assertion: (Diff, Blockchain) => Unit
assertion: (StateSnapshot, Blockchain) => Unit
): Unit =
assertDiffAndState(preconditions, block, fs, withNg = false)(assertion)

Expand Down Expand Up @@ -288,9 +289,8 @@ trait WithState extends BeforeAndAfterAll with DBCacheSettings with Matchers wit
val checkedBlock = blockWithComputedStateHash(block.block, block.signer, bcu).resultE.explicitGet()

differ(state, checkedBlock).map { result =>
val snapshot = SnapshotOps.fromDiff(result.snapshot.toDiff(state), state).explicitGet()
state.append(
snapshot,
result.snapshot,
result.carry,
result.totalFee,
None,
Expand All @@ -302,11 +302,15 @@ trait WithState extends BeforeAndAfterAll with DBCacheSettings with Matchers wit
})
}

def assertBalanceInvariant(diff: Diff): Unit = {
val portfolioDiff = diff.portfolios.values.fold(Portfolio())(_.combine(_).explicitGet())
portfolioDiff.balance shouldBe 0
portfolioDiff.effectiveBalance(false).explicitGet() shouldBe 0
all(portfolioDiff.assets.values) shouldBe 0
def assertBalanceInvariant(snapshot: StateSnapshot, db: RocksDBWriter, reward: Long = 0): Unit = {
snapshot.balances.toSeq
.map {
case ((`defaultAddress`, Waves), balance) => Waves -> (balance - db.balance(defaultAddress, Waves) - reward)
case ((address, asset), balance) => asset -> (balance - db.balance(address, asset))
}
.groupMap(_._1)(_._2)
.foreach { case (_, balances) => balances.sum shouldBe 0 }
snapshot.leaseBalances.foreach { case (address, balance) => balance shouldBe db.leaseBalance(address) }
}

def assertLeft(preconditions: Seq[BlockWithSigner], block: BlockWithSigner, fs: FunctionalitySettings = TFS.Enabled)(errorMessage: String): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,8 @@ class RideV6FailRejectTest extends FreeSpec with WithDomain with OptionValues wi
def failTxTest(invoke: Transaction): Unit = {
val complexity = ContractLimits.FailFreeInvokeComplexity + 1
test(complexity) { d =>
val diff = d.createDiffE(invoke).value
val (_, scriptResult) = diff.scriptResults.headOption.value
val snapshot = d.createDiffE(invoke).value
val (_, scriptResult) = snapshot.scriptResults.headOption.value
scriptResult.error.value.text should include(testCase.rejectError)

d.appendBlock(invoke)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ class BlockRewardSpec extends FreeSpec with WithDomain {
"when NG state is empty" in forAll(ngEmptyScenario) { case (miner1, miner2, b2s, b3, m3s) =>
withDomain(rewardSettings) { d =>
b2s.foldLeft[Option[Block]](None) { (prevBlock, curBlock) =>
val BlockDiffer.Result(diff, carryFee, totalFee, _, _, computedStateHash) = differ(d.rocksDBWriter, prevBlock, curBlock)
d.rocksDBWriter.append(diff, carryFee, totalFee, None, curBlock.header.generationSignature, computedStateHash, curBlock)
val BlockDiffer.Result(snapshot, carryFee, totalFee, _, _, computedStateHash) = differ(d.rocksDBWriter, prevBlock, curBlock)
d.rocksDBWriter.append(snapshot, carryFee, totalFee, None, curBlock.header.generationSignature, computedStateHash, curBlock)
Some(curBlock)
}

Expand Down
22 changes: 10 additions & 12 deletions node/src/test/scala/com/wavesplatform/history/Domain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.wavesplatform.database.{DBExt, Keys, RDB, RocksDBWriter}
import com.wavesplatform.events.BlockchainUpdateTriggers
import com.wavesplatform.features.BlockchainFeatures
import com.wavesplatform.features.BlockchainFeatures.{BlockV5, RideV6, LightNode}
import com.wavesplatform.history.SnapshotOps.TransactionStateSnapshotExt
import com.wavesplatform.lagonaki.mocks.TestBlock
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.lang.script.Script
Expand Down Expand Up @@ -60,15 +59,14 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri

val posSelector: PoSSelector = PoSSelector(blockchainUpdater, None)

val transactionDiffer: Transaction => TracedResult[ValidationError, Diff] =
TransactionDiffer(blockchain.lastBlockTimestamp, System.currentTimeMillis())(blockchain, _).map(_.toDiff(blockchain))
val transactionDiffer: Transaction => TracedResult[ValidationError, StateSnapshot] =
TransactionDiffer(blockchain.lastBlockTimestamp, System.currentTimeMillis())(blockchain, _)

val transactionDifferWithLog: Transaction => TracedResult[ValidationError, Diff] =
val transactionDifferWithLog: Transaction => TracedResult[ValidationError, StateSnapshot] =
TransactionDiffer(blockchain.lastBlockTimestamp, System.currentTimeMillis(), enableExecutionLog = true)(blockchain, _)
.map(_.toDiff(blockchain))

def createDiffE(tx: Transaction): Either[ValidationError, Diff] = transactionDiffer(tx).resultE
def createDiff(tx: Transaction): Diff = createDiffE(tx).explicitGet()
def createDiffE(tx: Transaction): Either[ValidationError, StateSnapshot] = transactionDiffer(tx).resultE
def createDiff(tx: Transaction): StateSnapshot = createDiffE(tx).explicitGet()

lazy val utxPool: UtxPoolImpl =
new UtxPoolImpl(SystemTime, blockchain, settings.utxSettings, settings.maxTxErrorLogSize, settings.minerSettings.enable)
Expand Down Expand Up @@ -123,7 +121,7 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri

def commonTransactionsApi(challenger: Option[BlockChallenger]): CommonTransactionsApi =
CommonTransactionsApi(
blockchainUpdater.bestLiquidSnapshot.map(diff => Height(blockchainUpdater.height) -> diff),
blockchainUpdater.bestLiquidSnapshot.map(Height(blockchainUpdater.height) -> _),
rdb,
blockchain,
utxPool,
Expand Down Expand Up @@ -175,8 +173,8 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri
.getOrElse(TestBlock.create(Nil).block)
}

def liquidDiff: Diff =
blockchainUpdater.bestLiquidSnapshot.orEmpty.toDiff(rocksDBWriter)
def liquidSnapshot: StateSnapshot =
blockchainUpdater.bestLiquidSnapshot.orEmpty

def microBlocks: Vector[MicroBlock] = blockchain.microblockIds.reverseIterator.flatMap(blockchain.microBlock).to(Vector)

Expand Down Expand Up @@ -211,7 +209,7 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri
AddressTransactions
.allAddressTransactions(
rdb,
blockchainUpdater.bestLiquidSnapshot.map(diff => Height(blockchainUpdater.height) -> diff),
blockchainUpdater.bestLiquidSnapshot.map(Height(blockchainUpdater.height) -> _),
address,
None,
Set.empty,
Expand Down Expand Up @@ -259,7 +257,7 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri
def appendAndAssertFailed(tx: Transaction, message: String): Block = {
appendBlock(tx)
assert(!blockchain.transactionSucceeded(tx.id()), s"should fail: $tx")
liquidDiff.errorMessage(tx.id()).get.text should include(message)
liquidSnapshot.errorMessage(tx.id()).get.text should include(message)
lastBlock
}

Expand Down
Loading