Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vsuharnikov committed Dec 26, 2024
1 parent 383e730 commit a3f8e70
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MicroBlockMinerImpl(
case Retry =>
Task
.defer(generateMicroBlockSequence(account, accumulatedBlock, restTotalConstraint, lastMicroBlock))
.delayExecution(1 second)
.delayExecution((settings.microBlockInterval / 2).max(1.millis))
case Stop =>
setDebugState(MinerDebugInfo.MiningBlocks)
Task(log.debug("MicroBlock mining completed, block is full"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,17 @@ class BlockchainUpdaterImpl(
.orElse(lastBlockReward)
}

/** Referenced blockchain for mining.
* @return
* SnapshotBlockchain with a reward for a next height
* @note
* Do not use for other purposes
*/
def referencedBlockchain(reference: ByteStr): Blockchain =
ngState
.flatMap(ng =>
.flatMap { ng =>
if (ng.base.header.reference == reference)
Some(SnapshotBlockchain(rocksdb, ng.reward))
Some(SnapshotBlockchain(rocksdb, ng.reward)) // Same reward for a competitor's block, because same height
else
ng.snapshotOf(reference)
.map { case (forgedBlock, liquidSnapshot, carry, _, stateHash, _) =>
Expand All @@ -214,8 +220,8 @@ class BlockchainUpdaterImpl(
Some(stateHash)
)
}
)
.getOrElse(SnapshotBlockchain(rocksdb, computeNextReward))
}
.getOrElse(SnapshotBlockchain(rocksdb, computeNextReward)) // WARN: This seems not happen

override def processBlock(
block: Block,
Expand Down Expand Up @@ -317,8 +323,7 @@ class BlockchainUpdaterImpl(
)
} else
metrics.forgeBlockTimeStats.measureOptional(ng.snapshotOf(block.header.reference)) match {
case None =>
Left(BlockAppendError(s"References incorrect or non-existing block", block))
case None => Left(BlockAppendError(s"References incorrect or non-existing block", block))
case Some((referencedForgedBlock, referencedLiquidSnapshot, carry, totalFee, referencedComputedStateHash, discarded)) =>
if (!verify || referencedForgedBlock.signatureValid()) {
val height = rocksdb.heightOf(referencedForgedBlock.header.reference).getOrElse(0)
Expand Down Expand Up @@ -820,15 +825,17 @@ class BlockchainUpdaterImpl(
snapshotBlockchain.resolveERC20Address(address)
}

override def lastStateHash(refId: Option[ByteStr]): ByteStr =
override def lastStateHash(refId: Option[ByteStr]): ByteStr = readLock {
ngState
.map { ng =>
refId.filter(ng.contains).fold(ng.bestLiquidComputedStateHash)(id => ng.snapshotFor(id)._4)
}
.getOrElse(rocksdb.lastStateHash(None))
}

def snapshotBlockchain: SnapshotBlockchain =
def snapshotBlockchain: SnapshotBlockchain = readLock {
ngState.fold[SnapshotBlockchain](SnapshotBlockchain(rocksdb, StateSnapshot.empty))(SnapshotBlockchain(rocksdb, _))
}

// noinspection ScalaStyle,TypeAnnotation
private[this] object metrics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ object BlockAppender extends ScorexLogging {
)(newBlock: Block, snapshot: Option[BlockSnapshotResponse]): Task[Either[ValidationError, BlockApplyResult]] =
Task {
if (
blockchainUpdater
.isLastBlockId(newBlock.header.reference) || blockchainUpdater.lastBlockHeader.exists(_.header.reference == newBlock.header.reference)
blockchainUpdater.isLastBlockId(newBlock.header.reference) ||
blockchainUpdater.lastBlockHeader.exists(_.header.reference == newBlock.header.reference)
) {
if (newBlock.header.challengedHeader.isDefined) {
appendChallengeBlock(blockchainUpdater, utxStorage, pos, time, log, verify, txSignParCheck)(newBlock, snapshot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,6 @@ object BlockDiffer {
Either.cond(
!blockchain.supportsLightNodeBlockFields() || blockStateHash.contains(computedStateHash),
(),
InvalidStateHash(blockStateHash)
InvalidStateHash(blockStateHash, Some(computedStateHash))
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object TxValidationError {
override def toString: String = s"InvalidSignature(${entity.toString + " reason: " + details})"
}

case class InvalidStateHash(blockStateHash: Option[ByteStr]) extends ValidationError
case class InvalidStateHash(blockStateHash: Option[ByteStr], computedStateHash: Option[ByteStr]) extends ValidationError

sealed trait WithLog extends Product with Serializable {
def log: Log[Id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ class BlockChallengeTest
val challengingMiner = TxHelpers.signer(3)
withDomain(settings, balances = AddrWithBalance.enoughBalances(sender)) { d =>
d.appendBlock()
val txs = Seq(TxHelpers.transfer(sender, amount = 1), TxHelpers.transfer(sender, amount = 2))
val challengedBlock = d.createBlock(Block.ProtoBlockVersion, txs, generator = challengedMiner, stateHash = Some(Some(invalidStateHash)))
val invalidHashChallengingBlock = d.createChallengingBlock(challengingMiner, challengedBlock, stateHash = Some(Some(invalidStateHash)))
val missedHashChallengingBlock = d.createChallengingBlock(challengingMiner, challengedBlock, stateHash = Some(None))

d.appendBlockE(invalidHashChallengingBlock) shouldBe Left(InvalidStateHash(Some(invalidStateHash)))
d.appendBlockE(missedHashChallengingBlock) shouldBe Left(InvalidStateHash(None))
val txs = Seq(TxHelpers.transfer(sender, amount = 1), TxHelpers.transfer(sender, amount = 2))
val invalidChallengedBlock = d.createBlock(Block.ProtoBlockVersion, txs, generator = challengedMiner, stateHash = Some(Some(invalidStateHash)))
val invalidHashChallengingBlock = d.createChallengingBlock(challengingMiner, invalidChallengedBlock, stateHash = Some(Some(invalidStateHash)))
val missedHashChallengingBlock = d.createChallengingBlock(challengingMiner, invalidChallengedBlock, stateHash = Some(None))
val validChallengingBlock = d.createChallengingBlock(challengingMiner, invalidChallengedBlock)

d.appendBlockE(invalidHashChallengingBlock) shouldBe Left(InvalidStateHash(Some(invalidStateHash), validChallengingBlock.header.stateHash))
d.appendBlockE(missedHashChallengingBlock) shouldBe Left(InvalidStateHash(None, validChallengingBlock.header.stateHash))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class LightNodeTest extends PropSpec with WithDomain {
d.appendBlockE(
invalidBlock,
Some(BlockSnapshot(invalidBlock.id(), txSnapshots))
) shouldBe Left(InvalidStateHash(Some(invalidStateHash)))
) shouldBe Left(InvalidStateHash(Some(invalidStateHash), validBlock.header.stateHash))
d.lastBlock shouldBe prevBlock

d.appendBlockE(
Expand Down

0 comments on commit a3f8e70

Please sign in to comment.