Skip to content

Commit cbc8986

Browse files
committed
feat: test pallet check with Alice balance
1 parent 8f87300 commit cbc8986

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cumulus/test/relay-sproof-builder/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ codec = { features = ["derive"], workspace = true }
1717
# Substrate
1818
sp-consensus-babe = { workspace = true }
1919
sp-core = { workspace = true }
20+
sp-io = { workspace = true }
21+
sp-keyring = { workspace = true }
2022
sp-runtime = { workspace = true }
2123
sp-state-machine = { workspace = true }
2224
sp-trie = { workspace = true }
@@ -38,6 +40,8 @@ std = [
3840
"polkadot-primitives/std",
3941
"sp-consensus-babe/std",
4042
"sp-core/std",
43+
"sp-io/std",
44+
"sp-keyring/std",
4145
"sp-runtime/std",
4246
"sp-state-machine/std",
4347
"sp-trie/std",

cumulus/test/relay-sproof-builder/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ impl RelayStateSproofBuilder {
247247
);
248248
insert(relay_chain::well_known_keys::CURRENT_SLOT.to_vec(), self.current_slot.encode());
249249

250+
let (alice_key, alice_data) = generate_alice_account();
251+
insert(alice_key, alice_data);
252+
250253
for (key, value) in self.additional_key_values {
251254
insert(key, value);
252255
}
@@ -273,6 +276,21 @@ fn convert_to_authority_weight_pair(
273276
.collect()
274277
}
275278

279+
/// Include to avoid `KeyToIncludeInRelayProof` test on test-pallet to break unit tests.
280+
fn generate_alice_account() -> (Vec<u8>, Vec<u8>) {
281+
use codec::Encode;
282+
use sp_keyring::Sr25519Keyring;
283+
284+
let alice = Sr25519Keyring::Alice.to_account_id();
285+
let mut alice_key = sp_io::hashing::twox_128(b"System").to_vec();
286+
alice_key.extend_from_slice(&sp_io::hashing::twox_128(b"Account"));
287+
alice_key.extend_from_slice(&sp_io::hashing::blake2_128(&alice.encode()));
288+
alice_key.extend_from_slice(&alice.encode());
289+
290+
let alice_account = (0u32, 0u32, 1u32, 0u32, 25_000_000_000_000u128, 0u128, 0u128, 0u128);
291+
(alice_key, alice_account.encode())
292+
}
293+
276294
/// Add a BABE pre-digest to a generic header
277295
fn add_babe_pre_digest(header: &mut Header, authority_index: u32, block_number: u64) {
278296
/// This method generates some vrf data, but only to make the compiler happy

cumulus/test/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,8 @@ impl_runtime_apis! {
648648

649649
RelayProofRequest {
650650
keys: vec![
651-
// Request a well-known key to verify its inclusion in the relay proof.
652-
RelayStorageKey::Top(test_pallet::RELAY_EPOCH_INDEX_KEY.to_vec()),
651+
// Request a key to verify its inclusion in the proof.
652+
RelayStorageKey::Top(test_pallet::relay_alice_account_key()),
653653
],
654654
}
655655
}

cumulus/test/runtime/src/test_pallet.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@
1717
/// A special pallet that exposes dispatchables that are only useful for testing.
1818
pub use pallet::*;
1919

20-
use polkadot_primitives::well_known_keys;
20+
use codec::Encode;
2121

2222
/// Some key that we set in genesis and only read in [`TestOnRuntimeUpgrade`] to ensure that
2323
/// [`OnRuntimeUpgrade`] works as expected.
2424
pub const TEST_RUNTIME_UPGRADE_KEY: &[u8] = b"+test_runtime_upgrade_key+";
2525

26-
/// A well-known key to request for inclusion in the proof.
27-
pub use well_known_keys::EPOCH_INDEX as RELAY_EPOCH_INDEX_KEY;
26+
/// Generates the storage key for Alice's account on the relay chain.
27+
pub fn relay_alice_account_key() -> alloc::vec::Vec<u8> {
28+
use sp_keyring::Sr25519Keyring;
29+
30+
let alice = Sr25519Keyring::Alice.to_account_id();
31+
32+
let mut key = sp_io::hashing::twox_128(b"System").to_vec();
33+
key.extend_from_slice(&sp_io::hashing::twox_128(b"Account"));
34+
key.extend_from_slice(&sp_io::hashing::blake2_128(&alice.encode()));
35+
key.extend_from_slice(&alice.encode());
36+
key
37+
}
2838

2939
#[frame_support::pallet(dev_mode)]
3040
pub mod pallet {
@@ -139,13 +149,17 @@ impl<T: Config> cumulus_pallet_parachain_system::OnSystemEvent for Pallet<T> {
139149
fn on_relay_state_proof(
140150
relay_state_proof: &cumulus_pallet_parachain_system::relay_state_snapshot::RelayChainStateProof,
141151
) -> frame_support::weights::Weight {
142-
use crate::test_pallet::RELAY_EPOCH_INDEX_KEY;
152+
use crate::{Balance, Nonce};
153+
use frame_system::AccountInfo;
154+
use pallet_balances::AccountData;
155+
156+
let alice_key = crate::test_pallet::relay_alice_account_key();
143157

144-
// Expect the requested key to be part of the proof.
158+
// Verify that Alice's account is included in the relay proof.
145159
relay_state_proof
146-
.read_optional_entry::<u64>(RELAY_EPOCH_INDEX_KEY)
160+
.read_optional_entry::<AccountInfo<Nonce, AccountData<Balance>>>(&alice_key)
147161
.expect("Invalid relay chain state proof")
148-
.expect("EPOCH_INDEX must be present");
162+
.expect("Alice's account must be present in the relay proof");
149163

150164
frame_support::weights::Weight::zero()
151165
}

0 commit comments

Comments
 (0)