Skip to content

Commit 136dfc2

Browse files
committed
fix all tests
1 parent f2bb38a commit 136dfc2

File tree

6 files changed

+78
-352
lines changed

6 files changed

+78
-352
lines changed

node/cli/src/service.rs

-252
Original file line numberDiff line numberDiff line change
@@ -647,255 +647,3 @@ pub fn new_full(config: Configuration, cli: Cli) -> Result<TaskManager, ServiceE
647647

648648
Ok(task_manager)
649649
}
650-
651-
#[cfg(test)]
652-
mod tests {
653-
use std::sync::Arc;
654-
655-
use codec::Encode;
656-
use kitchensink_runtime::{
657-
constants::{currency::CENTS, time::SLOT_DURATION},
658-
Address, BalancesCall, RuntimeCall, UncheckedExtrinsic,
659-
};
660-
use node_primitives::{Block, DigestItem, Signature};
661-
use sc_client_api::BlockBackend;
662-
use sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy};
663-
use sc_consensus_babe::{BabeIntermediate, CompatibleDigestItem, INTERMEDIATE_KEY};
664-
use sc_consensus_epochs::descendent_query;
665-
use sc_keystore::LocalKeystore;
666-
use sc_service_test::TestNetNode;
667-
use sc_transaction_pool_api::{ChainEvent, MaintainedTransactionPool};
668-
use sp_consensus::{BlockOrigin, Environment, Proposer};
669-
use sp_core::crypto::Pair;
670-
use sp_inherents::InherentDataProvider;
671-
use sp_keyring::AccountKeyring;
672-
use sp_keystore::KeystorePtr;
673-
use sp_runtime::{
674-
generic::{Digest, Era, SignedPayload},
675-
key_types::BABE,
676-
traits::{Block as BlockT, Header as HeaderT, IdentifyAccount, Verify},
677-
RuntimeAppPublic,
678-
};
679-
680-
use crate::service::{new_full_base, NewFullBase};
681-
682-
type AccountPublic = <Signature as Verify>::Signer;
683-
684-
#[test]
685-
// It is "ignored", but the node-cli ignored tests are running on the CI.
686-
// This can be run locally with `cargo test --release -p node-cli test_sync -- --ignored`.
687-
#[ignore]
688-
fn test_sync() {
689-
sp_tracing::try_init_simple();
690-
691-
let keystore_path = tempfile::tempdir().expect("Creates keystore path");
692-
let keystore: KeystorePtr =
693-
LocalKeystore::open(keystore_path.path(), None).expect("Creates keystore").into();
694-
let alice: sp_consensus_babe::AuthorityId = keystore
695-
.sr25519_generate_new(BABE, Some("//Alice"))
696-
.expect("Creates authority pair")
697-
.into();
698-
699-
let chain_spec = crate::chain_spec::tests::integration_test_config_with_single_authority();
700-
701-
// For the block factory
702-
let mut slot = 1u64;
703-
704-
// For the extrinsics factory
705-
let bob = Arc::new(AccountKeyring::Bob.pair());
706-
let charlie = Arc::new(AccountKeyring::Charlie.pair());
707-
let mut index = 0;
708-
709-
sc_service_test::sync(
710-
chain_spec,
711-
|config| {
712-
let mut setup_handles = None;
713-
let NewFullBase { task_manager, client, network, sync, transaction_pool, .. } =
714-
new_full_base(
715-
config,
716-
false,
717-
|block_import: &sc_consensus_babe::BabeBlockImport<Block, _, _>,
718-
babe_link: &sc_consensus_babe::BabeLink<Block>| {
719-
setup_handles = Some((block_import.clone(), babe_link.clone()));
720-
},
721-
None,
722-
)?;
723-
724-
let node = sc_service_test::TestNetComponents::new(
725-
task_manager,
726-
client,
727-
network,
728-
sync,
729-
transaction_pool,
730-
);
731-
Ok((node, setup_handles.unwrap()))
732-
},
733-
|service, &mut (ref mut block_import, ref babe_link)| {
734-
let parent_hash = service.client().chain_info().best_hash;
735-
let parent_header = service.client().header(parent_hash).unwrap().unwrap();
736-
let parent_number = *parent_header.number();
737-
738-
futures::executor::block_on(service.transaction_pool().maintain(
739-
ChainEvent::NewBestBlock { hash: parent_header.hash(), tree_route: None },
740-
));
741-
742-
let mut proposer_factory = sc_basic_authorship::ProposerFactory::new(
743-
service.spawn_handle(),
744-
service.client(),
745-
service.transaction_pool(),
746-
None,
747-
None,
748-
);
749-
750-
let mut digest = Digest::default();
751-
752-
// even though there's only one authority some slots might be empty,
753-
// so we must keep trying the next slots until we can claim one.
754-
let (babe_pre_digest, epoch_descriptor) = loop {
755-
let epoch_descriptor = babe_link
756-
.epoch_changes()
757-
.shared_data()
758-
.epoch_descriptor_for_child_of(
759-
descendent_query(&*service.client()),
760-
&parent_hash,
761-
parent_number,
762-
slot.into(),
763-
)
764-
.unwrap()
765-
.unwrap();
766-
767-
let epoch = babe_link
768-
.epoch_changes()
769-
.shared_data()
770-
.epoch_data(&epoch_descriptor, |slot| {
771-
sc_consensus_babe::Epoch::genesis(babe_link.config(), slot)
772-
})
773-
.unwrap();
774-
775-
if let Some(babe_pre_digest) =
776-
sc_consensus_babe::authorship::claim_slot(slot.into(), &epoch, &keystore)
777-
.map(|(digest, _)| digest)
778-
{
779-
break (babe_pre_digest, epoch_descriptor);
780-
}
781-
782-
slot += 1;
783-
};
784-
785-
let inherent_data = futures::executor::block_on(
786-
(
787-
sp_timestamp::InherentDataProvider::new(
788-
std::time::Duration::from_millis(SLOT_DURATION * slot).into(),
789-
),
790-
sp_consensus_babe::inherents::InherentDataProvider::new(slot.into()),
791-
)
792-
.create_inherent_data(),
793-
)
794-
.expect("Creates inherent data");
795-
796-
digest.push(<DigestItem as CompatibleDigestItem>::babe_pre_digest(babe_pre_digest));
797-
798-
let new_block = futures::executor::block_on(async move {
799-
let proposer = proposer_factory.init(&parent_header).await;
800-
proposer
801-
.unwrap()
802-
.propose(inherent_data, digest, std::time::Duration::from_secs(1), None)
803-
.await
804-
})
805-
.expect("Error making test block")
806-
.block;
807-
808-
let (new_header, new_body) = new_block.deconstruct();
809-
let pre_hash = new_header.hash();
810-
// sign the pre-sealed hash of the block and then
811-
// add it to a digest item.
812-
let to_sign = pre_hash.encode();
813-
let signature = keystore
814-
.sr25519_sign(sp_consensus_babe::AuthorityId::ID, alice.as_ref(), &to_sign)
815-
.unwrap()
816-
.unwrap();
817-
let item = <DigestItem as CompatibleDigestItem>::babe_seal(signature.into());
818-
slot += 1;
819-
820-
let mut params = BlockImportParams::new(BlockOrigin::File, new_header);
821-
params.post_digests.push(item);
822-
params.body = Some(new_body);
823-
params.insert_intermediate(
824-
INTERMEDIATE_KEY,
825-
BabeIntermediate::<Block> { epoch_descriptor },
826-
);
827-
params.fork_choice = Some(ForkChoiceStrategy::LongestChain);
828-
829-
futures::executor::block_on(block_import.import_block(params))
830-
.expect("error importing test block");
831-
},
832-
|service, _| {
833-
let amount = 5 * CENTS;
834-
let to: Address = AccountPublic::from(bob.public()).into_account().into();
835-
let from: Address = AccountPublic::from(charlie.public()).into_account().into();
836-
let genesis_hash = service.client().block_hash(0).unwrap().unwrap();
837-
let best_hash = service.client().chain_info().best_hash;
838-
let (spec_version, transaction_version) = {
839-
let version = service.client().runtime_version_at(best_hash).unwrap();
840-
(version.spec_version, version.transaction_version)
841-
};
842-
let signer = charlie.clone();
843-
844-
let function = RuntimeCall::Balances(BalancesCall::transfer_allow_death {
845-
dest: to,
846-
value: amount,
847-
});
848-
849-
let check_non_zero_sender = frame_system::CheckNonZeroSender::new();
850-
let check_spec_version = frame_system::CheckSpecVersion::new();
851-
let check_tx_version = frame_system::CheckTxVersion::new();
852-
let check_genesis = frame_system::CheckGenesis::new();
853-
let check_era = frame_system::CheckEra::from(Era::Immortal);
854-
let check_nonce = frame_system::CheckNonce::from(index);
855-
let check_weight = frame_system::CheckWeight::new();
856-
let tx_payment = pallet_asset_tx_payment::ChargeAssetTxPayment::from(0, None);
857-
let extra = (
858-
check_non_zero_sender,
859-
check_spec_version,
860-
check_tx_version,
861-
check_genesis,
862-
check_era,
863-
check_nonce,
864-
check_weight,
865-
tx_payment,
866-
);
867-
let raw_payload = SignedPayload::from_raw(
868-
function,
869-
extra,
870-
((), spec_version, transaction_version, genesis_hash, genesis_hash, (), (), ()),
871-
);
872-
let signature = raw_payload.using_encoded(|payload| signer.sign(payload));
873-
let (function, extra, _) = raw_payload.deconstruct();
874-
index += 1;
875-
UncheckedExtrinsic::new_signed(function, from, signature.into(), extra).into()
876-
},
877-
);
878-
}
879-
880-
#[test]
881-
#[ignore]
882-
fn test_consensus() {
883-
sp_tracing::try_init_simple();
884-
885-
sc_service_test::consensus(
886-
crate::chain_spec::tests::integration_test_config_with_two_authorities(),
887-
|config| {
888-
let NewFullBase { task_manager, client, network, sync, transaction_pool, .. } =
889-
new_full_base(config, false, |_, _| (), None)?;
890-
Ok(sc_service_test::TestNetComponents::new(
891-
task_manager,
892-
client,
893-
network,
894-
sync,
895-
transaction_pool,
896-
))
897-
},
898-
vec!["//Alice".into(), "//Bob".into()],
899-
)
900-
}
901-
}

