Batch signature verification (#5023)

* create parallel tasks extension

* make type system happy

* basic externalities

* test for dynamic extensions

* batching test

* remove premature verify_batch

* shnschnorrkel batch

* alter test

* shnschnorrkel test

* executive batching

* some docs

* also multi/any signatgures

* error propagation

* styling

* make verification extension optional

* experimental ed25519 parallelization

* some merge fallout

* utilize task executor

* merge fallout

* utilize task executor more

* another merge fallout

* feature-gate sp-io

* arrange toml

* fix no-std

* sr25519 batching and refactoring

* add docs

* fix name

* add newline

* fix block import test

* long sr25519 test

* blocking instead of parking

* move everything in crypto

* return batch_verify to check :)

* use condvars

* use multi-threaded executor for benches

* don't call via host interface

* try no spawning

* add true

* cleanup

* straighten batching

* remove signature check from this test (?)

* remove now pointless test

* remove another now useless test

* fix warnings

* Revert "remove another now useless test"

This reverts commit bbdec24bb67ed4373072daef7c863e1a8825bd8b.

* rethink the sp-io-part

* Revert "remove now pointless test"

This reverts commit 4d553066322e65782264caa6053d4cd5538df977.

* fix wording

* add  wording

* add todo and fix

* return check and fix

* add logging in sp-io

* Update primitives/io/src/batch_verifier.rs

Co-Authored-By: cheme <emericchevalier.pro@gmail.com>

* address review and use std condvar

* account for early exit

* address reivew

* address review

* more suggestions

* add docs for batch verification

* remove unused

* more review suggestions

* move to sp-runtime

* add expects

* remove blocks

* use entry

* Update primitives/io/src/batch_verifier.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/externalities/src/extensions.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* update overlooked note

* remove stupid return

* Update primitives/io/src/lib.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/io/src/lib.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* fix wording

* bump spec_version

Co-authored-by: cheme <emericchevalier.pro@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Nikolay Volf
2020-04-16 22:40:04 +03:00
committed by GitHub
parent 36243068bd
commit 372f8b2c7e
21 changed files with 711 additions and 47 deletions
@@ -21,7 +21,8 @@
//!
//! It is required that each extension implements the [`Extension`] trait.
use std::{collections::HashMap, any::{Any, TypeId}, ops::DerefMut};
use std::{collections::HashMap, collections::hash_map::Entry, any::{Any, TypeId}, ops::DerefMut};
use crate::Error;
/// Marker trait for types that should be registered as [`Externalities`](crate::Externalities) extension.
///
@@ -87,6 +88,16 @@ pub trait ExtensionStore {
/// It is advised to use [`ExternalitiesExt::extension`](crate::ExternalitiesExt::extension)
/// instead of this function to get type system support and automatic type downcasting.
fn extension_by_type_id(&mut self, type_id: TypeId) -> Option<&mut dyn Any>;
/// Register extension `extension` with speciifed `type_id`.
///
/// It should return error if extension is already registered.
fn register_extension_with_type_id(&mut self, type_id: TypeId, extension: Box<dyn Extension>) -> Result<(), Error>;
/// Deregister extension with speicifed 'type_id' and drop it.
///
/// It should return error if extension is not registered.
fn deregister_extension_by_type_id(&mut self, type_id: TypeId) -> Result<(), Error>;
}
/// Stores extensions that should be made available through the externalities.
@@ -95,6 +106,12 @@ pub struct Extensions {
extensions: HashMap<TypeId, Box<dyn Extension>>,
}
impl std::fmt::Debug for Extensions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Extensions: ({})", self.extensions.len())
}
}
impl Extensions {
/// Create new instance of `Self`.
pub fn new() -> Self {
@@ -106,10 +123,23 @@ impl Extensions {
self.extensions.insert(ext.type_id(), Box::new(ext));
}
/// Register extension `ext`.
pub fn register_with_type_id(&mut self, type_id: TypeId, extension: Box<dyn Extension>) -> Result<(), Error> {
match self.extensions.entry(type_id) {
Entry::Vacant(vacant) => { vacant.insert(extension); Ok(()) },
Entry::Occupied(_) => Err(Error::ExtensionAlreadyRegistered),
}
}
/// Return a mutable reference to the requested extension.
pub fn get_mut(&mut self, ext_type_id: TypeId) -> Option<&mut dyn Any> {
self.extensions.get_mut(&ext_type_id).map(DerefMut::deref_mut).map(Extension::as_mut_any)
}
/// Deregister extension of type `E`.
pub fn deregister(&mut self, type_id: TypeId) -> Option<Box<dyn Extension>> {
self.extensions.remove(&type_id)
}
}
#[cfg(test)]
@@ -32,6 +32,17 @@ pub use extensions::{Extension, Extensions, ExtensionStore};
mod extensions;
mod scope_limited;
/// Externalities error.
#[derive(Debug)]
pub enum Error {
/// Same extension cannot be registered twice.
ExtensionAlreadyRegistered,
/// Extensions are not supported.
ExtensionsAreNotSupported,
/// Extension `TypeId` is not registered.
ExtensionIsNotRegistered(TypeId),
}
/// The Substrate externalities.
///
/// Provides access to the storage and to other registered extensions.
@@ -198,10 +209,29 @@ pub trait Externalities: ExtensionStore {
pub trait ExternalitiesExt {
/// Tries to find a registered extension and returns a mutable reference.
fn extension<T: Any + Extension>(&mut self) -> Option<&mut T>;
/// Register extension `ext`.
///
/// Should return error if extension is already registered or extensions are not supported.
fn register_extension<T: Extension>(&mut self, ext: T) -> Result<(), Error>;
/// Deregister and drop extension of `T` type.
///
/// Should return error if extension of type `T` is not registered or
/// extensions are not supported.
fn deregister_extension<T: Extension>(&mut self) -> Result<(), Error>;
}
impl ExternalitiesExt for &mut dyn Externalities {
fn extension<T: Any + Extension>(&mut self) -> Option<&mut T> {
self.extension_by_type_id(TypeId::of::<T>()).and_then(Any::downcast_mut)
}
fn register_extension<T: Extension>(&mut self, ext: T) -> Result<(), Error> {
self.register_extension_with_type_id(TypeId::of::<T>(), Box::new(ext))
}
fn deregister_extension<T: Extension>(&mut self) -> Result<(), Error> {
self.deregister_extension_by_type_id(TypeId::of::<T>())
}
}