1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::{
    certificate::{PoolId, PoolRegistration},
    testing::{builders::StakePoolBuilder, data::address::AddressData, TestGen},
};

use chain_crypto::{Ed25519, KeyPair, PublicKey, RistrettoGroup2HashDh, SumEd25519_12};
use quickcheck::{Arbitrary, Gen};
use std::iter;

#[derive(Clone, Debug)]
pub struct StakePool {
    alias: String,
    id: PoolId,
    vrf: KeyPair<RistrettoGroup2HashDh>,
    kes: KeyPair<SumEd25519_12>,
    pool_info: PoolRegistration,
    reward_account: Option<AddressData>,
}

impl StakePool {
    pub fn new(
        alias: &str,
        id: PoolId,
        vrf: KeyPair<RistrettoGroup2HashDh>,
        kes: KeyPair<SumEd25519_12>,
        pool_info: PoolRegistration,
        reward_account: Option<AddressData>,
    ) -> Self {
        StakePool {
            alias: alias.to_owned(),
            id,
            vrf,
            kes,
            pool_info,
            reward_account,
        }
    }

    pub fn id(&self) -> PoolId {
        self.id.clone()
    }

    pub fn vrf(&self) -> KeyPair<RistrettoGroup2HashDh> {
        self.vrf.clone()
    }

    pub fn kes(&self) -> KeyPair<SumEd25519_12> {
        self.kes.clone()
    }

    pub fn info_mut(&mut self) -> &mut PoolRegistration {
        &mut self.pool_info
    }

    pub fn info(&self) -> PoolRegistration {
        self.pool_info.clone()
    }

    pub fn alias(&self) -> String {
        self.alias.clone()
    }

    pub fn reward_account(&self) -> Option<&AddressData> {
        self.reward_account.as_ref()
    }
}

impl Arbitrary for StakePool {
    fn arbitrary<G: Gen>(gen: &mut G) -> Self {
        let owners_count = std::cmp::max(usize::arbitrary(gen) % 8, 1);
        let operators_count = usize::arbitrary(gen) % 4;
        let owners: Vec<PublicKey<Ed25519>> = iter::from_fn(|| Some(TestGen::public_key()))
            .take(owners_count)
            .collect();
        let operators: Vec<PublicKey<Ed25519>> = iter::from_fn(|| Some(TestGen::public_key()))
            .take(operators_count)
            .collect();

        StakePoolBuilder::new()
            .with_owners(owners)
            .with_operators(operators)
            .with_pool_permissions(Arbitrary::arbitrary(gen))
            .with_reward_account(Arbitrary::arbitrary(gen))
            .with_tax_type(Arbitrary::arbitrary(gen))
            .build()
    }
}