ccp_test_utils/
test_values.rs

1/*
2 * Copyright 2024 Fluence DAO
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use ccp_shared::types::*;
18
19pub fn generate_epoch_params(nonce: u8, difficulty: u8) -> EpochParameters {
20    let global_nonce = generate_global_nonce(nonce);
21    let difficulty = generate_difficulty(difficulty);
22
23    EpochParameters::new(global_nonce, difficulty)
24}
25
26pub fn generate_global_nonce(first_byte: u8) -> GlobalNonce {
27    GlobalNonce::new([
28        first_byte, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 1, 2, 3, 2, 3, 3, 4, 2, 1, 4, 5, 6, 1, 2,
29        3, 4, 6, 3, 2,
30    ])
31}
32
33pub fn generate_local_nonce(first_byte: u8) -> LocalNonce {
34    LocalNonce::new([
35        first_byte, 2, 3, 4, 3, 4, 3, 1, 2, 4, 4, 5, 6, 1, 2, 3, 2, 3, 3, 4, 2, 1, 4, 5, 6, 1, 2,
36        3, 4, 6, 3, 2,
37    ])
38}
39
40pub fn generate_cu_id(first_byte: u8) -> CUID {
41    CUID::new([
42        first_byte, 2, 3, 4, 5, 6, 7, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43        0, 0, 0, 0, 0,
44    ])
45}
46
47pub fn generate_difficulty(difficulty: u8) -> Difficulty {
48    Difficulty::new([
49        0, difficulty, 3, 4, 3, 4, 3, 1, 2, 4, 4, 5, 6, 1, 2, 3, 2, 3, 3, 4, 2, 1, 4, 5, 6, 1, 2,
50        3, 4, 6, 3, 2,
51    ])
52}
53
54pub fn generate_allocation(cores: &[u8]) -> CUAllocation {
55    cores
56        .iter()
57        .map(|core_id| {
58            (
59                PhysicalCoreId::from(*core_id as u32),
60                generate_cu_id(*core_id),
61            )
62        })
63        .collect()
64}
65
66pub fn generate_random_allocation(
67    rng: &mut impl rand::Rng,
68    size: usize,
69    range: std::ops::Range<u8>,
70) -> CUAllocation {
71    use rand::Rng;
72    let distr = rand::distr::Uniform::try_from(range).expect("Wrong range");
73    rng.sample_iter(distr)
74        .take(size)
75        .map(|core_id| {
76            (
77                PhysicalCoreId::from(core_id as u32),
78                generate_cu_id(core_id),
79            )
80        })
81        .collect()
82}