Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Cumulative fixes to make working with consensus-pow easier #3617

Merged
merged 29 commits into from
Oct 3, 2019
Merged

Conversation

sorpaas
Copy link
Member

@sorpaas sorpaas commented Sep 14, 2019

This PR contains several cumulative fixes to make working with consensus-pow easier:

  • Added difficulty data to PowAux. The reason for this is that nearly all difficulty adjustment algorithms need this information.
  • Added TimestampApi in pow-primitives so that consensus engine can query timestamp data.
  • Added an implementation of FinalityProofProvider for (), which always return None. This seems to be needed because we don't currently have a "dummy" finality proof provider for those engines that do not need it.
  • Generalize Difficulty, removing assumption that it is u128.

@sorpaas sorpaas added A0-please_review Pull request needs code review. A2-insubstantial Pull request requires no code review (e.g., a sub-repository hash update). labels Sep 14, 2019
@sorpaas sorpaas changed the title consensus-pow: add difficulty data to auxiliary Several insubstantial fixes to make working with consensus-pow easier Sep 14, 2019
@@ -78,6 +78,12 @@ pub trait FinalityProofProvider<Block: BlockT>: Send + Sync {
fn prove_finality(&self, for_block: Block::Hash, request: &[u8]) -> Result<Option<Vec<u8>>, Error>;
}

impl<Block: BlockT> FinalityProofProvider<Block> for () {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the finality proof provider is optional when creating the service.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll try to provide a reproducible test case, but I think what I had trouble with is that if I remove .with_finality_proof_provider for the service builder, the whole type resolution will fail. But maybe we should fix that instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I see, it will fail to infer the generic type of the optional finality proof provider.

@sorpaas
Copy link
Member Author

sorpaas commented Sep 19, 2019

I made Difficulty a generic and removed the assumption that it is u128. After trying out some PoW implementations, I realized that we have a variety of cases -- we have difficulty that are always really small (Bitcoin -- u32) and cases where difficulty can be potentially really big (Monero -- u128). For the former, fixed size is a waste of space, and for the latter, it risks overflow. So making it generic may be a good thing to do.

The reason why it was there is because when mine_loop returns, it means an error
happened. In that case, we'd better sleep for a moment before trying again,
because immediately trying would most likely just fail.
So that it does not mine when major syncing
Instead of hardcode it as previously 100ms.
For PoW, older blocks are secured by the work, and can mostly be considered to
be finalized. Thus we can save both code complexity and validation time by
skipping checking inherents for them.
header,
BlockId::Hash(parent_hash),
)?;
aux.total_difficulty = aux.total_difficulty.saturating_add(difficulty);
aux.difficulty = difficulty;
aux.total_difficulty.add(difficulty);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this changed from a saturating_add()?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the add function from TotalDifficulty trait. The idea here is that some Difficulty definitions may want to use its own customized method for calculating the total difficulty from current difficulty. Most of the time TotalDifficulty::add will still be implemented as saturating_add.

Each engine can use its own Rng source.
@sorpaas sorpaas removed the A2-insubstantial Pull request requires no code review (e.g., a sub-repository hash update). label Sep 24, 2019
@sorpaas sorpaas changed the title Several insubstantial fixes to make working with consensus-pow easier Cumulative fixes to make working with consensus-pow easier Sep 24, 2019
header,
BlockId::Hash(parent_hash),
)?;
aux.total_difficulty = aux.total_difficulty.saturating_add(difficulty);
aux.difficulty = difficulty;
aux.total_difficulty.add(difficulty);
Copy link
Contributor

Choose a reason for hiding this comment

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

A small nit: my brain treats add as a function which takes &self and returns Self. For this case, it seems increase(&mut self, other: Self) → () would be more appropriate, don't you think so?

Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

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

changes lgtm. just some minor nits.

@@ -157,6 +173,10 @@ impl<C, Algorithm> PowVerifier<C, Algorithm> {
{
const MAX_TIMESTAMP_DRIFT_SECS: u64 = 60;

if *block.header().number() < self.check_inherents_after {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did we need to add this? Do we have to deal with any existing deployment?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added this parameter because runtime upgrades may break older inherent data check. For example, when the inherent data format is changed completely or an old inherent module is removed. In those cases, some chains might want to completely skip the check, because the older the block is, the more "finalized" it is because of PoW built on top of it.

This branch can be disabled by simply setting check_inherents_after to 0.

core/consensus/pow/primitives/src/lib.rs Outdated Show resolved Hide resolved
core/consensus/pow/src/lib.rs Outdated Show resolved Hide resolved
core/consensus/pow/src/lib.rs Show resolved Hide resolved
@sorpaas sorpaas merged commit ddd7368 into master Oct 3, 2019
@sorpaas sorpaas deleted the sp-pow-aux branch October 3, 2019 03:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants