-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Consensus: Proof of Work #3473
Consensus: Proof of Work #3473
Conversation
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.
The preruntime digest can be intepreted by the runtime as the block author/coinbase.
This makes it more apparent that currently in PoW engine, `difficulty` should be input, not output.
Co-Authored-By: DemiMarie-parity <[email protected]>
Co-Authored-By: DemiMarie-parity <[email protected]>
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>; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
1. Make preruntime Optional. 2. Add more docs on what is `preruntie` and `round`. 3. Replace `Default::default` with the approriate type.
@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. |
@shawntabrizi Yeah for sure! |
@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. |
* 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 <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/primitives/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/primitives/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * 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 <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: DemiMarie-parity <[email protected]> * Update core/consensus/pow/primitives/src/lib.rs Co-Authored-By: DemiMarie-parity <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: DemiMarie-parity <[email protected]> * 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.
* 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 <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/primitives/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * Update core/consensus/pow/primitives/src/lib.rs Co-Authored-By: Sergei Pepyakin <[email protected]> * 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 <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: DemiMarie-parity <[email protected]> * Update core/consensus/pow/primitives/src/lib.rs Co-Authored-By: DemiMarie-parity <[email protected]> * Update core/consensus/pow/src/lib.rs Co-Authored-By: DemiMarie-parity <[email protected]> * 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.
come on, How to implement this RPC? |
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:
getWork
andsubmitWork
.