mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 21:21:11 +00:00
Make Verifier::verify mutable (#3165)
* Make Verifier::verify mutable * Fix GrandPa tests * Fix doctest * Fix more doctests
This commit is contained in:
committed by
Gavin Wood
parent
f11291cd9a
commit
97febf4c30
@@ -45,7 +45,7 @@ fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock<Block>)
|
||||
#[test]
|
||||
fn import_single_good_block_works() {
|
||||
let (_, _hash, number, peer_id, block) = prepare_good_block();
|
||||
match import_single_block(&mut test_client::new(), BlockOrigin::File, block, Arc::new(PassThroughVerifier(true))) {
|
||||
match import_single_block(&mut test_client::new(), BlockOrigin::File, block, &mut PassThroughVerifier(true)) {
|
||||
Ok(BlockImportResult::ImportedUnknown(ref num, ref aux, ref org))
|
||||
if *num == number && *aux == Default::default() && *org == Some(peer_id) => {}
|
||||
_ => panic!()
|
||||
@@ -55,7 +55,7 @@ fn import_single_good_block_works() {
|
||||
#[test]
|
||||
fn import_single_good_known_block_is_ignored() {
|
||||
let (mut client, _hash, number, _, block) = prepare_good_block();
|
||||
match import_single_block(&mut client, BlockOrigin::File, block, Arc::new(PassThroughVerifier(true))) {
|
||||
match import_single_block(&mut client, BlockOrigin::File, block, &mut PassThroughVerifier(true)) {
|
||||
Ok(BlockImportResult::ImportedKnown(ref n)) if *n == number => {}
|
||||
_ => panic!()
|
||||
}
|
||||
@@ -65,7 +65,7 @@ fn import_single_good_known_block_is_ignored() {
|
||||
fn import_single_good_block_without_header_fails() {
|
||||
let (_, _, _, peer_id, mut block) = prepare_good_block();
|
||||
block.header = None;
|
||||
match import_single_block(&mut test_client::new(), BlockOrigin::File, block, Arc::new(PassThroughVerifier(true))) {
|
||||
match import_single_block(&mut test_client::new(), BlockOrigin::File, block, &mut PassThroughVerifier(true)) {
|
||||
Err(BlockImportError::IncompleteHeader(ref org)) if *org == Some(peer_id) => {}
|
||||
_ => panic!()
|
||||
}
|
||||
@@ -75,7 +75,7 @@ fn import_single_good_block_without_header_fails() {
|
||||
fn async_import_queue_drops() {
|
||||
// Perform this test multiple times since it exhibits non-deterministic behavior.
|
||||
for _ in 0..100 {
|
||||
let verifier = Arc::new(PassThroughVerifier(true));
|
||||
let verifier = PassThroughVerifier(true);
|
||||
let queue = BasicQueue::new(verifier, Box::new(test_client::new()), None, None);
|
||||
drop(queue);
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ pub struct PassThroughVerifier(pub bool);
|
||||
/// This `Verifier` accepts all data as valid.
|
||||
impl<B: BlockT> Verifier<B> for PassThroughVerifier {
|
||||
fn verify(
|
||||
&self,
|
||||
&mut self,
|
||||
origin: BlockOrigin,
|
||||
header: B::Header,
|
||||
justification: Option<Justification>,
|
||||
@@ -212,7 +212,7 @@ pub struct Peer<D, S: NetworkSpecialization<Block>> {
|
||||
client: PeersClient,
|
||||
/// We keep a copy of the verifier so that we can invoke it for locally-generated blocks,
|
||||
/// instead of going through the import queue.
|
||||
verifier: Arc<dyn Verifier<Block>>,
|
||||
verifier: VerifierAdapter<dyn Verifier<Block>>,
|
||||
/// We keep a copy of the block_import so that we can invoke it for locally-generated blocks,
|
||||
/// instead of going through the import queue.
|
||||
block_import: Box<dyn BlockImport<Block, Error = ConsensusError>>,
|
||||
@@ -395,6 +395,27 @@ impl<T: ?Sized + BlockImport<Block>> BlockImport<Block> for BlockImportAdapter<T
|
||||
}
|
||||
}
|
||||
|
||||
/// Implements `Verifier` on an `Arc<Mutex<impl Verifier>>`. Used internally.
|
||||
struct VerifierAdapter<T: ?Sized>(Arc<Mutex<Box<T>>>);
|
||||
|
||||
impl<T: ?Sized> Clone for VerifierAdapter<T> {
|
||||
fn clone(&self) -> Self {
|
||||
VerifierAdapter(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: BlockT, T: ?Sized + Verifier<B>> Verifier<B> for VerifierAdapter<T> {
|
||||
fn verify(
|
||||
&mut self,
|
||||
origin: BlockOrigin,
|
||||
header: B::Header,
|
||||
justification: Option<Justification>,
|
||||
body: Option<Vec<B::Extrinsic>>
|
||||
) -> Result<(BlockImportParams<B>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
|
||||
self.0.lock().verify(origin, header, justification, body)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TestNetFactory: Sized {
|
||||
type Specialization: NetworkSpecialization<Block> + SpecializationFactory;
|
||||
type Verifier: 'static + Verifier<Block>;
|
||||
@@ -402,7 +423,7 @@ pub trait TestNetFactory: Sized {
|
||||
|
||||
/// These two need to be implemented!
|
||||
fn from_config(config: &ProtocolConfig) -> Self;
|
||||
fn make_verifier(&self, client: PeersClient, config: &ProtocolConfig) -> Arc<Self::Verifier>;
|
||||
fn make_verifier(&self, client: PeersClient, config: &ProtocolConfig) -> Self::Verifier;
|
||||
|
||||
/// Get reference to peer.
|
||||
fn peer(&mut self, i: usize) -> &mut Peer<Self::PeerData, Self::Specialization>;
|
||||
@@ -448,6 +469,7 @@ pub trait TestNetFactory: Sized {
|
||||
fn add_full_peer(&mut self, config: &ProtocolConfig) {
|
||||
let client = Arc::new(test_client::new());
|
||||
let verifier = self.make_verifier(PeersClient::Full(client.clone()), config);
|
||||
let verifier = VerifierAdapter(Arc::new(Mutex::new(Box::new(verifier) as Box<_>)));
|
||||
let (block_import, justification_import, finality_proof_import, finality_proof_request_builder, data)
|
||||
= self.make_block_import(PeersClient::Full(client.clone()));
|
||||
let block_import = BlockImportAdapter(Arc::new(Mutex::new(block_import)));
|
||||
@@ -507,6 +529,7 @@ pub trait TestNetFactory: Sized {
|
||||
|
||||
let client = Arc::new(test_client::new_light());
|
||||
let verifier = self.make_verifier(PeersClient::Light(client.clone()), &config);
|
||||
let verifier = VerifierAdapter(Arc::new(Mutex::new(Box::new(verifier) as Box<_>)));
|
||||
let (block_import, justification_import, finality_proof_import, finality_proof_request_builder, data)
|
||||
= self.make_block_import(PeersClient::Light(client.clone()));
|
||||
let block_import = BlockImportAdapter(Arc::new(Mutex::new(block_import)));
|
||||
@@ -625,9 +648,9 @@ impl TestNetFactory for TestNet {
|
||||
}
|
||||
|
||||
fn make_verifier(&self, _client: PeersClient, _config: &ProtocolConfig)
|
||||
-> Arc<Self::Verifier>
|
||||
-> Self::Verifier
|
||||
{
|
||||
Arc::new(PassThroughVerifier(false))
|
||||
PassThroughVerifier(false)
|
||||
}
|
||||
|
||||
fn peer(&mut self, i: usize) -> &mut Peer<(), Self::Specialization> {
|
||||
@@ -670,9 +693,7 @@ impl TestNetFactory for JustificationTestNet {
|
||||
JustificationTestNet(TestNet::from_config(config))
|
||||
}
|
||||
|
||||
fn make_verifier(&self, client: PeersClient, config: &ProtocolConfig)
|
||||
-> Arc<Self::Verifier>
|
||||
{
|
||||
fn make_verifier(&self, client: PeersClient, config: &ProtocolConfig) -> Self::Verifier {
|
||||
self.0.make_verifier(client, config)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user