Skip to content

Commit 98d94bb

Browse files
author
Aurélien Richez
committed
create BlockchainBranch class with getBlockByNumber and getHashByBlockNumber functions
1 parent 5eb69c5 commit 98d94bb

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import io.iohk.ethereum.db.storage.BlockHeadersStorage
88
import io.iohk.ethereum.db.storage.BlockNumberMappingStorage
99
import io.iohk.ethereum.db.storage.ReceiptStorage
1010
import io.iohk.ethereum.db.storage.StateStorage
11+
import io.iohk.ethereum.domain.branch.BestBlockchainBranch
12+
import io.iohk.ethereum.domain.branch.BlockchainBranch
1113
import io.iohk.ethereum.mpt.MptNode
1214
import io.iohk.ethereum.utils.Logger
1315

@@ -86,6 +88,13 @@ class BlockchainReader(
8688
*/
8789
def getReceiptsByHash(blockhash: ByteString): Option[Seq[Receipt]] = receiptStorage.get(blockhash)
8890

91+
def getBestBranch(): BlockchainBranch =
92+
new BestBlockchainBranch(
93+
getBestBlock().map(_.header).getOrElse(genesisHeader),
94+
blockNumberMappingStorage,
95+
this
96+
)
97+
8998
def getBestBlockNumber(): BigInt = {
9099
val bestSavedBlockNumber = appStateStorage.getBestBlockNumber()
91100
val bestKnownBlockNumber = blockchainMetadata.bestKnownBlockAndLatestCheckpoint.get().bestBlockNumber
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.iohk.ethereum.domain.branch
2+
import akka.util.ByteString
3+
4+
import io.iohk.ethereum.db.storage.BlockNumberMappingStorage
5+
import io.iohk.ethereum.domain.Block
6+
import io.iohk.ethereum.domain.BlockHeader
7+
import io.iohk.ethereum.domain.BlockchainReader
8+
9+
/** A Branch instance which only works for the best canonical branch. As this branch
10+
* currently has specific indexes (particularly regarding accessing blocks by number),
11+
* it will uses thoses to provide better performance.
12+
*/
13+
class BestBlockchainBranch(
14+
tipBlockHeader: BlockHeader,
15+
bestChainBlockNumberMappingStorage: BlockNumberMappingStorage,
16+
blockchainReader: BlockchainReader
17+
) extends BlockchainBranch {
18+
19+
/* The following assumptions are made in this class :
20+
* - the whole branch exist in storage
21+
* - The various metadata and index are consistent
22+
*
23+
* If those assumptions are found to be false, then the application is in an inconsistent
24+
* state and the class will throw.
25+
* */
26+
27+
override def getBlockByNumber(number: BigInt): Option[Block] =
28+
if (tipBlockHeader.number <= number && number > 0) {
29+
for {
30+
hash <- blockchainReader.getHashByBlockNumber(number)
31+
block <- blockchainReader.getBlockByHash(hash)
32+
} yield block
33+
} else None
34+
35+
override def getHashByBlockNumber(number: BigInt): Option[ByteString] =
36+
bestChainBlockNumberMappingStorage.get(number)
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.iohk.ethereum.domain.branch
2+
3+
import akka.util.ByteString
4+
5+
import io.iohk.ethereum.domain.Block
6+
7+
// TODO choose a name : ChainInstance, BlockchainBranch, Branch, Blockchain ?
8+
trait BlockchainBranch {
9+
10+
/** Returns a block inside this branch based on its number */
11+
def getBlockByNumber(number: BigInt): Option[Block]
12+
13+
/** Returns a block hash for the block at the given height if any */
14+
def getHashByBlockNumber(number: BigInt): Option[ByteString]
15+
16+
}

0 commit comments

Comments
 (0)