pallets/relayer/src/mock.rs

+21-26
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,43 @@
11
use frame_election_provider_support::{onchain, SequentialPhragmen, VoteWeight};
22
use frame_support::{
33
parameter_types,
4-
traits::{ConstU32, FindAuthor, GenesisBuild, OneSessionHandler},
4+
traits::{ConstU32, FindAuthor, OneSessionHandler},
55
};
66
use frame_system as system;
77
use pallet_session::historical as pallet_session_historical;
88
use pallet_staking_extension::ServerInfo;
99
use sp_core::H256;
1010
use sp_runtime::{
1111
curve::PiecewiseLinear,
12-
testing::{Header, TestXt, UintAuthorityId},
12+
testing::{TestXt, UintAuthorityId},
1313
traits::{BlakeTwo256, ConvertInto, IdentityLookup},
14-
Perbill,
14+
BuildStorage, Perbill,
1515
};
1616
use sp_staking::{EraIndex, SessionIndex};
1717

1818
use crate as pallet_relayer;
1919

2020
const NULL_ARR: [u8; 32] = [0; 32];
21-
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
2221
type Block = frame_system::mocking::MockBlock<Test>;
2322
type BlockNumber = u64;
2423
type AccountId = u64;
2524
type Balance = u64;
2625

2726
// Configure a mock runtime to test the pallet.
2827
frame_support::construct_runtime!(
29-
pub enum Test where
30-
Block = Block,
31-
NodeBlock = Block,
32-
UncheckedExtrinsic = UncheckedExtrinsic,
28+
pub enum Test
3329
{
34-
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
35-
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
36-
Authorship: pallet_authorship::{Pallet, Storage},
37-
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
38-
Relayer: pallet_relayer::{Pallet, Call, Storage, Event<T>},
39-
Staking: pallet_staking_extension::{Pallet, Call, Storage, Event<T>, Config<T>},
40-
FrameStaking: pallet_staking::{Pallet, Call, Storage, Event<T>},
41-
Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>},
42-
Historical: pallet_session_historical::{Pallet},
43-
BagsList: pallet_bags_list::{Pallet, Call, Storage, Event<T>},
44-
Constraints: pallet_constraints::{Pallet, Call, Storage, Event<T>},
30+
System: frame_system,
31+
Balances: pallet_balances,
32+
Authorship: pallet_authorship,
33+
Timestamp: pallet_timestamp,
34+
Relayer: pallet_relayer,
35+
Staking: pallet_staking_extension,
36+
FrameStaking: pallet_staking,
37+
Session: pallet_session,
38+
Historical: pallet_session_historical,
39+
BagsList: pallet_bags_list,
40+
Constraints: pallet_constraints,
4541
}
4642
);
4743

