Make AuthorityId generic (#1296)

* BlockAuthorityId convenience type

* Rename AuthorityId -> Ed25519AuthorityId to make it more precise

* Generalize AuthorityId up to substrate-client

* Fix in client-db

* rename: BlockAuthorityId -> AuthorityIdFor

* typo: should be digest item

* Fix test-runtime authorityId mismatch

One states that AuthorityId is u64 while the other states that it's Ed25519AuthorityId.

* Fix more u64 - Ed25519AuthorityId mismatch

* Fix compile of most of the srml modules

* Continue to pin aura and grandpa with ed25519 and fix compile

* Add MaybeHash trait

* Fix node-runtime compile

* Fix network tests
This commit is contained in:
Wei Tang
2019-01-08 11:14:18 +01:00
committed by Benjamin Kampmann
parent 043831cfb0
commit 71d889b692
46 changed files with 234 additions and 216 deletions
+6 -7
View File
@@ -24,9 +24,8 @@ use kvdb::{KeyValueDB, DBTransaction};
use client::blockchain::Cache as BlockchainCache;
use client::error::Result as ClientResult;
use codec::{Encode, Decode};
use primitives::AuthorityId;
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor, As};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor, As, AuthorityIdFor};
use utils::{self, COLUMN_META};
use self::list_cache::ListCache;
@@ -65,7 +64,7 @@ impl<T> CacheItemT for T where T: Clone + Decode + Encode + PartialEq {}
/// Database-backed blockchain data cache.
pub struct DbCache<Block: BlockT> {
authorities_at: ListCache<Block, Vec<AuthorityId>, self::list_storage::DbStorage>,
authorities_at: ListCache<Block, Vec<AuthorityIdFor<Block>>, self::list_storage::DbStorage>,
}
impl<Block: BlockT> DbCache<Block> {
@@ -112,14 +111,14 @@ impl<Block: BlockT> DbCache<Block> {
/// Cache operations that are to be committed after database transaction is committed.
pub struct DbCacheTransactionOps<Block: BlockT> {
authorities_at_op: Option<self::list_cache::CommitOperation<Block, Vec<AuthorityId>>>,
authorities_at_op: Option<self::list_cache::CommitOperation<Block, Vec<AuthorityIdFor<Block>>>>,
}
/// Database-backed blockchain data cache transaction valid for single block import.
pub struct DbCacheTransaction<'a, Block: BlockT> {
cache: &'a mut DbCache<Block>,
tx: &'a mut DBTransaction,
authorities_at_op: Option<self::list_cache::CommitOperation<Block, Vec<AuthorityId>>>,
authorities_at_op: Option<self::list_cache::CommitOperation<Block, Vec<AuthorityIdFor<Block>>>>,
}
impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> {
@@ -135,7 +134,7 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> {
mut self,
parent: ComplexBlockId<Block>,
block: ComplexBlockId<Block>,
authorities_at: Option<Vec<AuthorityId>>,
authorities_at: Option<Vec<AuthorityIdFor<Block>>>,
is_final: bool,
) -> ClientResult<Self> {
assert!(self.authorities_at_op.is_none());
@@ -179,7 +178,7 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> {
pub struct DbCacheSync<Block: BlockT>(pub RwLock<DbCache<Block>>);
impl<Block: BlockT> BlockchainCache<Block> for DbCacheSync<Block> {
fn authorities_at(&self, at: BlockId<Block>) -> Option<Vec<AuthorityId>> {
fn authorities_at(&self, at: BlockId<Block>) -> Option<Vec<AuthorityIdFor<Block>>> {
let cache = self.0.read();
let storage = cache.authorities_at.storage();
let db = storage.db();
+3 -3
View File
@@ -64,10 +64,10 @@ use hash_db::Hasher;
use kvdb::{KeyValueDB, DBTransaction};
use trie::MemoryDB;
use parking_lot::RwLock;
use primitives::{H256, AuthorityId, Blake2Hasher, ChangesTrieConfiguration, convert_hash};
use primitives::{H256, Blake2Hasher, ChangesTrieConfiguration, convert_hash};
use primitives::storage::well_known_keys;
use runtime_primitives::{generic::BlockId, Justification, StorageMap, ChildrenStorageMap};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, As, NumberFor, Zero, Digest, DigestItem};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, As, NumberFor, Zero, Digest, DigestItem, AuthorityIdFor};
use runtime_primitives::BuildStorage;
use state_machine::backend::Backend as StateBackend;
use executor::RuntimeInfo;
@@ -315,7 +315,7 @@ where Block: BlockT<Hash=H256>,
Ok(())
}
fn update_authorities(&mut self, _authorities: Vec<AuthorityId>) {
fn update_authorities(&mut self, _authorities: Vec<AuthorityIdFor<Block>>) {
// currently authorities are not cached on full nodes
}
+7 -7
View File
@@ -28,10 +28,10 @@ use client::{cht, LeafSet};
use client::error::{ErrorKind as ClientErrorKind, Result as ClientResult};
use client::light::blockchain::Storage as LightBlockchainStorage;
use codec::{Decode, Encode};
use primitives::{AuthorityId, Blake2Hasher};
use primitives::Blake2Hasher;
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT,
Zero, One, As, NumberFor, Digest, DigestItem};
Zero, One, As, NumberFor, Digest, DigestItem, AuthorityIdFor};
use cache::{DbCacheSync, DbCache, ComplexBlockId};
use utils::{meta_keys, Meta, db_err, open_database,
read_db, block_id_to_lookup_key, read_meta};
@@ -306,7 +306,7 @@ impl<Block> LightBlockchainStorage<Block> for LightStorage<Block>
fn import_header(
&self,
header: Block::Header,
authorities: Option<Vec<AuthorityId>>,
authorities: Option<Vec<AuthorityIdFor<Block>>>,
leaf_state: NewBlockState,
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
) -> ClientResult<()> {
@@ -510,7 +510,7 @@ pub(crate) mod tests {
pub fn insert_block<F: Fn() -> Header>(
db: &LightStorage<Block>,
authorities: Option<Vec<AuthorityId>>,
authorities: Option<Vec<AuthorityIdFor<Block>>>,
header: F,
) -> Hash {
let header = header();
@@ -521,7 +521,7 @@ pub(crate) mod tests {
fn insert_final_block<F: Fn() -> Header>(
db: &LightStorage<Block>,
authorities: Option<Vec<AuthorityId>>,
authorities: Option<Vec<AuthorityIdFor<Block>>>,
header: F,
) -> Hash {
let header = header();
@@ -532,7 +532,7 @@ pub(crate) mod tests {
fn insert_non_best_block<F: Fn() -> Header>(
db: &LightStorage<Block>,
authorities: Option<Vec<AuthorityId>>,
authorities: Option<Vec<AuthorityIdFor<Block>>>,
header: F,
) -> Hash {
let header = header();
@@ -762,7 +762,7 @@ pub(crate) mod tests {
fn authorites_are_cached() {
let db = LightStorage::new_test();
fn run_checks(db: &LightStorage<Block>, max: u64, checks: &[(u64, Option<Vec<AuthorityId>>)]) {
fn run_checks(db: &LightStorage<Block>, max: u64, checks: &[(u64, Option<Vec<AuthorityIdFor<Block>>>)]) {
for (at, expected) in checks.iter().take_while(|(at, _)| *at <= max) {
let actual = db.cache().authorities_at(BlockId::Number(*at));
assert_eq!(*expected, actual);
+2 -3
View File
@@ -17,9 +17,8 @@
//! Substrate Client data backend
use error;
use primitives::AuthorityId;
use runtime_primitives::{generic::BlockId, Justification, StorageMap, ChildrenStorageMap};
use runtime_primitives::traits::{Block as BlockT, NumberFor};
use runtime_primitives::traits::{AuthorityIdFor, Block as BlockT, NumberFor};
use state_machine::backend::Backend as StateBackend;
use state_machine::ChangesTrieStorage as StateChangesTrieStorage;
use hash_db::Hasher;
@@ -67,7 +66,7 @@ pub trait BlockImportOperation<Block, H> where
/// Append authorities set to the transaction. This is a set of parent block (set which
/// has been used to check justification of this block).
fn update_authorities(&mut self, authorities: Vec<AuthorityId>);
fn update_authorities(&mut self, authorities: Vec<AuthorityIdFor<Block>>);
/// Inject storage data into the database.
fn update_storage(&mut self, update: <Self::State as StateBackend<H>>::Transaction) -> error::Result<()>;
/// Inject storage data into the database replacing any existing data.
+2 -3
View File
@@ -16,8 +16,7 @@
//! Substrate blockchain trait
use primitives::AuthorityId;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor};
use runtime_primitives::traits::{AuthorityIdFor, Block as BlockT, Header as HeaderT, NumberFor};
use runtime_primitives::generic::BlockId;
use runtime_primitives::Justification;
@@ -90,7 +89,7 @@ pub trait Backend<Block: BlockT>: HeaderBackend<Block> {
/// Blockchain optional data cache.
pub trait Cache<Block: BlockT>: Send + Sync {
/// Returns the set of authorities, that was active at given block or None if there's no entry in the cache.
fn authorities_at(&self, block: BlockId<Block>) -> Option<Vec<AuthorityId>>;
fn authorities_at(&self, block: BlockId<Block>) -> Option<Vec<AuthorityIdFor<Block>>>;
}
/// Block import outcome
+6 -7
View File
@@ -20,7 +20,6 @@ use std::{marker::PhantomData, collections::{HashSet, BTreeMap}, sync::Arc};
use error::Error;
use futures::sync::mpsc;
use parking_lot::{Mutex, RwLock};
use primitives::AuthorityId;
use runtime_primitives::{
Justification,
generic::{BlockId, SignedBlock},
@@ -28,7 +27,7 @@ use runtime_primitives::{
use consensus::{Error as ConsensusError, ErrorKind as ConsensusErrorKind, ImportBlock, ImportResult, BlockOrigin, ForkChoiceStrategy};
use runtime_primitives::traits::{
Block as BlockT, Header as HeaderT, Zero, As, NumberFor, CurrentHeight, BlockNumberToHash,
ApiRef, ProvideRuntimeApi, Digest, DigestItem,
ApiRef, ProvideRuntimeApi, Digest, DigestItem, AuthorityIdFor
};
use runtime_primitives::BuildStorage;
use runtime_api::{Core as CoreAPI, CallRuntimeAt, ConstructRuntimeApi};
@@ -281,11 +280,11 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
}
/// Get the set of authorities at a given block.
pub fn authorities_at(&self, id: &BlockId<Block>) -> error::Result<Vec<AuthorityId>> {
pub fn authorities_at(&self, id: &BlockId<Block>) -> error::Result<Vec<AuthorityIdFor<Block>>> {
match self.backend.blockchain().cache().and_then(|cache| cache.authorities_at(*id)) {
Some(cached_value) => Ok(cached_value),
None => self.executor.call(id, "Core_authorities", &[])
.and_then(|r| Vec::<AuthorityId>::decode(&mut &r.return_data[..])
.and_then(|r| Vec::<AuthorityIdFor<Block>>::decode(&mut &r.return_data[..])
.ok_or(error::ErrorKind::InvalidAuthoritiesSet.into()))
}
}
@@ -541,7 +540,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
import_headers: PrePostHeader<Block::Header>,
justification: Option<Justification>,
body: Option<Vec<Block::Extrinsic>>,
authorities: Option<Vec<AuthorityId>>,
authorities: Option<Vec<AuthorityIdFor<Block>>>,
finalized: bool,
aux: Vec<(Vec<u8>, Option<Vec<u8>>)>,
fork_choice: ForkChoiceStrategy,
@@ -1030,7 +1029,7 @@ impl<B, E, Block, RA> consensus::BlockImport<Block> for Client<B, E, Block, RA>
fn import_block(
&self,
import_block: ImportBlock<Block>,
new_authorities: Option<Vec<AuthorityId>>,
new_authorities: Option<Vec<AuthorityIdFor<Block>>>,
) -> Result<ImportResult, Self::Error> {
use runtime_primitives::traits::Digest;
@@ -1098,7 +1097,7 @@ impl<B, E, Block, RA> consensus::Authorities<Block> for Client<B, E, Block, RA>
Block: BlockT<Hash=H256>,
{
type Error = Error;
fn authorities(&self, at: &BlockId<Block>) -> Result<Vec<AuthorityId>, Self::Error> {
fn authorities(&self, at: &BlockId<Block>) -> Result<Vec<AuthorityIdFor<Block>>, Self::Error> {
self.authorities_at(at).map_err(|e| e.into())
}
}
+9 -9
View File
@@ -22,10 +22,10 @@ use parking_lot::RwLock;
use error;
use backend::{self, NewBlockState};
use light;
use primitives::{AuthorityId, storage::well_known_keys};
use primitives::storage::well_known_keys;
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, Zero,
NumberFor, As, Digest, DigestItem};
NumberFor, As, Digest, DigestItem, AuthorityIdFor};
use runtime_primitives::{Justification, StorageMap, ChildrenStorageMap};
use blockchain::{self, BlockStatus, HeaderBackend};
use state_machine::backend::{Backend as StateBackend, InMemory, Consolidate};
@@ -108,7 +108,7 @@ pub struct Blockchain<Block: BlockT> {
struct Cache<Block: BlockT> {
storage: Arc<RwLock<BlockchainStorage<Block>>>,
authorities_at: RwLock<HashMap<Block::Hash, Option<Vec<AuthorityId>>>>,
authorities_at: RwLock<HashMap<Block::Hash, Option<Vec<AuthorityIdFor<Block>>>>>,
}
impl<Block: BlockT + Clone> Clone for Blockchain<Block> {
@@ -368,7 +368,7 @@ impl<Block: BlockT> light::blockchain::Storage<Block> for Blockchain<Block>
fn import_header(
&self,
header: Block::Header,
authorities: Option<Vec<AuthorityId>>,
authorities: Option<Vec<AuthorityIdFor<Block>>>,
state: NewBlockState,
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
) -> error::Result<()> {
@@ -409,7 +409,7 @@ impl<Block: BlockT> light::blockchain::Storage<Block> for Blockchain<Block>
/// In-memory operation.
pub struct BlockImportOperation<Block: BlockT, H: Hasher> {
pending_block: Option<PendingBlock<Block>>,
pending_authorities: Option<Vec<AuthorityId>>,
pending_authorities: Option<Vec<AuthorityIdFor<Block>>>,
old_state: InMemory<H>,
new_state: Option<InMemory<H>>,
changes_trie_update: Option<MemoryDB<H>>,
@@ -444,7 +444,7 @@ where
Ok(())
}
fn update_authorities(&mut self, authorities: Vec<AuthorityId>) {
fn update_authorities(&mut self, authorities: Vec<AuthorityIdFor<Block>>) {
self.pending_authorities = Some(authorities);
}
@@ -632,13 +632,13 @@ where
{}
impl<Block: BlockT> Cache<Block> {
fn insert(&self, at: Block::Hash, authorities: Option<Vec<AuthorityId>>) {
fn insert(&self, at: Block::Hash, authorities: Option<Vec<AuthorityIdFor<Block>>>) {
self.authorities_at.write().insert(at, authorities);
}
}
impl<Block: BlockT> blockchain::Cache<Block> for Cache<Block> {
fn authorities_at(&self, block: BlockId<Block>) -> Option<Vec<AuthorityId>> {
fn authorities_at(&self, block: BlockId<Block>) -> Option<Vec<AuthorityIdFor<Block>>> {
let hash = match block {
BlockId::Hash(hash) => hash,
BlockId::Number(number) => self.storage.read().hashes.get(&number).cloned()?,
@@ -652,7 +652,7 @@ impl<Block: BlockT> blockchain::Cache<Block> for Cache<Block> {
pub fn cache_authorities_at<Block: BlockT>(
blockchain: &Blockchain<Block>,
at: Block::Hash,
authorities: Option<Vec<AuthorityId>>
authorities: Option<Vec<AuthorityIdFor<Block>>>
) {
blockchain.cache.insert(at, authorities);
}
+3 -4
View File
@@ -21,10 +21,9 @@ use std::sync::{Arc, Weak};
use futures::{Future, IntoFuture};
use parking_lot::RwLock;
use primitives::AuthorityId;
use runtime_primitives::{generic::BlockId, Justification, StorageMap, ChildrenStorageMap};
use state_machine::{Backend as StateBackend, InMemoryChangesTrieStorage, TrieBackend};
use runtime_primitives::traits::{Block as BlockT, NumberFor};
use runtime_primitives::traits::{Block as BlockT, NumberFor, AuthorityIdFor};
use in_mem;
use backend::{AuxStore, Backend as ClientBackend, BlockImportOperation, RemoteBackend, NewBlockState};
@@ -44,7 +43,7 @@ pub struct Backend<S, F> {
/// Light block (header and justification) import operation.
pub struct ImportOperation<Block: BlockT, S, F> {
header: Option<Block::Header>,
authorities: Option<Vec<AuthorityId>>,
authorities: Option<Vec<AuthorityIdFor<Block>>>,
leaf_state: NewBlockState,
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
_phantom: ::std::marker::PhantomData<(S, F)>,
@@ -185,7 +184,7 @@ where
Ok(())
}
fn update_authorities(&mut self, authorities: Vec<AuthorityId>) {
fn update_authorities(&mut self, authorities: Vec<AuthorityIdFor<Block>>) {
self.authorities = Some(authorities);
}
@@ -21,9 +21,8 @@ use std::sync::Weak;
use futures::{Future, IntoFuture};
use parking_lot::Mutex;
use primitives::AuthorityId;
use runtime_primitives::{Justification, generic::BlockId};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT,NumberFor, Zero};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero, AuthorityIdFor};
use backend::{AuxStore, NewBlockState};
use blockchain::{Backend as BlockchainBackend, BlockStatus, Cache as BlockchainCache,
@@ -41,7 +40,7 @@ pub trait Storage<Block: BlockT>: AuxStore + BlockchainHeaderBackend<Block> {
fn import_header(
&self,
header: Block::Header,
authorities: Option<Vec<AuthorityId>>,
authorities: Option<Vec<AuthorityIdFor<Block>>>,
state: NewBlockState,
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
) -> ClientResult<()>;
@@ -227,7 +226,7 @@ pub mod tests {
fn import_header(
&self,
_header: Header,
_authorities: Option<Vec<AuthorityId>>,
_authorities: Option<Vec<AuthorityIdFor<Block>>>,
_state: NewBlockState,
_aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
) -> ClientResult<()> {
+3 -3
View File
@@ -21,7 +21,7 @@
pub use state_machine::OverlayedChanges;
#[doc(hidden)]
pub use runtime_primitives::{
traits::{Block as BlockT, GetNodeBlockType, GetRuntimeBlockType, ApiRef, RuntimeApiInfo},
traits::{AuthorityIdFor, Block as BlockT, GetNodeBlockType, GetRuntimeBlockType, ApiRef, RuntimeApiInfo},
generic::BlockId, transaction_validity::TransactionValidity
};
#[doc(hidden)]
@@ -34,7 +34,7 @@ pub use codec::{Encode, Decode};
#[cfg(feature = "std")]
use error;
use rstd::vec::Vec;
use primitives::{AuthorityId, OpaqueMetadata};
use primitives::OpaqueMetadata;
/// Something that can be constructed to a runtime api.
@@ -91,7 +91,7 @@ decl_runtime_apis! {
/// Returns the version of the runtime.
fn version() -> RuntimeVersion;
/// Returns the authorities.
fn authorities() -> Vec<AuthorityId>;
fn authorities() -> Vec<AuthorityIdFor<Block>>;
/// Execute the given block.
fn execute_block(block: Block);
/// Initialise a block with the given header.