feat: Rebrand Polkadot/Substrate references to PezkuwiChain

This commit systematically rebrands various references from Parity Technologies'
Polkadot/Substrate ecosystem to PezkuwiChain within the kurdistan-sdk.

Key changes include:
- Updated external repository URLs (zombienet-sdk, parity-db, parity-scale-codec, wasm-instrument) to point to pezkuwichain forks.
- Modified internal documentation and code comments to reflect PezkuwiChain naming and structure.
- Replaced direct references to  with  or specific paths within the  for XCM, Pezkuwi, and other modules.
- Cleaned up deprecated  issue and PR references in various  and  files, particularly in  and  modules.
- Adjusted image and logo URLs in documentation to point to PezkuwiChain assets.
- Removed or rephrased comments related to external Polkadot/Substrate PRs and issues.

This is a significant step towards fully customizing the SDK for the PezkuwiChain ecosystem.
This commit is contained in:
2025-12-14 00:04:10 +03:00
parent 286de54384
commit 1c0e57d984
9084 changed files with 997839 additions and 997557 deletions
@@ -0,0 +1,49 @@
[package]
name = "pezsc-consensus-pow"
version = "0.33.0"
authors.workspace = true
description = "PoW consensus algorithm for bizinikiwi"
edition.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
homepage.workspace = true
repository.workspace = true
readme = "README.md"
[lints]
workspace = true
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
async-trait = { workspace = true }
codec = { features = ["derive"], workspace = true, default-features = true }
futures = { workspace = true }
futures-timer = { workspace = true }
log = { workspace = true, default-features = true }
parking_lot = { workspace = true, default-features = true }
prometheus-endpoint = { workspace = true, default-features = true }
pezsc-client-api = { workspace = true, default-features = true }
pezsc-consensus = { workspace = true, default-features = true }
pezsp-api = { workspace = true, default-features = true }
pezsp-block-builder = { workspace = true, default-features = true }
pezsp-blockchain = { workspace = true, default-features = true }
pezsp-consensus = { workspace = true, default-features = true }
pezsp-consensus-pow = { workspace = true, default-features = true }
pezsp-core = { workspace = true, default-features = true }
pezsp-inherents = { workspace = true, default-features = true }
pezsp-runtime = { workspace = true, default-features = true }
thiserror = { workspace = true }
[features]
runtime-benchmarks = [
"pezsc-client-api/runtime-benchmarks",
"pezsc-consensus/runtime-benchmarks",
"pezsp-api/runtime-benchmarks",
"pezsp-block-builder/runtime-benchmarks",
"pezsp-blockchain/runtime-benchmarks",
"pezsp-consensus-pow/runtime-benchmarks",
"pezsp-consensus/runtime-benchmarks",
"pezsp-inherents/runtime-benchmarks",
"pezsp-runtime/runtime-benchmarks",
]
+24
View File
@@ -0,0 +1,24 @@
Proof of work consensus for Bizinikiwi.
To use this engine, you can need to have a struct that implements
`PowAlgorithm`. After that, pass an instance of the struct, along
with other necessary client references to `import_queue` to setup
the queue.
This library also comes with an async mining worker, which can be
started via the `start_mining_worker` function. It returns a worker
handle together with a future. The future must be pulled. Through
the worker handle, you can pull the metadata needed to start the
mining process via `MiningWorker::metadata`, and then do the actual
mining on a standalone thread. Finally, when a seal is found, call
`MiningWorker::submit` to build the block.
The auxiliary storage for PoW engine only stores the total difficulty.
For other storage requirements for particular PoW algorithm (such as
the actual difficulty for each particular blocks), you can take a client
reference in your `PowAlgorithm` implementation, and use a separate prefix
for the auxiliary storage. It is also possible to just use the runtime
as the storage, but it is not recommended as it won't work well with light
clients.
License: GPL-3.0-or-later WITH Classpath-exception-2.0
+672
View File
@@ -0,0 +1,672 @@
// This file is part of Bizinikiwi.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Proof of work consensus for Bizinikiwi.
//!
//! To use this engine, you can need to have a struct that implements
//! [`PowAlgorithm`]. After that, pass an instance of the struct, along
//! with other necessary client references to [`import_queue`] to setup
//! the queue.
//!
//! This library also comes with an async mining worker, which can be
//! started via the [`start_mining_worker`] function. It returns a worker
//! handle together with a future. The future must be pulled. Through
//! the worker handle, you can pull the metadata needed to start the
//! mining process via [`MiningHandle::metadata`], and then do the actual
//! mining on a standalone thread. Finally, when a seal is found, call
//! [`MiningHandle::submit`] to build the block.
//!
//! The auxiliary storage for PoW engine only stores the total difficulty.
//! For other storage requirements for particular PoW algorithm (such as
//! the actual difficulty for each particular blocks), you can take a client
//! reference in your [`PowAlgorithm`] implementation, and use a separate prefix
//! for the auxiliary storage. It is also possible to just use the runtime
//! as the storage, but it is not recommended as it won't work well with light
//! clients.
mod worker;
pub use crate::worker::{MiningBuild, MiningHandle, MiningMetadata};
use crate::worker::UntilImportedOrTimeout;
use codec::{Decode, Encode};
use futures::{Future, StreamExt};
use log::*;
use prometheus_endpoint::Registry;
use pezsc_client_api::{self, backend::AuxStore, BlockOf, BlockchainEvents};
use pezsc_consensus::{
BasicQueue, BlockCheckParams, BlockImport, BlockImportParams, BoxBlockImport,
BoxJustificationImport, ForkChoiceStrategy, ImportResult, Verifier,
};
use pezsp_api::ProvideRuntimeApi;
use pezsp_block_builder::BlockBuilder as BlockBuilderApi;
use pezsp_blockchain::HeaderBackend;
use pezsp_consensus::{Environment, Error as ConsensusError, Proposer, SelectChain, SyncOracle};
use pezsp_consensus_pow::{Seal, TotalDifficulty, POW_ENGINE_ID};
use pezsp_inherents::{CreateInherentDataProviders, InherentDataProvider};
use pezsp_runtime::{
generic::{BlockId, Digest, DigestItem},
traits::{Block as BlockT, Header as HeaderT},
};
use std::{cmp::Ordering, marker::PhantomData, sync::Arc, time::Duration};
const LOG_TARGET: &str = "pow";
#[derive(Debug, thiserror::Error)]
pub enum Error<B: BlockT> {
#[error("Header uses the wrong engine {0:?}")]
WrongEngine([u8; 4]),
#[error("Header {0:?} is unsealed")]
HeaderUnsealed(B::Hash),
#[error("PoW validation error: invalid seal")]
InvalidSeal,
#[error("PoW validation error: preliminary verification failed")]
FailedPreliminaryVerify,
#[error("Rejecting block too far in future")]
TooFarInFuture,
#[error("Fetching best header failed using select chain: {0}")]
BestHeaderSelectChain(ConsensusError),
#[error("Fetching best header failed: {0}")]
BestHeader(pezsp_blockchain::Error),
#[error("Best header does not exist")]
NoBestHeader,
#[error("Block proposing error: {0}")]
BlockProposingError(String),
#[error("Fetch best hash failed via select chain: {0}")]
BestHashSelectChain(ConsensusError),
#[error("Error with block built on {0:?}: {1}")]
BlockBuiltError(B::Hash, ConsensusError),
#[error("Creating inherents failed: {0}")]
CreateInherents(pezsp_inherents::Error),
#[error("Checking inherents failed: {0}")]
CheckInherents(pezsp_inherents::Error),
#[error(
"Checking inherents unknown error for identifier: {}",
String::from_utf8_lossy(.0)
)]
CheckInherentsUnknownError(pezsp_inherents::InherentIdentifier),
#[error("Multiple pre-runtime digests")]
MultiplePreRuntimeDigests,
#[error(transparent)]
Client(pezsp_blockchain::Error),
#[error(transparent)]
Codec(codec::Error),
#[error("{0}")]
Environment(String),
#[error("{0}")]
Runtime(String),
#[error("{0}")]
Other(String),
}
impl<B: BlockT> From<Error<B>> for String {
fn from(error: Error<B>) -> String {
error.to_string()
}
}
impl<B: BlockT> From<Error<B>> for ConsensusError {
fn from(error: Error<B>) -> ConsensusError {
ConsensusError::ClientImport(error.to_string())
}
}
/// Auxiliary storage prefix for PoW engine.
pub const POW_AUX_PREFIX: [u8; 4] = *b"PoW:";
/// Get the auxiliary storage key used by engine to store total difficulty.
fn aux_key<T: AsRef<[u8]>>(hash: &T) -> Vec<u8> {
POW_AUX_PREFIX.iter().chain(hash.as_ref()).copied().collect()
}
/// Intermediate value passed to block importer.
#[derive(Encode, Decode, Clone, Debug, Default)]
pub struct PowIntermediate<Difficulty> {
/// Difficulty of the block, if known.
pub difficulty: Option<Difficulty>,
}
/// Intermediate key for PoW engine.
pub static INTERMEDIATE_KEY: &[u8] = b"pow1";
/// Auxiliary storage data for PoW.
#[derive(Encode, Decode, Clone, Debug, Default)]
pub struct PowAux<Difficulty> {
/// Difficulty of the current block.
pub difficulty: Difficulty,
/// Total difficulty up to current block.
pub total_difficulty: Difficulty,
}
impl<Difficulty> PowAux<Difficulty>
where
Difficulty: Decode + Default,
{
/// Read the auxiliary from client.
pub fn read<C: AuxStore, B: BlockT>(client: &C, hash: &B::Hash) -> Result<Self, Error<B>> {
let key = aux_key(&hash);
match client.get_aux(&key).map_err(Error::Client)? {
Some(bytes) => Self::decode(&mut &bytes[..]).map_err(Error::Codec),
None => Ok(Self::default()),
}
}
}
/// Algorithm used for proof of work.
pub trait PowAlgorithm<B: BlockT> {
/// Difficulty for the algorithm.
type Difficulty: TotalDifficulty + Default + Encode + Decode + Ord + Clone + Copy;
/// Get the next block's difficulty.
///
/// This function will be called twice during the import process, so the implementation
/// should be properly cached.
fn difficulty(&self, parent: B::Hash) -> Result<Self::Difficulty, Error<B>>;
/// Verify that the seal is valid against given pre hash when parent block is not yet imported.
///
/// None means that preliminary verify is not available for this algorithm.
fn preliminary_verify(
&self,
_pre_hash: &B::Hash,
_seal: &Seal,
) -> Result<Option<bool>, Error<B>> {
Ok(None)
}
/// Break a fork choice tie.
///
/// By default this chooses the earliest block seen. Using uniform tie
/// breaking algorithms will help to protect against selfish mining.
///
/// Returns if the new seal should be considered best block.
fn break_tie(&self, _own_seal: &Seal, _new_seal: &Seal) -> bool {
false
}
/// Verify that the difficulty is valid against given seal.
fn verify(
&self,
parent: &BlockId<B>,
pre_hash: &B::Hash,
pre_digest: Option<&[u8]>,
seal: &Seal,
difficulty: Self::Difficulty,
) -> Result<bool, Error<B>>;
}
/// A block importer for PoW.
pub struct PowBlockImport<B: BlockT, I, C, S, Algorithm, CIDP> {
algorithm: Algorithm,
inner: I,
select_chain: S,
client: Arc<C>,
create_inherent_data_providers: Arc<CIDP>,
check_inherents_after: <<B as BlockT>::Header as HeaderT>::Number,
}
impl<B: BlockT, I: Clone, C, S: Clone, Algorithm: Clone, CIDP> Clone
for PowBlockImport<B, I, C, S, Algorithm, CIDP>
{
fn clone(&self) -> Self {
Self {
algorithm: self.algorithm.clone(),
inner: self.inner.clone(),
select_chain: self.select_chain.clone(),
client: self.client.clone(),
create_inherent_data_providers: self.create_inherent_data_providers.clone(),
check_inherents_after: self.check_inherents_after,
}
}
}
impl<B, I, C, S, Algorithm, CIDP> PowBlockImport<B, I, C, S, Algorithm, CIDP>
where
B: BlockT,
I: BlockImport<B> + Send + Sync,
I::Error: Into<ConsensusError>,
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + BlockOf,
C::Api: BlockBuilderApi<B>,
Algorithm: PowAlgorithm<B>,
CIDP: CreateInherentDataProviders<B, ()>,
{
/// Create a new block import suitable to be used in PoW
pub fn new(
inner: I,
client: Arc<C>,
algorithm: Algorithm,
check_inherents_after: <<B as BlockT>::Header as HeaderT>::Number,
select_chain: S,
create_inherent_data_providers: CIDP,
) -> Self {
Self {
inner,
client,
algorithm,
check_inherents_after,
select_chain,
create_inherent_data_providers: Arc::new(create_inherent_data_providers),
}
}
async fn check_inherents(
&self,
block: B,
at_hash: B::Hash,
inherent_data_providers: CIDP::InherentDataProviders,
) -> Result<(), Error<B>> {
use pezsp_block_builder::CheckInherentsError;
if *block.header().number() < self.check_inherents_after {
return Ok(());
}
pezsp_block_builder::check_inherents(
self.client.clone(),
at_hash,
block,
&inherent_data_providers,
)
.await
.map_err(|e| match e {
CheckInherentsError::CreateInherentData(e) => Error::CreateInherents(e),
CheckInherentsError::Client(e) => Error::Client(e.into()),
CheckInherentsError::CheckInherents(e) => Error::CheckInherents(e),
CheckInherentsError::CheckInherentsUnknownError(id) =>
Error::CheckInherentsUnknownError(id),
})?;
Ok(())
}
}
#[async_trait::async_trait]
impl<B, I, C, S, Algorithm, CIDP> BlockImport<B> for PowBlockImport<B, I, C, S, Algorithm, CIDP>
where
B: BlockT,
I: BlockImport<B> + Send + Sync,
I::Error: Into<ConsensusError>,
S: SelectChain<B>,
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + BlockOf,
C::Api: BlockBuilderApi<B>,
Algorithm: PowAlgorithm<B> + Send + Sync,
Algorithm::Difficulty: 'static + Send,
CIDP: CreateInherentDataProviders<B, ()> + Send + Sync,
{
type Error = ConsensusError;
async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error> {
self.inner.check_block(block).await.map_err(Into::into)
}
async fn import_block(
&self,
mut block: BlockImportParams<B>,
) -> Result<ImportResult, Self::Error> {
let best_header = self
.select_chain
.best_chain()
.await
.map_err(|e| format!("Fetch best chain failed via select chain: {}", e))
.map_err(ConsensusError::ChainLookup)?;
let best_hash = best_header.hash();
let parent_hash = *block.header.parent_hash();
let best_aux = PowAux::read::<_, B>(self.client.as_ref(), &best_hash)?;
let mut aux = PowAux::read::<_, B>(self.client.as_ref(), &parent_hash)?;
if let Some(inner_body) = block.body.take() {
let check_block = B::new(block.header.clone(), inner_body);
if !block.state_action.skip_execution_checks() {
self.check_inherents(
check_block.clone(),
parent_hash,
self.create_inherent_data_providers
.create_inherent_data_providers(parent_hash, ())
.await?,
)
.await?;
}
block.body = Some(check_block.deconstruct().1);
}
let inner_seal = fetch_seal::<B>(block.post_digests.last(), block.header.hash())?;
let intermediate = block
.remove_intermediate::<PowIntermediate<Algorithm::Difficulty>>(INTERMEDIATE_KEY)?;
let difficulty = match intermediate.difficulty {
Some(difficulty) => difficulty,
None => self.algorithm.difficulty(parent_hash)?,
};
let pre_hash = block.header.hash();
let pre_digest = find_pre_digest::<B>(&block.header)?;
if !self.algorithm.verify(
&BlockId::hash(parent_hash),
&pre_hash,
pre_digest.as_ref().map(|v| &v[..]),
&inner_seal,
difficulty,
)? {
return Err(Error::<B>::InvalidSeal.into());
}
aux.difficulty = difficulty;
aux.total_difficulty.increment(difficulty);
let key = aux_key(&block.post_hash());
block.auxiliary.push((key, Some(aux.encode())));
if block.fork_choice.is_none() {
block.fork_choice = Some(ForkChoiceStrategy::Custom(
match aux.total_difficulty.cmp(&best_aux.total_difficulty) {
Ordering::Less => false,
Ordering::Greater => true,
Ordering::Equal => {
let best_inner_seal =
fetch_seal::<B>(best_header.digest().logs.last(), best_hash)?;
self.algorithm.break_tie(&best_inner_seal, &inner_seal)
},
},
));
}
self.inner.import_block(block).await.map_err(Into::into)
}
}
/// A verifier for PoW blocks.
pub struct PowVerifier<B: BlockT, Algorithm> {
algorithm: Algorithm,
_marker: PhantomData<B>,
}
impl<B: BlockT, Algorithm> PowVerifier<B, Algorithm> {
pub fn new(algorithm: Algorithm) -> Self {
Self { algorithm, _marker: PhantomData }
}
fn check_header(&self, mut header: B::Header) -> Result<(B::Header, DigestItem), Error<B>>
where
Algorithm: PowAlgorithm<B>,
{
let hash = header.hash();
let (seal, inner_seal) = match header.digest_mut().pop() {
Some(DigestItem::Seal(id, seal)) =>
if id == POW_ENGINE_ID {
(DigestItem::Seal(id, seal.clone()), seal)
} else {
return Err(Error::WrongEngine(id));
},
_ => return Err(Error::HeaderUnsealed(hash)),
};
let pre_hash = header.hash();
if !self.algorithm.preliminary_verify(&pre_hash, &inner_seal)?.unwrap_or(true) {
return Err(Error::FailedPreliminaryVerify);
}
Ok((header, seal))
}
}
#[async_trait::async_trait]
impl<B: BlockT, Algorithm> Verifier<B> for PowVerifier<B, Algorithm>
where
Algorithm: PowAlgorithm<B> + Send + Sync,
Algorithm::Difficulty: 'static + Send,
{
async fn verify(
&self,
mut block: BlockImportParams<B>,
) -> Result<BlockImportParams<B>, String> {
let hash = block.header.hash();
let (checked_header, seal) = self.check_header(block.header)?;
let intermediate = PowIntermediate::<Algorithm::Difficulty> { difficulty: None };
block.header = checked_header;
block.post_digests.push(seal);
block.insert_intermediate(INTERMEDIATE_KEY, intermediate);
block.post_hash = Some(hash);
Ok(block)
}
}
/// The PoW import queue type.
pub type PowImportQueue<B> = BasicQueue<B>;
/// Import queue for PoW engine.
pub fn import_queue<B, Algorithm>(
block_import: BoxBlockImport<B>,
justification_import: Option<BoxJustificationImport<B>>,
algorithm: Algorithm,
spawner: &impl pezsp_core::traits::SpawnEssentialNamed,
registry: Option<&Registry>,
) -> Result<PowImportQueue<B>, pezsp_consensus::Error>
where
B: BlockT,
Algorithm: PowAlgorithm<B> + Clone + Send + Sync + 'static,
Algorithm::Difficulty: Send,
{
let verifier = PowVerifier::new(algorithm);
Ok(BasicQueue::new(verifier, block_import, justification_import, spawner, registry))
}
/// Start the mining worker for PoW. This function provides the necessary helper functions that can
/// be used to implement a miner. However, it does not do the CPU-intensive mining itself.
///
/// Two values are returned -- a worker, which contains functions that allows querying the current
/// mining metadata and submitting mined blocks, and a future, which must be polled to fill in
/// information in the worker.
///
/// `pre_runtime` is a parameter that allows a custom additional pre-runtime digest to be inserted
/// for blocks being built. This can encode authorship information, or just be a graffiti.
pub fn start_mining_worker<Block, C, S, Algorithm, E, SO, L, CIDP>(
block_import: BoxBlockImport<Block>,
client: Arc<C>,
select_chain: S,
algorithm: Algorithm,
mut env: E,
sync_oracle: SO,
justification_sync_link: L,
pre_runtime: Option<Vec<u8>>,
create_inherent_data_providers: CIDP,
timeout: Duration,
build_time: Duration,
) -> (
MiningHandle<Block, Algorithm, L, <E::Proposer as Proposer<Block>>::Proof>,
impl Future<Output = ()>,
)
where
Block: BlockT,
C: BlockchainEvents<Block> + 'static,
S: SelectChain<Block> + 'static,
Algorithm: PowAlgorithm<Block> + Clone,
Algorithm::Difficulty: Send + 'static,
E: Environment<Block> + Send + Sync + 'static,
E::Error: std::fmt::Debug,
E::Proposer: Proposer<Block>,
SO: SyncOracle + Clone + Send + Sync + 'static,
L: pezsc_consensus::JustificationSyncLink<Block>,
CIDP: CreateInherentDataProviders<Block, ()>,
{
let mut timer = UntilImportedOrTimeout::new(client.import_notification_stream(), timeout);
let worker = MiningHandle::new(algorithm.clone(), block_import, justification_sync_link);
let worker_ret = worker.clone();
let task = async move {
loop {
if timer.next().await.is_none() {
break;
}
if sync_oracle.is_major_syncing() {
debug!(target: LOG_TARGET, "Skipping proposal due to sync.");
worker.on_major_syncing();
continue;
}
let best_header = match select_chain.best_chain().await {
Ok(x) => x,
Err(err) => {
warn!(
target: LOG_TARGET,
"Unable to pull new block for authoring. \
Select best chain error: {}",
err
);
continue;
},
};
let best_hash = best_header.hash();
if worker.best_hash() == Some(best_hash) {
continue;
}
// The worker is locked for the duration of the whole proposing period. Within this
// period, the mining target is outdated and useless anyway.
let difficulty = match algorithm.difficulty(best_hash) {
Ok(x) => x,
Err(err) => {
warn!(
target: LOG_TARGET,
"Unable to propose new block for authoring. \
Fetch difficulty failed: {}",
err,
);
continue;
},
};
let inherent_data_providers = match create_inherent_data_providers
.create_inherent_data_providers(best_hash, ())
.await
{
Ok(x) => x,
Err(err) => {
warn!(
target: LOG_TARGET,
"Unable to propose new block for authoring. \
Creating inherent data providers failed: {}",
err,
);
continue;
},
};
let inherent_data = match inherent_data_providers.create_inherent_data().await {
Ok(r) => r,
Err(e) => {
warn!(
target: LOG_TARGET,
"Unable to propose new block for authoring. \
Creating inherent data failed: {}",
e,
);
continue;
},
};
let mut inherent_digest = Digest::default();
if let Some(pre_runtime) = &pre_runtime {
inherent_digest.push(DigestItem::PreRuntime(POW_ENGINE_ID, pre_runtime.to_vec()));
}
let pre_runtime = pre_runtime.clone();
let proposer = match env.init(&best_header).await {
Ok(x) => x,
Err(err) => {
warn!(
target: LOG_TARGET,
"Unable to propose new block for authoring. \
Creating proposer failed: {:?}",
err,
);
continue;
},
};
let proposal =
match proposer.propose(inherent_data, inherent_digest, build_time, None).await {
Ok(x) => x,
Err(err) => {
warn!(
target: LOG_TARGET,
"Unable to propose new block for authoring. \
Creating proposal failed: {}",
err,
);
continue;
},
};
let build = MiningBuild::<Block, Algorithm, _> {
metadata: MiningMetadata {
best_hash,
pre_hash: proposal.block.header().hash(),
pre_runtime: pre_runtime.clone(),
difficulty,
},
proposal,
};
worker.on_build(build);
}
};
(worker_ret, task)
}
/// Find PoW pre-runtime.
fn find_pre_digest<B: BlockT>(header: &B::Header) -> Result<Option<Vec<u8>>, Error<B>> {
let mut pre_digest: Option<_> = None;
for log in header.digest().logs() {
trace!(target: LOG_TARGET, "Checking log {:?}, looking for pre runtime digest", log);
match (log, pre_digest.is_some()) {
(DigestItem::PreRuntime(POW_ENGINE_ID, _), true) =>
return Err(Error::MultiplePreRuntimeDigests),
(DigestItem::PreRuntime(POW_ENGINE_ID, v), false) => {
pre_digest = Some(v.clone());
},
(_, _) => trace!(target: LOG_TARGET, "Ignoring digest not meant for us"),
}
}
Ok(pre_digest)
}
/// Fetch PoW seal.
fn fetch_seal<B: BlockT>(digest: Option<&DigestItem>, hash: B::Hash) -> Result<Vec<u8>, Error<B>> {
match digest {
Some(DigestItem::Seal(id, seal)) =>
if id == &POW_ENGINE_ID {
Ok(seal.clone())
} else {
Err(Error::<B>::WrongEngine(*id))
},
_ => Err(Error::<B>::HeaderUnsealed(hash)),
}
}
@@ -0,0 +1,283 @@
// This file is part of Bizinikiwi.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use futures::{
prelude::*,
task::{Context, Poll},
};
use futures_timer::Delay;
use log::*;
use parking_lot::Mutex;
use pezsc_client_api::ImportNotifications;
use pezsc_consensus::{BlockImportParams, BoxBlockImport, StateAction, StorageChanges};
use pezsp_consensus::{BlockOrigin, Proposal};
use pezsp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT},
DigestItem,
};
use std::{
pin::Pin,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
time::Duration,
};
use crate::{PowAlgorithm, PowIntermediate, Seal, INTERMEDIATE_KEY, LOG_TARGET, POW_ENGINE_ID};
/// Mining metadata. This is the information needed to start an actual mining loop.
#[derive(Clone, Eq, PartialEq)]
pub struct MiningMetadata<H, D> {
/// Currently known best hash which the pre-hash is built on.
pub best_hash: H,
/// Mining pre-hash.
pub pre_hash: H,
/// Pre-runtime digest item.
pub pre_runtime: Option<Vec<u8>>,
/// Mining target difficulty.
pub difficulty: D,
}
/// A build of mining, containing the metadata and the block proposal.
pub struct MiningBuild<Block: BlockT, Algorithm: PowAlgorithm<Block>, Proof> {
/// Mining metadata.
pub metadata: MiningMetadata<Block::Hash, Algorithm::Difficulty>,
/// Mining proposal.
pub proposal: Proposal<Block, Proof>,
}
/// Version of the mining worker.
#[derive(Eq, PartialEq, Clone, Copy)]
pub struct Version(usize);
/// Mining worker that exposes structs to query the current mining build and submit mined blocks.
pub struct MiningHandle<
Block: BlockT,
Algorithm: PowAlgorithm<Block>,
L: pezsc_consensus::JustificationSyncLink<Block>,
Proof,
> {
version: Arc<AtomicUsize>,
algorithm: Arc<Algorithm>,
justification_sync_link: Arc<L>,
build: Arc<Mutex<Option<MiningBuild<Block, Algorithm, Proof>>>>,
block_import: Arc<Mutex<BoxBlockImport<Block>>>,
}
impl<Block, Algorithm, L, Proof> MiningHandle<Block, Algorithm, L, Proof>
where
Block: BlockT,
Algorithm: PowAlgorithm<Block>,
Algorithm::Difficulty: 'static + Send,
L: pezsc_consensus::JustificationSyncLink<Block>,
{
fn increment_version(&self) {
self.version.fetch_add(1, Ordering::SeqCst);
}
pub(crate) fn new(
algorithm: Algorithm,
block_import: BoxBlockImport<Block>,
justification_sync_link: L,
) -> Self {
Self {
version: Arc::new(AtomicUsize::new(0)),
algorithm: Arc::new(algorithm),
justification_sync_link: Arc::new(justification_sync_link),
build: Arc::new(Mutex::new(None)),
block_import: Arc::new(Mutex::new(block_import)),
}
}
pub(crate) fn on_major_syncing(&self) {
let mut build = self.build.lock();
*build = None;
self.increment_version();
}
pub(crate) fn on_build(&self, value: MiningBuild<Block, Algorithm, Proof>) {
let mut build = self.build.lock();
*build = Some(value);
self.increment_version();
}
/// Get the version of the mining worker.
///
/// This returns type `Version` which can only compare equality. If `Version` is unchanged, then
/// it can be certain that `best_hash` and `metadata` were not changed.
pub fn version(&self) -> Version {
Version(self.version.load(Ordering::SeqCst))
}
/// Get the current best hash. `None` if the worker has just started or the client is doing
/// major syncing.
pub fn best_hash(&self) -> Option<Block::Hash> {
self.build.lock().as_ref().map(|b| b.metadata.best_hash)
}
/// Get a copy of the current mining metadata, if available.
pub fn metadata(&self) -> Option<MiningMetadata<Block::Hash, Algorithm::Difficulty>> {
self.build.lock().as_ref().map(|b| b.metadata.clone())
}
/// Submit a mined seal. The seal will be validated again. Returns true if the submission is
/// successful.
pub async fn submit(&self, seal: Seal) -> bool {
if let Some(metadata) = self.metadata() {
match self.algorithm.verify(
&BlockId::Hash(metadata.best_hash),
&metadata.pre_hash,
metadata.pre_runtime.as_ref().map(|v| &v[..]),
&seal,
metadata.difficulty,
) {
Ok(true) => (),
Ok(false) => {
warn!(target: LOG_TARGET, "Unable to import mined block: seal is invalid",);
return false;
},
Err(err) => {
warn!(target: LOG_TARGET, "Unable to import mined block: {}", err,);
return false;
},
}
} else {
warn!(target: LOG_TARGET, "Unable to import mined block: metadata does not exist",);
return false;
}
let build = if let Some(build) = {
let mut build = self.build.lock();
let value = build.take();
if value.is_some() {
self.increment_version();
}
value
} {
build
} else {
warn!(target: LOG_TARGET, "Unable to import mined block: build does not exist",);
return false;
};
let seal = DigestItem::Seal(POW_ENGINE_ID, seal);
let (header, body) = build.proposal.block.deconstruct();
let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
import_block.post_digests.push(seal);
import_block.body = Some(body);
import_block.state_action =
StateAction::ApplyChanges(StorageChanges::Changes(build.proposal.storage_changes));
let intermediate = PowIntermediate::<Algorithm::Difficulty> {
difficulty: Some(build.metadata.difficulty),
};
import_block.insert_intermediate(INTERMEDIATE_KEY, intermediate);
let header = import_block.post_header();
let block_import = self.block_import.lock();
match block_import.import_block(import_block).await {
Ok(res) => {
res.handle_justification(
&header.hash(),
*header.number(),
&self.justification_sync_link,
);
info!(
target: LOG_TARGET,
"✅ Successfully mined block on top of: {}", build.metadata.best_hash
);
true
},
Err(err) => {
warn!(target: LOG_TARGET, "Unable to import mined block: {}", err,);
false
},
}
}
}
impl<Block, Algorithm, L, Proof> Clone for MiningHandle<Block, Algorithm, L, Proof>
where
Block: BlockT,
Algorithm: PowAlgorithm<Block>,
L: pezsc_consensus::JustificationSyncLink<Block>,
{
fn clone(&self) -> Self {
Self {
version: self.version.clone(),
algorithm: self.algorithm.clone(),
justification_sync_link: self.justification_sync_link.clone(),
build: self.build.clone(),
block_import: self.block_import.clone(),
}
}
}
/// A stream that waits for a block import or timeout.
pub struct UntilImportedOrTimeout<Block: BlockT> {
import_notifications: ImportNotifications<Block>,
timeout: Duration,
inner_delay: Option<Delay>,
}
impl<Block: BlockT> UntilImportedOrTimeout<Block> {
/// Create a new stream using the given import notification and timeout duration.
pub fn new(import_notifications: ImportNotifications<Block>, timeout: Duration) -> Self {
Self { import_notifications, timeout, inner_delay: None }
}
}
impl<Block: BlockT> Stream for UntilImportedOrTimeout<Block> {
type Item = ();
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<()>> {
let mut fire = false;
loop {
match Stream::poll_next(Pin::new(&mut self.import_notifications), cx) {
Poll::Pending => break,
Poll::Ready(Some(_)) => {
fire = true;
},
Poll::Ready(None) => return Poll::Ready(None),
}
}
let timeout = self.timeout;
let inner_delay = self.inner_delay.get_or_insert_with(|| Delay::new(timeout));
match Future::poll(Pin::new(inner_delay), cx) {
Poll::Pending => (),
Poll::Ready(()) => {
fire = true;
},
}
if fire {
self.inner_delay = None;
Poll::Ready(Some(()))
} else {
Poll::Pending
}
}
}