mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 22:11:06 +00:00
Rococo (mocky)Runtime and Client (#841)
* Implement fake rococo runtime. * Generate correct storage key. * cargo fmt --all * Move client-rococo. * Satisfy clippy. * Apply suggestions from code review Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com> * Address review comments. * Clippy again. Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
This commit is contained in:
committed by
Bastian Köcher
parent
60c1c24784
commit
51fc83941b
@@ -7,6 +7,7 @@ edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
parity-scale-codec = { version = "2.0.0", default-features = false, features = ["derive"] }
|
||||
|
||||
# Bridge Dependencies
|
||||
bp-messages = { path = "../messages", default-features = false }
|
||||
@@ -14,8 +15,14 @@ bp-polkadot-core = { path = "../polkadot-core", default-features = false }
|
||||
bp-runtime = { path = "../runtime", default-features = false }
|
||||
|
||||
# Substrate Based Dependencies
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
hex = "0.4"
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
@@ -23,6 +30,10 @@ std = [
|
||||
"bp-messages/std",
|
||||
"bp-polkadot-core/std",
|
||||
"bp-runtime/std",
|
||||
"frame-support/std",
|
||||
"parity-scale-codec/std",
|
||||
"sp-api/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
"sp-version/std",
|
||||
]
|
||||
|
||||
@@ -21,10 +21,60 @@
|
||||
#![allow(clippy::unnecessary_mut_passed)]
|
||||
|
||||
use bp_messages::{LaneId, MessageNonce, UnrewardedRelayersState, Weight};
|
||||
use frame_support::{Blake2_128Concat, StorageHasher, Twox128};
|
||||
use sp_std::prelude::*;
|
||||
use sp_version::RuntimeVersion;
|
||||
|
||||
pub use bp_polkadot_core::*;
|
||||
|
||||
pub type UncheckedExtrinsic = bp_polkadot_core::UncheckedExtrinsic<Call>;
|
||||
|
||||
pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
spec_name: sp_version::create_runtime_str!("rococo"),
|
||||
impl_name: sp_version::create_runtime_str!("parity-rococo-v1-1"),
|
||||
authoring_version: 0,
|
||||
spec_version: 30,
|
||||
impl_version: 0,
|
||||
apis: sp_version::create_apis_vec![[]],
|
||||
transaction_version: 6,
|
||||
};
|
||||
|
||||
#[derive(parity_scale_codec::Encode, parity_scale_codec::Decode, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum Call {
|
||||
MockModule,
|
||||
}
|
||||
|
||||
impl sp_runtime::traits::Dispatchable for Call {
|
||||
type Origin = ();
|
||||
type Config = ();
|
||||
type Info = ();
|
||||
type PostInfo = ();
|
||||
|
||||
fn dispatch(self, _origin: Self::Origin) -> sp_runtime::DispatchResultWithInfo<Self::PostInfo> {
|
||||
unimplemented!("The Call is not expected to be dispatched.")
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a storage key for account data.
|
||||
///
|
||||
/// This is based on FRAME storage-generation code from Substrate:
|
||||
/// https://github.com/paritytech/substrate/blob/c939ceba381b6313462d47334f775e128ea4e95d/frame/support/src/storage/generator/map.rs#L74
|
||||
/// The equivalent command to invoke in case full `Runtime` is known is this:
|
||||
/// `let key = frame_system::Account::<Runtime>::storage_map_final_key(&account_id);`
|
||||
pub fn account_info_storage_key(id: &AccountId) -> Vec<u8> {
|
||||
let module_prefix_hashed = Twox128::hash(b"System");
|
||||
let storage_prefix_hashed = Twox128::hash(b"Account");
|
||||
let key_hashed = parity_scale_codec::Encode::using_encoded(id, Blake2_128Concat::hash);
|
||||
|
||||
let mut final_key = Vec::with_capacity(module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len());
|
||||
|
||||
final_key.extend_from_slice(&module_prefix_hashed[..]);
|
||||
final_key.extend_from_slice(&storage_prefix_hashed[..]);
|
||||
final_key.extend_from_slice(&key_hashed);
|
||||
|
||||
final_key
|
||||
}
|
||||
|
||||
/// Rococo Chain
|
||||
pub type Rococo = PolkadotLike;
|
||||
|
||||
@@ -115,3 +165,19 @@ sp_api::decl_runtime_apis! {
|
||||
fn unrewarded_relayers_state(lane: LaneId) -> UnrewardedRelayersState;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_generate_storage_key() {
|
||||
let acc = [
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32,
|
||||
]
|
||||
.into();
|
||||
let key = account_info_storage_key(&acc);
|
||||
assert_eq!(hex::encode(key), "26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da92dccd599abfe1920a1cff8a7358231430102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
parity-scale-codec = { version = "2.0.0", default-features = false, features = ["derive"] }
|
||||
|
||||
# Bridge Dependencies
|
||||
|
||||
@@ -21,6 +22,7 @@ sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" ,
|
||||
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
@@ -29,8 +31,10 @@ std = [
|
||||
"bp-runtime/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"parity-scale-codec/std",
|
||||
"sp-api/std",
|
||||
"sp-core/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
"sp-version/std",
|
||||
]
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
use bp_messages::MessageNonce;
|
||||
use bp_runtime::Chain;
|
||||
use frame_support::{
|
||||
dispatch::Dispatchable,
|
||||
parameter_types,
|
||||
weights::{
|
||||
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, WEIGHT_PER_SECOND},
|
||||
@@ -31,7 +32,7 @@ use sp_core::Hasher as HasherT;
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, IdentifyAccount, Verify},
|
||||
MultiSignature, OpaqueExtrinsic as UncheckedExtrinsic, Perbill,
|
||||
MultiSignature, OpaqueExtrinsic, Perbill,
|
||||
};
|
||||
|
||||
// Re-export's to avoid extra substrate dependencies in chain-specific crates.
|
||||
@@ -141,6 +142,12 @@ pub type BlockNumber = u32;
|
||||
/// Hash type used in Polkadot-like chains.
|
||||
pub type Hash = <BlakeTwo256 as HasherT>::Out;
|
||||
|
||||
/// Account Index (a.k.a. nonce).
|
||||
pub type Index = u32;
|
||||
|
||||
/// Hashing type.
|
||||
pub type Hashing = BlakeTwo256;
|
||||
|
||||
/// The type of an object that can produce hashes on Polkadot-like chains.
|
||||
pub type Hasher = BlakeTwo256;
|
||||
|
||||
@@ -160,7 +167,7 @@ pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;
|
||||
pub type Nonce = u32;
|
||||
|
||||
/// Block type of Polkadot-like chains.
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
pub type Block = generic::Block<Header, OpaqueExtrinsic>;
|
||||
|
||||
/// Polkadot-like block signed with a Justification.
|
||||
pub type SignedBlock = generic::SignedBlock<Block>;
|
||||
@@ -168,6 +175,86 @@ pub type SignedBlock = generic::SignedBlock<Block>;
|
||||
/// The balance of an account on Polkadot-like chain.
|
||||
pub type Balance = u128;
|
||||
|
||||
/// Unchecked Extrinsic type.
|
||||
pub type UncheckedExtrinsic<Call> = generic::UncheckedExtrinsic<AccountId, Call, Signature, SignedExtensions<Call>>;
|
||||
|
||||
/// A type of the data encoded as part of the transaction.
|
||||
pub type SignedExtra = ((), (), (), sp_runtime::generic::Era, Nonce, (), Balance);
|
||||
|
||||
/// Parameters which are part of the payload used to produce transaction signature,
|
||||
/// but don't end up in the transaction itself (i.e. inherent part of the runtime).
|
||||
pub type AdditionalSigned = (u32, u32, Hash, Hash, (), (), ());
|
||||
|
||||
/// A simplified version of signed extensions meant for producing signed transactions
|
||||
/// and signed payload in the client code.
|
||||
#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
|
||||
pub struct SignedExtensions<Call> {
|
||||
encode_payload: SignedExtra,
|
||||
additional_signed: AdditionalSigned,
|
||||
_data: sp_std::marker::PhantomData<Call>,
|
||||
}
|
||||
|
||||
impl<Call> parity_scale_codec::Encode for SignedExtensions<Call> {
|
||||
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
self.encode_payload.using_encoded(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Call> parity_scale_codec::Decode for SignedExtensions<Call> {
|
||||
fn decode<I: parity_scale_codec::Input>(_input: &mut I) -> Result<Self, parity_scale_codec::Error> {
|
||||
unimplemented!("SignedExtensions are never meant to be decoded, they are only used to create transaction");
|
||||
}
|
||||
}
|
||||
|
||||
impl<Call> SignedExtensions<Call> {
|
||||
pub fn new(
|
||||
version: sp_version::RuntimeVersion,
|
||||
era: sp_runtime::generic::Era,
|
||||
genesis_hash: Hash,
|
||||
nonce: Nonce,
|
||||
tip: Balance,
|
||||
) -> Self {
|
||||
Self {
|
||||
encode_payload: (
|
||||
(), // spec version
|
||||
(), // tx version
|
||||
(), // genesis
|
||||
era, // era
|
||||
nonce, // nonce (compact encoding)
|
||||
(), // Check weight
|
||||
tip, // transaction payment / tip (compact encoding)
|
||||
),
|
||||
additional_signed: (
|
||||
version.spec_version,
|
||||
version.transaction_version,
|
||||
genesis_hash,
|
||||
genesis_hash,
|
||||
(),
|
||||
(),
|
||||
(),
|
||||
),
|
||||
_data: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Call> sp_runtime::traits::SignedExtension for SignedExtensions<Call>
|
||||
where
|
||||
Call: parity_scale_codec::Codec + sp_std::fmt::Debug + Sync + Send + Clone + Eq + PartialEq,
|
||||
Call: Dispatchable,
|
||||
{
|
||||
const IDENTIFIER: &'static str = "Not needed.";
|
||||
|
||||
type AccountId = AccountId;
|
||||
type Call = Call;
|
||||
type AdditionalSigned = AdditionalSigned;
|
||||
type Pre = ();
|
||||
|
||||
fn additional_signed(&self) -> Result<Self::AdditionalSigned, frame_support::unsigned::TransactionValidityError> {
|
||||
Ok(self.additional_signed)
|
||||
}
|
||||
}
|
||||
|
||||
/// Polkadot-like chain.
|
||||
#[derive(RuntimeDebug)]
|
||||
pub struct PolkadotLike;
|
||||
|
||||
Reference in New Issue
Block a user