Files
pezkuwi-subxt/substrate/client/rpc/src/author/tests.rs
T
Michal Kucharczyk 6a295e7c28 substrate-test-runtime migrated to "pure" frame runtime (#13737)
* substrate-test-runtime migrated to pure-frame based

* test block builder: helpers added

* simple renaming

* basic_authorship test adjusted

* block_building storage_proof test adjusted

* babe: tests: should_panic expected added

* babe: tests adjusted

ConsensusLog::NextEpochData is now added by pallet_babe as
pallet_babe::SameAuthoritiesForever trigger is used in runtime config.

* beefy: tests adjusted

test-substrate-runtime is now using frame::executive to finalize the
block. during finalization the digests stored during block execution are
checked against header digests:
https://github.com/paritytech/substrate/blob/91bb2d29ca905599098a5b35eaf24867c4fbd60a/frame/executive/src/lib.rs#L585-L591

It makes impossible to directly manipulate header's digets, w/o
depositing logs into system pallet storage `Digest<T: Config>`.

Instead of this dedicated extrinsic allowing to store logs items
(MmrRoot / AuthoritiesChange) is used.

* grandpa: tests adjusted

test-substrate-runtime is now using frame::executive to finalize the
block. during finalization the digest logs stored during block execution are
checked against header digest logs:
https://github.com/paritytech/substrate/blob/91bb2d29ca905599098a5b35eaf24867c4fbd60a/frame/executive/src/lib.rs#L585-L591

It makes impossible to directly manipulate header's digets, w/o
depositing logs into system pallet storage `Digest<T: Config>`.

Instead of this dedicated extrinsic allowing to store logs items
(ScheduledChange / ForcedChange and DigestItem::Other) is used.

* network:bitswap: test adjusted

The size of unchecked extrinsic was increased. The pattern used in test will
be placed at the end of scale-encoded buffer.

* runtime apis versions adjusted

* storage keys used in runtime adjusted

* wasm vs native tests removed

* rpc tests: adjusted

Transfer transaction processing was slightly improved, test was
adjusted.

* tests: sizes adjusted

Runtime extrinsic size was increased. Size of data read during block
execution was also increased due to usage of new pallets in runtime.

Sizes were adjusted in tests.

* cargo.lock update

cargo update -p substrate-test-runtime -p substrate-test-runtime-client

* warnings fixed

* builders cleanup: includes / std

* extrinsic validation cleanup

* txpool: benches performance fixed

* fmt

* spelling

* Apply suggestions from code review

Co-authored-by: Davide Galassi <davxy@datawok.net>

* Apply code review suggestions

* Apply code review suggestions

* get rid of 1063 const

* renaming: UncheckedExtrinsic -> Extrinsic

* test-utils-runtime: further step to pure-frame

* basic-authorship: tests OK

* CheckSubstrateCall added + tests fixes

* test::Transfer call removed

* priority / propagate / no sudo+root-testing

* fixing warnings + format

* cleanup: build2/nonce + format

* final tests fixes

all tests are passing

* logs/comments removal

* should_not_accept_old_signatures test removed

* make txpool benches work again

* Cargo.lock reset

* format

* sudo hack removed

* txpool benches fix+cleanup

* .gitignore reverted

* rebase fixing + unsigned cleanup

* Cargo.toml/Cargo.lock cleanup

* force-debug feature removed

* mmr tests fixed

* make cargo-clippy happy

* network sync test uses unsigned extrinsic

* cleanup

* ".git/.scripts/commands/fmt/fmt.sh"

* push_storage_change signed call remove

* GenesisConfig cleanup

* fix

* fix

* GenesisConfig simplified

* storage_keys_works: reworked

* storage_keys_works: expected keys in vec

* storage keys list moved to substrate-test-runtime

* substrate-test: some sanity tests + GenesisConfigBuilder rework

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Apply suggestions from code review

* Review suggestions

* fix

* fix

* beefy: generate_blocks_and_sync block_num sync with actaul value

* Apply suggestions from code review

Co-authored-by: Davide Galassi <davxy@datawok.net>

* Update test-utils/runtime/src/genesismap.rs

Co-authored-by: Davide Galassi <davxy@datawok.net>

* cargo update -p sc-rpc -p sc-transaction-pool

* Review suggestions

* fix

* doc added

* slot_duration adjusted for Babe::slot_duration

* small doc fixes

* array_bytes::hex used instead of hex

* tiny -> medium name fix

* Apply suggestions from code review

Co-authored-by: Sebastian Kunert <skunert49@gmail.com>

* TransferData::try_from_unchecked_extrinsic -> try_from

* Update Cargo.lock

---------

Co-authored-by: parity-processbot <>
Co-authored-by: Davide Galassi <davxy@datawok.net>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
2023-05-04 16:20:22 +00:00

332 lines
10 KiB
Rust

// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// 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 super::*;
use crate::testing::{test_executor, timeout_secs};
use assert_matches::assert_matches;
use codec::Encode;
use jsonrpsee::{
core::Error as RpcError,
types::{error::CallError, EmptyServerParams as EmptyParams},
RpcModule,
};
use sc_transaction_pool::{BasicPool, FullChainApi};
use sc_transaction_pool_api::TransactionStatus;
use sp_core::{
blake2_256,
bytes::to_hex,
crypto::{ByteArray, Pair},
ed25519,
testing::{ED25519, SR25519},
H256,
};
use sp_keystore::{testing::MemoryKeystore, Keystore};
use sp_runtime::Perbill;
use std::sync::Arc;
use substrate_test_runtime_client::{
self,
runtime::{Block, Extrinsic, ExtrinsicBuilder, SessionKeys, Transfer},
AccountKeyring, Backend, Client, DefaultTestClientBuilderExt, TestClientBuilderExt,
};
fn uxt(sender: AccountKeyring, nonce: u64) -> Extrinsic {
let tx = Transfer {
amount: Default::default(),
nonce,
from: sender.into(),
to: AccountKeyring::Bob.into(),
};
ExtrinsicBuilder::new_transfer(tx).build()
}
type FullTransactionPool = BasicPool<FullChainApi<Client<Backend>, Block>, Block>;
struct TestSetup {
pub client: Arc<Client<Backend>>,
pub keystore: Arc<MemoryKeystore>,
pub pool: Arc<FullTransactionPool>,
}
impl Default for TestSetup {
fn default() -> Self {
let keystore = Arc::new(MemoryKeystore::new());
let client_builder = substrate_test_runtime_client::TestClientBuilder::new();
let client = Arc::new(client_builder.set_keystore(keystore.clone()).build());
let spawner = sp_core::testing::TaskExecutor::new();
let pool =
BasicPool::new_full(Default::default(), true.into(), None, spawner, client.clone());
TestSetup { client, keystore, pool }
}
}
impl TestSetup {
fn author(&self) -> Author<FullTransactionPool, Client<Backend>> {
Author {
client: self.client.clone(),
pool: self.pool.clone(),
keystore: self.keystore.clone(),
deny_unsafe: DenyUnsafe::No,
executor: test_executor(),
}
}
fn into_rpc() -> RpcModule<Author<FullTransactionPool, Client<Backend>>> {
Self::default().author().into_rpc()
}
}
#[tokio::test]
async fn author_submit_transaction_should_not_cause_error() {
let _ = env_logger::try_init();
let author = TestSetup::default().author();
let api = author.into_rpc();
let xt: Bytes = uxt(AccountKeyring::Alice, 1).encode().into();
let extrinsic_hash: H256 = blake2_256(&xt).into();
let response: H256 = api.call("author_submitExtrinsic", [xt.clone()]).await.unwrap();
assert_eq!(response, extrinsic_hash);
assert_matches!(
api.call::<_, H256>("author_submitExtrinsic", [xt]).await,
Err(RpcError::Call(CallError::Custom(err))) if err.message().contains("Already Imported") && err.code() == 1013
);
}
#[tokio::test]
async fn author_should_watch_extrinsic() {
let api = TestSetup::into_rpc();
let xt = to_hex(
&ExtrinsicBuilder::new_call_with_priority(0)
.signer(AccountKeyring::Alice.into())
.build()
.encode(),
true,
);
let mut sub = api.subscribe("author_submitAndWatchExtrinsic", [xt]).await.unwrap();
let (tx, sub_id) = timeout_secs(10, sub.next::<TransactionStatus<H256, Block>>())
.await
.unwrap()
.unwrap()
.unwrap();
assert_matches!(tx, TransactionStatus::Ready);
assert_eq!(&sub_id, sub.subscription_id());
// Replace the extrinsic and observe the subscription is notified.
let (xt_replacement, xt_hash) = {
let tx = ExtrinsicBuilder::new_call_with_priority(1)
.signer(AccountKeyring::Alice.into())
.build()
.encode();
let hash = blake2_256(&tx);
(to_hex(&tx, true), hash)
};
let _ = api.call::<_, H256>("author_submitExtrinsic", [xt_replacement]).await.unwrap();
let (tx, sub_id) = timeout_secs(10, sub.next::<TransactionStatus<H256, Block>>())
.await
.unwrap()
.unwrap()
.unwrap();
assert_eq!(tx, TransactionStatus::Usurped(xt_hash.into()));
assert_eq!(&sub_id, sub.subscription_id());
}
#[tokio::test]
async fn author_should_return_watch_validation_error() {
const METHOD: &'static str = "author_submitAndWatchExtrinsic";
let invalid_xt = ExtrinsicBuilder::new_fill_block(Perbill::from_percent(100)).build();
let api = TestSetup::into_rpc();
let failed_sub = api.subscribe(METHOD, [to_hex(&invalid_xt.encode(), true)]).await;
assert_matches!(
failed_sub,
Err(RpcError::Call(CallError::Custom(err))) if err.message().contains("Invalid Transaction") && err.code() == 1010
);
}
#[tokio::test]
async fn author_should_return_pending_extrinsics() {
let api = TestSetup::into_rpc();
let xt_bytes: Bytes = uxt(AccountKeyring::Alice, 0).encode().into();
api.call::<_, H256>("author_submitExtrinsic", [to_hex(&xt_bytes, true)])
.await
.unwrap();
let pending: Vec<Bytes> =
api.call("author_pendingExtrinsics", EmptyParams::new()).await.unwrap();
assert_eq!(pending, vec![xt_bytes]);
}
#[tokio::test]
async fn author_should_remove_extrinsics() {
const METHOD: &'static str = "author_removeExtrinsic";
let setup = TestSetup::default();
let api = setup.author().into_rpc();
// Submit three extrinsics, then remove two of them (will cause the third to be removed as well,
// having a higher nonce)
let xt1_bytes = uxt(AccountKeyring::Alice, 0).encode();
let xt1 = to_hex(&xt1_bytes, true);
let xt1_hash: H256 = api.call("author_submitExtrinsic", [xt1]).await.unwrap();
let xt2 = to_hex(&uxt(AccountKeyring::Alice, 1).encode(), true);
let xt2_hash: H256 = api.call("author_submitExtrinsic", [xt2]).await.unwrap();
let xt3 = to_hex(&uxt(AccountKeyring::Bob, 0).encode(), true);
let xt3_hash: H256 = api.call("author_submitExtrinsic", [xt3]).await.unwrap();
assert_eq!(setup.pool.status().ready, 3);
// Now remove all three.
// Notice how we need an extra `Vec` wrapping the `Vec` we want to submit as params.
let removed: Vec<H256> = api
.call(
METHOD,
vec![vec![
hash::ExtrinsicOrHash::Hash(xt3_hash),
// Removing this one will also remove xt2
hash::ExtrinsicOrHash::Extrinsic(xt1_bytes.into()),
]],
)
.await
.unwrap();
assert_eq!(removed, vec![xt1_hash, xt2_hash, xt3_hash]);
}
#[tokio::test]
async fn author_should_insert_key() {
let setup = TestSetup::default();
let api = setup.author().into_rpc();
let suri = "//Alice";
let keypair = ed25519::Pair::from_string(suri, None).expect("generates keypair");
let params: (String, String, Bytes) = (
String::from_utf8(ED25519.0.to_vec()).expect("Keytype is a valid string"),
suri.to_string(),
keypair.public().0.to_vec().into(),
);
api.call::<_, ()>("author_insertKey", params).await.unwrap();
let pubkeys = setup.keystore.keys(ED25519).unwrap();
assert!(pubkeys.contains(&keypair.public().to_raw_vec()));
}
#[tokio::test]
async fn author_should_rotate_keys() {
let setup = TestSetup::default();
let api = setup.author().into_rpc();
let new_pubkeys: Bytes = api.call("author_rotateKeys", EmptyParams::new()).await.unwrap();
let session_keys =
SessionKeys::decode(&mut &new_pubkeys[..]).expect("SessionKeys decode successfully");
let ed25519_pubkeys = setup.keystore.keys(ED25519).unwrap();
let sr25519_pubkeys = setup.keystore.keys(SR25519).unwrap();
assert!(ed25519_pubkeys.contains(&session_keys.ed25519.to_raw_vec()));
assert!(sr25519_pubkeys.contains(&session_keys.sr25519.to_raw_vec()));
}
#[tokio::test]
async fn author_has_session_keys() {
// Setup
let api = TestSetup::into_rpc();
// Add a valid session key
let pubkeys: Bytes = api
.call("author_rotateKeys", EmptyParams::new())
.await
.expect("Rotates the keys");
// Add a session key in a different keystore
let non_existent_pubkeys: Bytes = {
let api2 = TestSetup::default().author().into_rpc();
api2.call("author_rotateKeys", EmptyParams::new())
.await
.expect("Rotates the keys")
};
// Then…
let existing = api.call::<_, bool>("author_hasSessionKeys", vec![pubkeys]).await.unwrap();
assert!(existing, "Existing key is in the session keys");
let inexistent = api
.call::<_, bool>("author_hasSessionKeys", vec![non_existent_pubkeys])
.await
.unwrap();
assert_eq!(inexistent, false, "Inexistent key is not in the session keys");
assert_matches!(
api.call::<_, bool>("author_hasSessionKeys", vec![Bytes::from(vec![1, 2, 3])]).await,
Err(RpcError::Call(CallError::Custom(err))) if err.message().contains("Session keys are not encoded correctly")
);
}
#[tokio::test]
async fn author_has_key() {
let _ = env_logger::try_init();
let api = TestSetup::into_rpc();
let suri = "//Alice";
let alice_keypair = ed25519::Pair::from_string(suri, None).expect("Generates keypair");
let params = (
String::from_utf8(ED25519.0.to_vec()).expect("Keytype is a valid string"),
suri.to_string(),
Bytes::from(alice_keypair.public().0.to_vec()),
);
api.call::<_, ()>("author_insertKey", params).await.expect("insertKey works");
let bob_keypair = ed25519::Pair::from_string("//Bob", None).expect("Generates keypair");
// Alice's ED25519 key is there
let has_alice_ed: bool = {
let params = (
Bytes::from(alice_keypair.public().to_raw_vec()),
String::from_utf8(ED25519.0.to_vec()).expect("Keytype is a valid string"),
);
api.call("author_hasKey", params).await.unwrap()
};
assert!(has_alice_ed);
// Alice's SR25519 key is not there
let has_alice_sr: bool = {
let params = (
Bytes::from(alice_keypair.public().to_raw_vec()),
String::from_utf8(SR25519.0.to_vec()).expect("Keytype is a valid string"),
);
api.call("author_hasKey", params).await.unwrap()
};
assert!(!has_alice_sr);
// Bob's ED25519 key is not there
let has_bob_ed: bool = {
let params = (
Bytes::from(bob_keypair.public().to_raw_vec()),
String::from_utf8(ED25519.0.to_vec()).expect("Keytype is a valid string"),
);
api.call("author_hasKey", params).await.unwrap()
};
assert!(!has_bob_ed);
}