-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ETCM-969] simple Branch implementation (#1039)
* create Branch class with getBlockByNumber and getHashByBlockNumber functions * make getBlockByNumber private in blockchain reader * Create EmptyBlochainBranch and make getBestChain return it if there no known best block * Rephrase scaladoc comment and remove TODO
- Loading branch information
Aurélien Richez
authored
Jul 16, 2021
1 parent
2940e56
commit 2e078af
Showing
27 changed files
with
175 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/scala/io/iohk/ethereum/domain/branch/BestBranch.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.iohk.ethereum.domain.branch | ||
import akka.util.ByteString | ||
|
||
import io.iohk.ethereum.db.storage.BlockNumberMappingStorage | ||
import io.iohk.ethereum.domain.Block | ||
import io.iohk.ethereum.domain.BlockHeader | ||
import io.iohk.ethereum.domain.BlockchainReader | ||
|
||
/** A Branch instance which only works for the best canonical branch or a subset of this branch. | ||
* This implementation uses the existing storage indexes to access blocks by number more efficiently. | ||
*/ | ||
class BestBranch( | ||
tipBlockHeader: BlockHeader, | ||
bestChainBlockNumberMappingStorage: BlockNumberMappingStorage, | ||
blockchainReader: BlockchainReader | ||
) extends Branch { | ||
|
||
/* The following assumptions are made in this class : | ||
* - The whole branch exists in storage | ||
* - The various metadata and index are consistent | ||
*/ | ||
|
||
override def getBlockByNumber(number: BigInt): Option[Block] = | ||
if (tipBlockHeader.number >= number && number >= 0) { | ||
for { | ||
hash <- getHashByBlockNumber(number) | ||
block <- blockchainReader.getBlockByHash(hash) | ||
} yield block | ||
} else None | ||
|
||
override def getHashByBlockNumber(number: BigInt): Option[ByteString] = | ||
if (tipBlockHeader.number >= number && number >= 0) { | ||
bestChainBlockNumberMappingStorage.get(number) | ||
} else None | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/scala/io/iohk/ethereum/domain/branch/Branch.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.iohk.ethereum.domain.branch | ||
|
||
import akka.util.ByteString | ||
|
||
import io.iohk.ethereum.domain.Block | ||
|
||
/** An interface to manipulate blockchain branches */ | ||
trait Branch { | ||
|
||
/** Returns a block inside this branch based on its number */ | ||
def getBlockByNumber(number: BigInt): Option[Block] | ||
|
||
/** Returns a block hash for the block at the given height if any */ | ||
def getHashByBlockNumber(number: BigInt): Option[ByteString] | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/scala/io/iohk/ethereum/domain/branch/EmptyBranch$.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.iohk.ethereum.domain.branch | ||
import akka.util.ByteString | ||
|
||
import io.iohk.ethereum.domain.Block | ||
|
||
object EmptyBranch$ extends Branch { | ||
|
||
override def getBlockByNumber(number: BigInt): Option[Block] = None | ||
|
||
override def getHashByBlockNumber(number: BigInt): Option[ByteString] = None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.