referrerpolicy=no-referrer-when-downgrade

sc_statement_store/
test_utils.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Test utilities for the statement store
20
21use sp_core::{sr25519, Encode, Pair};
22use sp_statement_store::{statement_allowance_key, Channel, StatementAllowance, Topic};
23
24/// Generate a deterministic keypair for a given client index
25pub fn get_keypair(idx: u32) -> sr25519::Pair {
26	sr25519::Pair::from_string(&format!("//StatementClient//{idx}"), None)
27		.expect("Derivation path is always valid; qed")
28}
29
30/// Creates uniform allowance storage items for a range of participants
31pub fn create_uniform_allowance_items(
32	count: u32,
33	allowance: StatementAllowance,
34) -> Vec<(Vec<u8>, Vec<u8>)> {
35	let allowance_encoded = allowance.encode();
36	let mut items = Vec::with_capacity(count as usize);
37	for idx in 0..count {
38		let keypair = get_keypair(idx);
39		let account_id = keypair.public();
40		let storage_key = statement_allowance_key(account_id.0);
41		items.push((storage_key.to_vec(), allowance_encoded.clone()));
42	}
43	items
44}
45
46/// Creates storage items for custom per-participant allowances
47pub fn create_allowance_items(allowances: &[(u32, StatementAllowance)]) -> Vec<(Vec<u8>, Vec<u8>)> {
48	let mut items = Vec::with_capacity(allowances.len());
49	for (idx, allowance) in allowances {
50		let keypair = get_keypair(*idx);
51		let account_id = keypair.public();
52		let storage_key = statement_allowance_key(account_id.0);
53		items.push((storage_key.to_vec(), allowance.encode()));
54	}
55	items
56}
57
58/// Creates a signed statement with the given topics, channel, data, and expiry
59pub fn create_test_statement(
60	keypair: &sr25519::Pair,
61	topics: &[Topic],
62	channel: Option<Channel>,
63	data: Vec<u8>,
64	expiry_ts: u32,
65	seq: u32,
66) -> sp_statement_store::Statement {
67	let mut statement = sp_statement_store::Statement::new();
68	for (i, topic) in topics.iter().enumerate() {
69		statement.set_topic(i, *topic);
70	}
71	if let Some(ch) = channel {
72		statement.set_channel(ch);
73	}
74	statement.set_plain_data(data);
75	statement.set_expiry_from_parts(expiry_ts, seq);
76	statement.sign_sr25519_private(keypair);
77	statement
78}