4666047395
Updated 4763 files with dual copyright: - Parity Technologies (UK) Ltd. - Dijital Kurdistan Tech Institute
198 lines
6.1 KiB
Rust
198 lines
6.1 KiB
Rust
// This file is part of Bizinikiwi.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute
|
|
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
|
|
|
// This program 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.
|
|
|
|
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
use codec::{Decode, Encode};
|
|
use pezframe_support::Hashable;
|
|
use pezframe_system::offchain::AppCrypto;
|
|
use pezkuwi_sdk::*;
|
|
use pezsc_executor::error::Result;
|
|
use pezsp_consensus_babe::{
|
|
digests::{PreDigest, SecondaryPlainPreDigest},
|
|
Slot, BABE_ENGINE_ID,
|
|
};
|
|
use pezsp_core::{
|
|
crypto::KeyTypeId,
|
|
sr25519::Signature,
|
|
traits::{CallContext, CodeExecutor, RuntimeCode},
|
|
};
|
|
use pezsp_runtime::{
|
|
traits::{BlakeTwo256, Header as HeaderT},
|
|
ApplyExtrinsicResult, Digest, DigestItem, MultiSignature, MultiSigner,
|
|
};
|
|
use pezsp_state_machine::TestExternalities as CoreTestExternalities;
|
|
|
|
use pez_kitchensink_runtime::{
|
|
constants::currency::*, Block, BuildStorage, CheckedExtrinsic, Header, Runtime,
|
|
UncheckedExtrinsic,
|
|
};
|
|
use pez_node_primitives::{BlockNumber, Hash};
|
|
use pez_node_testing::keyring::*;
|
|
use pezsp_externalities::Externalities;
|
|
use pezstaging_node_cli::service::RuntimeExecutor;
|
|
|
|
pub const TEST_KEY_TYPE_ID: KeyTypeId = KeyTypeId(*b"test");
|
|
|
|
pub mod sr25519 {
|
|
mod app_sr25519 {
|
|
use super::super::TEST_KEY_TYPE_ID;
|
|
use pezkuwi_sdk::pezsp_application_crypto::{app_crypto, sr25519};
|
|
app_crypto!(sr25519, TEST_KEY_TYPE_ID);
|
|
}
|
|
|
|
pub type AuthorityId = app_sr25519::Public;
|
|
}
|
|
|
|
pub struct TestAuthorityId;
|
|
impl AppCrypto<MultiSigner, MultiSignature> for TestAuthorityId {
|
|
type RuntimeAppPublic = sr25519::AuthorityId;
|
|
type GenericSignature = Signature;
|
|
type GenericPublic = pezsp_core::sr25519::Public;
|
|
}
|
|
|
|
/// The wasm runtime code.
|
|
///
|
|
/// `compact` since it is after post-processing with wasm-gc which performs tree-shaking thus
|
|
/// making the binary slimmer. There is a convention to use compact version of the runtime
|
|
/// as canonical.
|
|
pub fn compact_code_unwrap() -> &'static [u8] {
|
|
pez_kitchensink_runtime::WASM_BINARY.expect(
|
|
"Development wasm binary is not available. Testing is only supported with the flag \
|
|
disabled.",
|
|
)
|
|
}
|
|
|
|
pub const GENESIS_HASH: [u8; 32] = [69u8; 32];
|
|
|
|
pub const SPEC_VERSION: u32 = pez_kitchensink_runtime::VERSION.spec_version;
|
|
|
|
pub const TRANSACTION_VERSION: u32 = pez_kitchensink_runtime::VERSION.transaction_version;
|
|
|
|
pub type TestExternalities<H> = CoreTestExternalities<H>;
|
|
|
|
pub fn sign(xt: CheckedExtrinsic) -> UncheckedExtrinsic {
|
|
pez_node_testing::keyring::sign(xt, SPEC_VERSION, TRANSACTION_VERSION, GENESIS_HASH, None)
|
|
}
|
|
|
|
pub fn default_transfer_call() -> pezpallet_balances::Call<Runtime> {
|
|
pezpallet_balances::Call::<Runtime>::transfer_allow_death {
|
|
dest: bob().into(),
|
|
value: 69 * DOLLARS,
|
|
}
|
|
}
|
|
|
|
pub fn from_block_number(n: u32) -> Header {
|
|
Header::new(n, Default::default(), Default::default(), [69; 32].into(), Default::default())
|
|
}
|
|
|
|
pub fn executor() -> RuntimeExecutor {
|
|
RuntimeExecutor::builder().build()
|
|
}
|
|
|
|
pub fn executor_call(
|
|
t: &mut TestExternalities<BlakeTwo256>,
|
|
method: &str,
|
|
data: &[u8],
|
|
) -> (Result<Vec<u8>>, bool) {
|
|
let mut t = t.ext();
|
|
|
|
let code = t.storage(pezsp_core::storage::well_known_keys::CODE).unwrap();
|
|
let heap_pages = t.storage(pezsp_core::storage::well_known_keys::HEAP_PAGES);
|
|
let runtime_code = RuntimeCode {
|
|
code_fetcher: &pezsp_core::traits::WrappedRuntimeCode(code.as_slice().into()),
|
|
hash: pezsp_crypto_hashing::blake2_256(&code).to_vec(),
|
|
heap_pages: heap_pages.and_then(|hp| Decode::decode(&mut &hp[..]).ok()),
|
|
};
|
|
pezsp_tracing::try_init_simple();
|
|
executor().call(&mut t, &runtime_code, method, data, CallContext::Onchain)
|
|
}
|
|
|
|
pub fn new_test_ext(code: &[u8]) -> TestExternalities<BlakeTwo256> {
|
|
pezsp_tracing::try_init_simple();
|
|
let ext = TestExternalities::new_with_code(
|
|
code,
|
|
pez_node_testing::genesis::config().build_storage().unwrap(),
|
|
);
|
|
ext
|
|
}
|
|
|
|
/// Construct a fake block.
|
|
///
|
|
/// `extrinsics` must be a list of valid extrinsics, i.e. none of the extrinsics for example
|
|
/// can report `ExhaustResources`. Otherwise, this function panics.
|
|
pub fn construct_block(
|
|
env: &mut TestExternalities<BlakeTwo256>,
|
|
number: BlockNumber,
|
|
parent_hash: Hash,
|
|
extrinsics: Vec<CheckedExtrinsic>,
|
|
babe_slot: Slot,
|
|
) -> (Vec<u8>, Hash) {
|
|
use pezsp_trie::{LayoutV1 as Layout, TrieConfiguration};
|
|
|
|
// sign extrinsics.
|
|
let extrinsics = extrinsics.into_iter().map(sign).collect::<Vec<_>>();
|
|
|
|
// calculate the header fields that we can.
|
|
let extrinsics_root =
|
|
Layout::<BlakeTwo256>::ordered_trie_root(extrinsics.iter().map(Encode::encode))
|
|
.to_fixed_bytes()
|
|
.into();
|
|
|
|
let header = Header {
|
|
parent_hash,
|
|
number,
|
|
extrinsics_root,
|
|
state_root: Default::default(),
|
|
digest: Digest {
|
|
logs: vec![DigestItem::PreRuntime(
|
|
BABE_ENGINE_ID,
|
|
PreDigest::SecondaryPlain(SecondaryPlainPreDigest {
|
|
slot: babe_slot,
|
|
authority_index: 42,
|
|
})
|
|
.encode(),
|
|
)],
|
|
},
|
|
};
|
|
|
|
// execute the block to get the real header.
|
|
executor_call(env, "Core_initialize_block", &header.encode()).0.unwrap();
|
|
|
|
for extrinsic in extrinsics.iter() {
|
|
// Try to apply the `extrinsic`. It should be valid, in the sense that it passes
|
|
// all pre-inclusion checks.
|
|
let r = executor_call(env, "BlockBuilder_apply_extrinsic", &extrinsic.encode())
|
|
.0
|
|
.expect("application of an extrinsic failed");
|
|
|
|
match ApplyExtrinsicResult::decode(&mut &r[..])
|
|
.expect("apply result deserialization failed")
|
|
{
|
|
Ok(_) => {},
|
|
Err(e) => panic!("Applying extrinsic failed: {:?}", e),
|
|
}
|
|
}
|
|
|
|
let header = Header::decode(
|
|
&mut &executor_call(env, "BlockBuilder_finalize_block", &[0u8; 0]).0.unwrap()[..],
|
|
)
|
|
.unwrap();
|
|
|
|
let hash = header.blake2_256();
|
|
(Block { header, extrinsics }.encode(), hash.into())
|
|
}
|