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

Consensus: Proof of Work #3473

Merged
merged 59 commits into from Sep 6, 2019
Merged

Consensus: Proof of Work #3473

merged 59 commits into from Sep 6, 2019

Conversation

sorpaas
Copy link
Member

@sorpaas sorpaas commented Aug 23, 2019

This PR implements PoW consensus engine in Substrate. We currently have a PoW verifier, and a simple thread-based miner. The PoW algorithm and difficulty adjustment algorithm is generic over PowAlgorithm struct. The caller is expected to implement one of this that suits its need.

For mining, we currently just use a simple runtime function PowAlgorithm::mine. It takes a header pre-hash, and is expected to loop until it finds a seal that satisfies the current difficulty.

Things that can be improved, but probably not in this PR:

  • RPC methods for getWork and submitWork.

@sorpaas sorpaas added the A3-in_progress Pull request is in progress. No review needed at this stage. label Aug 23, 2019
@sorpaas sorpaas requested a review from 0x7CFE August 23, 2019 18:43
@rphmeier
Copy link
Contributor

rphmeier commented Aug 23, 2019

You can use a pre-runtime digest for the coinbase - that'll put all necessary info in the header. A runtime API to fill it out using the runtime's keystore API would work perfectly for that.

No run-on expressions allowed.
core/consensus/pow/src/lib.rs Outdated Show resolved Hide resolved
core/consensus/pow/primitives/src/lib.rs Outdated Show resolved Hide resolved
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 Outdated Show resolved Hide resolved
core/consensus/pow/src/lib.rs Outdated Show resolved Hide resolved
core/consensus/pow/src/lib.rs Outdated Show resolved Hide resolved
Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>
@burdges
Copy link

burdges commented Sep 1, 2019

As an aside, we should make doing parachains with proof-of-stake block production relatively easy and discourage parachains from actually using proof-of-work. It's fine for testing of course but maybe bad optics if we have real parachains doing this.

/// Algorithm used for proof of work.
pub trait PowAlgorithm<B: BlockT> {
/// Get the next block's difficulty.
fn difficulty(&self, parent: &BlockId<B>) -> Result<Difficulty, String>;

Choose a reason for hiding this comment

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

How would this work for eg. Nakamoto PoW where block difficulty depends on the last 2016 blocks?

Copy link
Member Author

Choose a reason for hiding this comment

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

The struct that implements PowAlgorithm is expected to take a reference to client Arc<C>. That will give it access to all past blocks, where it can find out their difficulty through auxiliary store. This way we can implement Nakamoto PoW. :)

Choose a reason for hiding this comment

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

Great, thanks!

Copy link
Contributor

@0x7CFE 0x7CFE Sep 4, 2019

Choose a reason for hiding this comment

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

But what about light clients? Is aux store available for them? I thought they have access to headers only.

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I understand light client should have access to aux store. Even if they don't, because we only need to store tiny amount of metadata in aux store, it's also quite easy to move that elsewhere.

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>
@Demi-Marie Demi-Marie self-requested a review September 4, 2019 16:24
Copy link
Contributor

@Demi-Marie Demi-Marie left a comment

Choose a reason for hiding this comment

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

LGTM!

core/consensus/pow/src/lib.rs Outdated Show resolved Hide resolved
core/consensus/pow/src/lib.rs Show resolved Hide resolved
core/consensus/pow/src/lib.rs Show resolved Hide resolved
core/consensus/pow/src/lib.rs Show resolved Hide resolved
core/consensus/pow/src/lib.rs Show resolved Hide resolved
core/consensus/pow/src/lib.rs Show resolved Hide resolved
1. Make preruntime Optional.
2. Add more docs on what is `preruntie` and `round`.
3. Replace `Default::default` with the approriate type.
@sorpaas sorpaas merged commit b9e1782 into master Sep 6, 2019
@sorpaas sorpaas deleted the pow branch September 6, 2019 13:45
@sorpaas sorpaas added A8-looksgood and removed A0-please_review Pull request needs code review. labels Sep 6, 2019
@shawntabrizi
Copy link
Contributor

@sorpaas Do you have time to work with someone from the docs team to help write some information about this?

How to use it, how it works, etc...

Would be looking to you to answer questions for whomever writes the docs.

@sorpaas
Copy link
Member Author

sorpaas commented Sep 7, 2019

@shawntabrizi Yeah for sure!

@JoshOrndorff
Copy link
Contributor

JoshOrndorff commented Sep 8, 2019

@sorpaas I've taken the lead on consensus on the docs team. Could we set up a call this week? I'll do my homework first. Tuesday or Wednesday would be fine.

