Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions client/consensus/pow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
//! clients.

use std::sync::Arc;
use std::any::Any;
use std::thread;
use std::collections::HashMap;
use std::marker::PhantomData;
Expand Down Expand Up @@ -257,6 +258,7 @@ impl<B, I, C, S, Algorithm> BlockImport<B> for PowBlockImport<B, I, C, S, Algori
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + ProvideCache<B> + BlockOf,
C::Api: BlockBuilderApi<B, Error = sp_blockchain::Error>,
Algorithm: PowAlgorithm<B>,
Algorithm::Difficulty: 'static,
{
type Error = ConsensusError;
type Transaction = sp_api::TransactionFor<C, B>;
Expand Down Expand Up @@ -312,10 +314,9 @@ impl<B, I, C, S, Algorithm> BlockImport<B> for PowBlockImport<B, I, C, S, Algori
_ => return Err(Error::<B>::HeaderUnsealed(block.header.hash()).into()),
};

let intermediate = PowIntermediate::<B, Algorithm::Difficulty>::decode(
&mut &block.intermediates.remove(INTERMEDIATE_KEY)
.ok_or(Error::<B>::NoIntermediate)?[..]
).map_err(|_| Error::<B>::NoIntermediate)?;
let intermediate = block.take_intermediate::<PowIntermediate::<B, Algorithm::Difficulty>>(
INTERMEDIATE_KEY
).ok_or(Error::<B>::NoIntermediate)?;

let difficulty = match intermediate.difficulty {
Some(difficulty) => difficulty,
Expand Down Expand Up @@ -392,6 +393,7 @@ impl<B: BlockT, Algorithm> PowVerifier<B, Algorithm> {

impl<B: BlockT, Algorithm> Verifier<B> for PowVerifier<B, Algorithm> where
Algorithm: PowAlgorithm<B> + Send + Sync,
Algorithm::Difficulty: 'static,
{
fn verify(
&mut self,
Expand All @@ -418,7 +420,7 @@ impl<B: BlockT, Algorithm> Verifier<B> for PowVerifier<B, Algorithm> where
justification,
intermediates: {
let mut ret = HashMap::new();
ret.insert(INTERMEDIATE_KEY.to_vec(), intermediate.encode());
ret.insert(INTERMEDIATE_KEY, Box::new(intermediate) as Box<dyn Any>);
ret
},
auxiliary: vec![],
Expand Down Expand Up @@ -553,6 +555,7 @@ fn mine_loop<B: BlockT, C, Algorithm, E, SO, S, CAW>(
) -> Result<(), Error<B>> where
C: HeaderBackend<B> + AuxStore + ProvideRuntimeApi<B>,
Algorithm: PowAlgorithm<B>,
Algorithm::Difficulty: 'static,
E: Environment<B>,
E::Proposer: Proposer<B, Transaction = sp_api::TransactionFor<C, B>>,
E::Error: std::fmt::Debug,
Expand Down Expand Up @@ -659,7 +662,7 @@ fn mine_loop<B: BlockT, C, Algorithm, E, SO, S, CAW>(
storage_changes: Some(proposal.storage_changes),
intermediates: {
let mut ret = HashMap::new();
ret.insert(INTERMEDIATE_KEY.to_vec(), intermediate.encode());
ret.insert(INTERMEDIATE_KEY, Box::new(intermediate) as Box<dyn Any>);
ret
},
finalized: false,
Expand Down
29 changes: 28 additions & 1 deletion primitives/consensus/common/src/block_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use serde::{Serialize, Deserialize};
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use std::any::Any;

use crate::import_queue::{Verifier, CacheKeyId};

Expand Down Expand Up @@ -144,7 +145,7 @@ pub struct BlockImportParams<Block: BlockT, Transaction> {
/// Intermediate values that are interpreted by block importers. Each block importer,
/// upon handling a value, removes it from the intermediate list. The final block importer
/// rejects block import if there are still intermediate values that remain unhandled.
pub intermediates: HashMap<Vec<u8>, Vec<u8>>,
pub intermediates: HashMap<&'static [u8], Box<dyn Any>>,
Comment thread
sorpaas marked this conversation as resolved.
Outdated
/// Auxiliary consensus data produced by the block.
/// Contains a list of key-value pairs. If values are `None`, the keys
/// will be deleted.
Expand Down Expand Up @@ -223,6 +224,32 @@ impl<Block: BlockT, Transaction> BlockImportParams<Block, Transaction> {
import_existing: self.import_existing,
}
}

/// Take interemdiate by given key, and remove it from the processing list.
pub fn take_intermediate<T: 'static>(&mut self, key: &'static [u8]) -> Option<Box<T>> {
Comment thread
sorpaas marked this conversation as resolved.
Outdated
self.intermediates.remove(key)
.and_then(|value| {
match value.downcast::<T>() {
Ok(v) => Some(v),
Err(v) => {
self.intermediates.insert(key, v);
None
},
}
})
}

/// Get a reference to a given intermediate.
pub fn intermediate<T: 'static>(&self, key: &'static [u8]) -> Option<&T> {
self.intermediates.get(key)
.and_then(|value| value.downcast_ref::<T>())
}

/// Get a mutable reference to a given intermediate.
pub fn intermediate_mut<T: 'static>(&mut self, key: &'static [u8]) -> Option<&mut T> {
self.intermediates.get_mut(key)
.and_then(|value| value.downcast_mut::<T>())
}
}

/// Block import trait.
Expand Down