mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 02:51:01 +00:00
Runtime benchmarks: start (#136)
* runtime benchmarks: start * merge tests + benchmarks infrastructure * fix compilation * Fix compilation issues with runtime-benchmark feature flag Mainly involved pulling in correct dependencies and adding some functions which were called but didn't yet exist. * Fix broken compilation for tests * Move header signing methods into trait * Move signing related test helpers to own module * Remove comment about feature flag * Add constants to tests * Add top level comment for testing utilities Co-authored-by: Hernando Castano <castano.ha@gmail.com>
This commit is contained in:
committed by
Bastian Köcher
parent
ea45fa8da7
commit
e39ca0dc16
@@ -18,8 +18,6 @@
|
||||
|
||||
pub use parity_bytes::Bytes;
|
||||
pub use primitive_types::{H160, H256, H512, U128, U256};
|
||||
|
||||
#[cfg(feature = "test-helpers")]
|
||||
pub use rlp::encode as rlp_encode;
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
@@ -49,6 +47,9 @@ pub type RawTransaction = Vec<u8>;
|
||||
/// An ethereum address.
|
||||
pub type Address = H160;
|
||||
|
||||
#[cfg(any(feature = "test-helpers", test))]
|
||||
pub mod signatures;
|
||||
|
||||
/// Complete header id.
|
||||
#[derive(Encode, Decode, Default, RuntimeDebug, PartialEq, Clone, Copy)]
|
||||
pub struct HeaderId {
|
||||
@@ -59,8 +60,8 @@ pub struct HeaderId {
|
||||
}
|
||||
|
||||
/// An Aura header.
|
||||
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug)]
|
||||
#[cfg_attr(feature = "std", derive(Default, Serialize, Deserialize))]
|
||||
#[derive(Clone, Default, Encode, Decode, PartialEq, RuntimeDebug)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||
pub struct Header {
|
||||
/// Parent block hash.
|
||||
pub parent_hash: H256,
|
||||
@@ -457,6 +458,11 @@ pub fn compute_merkle_root<T: AsRef<[u8]>>(items: impl Iterator<Item = T>) -> H2
|
||||
triehash::ordered_trie_root::<Keccak256Hasher, _>(items)
|
||||
}
|
||||
|
||||
/// Get validator that should author the block at given step.
|
||||
pub fn step_validator<T>(header_validators: &[T], header_step: u64) -> &T {
|
||||
&header_validators[(header_step % header_validators.len() as u64) as usize]
|
||||
}
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
/// API for headers submitters.
|
||||
pub trait EthereumHeadersApi {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common 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.
|
||||
|
||||
// Parity Bridges Common 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 Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
//! Helpers related to signatures.
|
||||
//!
|
||||
//! Used for testing and benchmarking.
|
||||
|
||||
use crate::{public_to_address, rlp_encode, step_validator, Address, Header, H256, H520};
|
||||
|
||||
use secp256k1::{Message, PublicKey, SecretKey};
|
||||
|
||||
/// Utilities for signing headers.
|
||||
pub trait SignHeader {
|
||||
/// Signs header by given author.
|
||||
fn sign_by(self, author: &SecretKey) -> Header;
|
||||
/// Signs header by given authors set.
|
||||
fn sign_by_set(self, authors: &[SecretKey]) -> Header;
|
||||
}
|
||||
|
||||
impl SignHeader for Header {
|
||||
fn sign_by(mut self, author: &SecretKey) -> Self {
|
||||
self.author = secret_to_address(author);
|
||||
|
||||
let message = self.seal_hash(false).unwrap();
|
||||
let signature = sign(author, message);
|
||||
self.seal[1] = rlp_encode(&signature);
|
||||
self
|
||||
}
|
||||
|
||||
fn sign_by_set(self, authors: &[SecretKey]) -> Self {
|
||||
let step = self.step().unwrap();
|
||||
let author = step_validator(authors, step);
|
||||
self.sign_by(author)
|
||||
}
|
||||
}
|
||||
|
||||
/// Return author's signature over given message.
|
||||
pub fn sign(author: &SecretKey, message: H256) -> H520 {
|
||||
let (signature, recovery_id) = secp256k1::sign(&Message::parse(message.as_fixed_bytes()), author);
|
||||
let mut raw_signature = [0u8; 65];
|
||||
raw_signature[..64].copy_from_slice(&signature.serialize());
|
||||
raw_signature[64] = recovery_id.serialize();
|
||||
raw_signature.into()
|
||||
}
|
||||
|
||||
/// Returns address corresponding to given secret key.
|
||||
pub fn secret_to_address(secret: &SecretKey) -> Address {
|
||||
let public = PublicKey::from_secret_key(secret);
|
||||
let mut raw_public = [0u8; 64];
|
||||
raw_public.copy_from_slice(&public.serialize()[1..]);
|
||||
public_to_address(&raw_public)
|
||||
}
|
||||
Reference in New Issue
Block a user