-
Notifications
You must be signed in to change notification settings - Fork 75
[ETCM-969] simple Branch implementation #1039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/main/scala/io/iohk/ethereum/domain/branch/BestBlockchainBranch.scala
Outdated
Show resolved
Hide resolved
d123b39
to
fbf9ab8
Compare
a870b57
to
ea2596b
Compare
* */ | ||
|
||
override def getBlockByNumber(number: BigInt): Option[Block] = | ||
if (tipBlockHeader.number >= number && number >= 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is some additionnal logic compared to what was existing. The rationale is that if we want a branch to be an immutable view, then I have to avoid fetching block "in the future" compared to the selected tip.
This is still not perfect because if the number -> block hash
index is rewritten, then the interface is not really immutable. The problem should not be apparent unless the instance is used for too long.
ab3942b
to
3fc9a98
Compare
3fc9a98
to
baf5cd0
Compare
/** get the current best stored branch */ | ||
// FIXME this should not be an option as we should always have the genesis | ||
// but some tests prevent it to simply be BlockchainBranch for now | ||
def getBestBranch(): Option[BlockchainBranch] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in our discusssion, I'm hoping that this method will be part of Consensus soon
baf5cd0
to
2707a5b
Compare
d572024
to
555880c
Compare
src/main/scala/io/iohk/ethereum/domain/branch/BestBlockchainBranch.scala
Outdated
Show resolved
Hide resolved
src/main/scala/io/iohk/ethereum/domain/branch/BestBlockchainBranch.scala
Outdated
Show resolved
Hide resolved
import io.iohk.ethereum.domain.Block | ||
|
||
// TODO choose a name : ChainInstance, BlockchainBranch, Branch, Blockchain ? | ||
trait BlockchainBranch { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it gonna be used only for the main chain? or we could be interested in alternative branches?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intent is to be able to use it for other chains as well. So the interface itself does not make any assumption that we are indeed using the best chain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then maybe just Branch as a name will suit better, that'd be my choice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Let's go for it for now. This part is still heavily changing right now so we will see how this pans out.
src/main/scala/io/iohk/ethereum/domain/branch/EmptyBlockchainBranch.scala
Outdated
Show resolved
Hide resolved
c62dad2
to
985804f
Compare
985804f
to
d0ce7ff
Compare
…o known best block
Co-authored-by: Anastasiia Pushkina <[email protected]>
d0ce7ff
to
00cb79e
Compare
Description
This PR creates a simple
BlockchainBranch
(name up for discussion). The goal is to create an immutable view of a specific branch. Currently it only works for the canonical branch, but the goal would be to have an unified representation for any branch we might want to use and allow to easily switch between branches.For instance, instead of calling
blockchainReader.getBlockByNumber(nb)
we would callblockchainReader.getBestBranch().getBlockByNumber(nb)
Important Changes Introduced
the interface
BlockchainBranch
is the biggest change. At the end it will contain every methods which only make sense in the context of a specific branch (mostly those which need a block number).This implementation has a few drawback currently though :
getBestBranch
inBlockchainReader
with aBlockchainBranch
because I figured that we should always at least have the genesis block (so at worst the best branch is just the genesis block). However this is not the case in the tests so I have to return an option which makes the interface cumbersome to use. I think it would be worthwhile to actually enforce this in order to have a more elegant interface.getBestBranch
is roughly equivalent togetBestBlock
because aBlockchainBranch
is defined by its tip and contains the block header. So for instance it would mean that to callisInchain
we would get the tip and then check that a given block is in the right branch. This could probably be done without fetching the tip. I think this might be ok as the best block header can easily be cached if it becomes a problemI had to change lots of tests for this to work. The problem is that lots of tests wrote a block in the storages but did not update the best block. As a consequence, the
getBestBranch
method does not return the actual best branch in the unit test. Ichanged the test to maintain consistency but I am not really sure that it is a good solution.
See #1044 for an improvement on the
Option
return value