Skip to content

Commit beb908b

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

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/main/scala/io/iohk/ethereum/domain/BlockchainReader.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.iohk.ethereum.domain
22

33
import akka.util.ByteString
4+
45
import io.iohk.ethereum.db.storage.AppStateStorage
56
import io.iohk.ethereum.db.storage.BlockBodiesStorage
67
import io.iohk.ethereum.db.storage.BlockHeadersStorage
@@ -12,7 +13,8 @@ import io.iohk.ethereum.domain.branch.Branch
1213
import io.iohk.ethereum.domain.branch.EmptyBranch
1314
import io.iohk.ethereum.mpt.MerklePatriciaTrie
1415
import io.iohk.ethereum.mpt.MptNode
15-
import io.iohk.ethereum.utils.{Hex, Logger}
16+
import io.iohk.ethereum.utils.Hex
17+
import io.iohk.ethereum.utils.Logger
1618

1719
class BlockchainReader(
1820
blockHeadersStorage: BlockHeadersStorage,

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
@@ -18,6 +19,7 @@ import io.iohk.ethereum.network.discovery.PeerDiscoveryManager
1819
import io.iohk.ethereum.nodebuilder.tooling.PeriodicConsistencyCheck
1920
import io.iohk.ethereum.nodebuilder.tooling.StorageConsistencyChecker
2021
import io.iohk.ethereum.utils.Config
22+
import io.iohk.ethereum.utils.Hex
2123

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

37+
fixDatabase()
38+
3539
loadGenesisData()
3640

3741
runDBConsistencyCheck()
@@ -132,6 +136,24 @@ abstract class BaseNode extends Node {
132136
tryAndLogFailure(() => Metrics.get().close())
133137
tryAndLogFailure(() => storagesInstance.dataSource.close())
134138
}
139+
140+
def fixDatabase(): Unit = {
141+
// FIXME this is a temporary solution to avoid an incompatibility due to the introduction of the best block hash
142+
// We can remove this fix when we release an incompatible version.
143+
val bestBlockInfo = storagesInstance.storages.appStateStorage.getBestBlockInfo()
144+
if (bestBlockInfo.hash == ByteString.empty && bestBlockInfo.number > 0) {
145+
log.warn("Fixing best block hash into database for block {}", bestBlockInfo.number)
146+
storagesInstance.storages.blockNumberMappingStorage.get(bestBlockInfo.number) match {
147+
case Some(hash) =>
148+
log.warn("Putting {} as the best block hash", Hex.toHexString(hash.toArray))
149+
storagesInstance.storages.appStateStorage.putBestBlockInfo(bestBlockInfo.copy(hash = hash)).commit()
150+
case None =>
151+
log.error("No block found for number {} when trying to fix database", bestBlockInfo.number)
152+
}
153+
154+
}
155+
156+
}
135157
}
136158

137159
class StdNode extends BaseNode with StdMiningBuilder

0 commit comments

Comments
 (0)