@@ -54,17 +50,16 @@ impl system::Config for Test {
5450
type AccountData = pallet_balances::AccountData<Balance>;
5551
type AccountId = u64;
5652
type BaseCallFilter = frame_support::traits::Everything;
53+
type Block = Block;
5754
type BlockHashCount = BlockHashCount;
5855
type BlockLength = ();
59-
type BlockNumber = u64;
6056
type BlockWeights = ();
6157
type DbWeight = ();
6258
type Hash = H256;
6359
type Hashing = BlakeTwo256;
64-
type Header = Header;
65-
type Index = u64;
6660
type Lookup = IdentityLookup<Self::AccountId>;
6761
type MaxConsumers = frame_support::traits::ConstU32<16>;
62+
type Nonce = u64;
6863
type OnKilledAccount = ();
6964
type OnNewAccount = ();
7065
type OnSetCode = ();
@@ -98,13 +93,13 @@ impl pallet_balances::Config for Test {
9893
type DustRemoval = ();
9994
type ExistentialDeposit = ExistentialDeposit;
10095
type FreezeIdentifier = ();
101-
type HoldIdentifier = ();
10296
type MaxFreezes = ();
10397
type MaxHolds = ();
10498
type MaxLocks = MaxLocks;
10599
type MaxReserves = ();
106100
type ReserveIdentifier = [u8; 8];
107101
type RuntimeEvent = RuntimeEvent;
102+
type RuntimeHoldReason = RuntimeHoldReason;
108103
type WeightInfo = ();
109104
}
110105

@@ -218,17 +213,17 @@ impl pallet_staking::Config for Test {
218213
type BondingDuration = BondingDuration;
219214
type Currency = Balances;
220215
type CurrencyBalance = Balance;
221-
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
216+
type CurrencyToVote = ();
222217
type ElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
223218
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
219+
type EventListeners = ();
224220
type GenesisElectionProvider = Self::ElectionProvider;
225221
type HistoryDepth = ConstU32<84>;
226222
type MaxNominations = MaxNominations;
227223
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
228224
type MaxUnlockingChunks = ConstU32<32>;
229225
type NextNewSession = Session;
230226
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
231-
type OnStakerSlash = ();
232227
type Reward = ();
233228
type RewardRemainder = ();
234229
type RuntimeEvent = RuntimeEvent;
@@ -314,7 +309,7 @@ impl pallet_constraints::Config for Test {
314309

315310
// Build genesis storage according to the mock runtime.
316311
pub fn new_test_ext() -> sp_io::TestExternalities {
317-
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
312+
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap();
318313
let pallet_staking_extension = pallet_staking_extension::GenesisConfig::<Test> {
319314
threshold_servers: vec![
320315
(5, ServerInfo { tss_account: 7, x25519_public_key: NULL_ARR, endpoint: vec![20] }),

0 commit comments

Comments
 (0)