Skip to content

Commit

Permalink
NODE-2609 Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mashonskii committed Oct 3, 2023
1 parent 49bd78f commit b0fb812
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 34 deletions.
30 changes: 18 additions & 12 deletions node/src/test/scala/com/wavesplatform/history/Domain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri
generator: KeyPair = defaultSigner,
stateHash: Option[Option[ByteStr]] = None,
challengedHeader: Option[ChallengedHeader] = None,
rewardVote: Long = -1L
): Block = createBlockE(version, txs, ref, strictTime, generator, stateHash, challengedHeader, rewardVote).explicitGet()
rewardVote: Long = -1L,
timestamp: Option[Long] = None
): Block = createBlockE(version, txs, ref, strictTime, generator, stateHash, challengedHeader, rewardVote, timestamp).explicitGet()

def createBlockE(
version: Byte,
Expand All @@ -392,7 +393,8 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri
generator: KeyPair = defaultSigner,
stateHash: Option[Option[ByteStr]] = None,
challengedHeader: Option[ChallengedHeader] = None,
rewardVote: Long = -1L
rewardVote: Long = -1L,
timestamp: Option[Long] = None
): Either[ValidationError, Block] = {
val reference = ref.getOrElse(randomSig)

Expand All @@ -401,12 +403,16 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri
val greatGrandParent = blockchain.blockHeader(parentHeight - 2).map(_.header)

for {
timestamp <-
if (blockchain.height > 0)
posSelector
.getValidBlockDelay(blockchain.height, generator, parent.baseTarget, blockchain.balance(generator.toAddress) max 1e11.toLong)
.map(_ + parent.timestamp)
else
resultTimestamp <-
if (blockchain.height > 0) {
timestamp
.map(Right(_))
.getOrElse(
posSelector
.getValidBlockDelay(blockchain.height, generator, parent.baseTarget, blockchain.balance(generator.toAddress) max 1e11.toLong)
.map(_ + parent.timestamp)
)
} else
Right(System.currentTimeMillis() - (1 hour).toMillis)
consensus <-
if (blockchain.height > 0)
Expand All @@ -418,7 +424,7 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri
parent.baseTarget,
parent.timestamp,
greatGrandParent.map(_.timestamp),
timestamp
resultTimestamp
)
else Right(NxtLikeConsensusBlockData(60, generationSignature))
resultBt =
Expand All @@ -429,7 +435,7 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri
blockWithoutStateHash <- Block
.buildAndSign(
version = if (consensus.generationSignature.size == 96) Block.ProtoBlockVersion else version,
timestamp = if (strictTime) timestamp else SystemTime.getTimestamp(),
timestamp = if (strictTime) resultTimestamp else SystemTime.getTimestamp(),
reference = reference,
baseTarget = resultBt,
generationSignature = consensus.generationSignature,
Expand Down Expand Up @@ -471,7 +477,7 @@ case class Domain(rdb: RDB, blockchainUpdater: BlockchainUpdaterImpl, rocksDBWri
resultBlock <- Block
.buildAndSign(
version = if (consensus.generationSignature.size == 96) Block.ProtoBlockVersion else version,
timestamp = if (strictTime) timestamp else SystemTime.getTimestamp(),
timestamp = if (strictTime) resultTimestamp else SystemTime.getTimestamp(),
reference = reference,
baseTarget = resultBt,
generationSignature = consensus.generationSignature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,16 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
val challengingMiner = d.wallet.generateNewAccount().get
d.appendBlock(TxHelpers.transfer(TxHelpers.defaultSigner, challengingMiner.toAddress, 1000.waves))
(1 to 999).foreach(_ => d.appendBlock())
appendAndCheck(d.createBlock(Block.ProtoBlockVersion, Seq.empty, strictTime = true, stateHash = Some(Some(invalidStateHash))), d) { block =>
appendAndCheck(
d.createBlock(
Block.ProtoBlockVersion,
Seq.empty,
strictTime = true,
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
),
d
) { block =>
block.header.challengedHeader shouldBe defined
val challengedHeader = block.header.challengedHeader.get

Expand All @@ -142,7 +151,14 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
(1 to 999).foreach(_ => d.appendBlock())

appendAndCheck(
d.createBlock(Block.ProtoBlockVersion, Seq.empty, strictTime = true, generator = challengedMiner, stateHash = Some(Some(invalidStateHash))),
d.createBlock(
Block.ProtoBlockVersion,
Seq.empty,
strictTime = true,
generator = challengedMiner,
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
),
d
) { block =>
block.header.challengedHeader shouldBe defined
Expand All @@ -153,7 +169,14 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
(1 to 999).foreach(_ => d.appendBlock())

appendAndCheck(
d.createBlock(Block.ProtoBlockVersion, Seq.empty, strictTime = true, generator = challengedMiner, stateHash = Some(Some(invalidStateHash))),
d.createBlock(
Block.ProtoBlockVersion,
Seq.empty,
strictTime = true,
generator = challengedMiner,
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
),
d
) { block =>
block.header.challengedHeader shouldBe defined
Expand Down Expand Up @@ -189,7 +212,13 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
val challengingMiner = d.wallet.generateNewAccount().get
d.appendBlock(TxHelpers.transfer(TxHelpers.defaultSigner, challengingMiner.toAddress, 1000.waves))
(1 to 999).foreach(_ => d.appendBlock())
val originalBlock = d.createBlock(Block.ProtoBlockVersion, Seq.empty, strictTime = true, stateHash = Some(Some(invalidStateHash)))
val originalBlock = d.createBlock(
Block.ProtoBlockVersion,
Seq.empty,
strictTime = true,
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)
appendAndCheck(originalBlock, d) { block =>
block.header.challengedHeader shouldBe defined
val challengedHeader = block.header.challengedHeader.get
Expand Down Expand Up @@ -232,7 +261,13 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
val challengingMiner = d.wallet.generateNewAccount().get
d.appendBlock(TxHelpers.transfer(TxHelpers.defaultSigner, challengingMiner.toAddress, 1000.waves))
(1 to 999).foreach(_ => d.appendBlock())
val originalBlock = d.createBlock(Block.ProtoBlockVersion, Seq.empty, strictTime = true, stateHash = Some(Some(invalidStateHash)))
val originalBlock = d.createBlock(
Block.ProtoBlockVersion,
Seq.empty,
strictTime = true,
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)
appendAndCheck(originalBlock, d) { block =>
block.transactionData shouldBe originalBlock.transactionData
}
Expand Down Expand Up @@ -269,7 +304,13 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
val challengingMiner = d.wallet.generateNewAccount().get
d.appendBlock(TxHelpers.transfer(TxHelpers.defaultSigner, challengingMiner.toAddress, 1000.waves))
(1 to 999).foreach(_ => d.appendBlock())
val originalBlock = d.createBlock(Block.ProtoBlockVersion, Seq.empty, strictTime = true, stateHash = Some(Some(invalidStateHash)))
val originalBlock = d.createBlock(
Block.ProtoBlockVersion,
Seq.empty,
strictTime = true,
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)
appendAndCheck(originalBlock, d) { block =>
block.header.reference shouldBe originalBlock.header.reference
}
Expand Down Expand Up @@ -651,7 +692,8 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
Seq(challengedBlockTx),
strictTime = true,
generator = challengedMiner,
stateHash = Some(Some(invalidStateHash))
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)

appendAndCheck(originalBlock, d) { block =>
Expand Down Expand Up @@ -970,7 +1012,8 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
Block.ProtoBlockVersion,
Seq.empty,
strictTime = true,
stateHash = Some(Some(invalidStateHash))
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)

appendAndCheck(originalBlock, d) { block =>
Expand Down Expand Up @@ -999,22 +1042,16 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
val challengingMiner = d.wallet.generateNewAccount().get

d.appendBlock(TxHelpers.transfer(defaultSigner, challengingMiner.toAddress, 1000.waves))
(1 to 999).foreach(_ => d.appendBlock())
d.appendBlock(
d.createBlock(
Block.ProtoBlockVersion,
Seq.empty,
strictTime = true
)
)
(1 to 1000).foreach(_ => d.appendBlock())

d.blockchain.isFeatureActivated(BlockchainFeatures.TransactionStateSnapshot) shouldBe false

val originalBlock = d.createBlock(
Block.ProtoBlockVersion,
Seq.empty,
strictTime = true,
stateHash = Some(Some(invalidStateHash))
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)

appendAndCheck(originalBlock, d) { block =>
Expand Down Expand Up @@ -1551,7 +1588,14 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes

val txs = Seq(TxHelpers.transfer(amount = 1.waves), TxHelpers.transfer(amount = 2.waves))
val invalidBlock =
d.createBlock(Block.ProtoBlockVersion, txs, strictTime = true, generator = challengedMiner, stateHash = Some(Some(invalidStateHash)))
d.createBlock(
Block.ProtoBlockVersion,
txs,
strictTime = true,
generator = challengedMiner,
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)

val blockChallenger: Option[BlockChallenger] =
Some(
Expand Down Expand Up @@ -1718,7 +1762,14 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
(1 to 999).foreach(_ => d.appendBlock())
val txs = Seq(TxHelpers.transfer(sender, amount = 1), TxHelpers.transfer(sender, amount = 2))
val originalBlock =
d.createBlock(Block.ProtoBlockVersion, txs, strictTime = true, generator = challengedMiner, stateHash = Some(Some(invalidStateHash)))
d.createBlock(
Block.ProtoBlockVersion,
txs,
strictTime = true,
generator = challengedMiner,
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)

txs.foreach(d.utxPool.putIfNew(_))
d.utxPool.size shouldBe txs.size
Expand Down Expand Up @@ -1854,7 +1905,8 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
txs,
strictTime = true,
generator = challengedMiner,
stateHash = Some(Some(invalidStateHash))
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)

appendAndCheck(originalBlock, d)(_ => (1 to 10).foreach(_ => d.appendBlock()))
Expand All @@ -1870,7 +1922,8 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
txs,
strictTime = true,
generator = challengedMiner,
stateHash = Some(Some(invalidStateHash))
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)

appendAndCheck(originalBlock, d)(_ => ())
Expand All @@ -1888,7 +1941,8 @@ class BlockChallengeTest extends PropSpec with WithDomain with ScalatestRouteTes
txs,
strictTime = true,
generator = challengedMiner,
stateHash = Some(Some(invalidStateHash))
stateHash = Some(Some(invalidStateHash)),
timestamp = Some(Long.MaxValue)
)

appendAndCheck(originalBlock, d)(_ => ())
Expand Down

0 comments on commit b0fb812

Please sign in to comment.