Skip to content
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

Reorg [WIP] #164

Closed
wants to merge 2 commits into from
Closed

Reorg [WIP] #164

wants to merge 2 commits into from

Conversation

frolosofsky
Copy link
Member

Rough implementation of applying finalization to chain reorganization. It's WIP and in fact just a result of my initial unite/bitcoin sources research.

Some bullets/todo:

  • Need to explain new rule in an issue/ADR.
  • Because we started to reject parallel chains from older justified epochs, some of bitcoin's functional tests fail, e.g. we have 10 blocks in the main chain and 5th has been justified: now we reject all the blocks from parallel chains which height is less or equal 5. Discussed with @Gnappuraz, working around.
  • Rebase
  • Tests
  • It's might be a good optimization to store FinalizationState per chain instead of per block.

@frolosofsky frolosofsky added the wip Work in progress which is not supposed to be merged yet label Oct 12, 2018
BOUNTY_FRACTION_DENOMINATOR(params.m_bountyFractionDenominator),
BASE_INTEREST_FACTOR(params.m_baseInterestFactor),
BASE_PENALTY_FACTOR(params.m_basePenaltyFactor) {
const esperanza::FinalizationParams &settings) : settings(settings) {
Copy link
Member

@kostyantyn kostyantyn Oct 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably not related to the reorg. would you mind to make a separate PR (refactoring and the actual reorg)? It would make the review easier. Then the refactoring can be merged right away and we will focus on the actual code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno. It's not really refactoring, just splitting FinalizationState into two classes in order to simplify copying of state objects. I guess those changes don't make sense without current reorg approach.

Copy link
Member Author

@frolosofsky frolosofsky Oct 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I agree diff looks weird.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also see that we introduced FinalizationStateData. I'd really like to see this part of the code which doesn't change the behavior in the separate PR. And since this PR is a WIP, I think more code is coming.
But if it's OK for others, then let's keep as it is, I'll adapt :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is refactoring. I agree it should be done in a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You win :)
Just realized the real changes are in ~50 lines, good job for two weeks.

auto sa = esperanza::FinalizationState::GetState(pa);
auto sb = esperanza::FinalizationState::GetState(pb);
if(sa->GetLastJustifiedEpoch() > sb->GetLastJustifiedEpoch()) return false;
if(sa->GetLastJustifiedEpoch() < sb->GetLastJustifiedEpoch()) return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: In existing bitcoin code and also in the code we add and the style guide we adopted there's a space between if (else, switch, ...)


// the current dynasy number
uint32_t m_currentDynasty;
uint32_t m_currentDynasty = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of these changes, in fact almost all of the changes, are purely refactoring and should be separated in a pull request that is doing these refactorings exclusively. It will be much easier to review this one then, the other one will probably be merged much faster, and so on.

In general that is something which is recommended across a lot of open source projects and would also happen when contributing to bitcoin.

@@ -3624,6 +3638,26 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons

NotifyHeaderTip();

assert(pindex != nullptr);
esperanza::FinalizationState::ProcessNewTip(*pindex, *pblock);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that once we have a new block a signal will be emitted. CValidationInterface should be implemented for this and act accordingly.

}

// Check whether new block's chain has forked after current's chain last justified block
auto checkpoint = c->GetCheckpointHash(c->GetLastJustifiedEpoch());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think that manifest typing helps readability and auto should be used sparingly.

This I think also follows from the Google C++ Style Guide which we adopted: https://google.github.io/styleguide/cppguide.html#auto


uint64_t GetDepositSize(const uint256 &validatorIndex) const;

Vote GetRecommendedVote(const uint256 &validatorIndex) const;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is moving all these function declarations down good for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I need to copy FinalizationState objects I need to either write really long copy c-tor or allow C++ to do it. I prefer the second option because it saves time, lines and possibility to lose something when somebody add new data members. FinalizationState has uncopiable member (mutex), so I factored out all the data into base Data-class which is auto copiable.

Copy link
Member

@scravy scravy Oct 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. Sounds like a very good approach. Would love to see it in it's own pull though ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wip Work in progress which is not supposed to be merged yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants