referrerpolicy=no-referrer-when-downgrade

sp_consensus_pow/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Primitives for Substrate Proof-of-Work (PoW) consensus.
19
20#![cfg_attr(not(feature = "std"), no_std)]
21
22extern crate alloc;
23
24#[cfg(not(feature = "std"))]
25use alloc::vec::Vec;
26use codec::Decode;
27use sp_runtime::ConsensusEngineId;
28
29/// The `ConsensusEngineId` of PoW.
30pub const POW_ENGINE_ID: ConsensusEngineId = [b'p', b'o', b'w', b'_'];
31
32/// Type of seal.
33pub type Seal = Vec<u8>;
34
35/// Define methods that total difficulty should implement.
36pub trait TotalDifficulty {
37	fn increment(&mut self, other: Self);
38}
39
40impl TotalDifficulty for sp_core::U256 {
41	fn increment(&mut self, other: Self) {
42		let ret = self.saturating_add(other);
43		*self = ret;
44	}
45}
46
47impl TotalDifficulty for u128 {
48	fn increment(&mut self, other: Self) {
49		let ret = self.saturating_add(other);
50		*self = ret;
51	}
52}
53
54sp_api::decl_runtime_apis! {
55	/// API necessary for timestamp-based difficulty adjustment algorithms.
56	pub trait TimestampApi<Moment: Decode> {
57		/// Return the timestamp in the current block.
58		fn timestamp() -> Moment;
59	}
60
61	/// API for those chains that put their difficulty adjustment algorithm directly
62	/// onto runtime. Note that while putting difficulty adjustment algorithm to
63	/// runtime is safe, putting the PoW algorithm on runtime is not.
64	pub trait DifficultyApi<Difficulty: Decode> {
65		/// Return the target difficulty of the next block.
66		fn difficulty() -> Difficulty;
67	}
68}