Skip to content

Commit

Permalink
Add a fix for the database
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Richez committed Aug 10, 2021
1 parent 1d57b44 commit e6d128c
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/main/scala/io/iohk/ethereum/nodebuilder/StdNode.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.iohk.ethereum.nodebuilder

import akka.actor.typed.ActorSystem
import akka.util.ByteString

import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Failure
import scala.util.Success
import scala.util.Try

import io.iohk.ethereum.blockchain.sync.SyncProtocol
import io.iohk.ethereum.consensus.mining.StdMiningBuilder
import io.iohk.ethereum.metrics.Metrics
Expand All @@ -17,7 +17,7 @@ import io.iohk.ethereum.network.ServerActor
import io.iohk.ethereum.network.discovery.PeerDiscoveryManager
import io.iohk.ethereum.nodebuilder.tooling.PeriodicConsistencyCheck
import io.iohk.ethereum.nodebuilder.tooling.StorageConsistencyChecker
import io.iohk.ethereum.utils.Config
import io.iohk.ethereum.utils.{Config, Hex}

/** A standard node is everything Ethereum prescribes except the mining algorithm,
* which is plugged in dynamically.
Expand All @@ -32,6 +32,8 @@ abstract class BaseNode extends Node {
def start(): Unit = {
startMetricsClient()

fixDatabase()

loadGenesisData()

runDBConsistencyCheck()
Expand Down Expand Up @@ -132,6 +134,24 @@ abstract class BaseNode extends Node {
tryAndLogFailure(() => Metrics.get().close())
tryAndLogFailure(() => storagesInstance.dataSource.close())
}

def fixDatabase(): Unit = {
// FIXME this is a temporary solution to avoid an incompatibility due to the introduction of the best block hash
// We can remove this fix when we release an incompatible version.
val bestBlockInfo = storagesInstance.storages.appStateStorage.getBestBlockInfo()
if (bestBlockInfo.hash == ByteString.empty && bestBlockInfo.number > 0) {
log.warn("Fixing best block hash into database for block {}", bestBlockInfo.number)
storagesInstance.storages.blockNumberMappingStorage.get(bestBlockInfo.number) match {
case Some(hash) =>
log.warn("Putting {} as the best block hash", Hex.toHexString(hash.toArray))
storagesInstance.storages.appStateStorage.putBestBlockInfo(bestBlockInfo.copy(hash = hash)).commit()
case None =>
log.error("No block found for number {} when trying to fix database", bestBlockInfo.number)
}

}

}
}

class StdNode extends BaseNode with StdMiningBuilder

0 comments on commit e6d128c

Please sign in to comment.