Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Total Difficulty in IBFT #182

Closed
wants to merge 10 commits into from
Closed

Conversation

dbrajovic
Copy link
Contributor

@dbrajovic dbrajovic commented Oct 14, 2021

Description

Defining total difficulty in our IBFT implementation.
Addresses #179

Update:

Hardcoding GenesisDifficulty to 1 breaks backwards compatibility for existing chains due to new nodes having to generate the genesis block with difficulty 1.

Changes include

  • Bugfix (non-breaking change that solves an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (change that is not backwards-compatible and/or changes current functionality)

Checklist

  • I have assigned this PR to myself
  • I have added at least 1 reviewer
  • I have tested this code
  • I have updated the README and other relevant documents (guides...)
  • I have added sufficient documentation both in code, as well as in the READMEs

Additional comments

This PR is about coming to terms on what Total Difficulty means in IBFT.

In other consensuses, TD is what helps identify the correct chain (fork) based on the sum of all blocks' difficulty field (which varies from block to block).
In IBFT, TD has nothing to do with the difficulty field, but serves the same purpose, in a sense that it decides what is the latest block (number) - hence this value should represent the block's number field.

In short (our IBFT impl):

Block's difficulty -> Block's number
Block's TD -> Sum of all previous block numbers (up to current)
Chain TD -> TD of the latest block in the chain

Related issues:
https://github.com/0xPolygon/polygon-sdk/issues/179

This fix should provide the Syncer with the correct value (fetched with CurrentTD()) in order to resolve its best peer.

@dbrajovic dbrajovic self-assigned this Oct 14, 2021
@dbrajovic
Copy link
Contributor Author

dbrajovic commented Oct 15, 2021

After tuning the unit tests, I discovered that TD is calculated in other places as well (advanceHead()). At all points, there is a fetching of the parent difficulty used only to calculate new TD (which is redundant in this new approach).

I've left _ = variableName to suppress compiler warnings - I intend to remove these once proven that they were not meant for anything else.

Regarding the TD field in the units test (blockchain_test.go), this is the value we would like to see hardcoded to 1, in which case, all that is needed is to hardcode GetChainTD() to always return 1.

@dbrajovic
Copy link
Contributor Author

dbrajovic commented Oct 26, 2021

After revising the aforementioned calculation of TD, I decided to go with the less invasive approach of hardcoding each block's difficulty to 1, keeping the existing logic (summing of difficulties) intact, such that the following are true:

Block difficulty = 1
Block TD = Block number (1 + 1 + ... + 1)
ChainTD = TD of the latest block (its number)

@dbrajovic dbrajovic marked this pull request as ready for review October 27, 2021 08:04
Copy link
Contributor

@zivkovicmilos zivkovicmilos left a comment

Choose a reason for hiding this comment

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

Looks great to me 💯

Left a minor question that needs clarification

Copy link
Contributor

@zivkovicmilos zivkovicmilos left a comment

Choose a reason for hiding this comment

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

Thank you for making the requested changes 🙏

Looks great now 💯

Copy link
Contributor

@Kourin1996 Kourin1996 left a comment

Choose a reason for hiding this comment

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

Thank you for the PR. I like renaming variable to proper one. I've left small comments. I'd appreciated if you could check and fix if need.

blockchain/blockchain.go Outdated Show resolved Hide resolved
blockchain/blockchain.go Outdated Show resolved Hide resolved
Copy link
Contributor

@Kourin1996 Kourin1996 left a comment

Choose a reason for hiding this comment

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

Thank you for fixing. LGTM👍

Copy link
Contributor

@zivkovicmilos zivkovicmilos left a comment

Choose a reason for hiding this comment

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

Approving for a second time because Github reset my approval for some reason 🙂

@lazartravica
Copy link
Contributor

lazartravica commented Nov 8, 2021

@dbrajovic feel free to close this PR and delete the branch if you plan on opening a new one 👍

@lazartravica
Copy link
Contributor

Closing this PR in favour of #208

cc @dbrajovic

@dbrajovic dbrajovic mentioned this pull request Nov 18, 2021
8 tasks
@dbrajovic dbrajovic deleted the bug/total-difficulty-is-one branch November 21, 2021 15:54
igorcrevar pushed a commit that referenced this pull request Jun 12, 2023
* Add etherscan gas price

* Update Changelog
goran-ethernal pushed a commit that referenced this pull request Apr 11, 2024
Bumps [golang.org/x/tools](https://github.com/golang/tools) from 0.18.0 to 0.19.0.
- [Release notes](https://github.com/golang/tools/releases)
- [Commits](golang/tools@v0.18.0...v0.19.0)

---
updated-dependencies:
- dependency-name: golang.org/x/tools
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants