Skip to content

[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

Merged
merged 9 commits into from
Jul 16, 2021

Conversation

AurelienRichez
Copy link
Contributor

@AurelienRichez AurelienRichez commented Jul 7, 2021

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 call blockchainReader.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 :

  • Initially I created getBestBranch in BlockchainReader with a BlockchainBranch 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.
  • The implementation might lead to a little more database access because getBestBranch is roughly equivalent to getBestBlock because a BlockchainBranch is defined by its tip and contains the block header. So for instance it would mean that to call isInchain 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 problem

I 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. I
changed 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

@AurelienRichez AurelienRichez marked this pull request as draft July 7, 2021 08:50
@AurelienRichez AurelienRichez force-pushed the refactor/ETCM-969-move-getBestBlock branch 2 times, most recently from d123b39 to fbf9ab8 Compare July 7, 2021 12:56
@AurelienRichez AurelienRichez force-pushed the refactor/ETCM-969-read-layer branch from a870b57 to ea2596b Compare July 7, 2021 13:10
* */

override def getBlockByNumber(number: BigInt): Option[Block] =
if (tipBlockHeader.number >= number && number >= 0) {
Copy link
Contributor Author

@AurelienRichez AurelienRichez Jul 7, 2021

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.

Base automatically changed from refactor/ETCM-969-move-getBestBlock to develop July 7, 2021 13:37
@AurelienRichez AurelienRichez force-pushed the refactor/ETCM-969-read-layer branch 2 times, most recently from ab3942b to 3fc9a98 Compare July 7, 2021 13:39
@AurelienRichez AurelienRichez marked this pull request as ready for review July 7, 2021 13:50
@AurelienRichez AurelienRichez force-pushed the refactor/ETCM-969-read-layer branch from 3fc9a98 to baf5cd0 Compare July 8, 2021 07:15
/** 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] =
Copy link
Contributor

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

@AurelienRichez AurelienRichez force-pushed the refactor/ETCM-969-read-layer branch from baf5cd0 to 2707a5b Compare July 8, 2021 12:30
@AurelienRichez AurelienRichez force-pushed the refactor/ETCM-969-read-layer branch 2 times, most recently from d572024 to 555880c Compare July 12, 2021 06:40
import io.iohk.ethereum.domain.Block

// TODO choose a name : ChainInstance, BlockchainBranch, Branch, Blockchain ?
trait BlockchainBranch {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

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.

@AurelienRichez AurelienRichez force-pushed the refactor/ETCM-969-read-layer branch from d0ce7ff to 00cb79e Compare July 16, 2021 07:04
@AurelienRichez AurelienRichez merged commit 2e078af into develop Jul 16, 2021
@AurelienRichez AurelienRichez deleted the refactor/ETCM-969-read-layer branch July 16, 2021 08:26
@AurelienRichez AurelienRichez restored the refactor/ETCM-969-read-layer branch July 21, 2021 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants