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

Remove invalid headers on chain repair #1856

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/resources/api/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: "3.0.2"

info:
version: "4.0.103"
version: "4.0.104"
title: Ergo Node API
description: API docs for Ergo Node. Models are shared between all Ergo products
contact:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ scorex {
nodeName = "ergo-node"

# Network protocol version to be sent in handshakes
appVersion = 4.0.103
appVersion = 4.0.104

# Network agent name. May contain information about client code
# stack, starting from core code-base up to the end graphical interface.
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/mainnet.conf
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ scorex {
network {
magicBytes = [1, 0, 2, 4]
bindAddress = "0.0.0.0:9030"
nodeName = "ergo-mainnet-4.0.103"
nodeName = "ergo-mainnet-4.0.104"
nodeName = ${?NODENAME}
knownPeers = [
"213.239.193.208:9030",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,9 @@ object ErgoNodeViewHolder {
history.bestFullBlockOpt
.filter(_.id != lastMod.id)
.fold("")(fb => s"\n best full block: $fb")
val repairNeeded = ErgoHistory.repairIfNeeded(history)
ErgoHistory.repairIfNeeded(history) // todo: do we need to do it?
ChainIsStuck(s"Chain not modified for $chainUpdateDelay ms, headers-height: $headersHeight, " +
s"block-height $blockHeight, chain synced: $chainSynced, repair needed: $repairNeeded, " +
s"last modifier applied: $lastMod, " +
s"block-height $blockHeight, chain synced: $chainSynced, last modifier applied: $lastMod, " +
s"possible best full block $bestFullBlockOpt")
} else {
ChainIsHealthy
Expand Down
47 changes: 34 additions & 13 deletions src/main/scala/org/ergoplatform/nodeView/history/ErgoHistory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import org.ergoplatform.mining.AutolykosPowScheme
import org.ergoplatform.modifiers.history._
import org.ergoplatform.modifiers.history.header.{Header, PreGenesisHeader}
import org.ergoplatform.modifiers.state.UTXOSnapshotChunk
import org.ergoplatform.modifiers.{NonHeaderBlockSection, ErgoFullBlock, BlockSection}
import org.ergoplatform.modifiers.{BlockSection, ErgoFullBlock, NonHeaderBlockSection}
import org.ergoplatform.nodeView.history.storage.HistoryStorage
import org.ergoplatform.nodeView.history.storage.modifierprocessors._
import org.ergoplatform.nodeView.history.storage.modifierprocessors.popow.{EmptyPoPoWProofsProcessor, FullPoPoWProofsProcessor}
import org.ergoplatform.settings._
import org.ergoplatform.utils.LoggingUtil
import scorex.core.consensus.ModifierSemanticValidity.Invalid
import scorex.core.consensus.ProgressInfo
import scorex.core.utils.NetworkTimeProvider
import scorex.core.validation.RecoverableModifierError
import scorex.util.{ModifierId, ScorexLogging, idToBytes}

import scala.annotation.tailrec
import scala.util.{Failure, Success, Try}

/**
Expand Down Expand Up @@ -219,7 +221,7 @@ trait ErgoHistory
log.info(s"Result of removing header $headerId: " + hRes)

hOpt.foreach { h =>
requiredModifiersForHeader(h).foreach { case (_, mId) =>
h.sectionIds.foreach { case (_, mId) =>
val mRes = historyStorage.remove(
indicesToRemove = Array(validityKey(mId)),
idsToRemove = Array(mId)
Expand Down Expand Up @@ -258,21 +260,40 @@ object ErgoHistory extends ScorexLogging {

// check if there is possible database corruption when there is header after
// recognized blockchain tip marked as invalid
protected[nodeView] def repairIfNeeded(history: ErgoHistory): Boolean = history.historyStorage.synchronized {
protected[nodeView] def repairIfNeeded(history: ErgoHistory): Unit = history.historyStorage.synchronized {
val RepairDepth = 128

val bestHeaderHeight = history.headersHeight
val bestFullBlockHeight = history.bestFullBlockOpt.map(_.height).getOrElse(-1)
val afterHeaders = history.headerIdsAtHeight(bestHeaderHeight + 1)

if (bestHeaderHeight == bestFullBlockHeight && afterHeaders.nonEmpty) {
log.warn("Found suspicious continuation, clearing it...")
afterHeaders.map { hId =>
history.forgetHeader(hId)
@tailrec
def checkHeightsFrom(h: Int): Unit = {
val headerIds = history.headerIdsAtHeight(h)
if (headerIds.nonEmpty) {
val notInvalidHeaders = headerIds.filter { headerId =>
if (history.isSemanticallyValid(headerId) == Invalid) {
log.warn(s"Clearing invalid header: $headerId at height $h")
history.forgetHeader(headerId)
false
} else {
true
}
}
val updatedHeightIdsValue: Array[Byte] = notInvalidHeaders.foldLeft(Array.empty[Byte]) { case (acc, id) =>
acc ++ idToBytes(id)
}
if(updatedHeightIdsValue.isEmpty) {
//could be the case after bestHeaderHeight
history.historyStorage.remove(Array(history.heightIdsKey(h)), Nil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also remove the Modifier at this height? Not just index? Or is there an idempotency in place that the modifer gets overriden if index is empty?

} else {
history.historyStorage.insert(Array(history.heightIdsKey(h) -> updatedHeightIdsValue), Nil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what we are updating the valid header index with and why, that would need at least a comment

}
checkHeightsFrom(h + 1)
}
history.historyStorage.remove(Array(history.heightIdsKey(bestHeaderHeight + 1)), Nil)
true
} else {
false
}

log.info("Checking invalid headers started")
checkHeightsFrom(bestHeaderHeight - RepairDepth)
log.info("Checking invalid headers finished")
}

def readOrGenerate(ergoSettings: ErgoSettings, ntp: NetworkTimeProvider): ErgoHistory = {
Expand Down