11package io .iohk .ethereum .nodebuilder
22
33import akka .actor .typed .ActorSystem
4+ import akka .util .ByteString
45
56import scala .concurrent .Await
67import scala .concurrent .ExecutionContext .Implicits .global
78import scala .util .Failure
89import scala .util .Success
910import scala .util .Try
10-
1111import io .iohk .ethereum .blockchain .sync .SyncProtocol
1212import io .iohk .ethereum .consensus .mining .StdMiningBuilder
1313import io .iohk .ethereum .metrics .Metrics
@@ -17,7 +17,7 @@ import io.iohk.ethereum.network.ServerActor
1717import io .iohk .ethereum .network .discovery .PeerDiscoveryManager
1818import io .iohk .ethereum .nodebuilder .tooling .PeriodicConsistencyCheck
1919import io .iohk .ethereum .nodebuilder .tooling .StorageConsistencyChecker
20- import io .iohk .ethereum .utils .Config
20+ import io .iohk .ethereum .utils .{ Config , Hex }
2121
2222/** A standard node is everything Ethereum prescribes except the mining algorithm,
2323 * which is plugged in dynamically.
@@ -32,6 +32,8 @@ abstract class BaseNode extends Node {
3232 def start (): Unit = {
3333 startMetricsClient()
3434
35+ fixDatabase()
36+
3537 loadGenesisData()
3638
3739 runDBConsistencyCheck()
@@ -132,6 +134,24 @@ abstract class BaseNode extends Node {
132134 tryAndLogFailure(() => Metrics .get().close())
133135 tryAndLogFailure(() => storagesInstance.dataSource.close())
134136 }
137+
138+ def fixDatabase (): Unit = {
139+ // FIXME this is a temporary solution to avoid an incompatibility due to the introduction of the best block hash
140+ // We can remove this fix when we release an incompatible version.
141+ val bestBlockInfo = storagesInstance.storages.appStateStorage.getBestBlockInfo()
142+ if (bestBlockInfo.hash == ByteString .empty && bestBlockInfo.number > 0 ) {
143+ log.warn(" Fixing best block hash into database for block {}" , bestBlockInfo.number)
144+ storagesInstance.storages.blockNumberMappingStorage.get(bestBlockInfo.number) match {
145+ case Some (hash) =>
146+ log.warn(" Putting {} as the best block hash" , Hex .toHexString(hash.toArray))
147+ storagesInstance.storages.appStateStorage.putBestBlockInfo(bestBlockInfo.copy(hash = hash)).commit()
148+ case None =>
149+ log.error(" No block found for number {} when trying to fix database" , bestBlockInfo.number)
150+ }
151+
152+ }
153+
154+ }
135155}
136156
137157class StdNode extends BaseNode with StdMiningBuilder
0 commit comments