andresilva pushed a commit that referenced this pull request Sep 17, 2019
* consensus-pow: init primtives and verifier

* consensus-pow: add total difficulty auxiliary

* consensus-pow: implement total difficulty chain selection

* consensus-pow: implement pow import queue

* consensus-pow-primitives: add mine into PowApi

* consensus-pow: implement mining

* Update lock file

* Style fixes

No run-on expressions allowed.

* consensus-pow: refactor register_pow_inherent_data_provider

* consensus-pow: make PowApi::mine yieldable

* consensus-pow: better mining loop

* Add missing license header

* consensus-pow-primitives: clarify the meaning of None for PowApi::verify

* consensus-pow: changing total difficulty addition to use saturating add

* consensus-pow: change mine-loop error to log on error! level

* consensus-pow: allow inserting arbitrary preruntime digest for pow

The preruntime digest can be intepreted by the runtime as the block author/coinbase.

* Fix line width

* More line width fixes

* consensus-pow: separate difficulty, verify API

This makes it more apparent that currently in PoW engine, `difficulty` should
be input, not output.

* srml-pow: implementation of average_span difficulty adjustment

* srml-pow: basic blake2 algo example

* srml-pow-average-span: make it not require genesis config

* srml-pow: add support for authorship

* Missing license headers

* consensus-pow: PowAlgorithm trait generalization

* Missing docs for consensus-pow

* More docs

* node-runtime: bump impl_version

* Add rationale for difficulty type

* consensus-pow: refactor aux_key

* Update lock file

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/primitives/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/primitives/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Remove PowRuntimeAlgorithm

* block_id -> parent_block_id

* Auxiliary data -> auxiliary storage data

* Fix error message

* Fix compile

* Update core/consensus/pow/primitives/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Update core/consensus/pow/primitives/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Fix crate description

* More docs

* Address grumbles

1. Make preruntime Optional.
2. Add more docs on what is `preruntie` and `round`.
3. Replace `Default::default` with the approriate type.
Demi-Marie pushed a commit to Demi-Marie/substrate that referenced this pull request Sep 17, 2019
* consensus-pow: init primtives and verifier

* consensus-pow: add total difficulty auxiliary

* consensus-pow: implement total difficulty chain selection

* consensus-pow: implement pow import queue

* consensus-pow-primitives: add mine into PowApi

* consensus-pow: implement mining

* Update lock file

* Style fixes

No run-on expressions allowed.

* consensus-pow: refactor register_pow_inherent_data_provider

* consensus-pow: make PowApi::mine yieldable

* consensus-pow: better mining loop

* Add missing license header

* consensus-pow-primitives: clarify the meaning of None for PowApi::verify

* consensus-pow: changing total difficulty addition to use saturating add

* consensus-pow: change mine-loop error to log on error! level

* consensus-pow: allow inserting arbitrary preruntime digest for pow

The preruntime digest can be intepreted by the runtime as the block author/coinbase.

* Fix line width

* More line width fixes

* consensus-pow: separate difficulty, verify API

This makes it more apparent that currently in PoW engine, `difficulty` should
be input, not output.

* srml-pow: implementation of average_span difficulty adjustment

* srml-pow: basic blake2 algo example

* srml-pow-average-span: make it not require genesis config

* srml-pow: add support for authorship

* Missing license headers

* consensus-pow: PowAlgorithm trait generalization

* Missing docs for consensus-pow

* More docs

* node-runtime: bump impl_version

* Add rationale for difficulty type

* consensus-pow: refactor aux_key

* Update lock file

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/primitives/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/consensus/pow/primitives/src/lib.rs

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Remove PowRuntimeAlgorithm

* block_id -> parent_block_id

* Auxiliary data -> auxiliary storage data

* Fix error message

* Fix compile

* Update core/consensus/pow/primitives/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Update core/consensus/pow/primitives/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Update core/consensus/pow/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Fix crate description

* More docs

* Address grumbles

1. Make preruntime Optional.
2. Add more docs on what is `preruntie` and `round`.
3. Replace `Default::default` with the approriate type.
@IPSE-TEAM
Copy link

This PR implements PoW consensus engine in Substrate. We currently have a PoW verifier, and a simple thread-based miner. The PoW algorithm and difficulty adjustment algorithm is generic over PowAlgorithm struct. The caller is expected to implement one of this that suits its need.

For mining, we currently just use a simple runtime function PowAlgorithm::mine. It takes a header pre-hash, and is expected to loop until it finds a seal that satisfies the current difficulty.

Things that can be improved, but probably not in this PR:

  • RPC methods for getWork and submitWork.

come on, How to implement this RPC?

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.

None yet