mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 06:27:58 +00:00
Migrate remaining old decl_* macros to the new pallet attribute macros (#12271)
* Migrate remaining old decl_* macros to the new pallet attribute macros Signed-off-by: koushiro <koushiro.cqx@gmail.com> * Apply review suggestions Signed-off-by: koushiro <koushiro.cqx@gmail.com> * Apply review suggestions Signed-off-by: koushiro <koushiro.cqx@gmail.com> * use pallet::storage * Fix dev rpc test Signed-off-by: koushiro <koushiro.cqx@gmail.com> * Fix service tests Signed-off-by: koushiro <koushiro.cqx@gmail.com> Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
@@ -17,8 +17,9 @@
|
||||
|
||||
//! Tool for creating the genesis block.
|
||||
|
||||
use super::{system, wasm_binary_unwrap, AccountId, AuthorityId};
|
||||
use super::{system, wasm_binary_unwrap, AccountId, AuthorityId, Runtime};
|
||||
use codec::{Encode, Joiner, KeyedVec};
|
||||
use frame_support::traits::GenesisBuild;
|
||||
use sc_service::client::genesis;
|
||||
use sp_core::{
|
||||
map,
|
||||
@@ -80,10 +81,11 @@ impl GenesisConfig {
|
||||
// Assimilate the system genesis config.
|
||||
let mut storage =
|
||||
Storage { top: map, children_default: self.extra_storage.children_default.clone() };
|
||||
let config = system::GenesisConfig { authorities: self.authorities.clone() };
|
||||
config
|
||||
.assimilate_storage(&mut storage)
|
||||
.expect("Adding `system::GensisConfig` to the genesis");
|
||||
<system::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
|
||||
&system::GenesisConfig { authorities: self.authorities.clone() },
|
||||
&mut storage,
|
||||
)
|
||||
.expect("Adding `system::GensisConfig` to the genesis");
|
||||
|
||||
storage
|
||||
}
|
||||
|
||||
@@ -610,6 +610,8 @@ impl frame_system::Config for Runtime {
|
||||
type MaxConsumers = ConstU32<16>;
|
||||
}
|
||||
|
||||
impl system::Config for Runtime {}
|
||||
|
||||
impl pallet_timestamp::Config for Runtime {
|
||||
/// A timestamp: milliseconds since the unix epoch.
|
||||
type Moment = u64;
|
||||
|
||||
@@ -19,11 +19,11 @@
|
||||
//! and depositing logs.
|
||||
|
||||
use crate::{
|
||||
AccountId, AuthorityId, Block, BlockNumber, Digest, Extrinsic, Header, Transfer, H256 as Hash,
|
||||
AccountId, AuthorityId, Block, BlockNumber, Digest, Extrinsic, Header, Runtime, Transfer,
|
||||
H256 as Hash,
|
||||
};
|
||||
use codec::{Decode, Encode, KeyedVec};
|
||||
use frame_support::{decl_module, decl_storage, storage};
|
||||
use frame_system::Config;
|
||||
use frame_support::storage;
|
||||
use sp_core::storage::well_known_keys;
|
||||
use sp_io::{hashing::blake2_256, storage::root as storage_root, trie};
|
||||
use sp_runtime::{
|
||||
@@ -39,19 +39,51 @@ use sp_std::prelude::*;
|
||||
const NONCE_OF: &[u8] = b"nonce:";
|
||||
const BALANCE_OF: &[u8] = b"balance:";
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {}
|
||||
}
|
||||
pub use self::pallet::*;
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Config> as TestRuntime {
|
||||
ExtrinsicData: map hasher(blake2_128_concat) u32 => Vec<u8>;
|
||||
// The current block number being processed. Set by `execute_block`.
|
||||
Number get(fn number): Option<BlockNumber>;
|
||||
ParentHash get(fn parent_hash): Hash;
|
||||
NewAuthorities get(fn new_authorities): Option<Vec<AuthorityId>>;
|
||||
StorageDigest get(fn storage_digest): Option<Digest>;
|
||||
Authorities get(fn authorities) config(): Vec<AuthorityId>;
|
||||
#[frame_support::pallet]
|
||||
mod pallet {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
#[pallet::generate_store(pub(super) trait Store)]
|
||||
#[pallet::without_storage_info]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::storage]
|
||||
pub type ExtrinsicData<T> = StorageMap<_, Blake2_128Concat, u32, Vec<u8>, ValueQuery>;
|
||||
|
||||
// The current block number being processed. Set by `execute_block`.
|
||||
#[pallet::storage]
|
||||
pub type Number<T> = StorageValue<_, BlockNumber, OptionQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type ParentHash<T> = StorageValue<_, Hash, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type NewAuthorities<T> = StorageValue<_, Vec<AuthorityId>, OptionQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type StorageDigest<T> = StorageValue<_, Digest, OptionQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Authorities<T> = StorageValue<_, Vec<AuthorityId>, ValueQuery>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
#[cfg_attr(feature = "std", derive(Default))]
|
||||
pub struct GenesisConfig {
|
||||
pub authorities: Vec<AuthorityId>,
|
||||
}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig {
|
||||
fn build(&self) {
|
||||
<Authorities<T>>::put(self.authorities.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,9 +101,9 @@ pub fn nonce_of(who: AccountId) -> u64 {
|
||||
|
||||
pub fn initialize_block(header: &Header) {
|
||||
// populate environment.
|
||||
<Number>::put(&header.number);
|
||||
<ParentHash>::put(&header.parent_hash);
|
||||
<StorageDigest>::put(header.digest());
|
||||
<Number<Runtime>>::put(&header.number);
|
||||
<ParentHash<Runtime>>::put(&header.parent_hash);
|
||||
<StorageDigest<Runtime>>::put(header.digest());
|
||||
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
|
||||
|
||||
// try to read something that depends on current header digest
|
||||
@@ -82,15 +114,15 @@ pub fn initialize_block(header: &Header) {
|
||||
}
|
||||
|
||||
pub fn authorities() -> Vec<AuthorityId> {
|
||||
Authorities::get()
|
||||
<Authorities<Runtime>>::get()
|
||||
}
|
||||
|
||||
pub fn get_block_number() -> Option<BlockNumber> {
|
||||
Number::get()
|
||||
<Number<Runtime>>::get()
|
||||
}
|
||||
|
||||
pub fn take_block_number() -> Option<BlockNumber> {
|
||||
Number::take()
|
||||
<Number<Runtime>>::take()
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
@@ -124,8 +156,8 @@ fn execute_block_with_state_root_handler(block: &mut Block, mode: Mode) -> Heade
|
||||
header.state_root = new_header.state_root;
|
||||
} else {
|
||||
info_expect_equal_hash(&new_header.state_root, &header.state_root);
|
||||
assert!(
|
||||
new_header.state_root == header.state_root,
|
||||
assert_eq!(
|
||||
new_header.state_root, header.state_root,
|
||||
"Storage root must match that calculated.",
|
||||
);
|
||||
}
|
||||
@@ -134,8 +166,8 @@ fn execute_block_with_state_root_handler(block: &mut Block, mode: Mode) -> Heade
|
||||
header.extrinsics_root = new_header.extrinsics_root;
|
||||
} else {
|
||||
info_expect_equal_hash(&new_header.extrinsics_root, &header.extrinsics_root);
|
||||
assert!(
|
||||
new_header.extrinsics_root == header.extrinsics_root,
|
||||
assert_eq!(
|
||||
new_header.extrinsics_root, header.extrinsics_root,
|
||||
"Transaction trie root must be valid.",
|
||||
);
|
||||
}
|
||||
@@ -187,7 +219,7 @@ pub fn execute_transaction(utx: Extrinsic) -> ApplyExtrinsicResult {
|
||||
let extrinsic_index: u32 =
|
||||
storage::unhashed::get(well_known_keys::EXTRINSIC_INDEX).unwrap_or_default();
|
||||
let result = execute_transaction_backend(&utx, extrinsic_index);
|
||||
ExtrinsicData::insert(extrinsic_index, utx.encode());
|
||||
<ExtrinsicData<Runtime>>::insert(extrinsic_index, utx.encode());
|
||||
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &(extrinsic_index + 1));
|
||||
result
|
||||
}
|
||||
@@ -196,13 +228,14 @@ pub fn execute_transaction(utx: Extrinsic) -> ApplyExtrinsicResult {
|
||||
pub fn finalize_block() -> Header {
|
||||
use sp_core::storage::StateVersion;
|
||||
let extrinsic_index: u32 = storage::unhashed::take(well_known_keys::EXTRINSIC_INDEX).unwrap();
|
||||
let txs: Vec<_> = (0..extrinsic_index).map(ExtrinsicData::take).collect();
|
||||
let txs: Vec<_> = (0..extrinsic_index).map(<ExtrinsicData<Runtime>>::take).collect();
|
||||
let extrinsics_root = trie::blake2_256_ordered_root(txs, StateVersion::V0);
|
||||
let number = <Number>::take().expect("Number is set by `initialize_block`");
|
||||
let parent_hash = <ParentHash>::take();
|
||||
let mut digest = <StorageDigest>::take().expect("StorageDigest is set by `initialize_block`");
|
||||
let number = <Number<Runtime>>::take().expect("Number is set by `initialize_block`");
|
||||
let parent_hash = <ParentHash<Runtime>>::take();
|
||||
let mut digest =
|
||||
<StorageDigest<Runtime>>::take().expect("StorageDigest is set by `initialize_block`");
|
||||
|
||||
let o_new_authorities = <NewAuthorities>::take();
|
||||
let o_new_authorities = <NewAuthorities<Runtime>>::take();
|
||||
|
||||
// This MUST come after all changes to storage are done. Otherwise we will fail the
|
||||
// “Storage root does not match that calculated” assertion.
|
||||
@@ -280,7 +313,7 @@ fn execute_store(data: Vec<u8>) -> ApplyExtrinsicResult {
|
||||
}
|
||||
|
||||
fn execute_new_authorities_backend(new_authorities: &[AuthorityId]) -> ApplyExtrinsicResult {
|
||||
NewAuthorities::put(new_authorities.to_vec());
|
||||
<NewAuthorities<Runtime>>::put(new_authorities.to_vec());
|
||||
Ok(Ok(()))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user