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

Commit

Permalink
Cumulative fixes to make working with consensus-pow easier (#3617)
Browse files Browse the repository at this point in the history
* consensus-pow: add difficulty data to auxiliary

* Timestamp api

* Implement FinalityProofProvider for ()

* Add DifficultyApi

* Remove assumption that Difficulty is u128

* Use a separate trait for add instead of hard-code it as Saturating

* Some convenience functions to work with PowVerifier

* Try to fix mining unstability

* Fix generic resolution

* Unused best_header variable

* Fix hash calculation

* Remove artificial sleep

* Tweak proposer waiting time

* Revert sleep removal

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.

* Pass sync oracle to mining

So that it does not mine when major syncing

* Expose build time as a parameter

Instead of hardcode it as previously 100ms.

* Update lock file

* Fix compile

* Support skipping check_inherents for ancient blocks

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.

* Move difficulty fetch function out of loop

To make things faster

* Remove seed from mining

Each engine can use its own Rng source.

* Better comments

* Add TotalDifficulty definition for U256 and u128

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

Co-Authored-By: André Silva <andre.beat@gmail.com>

* Rename TotalDifficulty::add -> increment

* Use SelectChain to fetch the best header/hash

* Update lock file
  • Loading branch information
sorpaas authored Oct 3, 2019
1 parent 8492a26 commit ddd7368
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 50 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions core/consensus/pow/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ substrate-client = { path = "../../../client", default-features = false }
rstd = { package = "sr-std", path = "../../../sr-std", default-features = false }
sr-primitives = { path = "../../../sr-primitives", default-features = false }
primitives = { package = "substrate-primitives", path = "../../../primitives", default-features = false }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }

[features]
default = ["std"]
Expand All @@ -18,4 +19,5 @@ std = [
"substrate-client/std",
"sr-primitives/std",
"primitives/std",
"codec/std",
]
45 changes: 37 additions & 8 deletions core/consensus/pow/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,46 @@

use rstd::vec::Vec;
use sr_primitives::ConsensusEngineId;
use codec::Decode;
use substrate_client::decl_runtime_apis;

/// The `ConsensusEngineId` of PoW.
pub const POW_ENGINE_ID: ConsensusEngineId = [b'p', b'o', b'w', b'_'];

/// Type of difficulty.
///
/// For runtime designed for Substrate, it's always possible to fit its total
/// difficulty range under `u128::max_value()` because it can be freely scaled
/// up or scaled down. Very few PoW chains use difficulty values
/// larger than `u128::max_value()`.
pub type Difficulty = u128;

/// Type of seal.
pub type Seal = Vec<u8>;

/// Define methods that total difficulty should implement.
pub trait TotalDifficulty {
fn increment(&mut self, other: Self);
}

impl TotalDifficulty for primitives::U256 {
fn increment(&mut self, other: Self) {
let ret = self.saturating_add(other);
*self = ret;
}
}

impl TotalDifficulty for u128 {
fn increment(&mut self, other: Self) {
let ret = self.saturating_add(other);
*self = ret;
}
}

decl_runtime_apis! {
/// API necessary for timestamp-based difficulty adjustment algorithms.
pub trait TimestampApi<Moment: Decode> {
/// Return the timestamp in the current block.
fn timestamp() -> Moment;
}

/// API for those chains that put their difficulty adjustment algorithm directly
/// onto runtime. Note that while putting difficulty adjustment algorithm to
/// runtime is safe, putting the PoW algorithm on runtime is not.
pub trait DifficultyApi<Difficulty: Decode> {
/// Return the target difficulty of the next block.
fn difficulty() -> Difficulty;
}
}
Loading

0 comments on commit ddd7368

Please sign in to comment.