Skip to content

Commit 35af28c

Browse files
author
Aurélien Richez
committed
use getBlockByHash to get the best block number instead of getBlockByNumber
1 parent 4fde4a3 commit 35af28c

File tree

11 files changed

+35
-30
lines changed

11 files changed

+35
-30
lines changed

bytes/src/main/scala/io/iohk/ethereum/utils/Hex.scala

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

3-
import akka.util.ByteString
4-
53
object Hex {
64
def toHexString(bytes: Array[Byte]): String =
75
bytes.map("%02x".format(_)).mkString

src/it/scala/io/iohk/ethereum/sync/util/CommonFakePeer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ abstract class CommonFakePeer(peerName: String, fakePeerCustomConfig: FakePeerCu
139139
)
140140

141141
val blockchainMetadata = new BlockchainMetadata(
142-
storagesInstance.storages.appStateStorage.getBestBlockData(),
142+
storagesInstance.storages.appStateStorage.getBestBlockInfo(),
143143
storagesInstance.storages.appStateStorage.getLatestCheckpointBlockNumber()
144144
)
145145
val blockchainReader: BlockchainReader = BlockchainReader(storagesInstance.storages, blockchainMetadata)

src/main/scala/io/iohk/ethereum/db/storage/AppStateStorage.scala

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package io.iohk.ethereum.db.storage
22

3+
import java.math.BigInteger
4+
35
import akka.util.ByteString
6+
7+
import scala.collection.immutable.ArraySeq
8+
49
import boopickle.Default.Unpickle
5-
import boopickle.Pickler
610
import boopickle.DefaultBasic._
11+
import boopickle.Pickler
712

8-
import java.math.BigInteger
9-
import scala.collection.immutable.ArraySeq
1013
import io.iohk.ethereum.db.dataSource.DataSource
1114
import io.iohk.ethereum.db.dataSource.DataSourceBatchUpdate
1215
import io.iohk.ethereum.db.storage.AppStateStorage._
1316
import io.iohk.ethereum.domain.appstate.BestBlockInfo
14-
import io.iohk.ethereum.utils.ByteUtils.{byteSequenceToBuffer, compactPickledBytes}
17+
import io.iohk.ethereum.utils.ByteUtils.byteSequenceToBuffer
18+
import io.iohk.ethereum.utils.ByteUtils.compactPickledBytes
1519
import io.iohk.ethereum.utils.Hex
1620
import io.iohk.ethereum.utils.Picklers._
1721

@@ -34,7 +38,7 @@ class AppStateStorage(val dataSource: DataSource) extends TransactionalKeyValueS
3438
def getBestBlockNumber(): BigInt =
3539
getBigInt(Keys.BestBlockNumber)
3640

37-
def getBestBlockData(): BestBlockInfo =
41+
def getBestBlockInfo(): BestBlockInfo =
3842
BestBlockInfo( // FIXME default value for hash ?
3943
get(Keys.BestBlockHash).map(v => ByteString(Hex.decode(v))).getOrElse(ByteString.empty),
4044
getBigInt(Keys.BestBlockNumber)
@@ -96,10 +100,4 @@ object AppStateStorage {
96100
val LatestCheckpointBlockNumber = "LatestCheckpointBlockNumber"
97101
}
98102

99-
implicit private val bestBlockDataPickler: Pickler[BestBlockInfo] = generatePickler[BestBlockInfo]
100-
private val bestBlockDataSerializer = (bestBlockData: BestBlockInfo) =>
101-
compactPickledBytes(Pickle.intoBytes(bestBlockData))
102-
private val bestBlockDataDeserializer =
103-
(byteSequenceToBuffer _).andThen(Unpickle[BestBlockInfo].fromBytes)
104-
105103
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.iohk.ethereum.domain
22

33
import akka.util.ByteString
44
import scala.annotation.tailrec
5+
56
import io.iohk.ethereum.db.dataSource.DataSourceBatchUpdate
67
import io.iohk.ethereum.db.storage._
78
import io.iohk.ethereum.domain

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

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

3-
import io.iohk.ethereum.domain.appstate.BestBlockInfo
4-
53
import java.util.concurrent.atomic.AtomicReference
64

5+
import io.iohk.ethereum.domain.appstate.BestBlockInfo
6+
77
class BlockchainMetadata(bestBlockData: BestBlockInfo, latestCheckpointNumber: BigInt) {
88
lazy val bestKnownBlockAndLatestCheckpoint: AtomicReference[BestBlockLatestCheckpointNumbers] =
99
new AtomicReference(BestBlockLatestCheckpointNumbers(bestBlockData, latestCheckpointNumber))

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ class BlockchainReader(
9797

9898
//returns the best known block if it's available in the storage, otherwise the best stored block
9999
def getBestBlock(): Option[Block] = {
100-
val bestBlockNumber = getBestBlockNumber()
101-
log.debug("Trying to get best block with number {}", bestBlockNumber)
102-
getBlockByNumber(bestBlockNumber).orElse(
103-
getBlockByNumber(
104-
appStateStorage.getBestBlockNumber()
105-
)
106-
)
100+
val bestKnownBlockinfo = blockchainMetadata.bestKnownBlockAndLatestCheckpoint.get().bestBlockInfo
101+
log.debug("Trying to get best block with number {}", bestKnownBlockinfo.number)
102+
getBlockByHash(bestKnownBlockinfo.hash)
103+
.orElse {
104+
val bestBlockInfo = appStateStorage.getBestBlockInfo()
105+
log.warn("Falling back to best block in storage number {}", bestBlockInfo.number)
106+
getBlockByHash(bestBlockInfo.hash)
107+
}
107108
}
108109

109110
def genesisHeader: BlockHeader =

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

Lines changed: 1 addition & 0 deletions
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.dataSource.DataSourceBatchUpdate
56
import io.iohk.ethereum.db.storage.AppStateStorage
67
import io.iohk.ethereum.db.storage.BlockBodiesStorage

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ trait BlockchainBuilder {
176176

177177
private lazy val blockchainMetadata: BlockchainMetadata =
178178
new BlockchainMetadata(
179-
storagesInstance.storages.appStateStorage.getBestBlockData(),
179+
storagesInstance.storages.appStateStorage.getBestBlockInfo(),
180180
storagesInstance.storages.appStateStorage.getLatestCheckpointBlockNumber()
181181
)
182182
lazy val blockchainReader: BlockchainReader = BlockchainReader(storagesInstance.storages, blockchainMetadata)

src/test/scala/io/iohk/ethereum/jsonrpc/EthTxServiceSpec.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,12 @@ class EthTxServiceSpec
174174
}
175175

176176
it should "return average gas price" in new TestSetup {
177-
blockchain.saveBestKnownBlocks(ByteString.empty, 42)
177+
private val block: Block =
178+
Block(Fixtures.Blocks.Block3125369.header.copy(number = 42), Fixtures.Blocks.Block3125369.body)
178179
blockchainWriter
179-
.storeBlock(Block(Fixtures.Blocks.Block3125369.header.copy(number = 42), Fixtures.Blocks.Block3125369.body))
180+
.storeBlock(block)
180181
.commit()
182+
blockchain.saveBestKnownBlocks(block.hash, block.number)
181183

182184
val response = ethTxService.getGetGasPrice(GetGasPriceRequest())
183185
response.runSyncUnsafe() shouldEqual Right(GetGasPriceResponse(BigInt("20000000000")))

src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,10 @@ class JsonRpcControllerEthSpec
413413
}
414414

415415
it should "eth_gasPrice" in new JsonRpcControllerFixture {
416-
blockchainWriter
417-
.storeBlock(Block(Fixtures.Blocks.Block3125369.header.copy(number = 42), Fixtures.Blocks.Block3125369.body))
418-
.commit()
419-
blockchain.saveBestKnownBlocks(ByteString.empty, 42)
416+
private val block: Block =
417+
Block(Fixtures.Blocks.Block3125369.header.copy(number = 42), Fixtures.Blocks.Block3125369.body)
418+
blockchainWriter.storeBlock(block).commit()
419+
blockchain.saveBestKnownBlocks(block.hash, 42)
420420

421421
val request: JsonRpcRequest = newJsonRpcRequest("eth_gasPrice")
422422

src/test/scala/io/iohk/ethereum/ledger/BlockImportSpec.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import io.iohk.ethereum.consensus.validators.BlockHeaderError.HeaderParentNotFou
1717
import io.iohk.ethereum.consensus.validators._
1818
import io.iohk.ethereum.db.storage.MptStorage
1919
import io.iohk.ethereum.domain._
20+
import io.iohk.ethereum.domain.appstate.BestBlockInfo
2021
import io.iohk.ethereum.ledger.BlockQueue.Leaf
2122
import io.iohk.ethereum.mpt.LeafNode
2223
import io.iohk.ethereum.mpt.MerklePatriciaTrie
@@ -171,6 +172,9 @@ class BlockImportSpec extends AnyFlatSpec with Matchers with ScalaFutures {
171172
blockchainWriter.save(oldBlock4, Nil, oldWeight4, saveAsBestBlock = true)
172173

173174
val ancestorForValidation: Block = getBlock(0, difficulty = 1)
175+
storagesInstance.storages.appStateStorage
176+
.putBestBlockData(BestBlockInfo(ancestorForValidation.hash, ancestorForValidation.number))
177+
.commit()
174178
blockchainWriter.save(ancestorForValidation, Nil, ChainWeight.totalDifficultyOnly(1), saveAsBestBlock = false)
175179

176180
val oldBranch = List(oldBlock2, oldBlock3, oldBlock4)

0 commit comments

Comments
 (0)