mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-23 20:11:06 +00:00
pow: re-add support for algorithms where only linear verification is possible (#4843)
* pow: re-add support for algorithms where only linear verification is possible * Remove unused generic parameters * Clone impl for PowBlockImport
This commit is contained in:
@@ -63,8 +63,8 @@ pub enum Error<B: BlockT> {
|
|||||||
HeaderUnsealed(B::Hash),
|
HeaderUnsealed(B::Hash),
|
||||||
#[display(fmt = "PoW validation error: invalid seal")]
|
#[display(fmt = "PoW validation error: invalid seal")]
|
||||||
InvalidSeal,
|
InvalidSeal,
|
||||||
#[display(fmt = "PoW validation error: invalid difficulty")]
|
#[display(fmt = "PoW validation error: preliminary verification failed")]
|
||||||
InvalidDifficulty,
|
FailedPreliminaryVerify,
|
||||||
#[display(fmt = "Rejecting block too far in future")]
|
#[display(fmt = "Rejecting block too far in future")]
|
||||||
TooFarInFuture,
|
TooFarInFuture,
|
||||||
#[display(fmt = "Fetching best header failed using select chain: {:?}", _0)]
|
#[display(fmt = "Fetching best header failed using select chain: {:?}", _0)]
|
||||||
@@ -154,18 +154,23 @@ pub trait PowAlgorithm<B: BlockT> {
|
|||||||
/// This function will be called twice during the import process, so the implementation
|
/// This function will be called twice during the import process, so the implementation
|
||||||
/// should be properly cached.
|
/// should be properly cached.
|
||||||
fn difficulty(&self, parent: &BlockId<B>) -> Result<Self::Difficulty, Error<B>>;
|
fn difficulty(&self, parent: &BlockId<B>) -> Result<Self::Difficulty, Error<B>>;
|
||||||
/// Verify that the seal is valid against given pre hash.
|
/// Verify that the seal is valid against given pre hash when parent block is not yet imported.
|
||||||
fn verify_seal(
|
///
|
||||||
|
/// None means that preliminary verify is not available for this algorithm.
|
||||||
|
fn preliminary_verify(
|
||||||
&self,
|
&self,
|
||||||
|
_pre_hash: &B::Hash,
|
||||||
|
_seal: &Seal,
|
||||||
|
) -> Result<Option<bool>, Error<B>> {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
/// Verify that the difficulty is valid against given seal.
|
||||||
|
fn verify(
|
||||||
|
&self,
|
||||||
|
parent: &BlockId<B>,
|
||||||
pre_hash: &B::Hash,
|
pre_hash: &B::Hash,
|
||||||
seal: &Seal,
|
seal: &Seal,
|
||||||
) -> Result<bool, Error<B>>;
|
|
||||||
/// Verify that the difficulty is valid against given seal.
|
|
||||||
fn verify_difficulty(
|
|
||||||
&self,
|
|
||||||
difficulty: Self::Difficulty,
|
difficulty: Self::Difficulty,
|
||||||
parent: &BlockId<B>,
|
|
||||||
seal: &Seal,
|
|
||||||
) -> Result<bool, Error<B>>;
|
) -> Result<bool, Error<B>>;
|
||||||
/// Mine a seal that satisfies the given difficulty.
|
/// Mine a seal that satisfies the given difficulty.
|
||||||
fn mine(
|
fn mine(
|
||||||
@@ -187,6 +192,19 @@ pub struct PowBlockImport<B: BlockT, I, C, S, Algorithm> {
|
|||||||
check_inherents_after: <<B as BlockT>::Header as HeaderT>::Number,
|
check_inherents_after: <<B as BlockT>::Header as HeaderT>::Number,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<B: BlockT, I: Clone, C, S: Clone, Algorithm: Clone> Clone for PowBlockImport<B, I, C, S, Algorithm> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
algorithm: self.algorithm.clone(),
|
||||||
|
inner: self.inner.clone(),
|
||||||
|
select_chain: self.select_chain.clone(),
|
||||||
|
client: self.client.clone(),
|
||||||
|
inherent_data_providers: self.inherent_data_providers.clone(),
|
||||||
|
check_inherents_after: self.check_inherents_after.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<B, I, C, S, Algorithm> PowBlockImport<B, I, C, S, Algorithm> where
|
impl<B, I, C, S, Algorithm> PowBlockImport<B, I, C, S, Algorithm> where
|
||||||
B: BlockT,
|
B: BlockT,
|
||||||
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
|
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
|
||||||
@@ -322,12 +340,14 @@ impl<B, I, C, S, Algorithm> BlockImport<B> for PowBlockImport<B, I, C, S, Algori
|
|||||||
None => self.algorithm.difficulty(&BlockId::hash(parent_hash))?,
|
None => self.algorithm.difficulty(&BlockId::hash(parent_hash))?,
|
||||||
};
|
};
|
||||||
|
|
||||||
if !self.algorithm.verify_difficulty(
|
let pre_hash = block.header.hash();
|
||||||
difficulty,
|
if !self.algorithm.verify(
|
||||||
&BlockId::hash(parent_hash),
|
&BlockId::hash(parent_hash),
|
||||||
|
&pre_hash,
|
||||||
&inner_seal,
|
&inner_seal,
|
||||||
|
difficulty,
|
||||||
)? {
|
)? {
|
||||||
return Err(Error::<B>::InvalidDifficulty.into())
|
return Err(Error::<B>::InvalidSeal.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
aux.difficulty = difficulty;
|
aux.difficulty = difficulty;
|
||||||
@@ -379,11 +399,8 @@ impl<B: BlockT, Algorithm> PowVerifier<B, Algorithm> {
|
|||||||
|
|
||||||
let pre_hash = header.hash();
|
let pre_hash = header.hash();
|
||||||
|
|
||||||
if !self.algorithm.verify_seal(
|
if !self.algorithm.preliminary_verify(&pre_hash, &inner_seal)?.unwrap_or(true) {
|
||||||
&pre_hash,
|
return Err(Error::FailedPreliminaryVerify);
|
||||||
&inner_seal,
|
|
||||||
)? {
|
|
||||||
return Err(Error::InvalidSeal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((header, seal))
|
Ok((header, seal))
|
||||||
@@ -450,20 +467,17 @@ pub fn register_pow_inherent_data_provider(
|
|||||||
pub type PowImportQueue<B, Transaction> = BasicQueue<B, Transaction>;
|
pub type PowImportQueue<B, Transaction> = BasicQueue<B, Transaction>;
|
||||||
|
|
||||||
/// Import queue for PoW engine.
|
/// Import queue for PoW engine.
|
||||||
pub fn import_queue<B, C, S, Algorithm>(
|
pub fn import_queue<B, Transaction, Algorithm>(
|
||||||
block_import: BoxBlockImport<B, sp_api::TransactionFor<C, B>>,
|
block_import: BoxBlockImport<B, Transaction>,
|
||||||
algorithm: Algorithm,
|
algorithm: Algorithm,
|
||||||
inherent_data_providers: InherentDataProviders,
|
inherent_data_providers: InherentDataProviders,
|
||||||
) -> Result<
|
) -> Result<
|
||||||
PowImportQueue<B, sp_api::TransactionFor<C, B>>,
|
PowImportQueue<B, Transaction>,
|
||||||
sp_consensus::Error
|
sp_consensus::Error
|
||||||
> where
|
> where
|
||||||
B: BlockT,
|
B: BlockT,
|
||||||
C: ProvideRuntimeApi<B> + HeaderBackend<B> + BlockOf + ProvideCache<B> + AuxStore,
|
Transaction: Send + Sync + 'static,
|
||||||
C: Send + Sync + AuxStore + 'static,
|
|
||||||
C::Api: BlockBuilderApi<B, Error = sp_blockchain::Error>,
|
|
||||||
Algorithm: PowAlgorithm<B> + Clone + Send + Sync + 'static,
|
Algorithm: PowAlgorithm<B> + Clone + Send + Sync + 'static,
|
||||||
S: SelectChain<B> + 'static,
|
|
||||||
{
|
{
|
||||||
register_pow_inherent_data_provider(&inherent_data_providers)?;
|
register_pow_inherent_data_provider(&inherent_data_providers)?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user