Skip to content

Commit ec740a6

Browse files
author
Aurélien Richez
committed
Add a fix for the database
1 parent 1d57b44 commit ec740a6

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/main/scala/io/iohk/ethereum/nodebuilder/StdNode.scala

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package io.iohk.ethereum.nodebuilder
22

33
import akka.actor.typed.ActorSystem
4+
import akka.util.ByteString
45

56
import scala.concurrent.Await
67
import scala.concurrent.ExecutionContext.Implicits.global
78
import scala.util.Failure
89
import scala.util.Success
910
import scala.util.Try
10-
1111
import io.iohk.ethereum.blockchain.sync.SyncProtocol
1212
import io.iohk.ethereum.consensus.mining.StdMiningBuilder
1313
import io.iohk.ethereum.metrics.Metrics
@@ -17,7 +17,7 @@ import io.iohk.ethereum.network.ServerActor
1717
import io.iohk.ethereum.network.discovery.PeerDiscoveryManager
1818
import io.iohk.ethereum.nodebuilder.tooling.PeriodicConsistencyCheck
1919
import 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))
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

137157
class StdNode extends BaseNode with StdMiningBuilder

0 commit comments

Comments
 (0)