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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
use super::CertificateSlice;
use crate::key::{deserialize_public_key, deserialize_signature, GenesisPraosLeader};
use crate::rewards::TaxType;
use crate::transaction::{
    AccountIdentifier, Payload, PayloadAuthData, PayloadData, PayloadSlice,
    SingleAccountBindingSignature, TransactionBindingAuthData,
};
use chain_core::{
    packer::Codec,
    property::{Deserialize, DeserializeFromSlice, ReadError, Serialize, WriteError},
};
use chain_crypto::{digest::DigestOf, Blake2b256, Ed25519, PublicKey, Verification};
use chain_time::{DurationSeconds, TimeOffsetSeconds};
use std::marker::PhantomData;
use typed_bytes::{ByteArray, ByteBuilder};

/// Pool ID
pub type PoolId = PoolRegistrationHash;

/// Pool Registration Cryptographic Hash
pub type PoolRegistrationHash = DigestOf<Blake2b256, PoolRegistration>;

/// Hash of keys used for pool
pub type GenesisPraosLeaderHash = DigestOf<Blake2b256, GenesisPraosLeader>;

/// signatures with indices
pub type IndexSignatures = Vec<(u8, SingleAccountBindingSignature)>;

/// Pool information
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
    any(test, feature = "property-test-api"),
    derive(test_strategy::Arbitrary)
)]
pub struct PoolRegistration {
    /// A random value, for user purpose similar to a UUID.
    /// it may not be unique over a blockchain, so shouldn't be used a unique identifier
    pub serial: u128,
    /// Beginning of validity for this pool, this is used
    /// to keep track of the period of the expected key and the expiry
    pub start_validity: TimeOffsetSeconds,
    /// Permission system for this pool
    /// * Management threshold for owners, this need to be <= #owners and > 0.
    pub permissions: PoolPermissions,
    /// Owners of this pool
    pub owners: Vec<PublicKey<Ed25519>>,
    /// Operators of this pool
    pub operators: Box<[PublicKey<Ed25519>]>,
    /// Rewarding
    pub rewards: TaxType,
    /// Reward account
    pub reward_account: Option<AccountIdentifier>,
    /// Genesis Praos keys
    pub keys: GenesisPraosLeader,
}

/// Permission system related to the pool
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
    any(test, feature = "property-test-api"),
    derive(test_strategy::Arbitrary)
)]
pub struct PoolPermissions(u64);

pub type ManagementThreshold = u8;

const MANAGEMENT_THRESHOLD_BITMASK: u64 = 0b11_1111; // only support 32, reserved one for later extension if needed

#[allow(clippy::unusual_byte_groupings)]
const ALL_USED_BITMASK: u64 =
    0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00111111;

impl PoolPermissions {
    pub fn new(management_threshold: u8) -> PoolPermissions {
        let v = management_threshold as u64 & MANAGEMENT_THRESHOLD_BITMASK;
        PoolPermissions(v)
    }

    pub fn from_u64(v: u64) -> Option<PoolPermissions> {
        if (v & !ALL_USED_BITMASK) > 0 {
            None
        } else {
            Some(PoolPermissions(v))
        }
    }

    pub fn management_threshold(self) -> ManagementThreshold {
        (self.0 & MANAGEMENT_THRESHOLD_BITMASK) as ManagementThreshold
    }
}

/// Updating info for a pool
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PoolUpdate {
    pub pool_id: PoolId,
    pub last_pool_reg_hash: PoolRegistrationHash,
    pub new_pool_reg: PoolRegistration,
}

/// Retirement info for a pool
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PoolRetirement {
    pub pool_id: PoolId,
    pub retirement_time: TimeOffsetSeconds,
}

#[derive(Debug, Clone)]
pub enum PoolSignature {
    Operator(SingleAccountBindingSignature),
    Owners(PoolOwnersSignature),
}

/// Representant of a structure signed by a pool's owners
#[derive(Debug, Clone)]
pub struct PoolOwnersSignature {
    pub signatures: IndexSignatures,
}

pub type PoolOwnersSigned = PoolOwnersSignature;

impl PoolRegistration {
    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        let bb = bb
            .u128(self.serial)
            .u64(self.start_validity.into())
            .u64(self.permissions.0)
            .bytes(self.keys.vrf_public_key.as_ref())
            .bytes(self.keys.kes_public_key.as_ref())
            .iter8(&mut self.owners.iter(), |bb, o| bb.bytes(o.as_ref()))
            .iter8(&mut self.operators.iter(), |bb, o| bb.bytes(o.as_ref()))
            .sub(|sbb| self.rewards.serialize_in(sbb));

        match &self.reward_account {
            None => bb.u8(0),
            Some(AccountIdentifier::Single(pk)) => bb.u8(1).bytes(pk.as_ref().as_ref()),
            Some(AccountIdentifier::Multi(pk)) => bb.u8(2).bytes(pk.as_ref()),
        }
    }

    pub fn serialize(&self) -> ByteArray<Self> {
        self.serialize_in(ByteBuilder::new()).finalize()
    }

    pub fn to_id(&self) -> PoolId {
        let ba = self.serialize();
        DigestOf::digest_byteslice(&ba.as_byteslice())
    }

    pub fn management_threshold(&self) -> u8 {
        self.permissions.management_threshold()
    }
}

impl PoolUpdate {
    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        bb.bytes(self.pool_id.as_ref())
            .bytes(self.last_pool_reg_hash.as_ref())
            .sub(|bb| self.new_pool_reg.serialize_in(bb))
    }

    pub fn serialize(&self) -> ByteArray<Self> {
        self.serialize_in(ByteBuilder::new()).finalize()
    }
}

impl DeserializeFromSlice for PoolUpdate {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        let pool_id = <[u8; 32]>::deserialize(codec)?.into();
        let last_pool_reg_hash = <[u8; 32]>::deserialize(codec)?.into();
        let new_pool_reg = PoolRegistration::deserialize_from_slice(codec)?;
        Ok(PoolUpdate {
            pool_id,
            last_pool_reg_hash,
            new_pool_reg,
        })
    }
}

impl PoolRetirement {
    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        bb.bytes(self.pool_id.as_ref())
            .u64(self.retirement_time.into())
    }

    pub fn serialize(&self) -> ByteArray<Self> {
        self.serialize_in(ByteBuilder::new()).finalize()
    }
}

impl Deserialize for PoolRetirement {
    fn deserialize<R: std::io::Read>(codec: &mut Codec<R>) -> Result<Self, ReadError> {
        let pool_id = <[u8; 32]>::deserialize(codec)?.into();
        let retirement_time = DurationSeconds::from(codec.get_be_u64()?).into();
        Ok(PoolRetirement {
            pool_id,
            retirement_time,
        })
    }
}

impl Serialize for PoolUpdate {
    fn serialized_size(&self) -> usize {
        self.serialize().as_slice().len()
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        codec.put_bytes(self.serialize().as_slice())
    }
}

impl Serialize for PoolRetirement {
    fn serialized_size(&self) -> usize {
        self.serialize().as_slice().len()
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        codec.put_bytes(self.serialize().as_slice())
    }
}

impl Payload for PoolUpdate {
    const HAS_DATA: bool = true;
    const HAS_AUTH: bool = true;
    type Auth = PoolSignature;
    fn payload_data(&self) -> PayloadData<Self> {
        PayloadData(
            self.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            PhantomData,
        )
    }
    fn payload_auth_data(auth: &Self::Auth) -> PayloadAuthData<Self> {
        PayloadAuthData(
            auth.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            PhantomData,
        )
    }
    fn payload_to_certificate_slice(p: PayloadSlice<'_, Self>) -> Option<CertificateSlice<'_>> {
        Some(CertificateSlice::from(p))
    }
}

impl Payload for PoolRetirement {
    const HAS_DATA: bool = true;
    const HAS_AUTH: bool = true;
    type Auth = PoolSignature;
    fn payload_data(&self) -> PayloadData<Self> {
        PayloadData(
            self.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            PhantomData,
        )
    }
    fn payload_auth_data(auth: &Self::Auth) -> PayloadAuthData<Self> {
        PayloadAuthData(
            auth.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            PhantomData,
        )
    }
    fn payload_to_certificate_slice(p: PayloadSlice<'_, Self>) -> Option<CertificateSlice<'_>> {
        Some(CertificateSlice::from(p))
    }
}

impl Serialize for PoolRegistration {
    fn serialized_size(&self) -> usize {
        self.serialize().as_slice().len()
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        codec.put_bytes(self.serialize().as_slice())
    }
}

impl DeserializeFromSlice for PoolRegistration {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        let serial = codec.get_be_u128()?;
        let start_validity = DurationSeconds::from(codec.get_be_u64()?).into();
        let permissions = PoolPermissions::from_u64(codec.get_be_u64()?).ok_or_else(|| {
            ReadError::StructureInvalid("permission value not correct".to_string())
        })?;
        let keys = GenesisPraosLeader::deserialize_from_slice(codec)?;

        let owners_nb = codec.get_u8()?;
        let mut owners = Vec::with_capacity(owners_nb as usize);
        for _ in 0..owners_nb {
            owners.push(deserialize_public_key(codec)?);
        }

        let operators_nb = codec.get_u8()?;
        let mut operators = Vec::with_capacity(operators_nb as usize);
        for _ in 0..operators_nb {
            operators.push(deserialize_public_key(codec)?);
        }

        let rewards = TaxType::read_frombuf(codec)?;
        let reward_account = match codec.get_u8()? {
            0 => None,
            1 => {
                let pk = deserialize_public_key(codec)?;
                Some(AccountIdentifier::Single(pk.into()))
            }
            2 => {
                let mut pk = [0u8; 32];
                codec.copy_to_slice(&mut pk)?;
                Some(AccountIdentifier::Multi(pk.into()))
            }
            n => return Err(ReadError::UnknownTag(n as u32)),
        };

        let info = Self {
            serial,
            start_validity,
            permissions,
            owners,
            operators: operators.into(),
            rewards,
            reward_account,
            keys,
        };
        Ok(info)
    }
}

impl Payload for PoolRegistration {
    const HAS_DATA: bool = true;
    const HAS_AUTH: bool = true;
    type Auth = PoolSignature;
    fn payload_data(&self) -> PayloadData<Self> {
        PayloadData(
            self.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            PhantomData,
        )
    }

    fn payload_auth_data(auth: &Self::Auth) -> PayloadAuthData<Self> {
        PayloadAuthData(
            auth.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            PhantomData,
        )
    }

    fn payload_to_certificate_slice(p: PayloadSlice<'_, Self>) -> Option<CertificateSlice<'_>> {
        Some(CertificateSlice::from(p))
    }
}

impl PoolSignature {
    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        match self {
            PoolSignature::Operator(op) => bb.u8(0).bytes(op.0.as_ref()),
            PoolSignature::Owners(owners) => {
                assert!(!owners.signatures.is_empty());
                assert!(owners.signatures.len() < 256);
                bb.u8(1).iter8(&mut owners.signatures.iter(), |bb, (i, s)| {
                    bb.u8(*i).bytes(s.as_ref())
                })
            }
        }
    }

    pub fn verify(
        &self,
        pool_info: &PoolRegistration,
        verify_data: &TransactionBindingAuthData<'_>,
    ) -> Verification {
        match self {
            PoolSignature::Operator(_) => Verification::Failed,
            PoolSignature::Owners(owners) => owners.verify(pool_info, verify_data),
        }
    }
}

impl PoolOwnersSignature {
    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        bb.iter8(&mut self.signatures.iter(), |bb, (i, s)| {
            bb.u8(*i).bytes(s.as_ref())
        })
    }

    pub fn verify(
        &self,
        pool_info: &PoolRegistration,
        verify_data: &TransactionBindingAuthData<'_>,
    ) -> Verification {
        // fast track if we don't meet the management threshold already
        if self.signatures.len() < pool_info.management_threshold() as usize {
            return Verification::Failed;
        }

        let mut present = vec![false; pool_info.owners.len()];
        let mut signatories = 0;

        for (i, sig) in self.signatures.iter() {
            let i = *i as usize;
            // Check for out of bounds indices
            if i >= pool_info.owners.len() {
                return Verification::Failed;
            }

            // If already present, then we have a duplicate hence fail
            if present[i] {
                return Verification::Failed;
            } else {
                present[i] = true;
            }

            // Verify the cryptographic signature of a signatory
            let pk = &pool_info.owners[i];
            if sig.verify_slice(pk, verify_data) == Verification::Failed {
                return Verification::Failed;
            }
            signatories += 1
        }

        // check if we seen enough unique signatures; it is a redundant check
        // from the duplicated check + the threshold check
        if signatories < pool_info.management_threshold() as usize {
            return Verification::Failed;
        }

        Verification::Success
    }
}

impl DeserializeFromSlice for PoolOwnersSigned {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        let sigs_nb = codec.get_u8()? as usize;
        if sigs_nb == 0 {
            return Err(ReadError::StructureInvalid(
                "pool owner signature with 0 signatures".to_string(),
            ));
        }
        let mut signatures = Vec::new();
        for _ in 0..sigs_nb {
            let nb = codec.get_u8()?;
            let sig = deserialize_signature(codec)?;
            signatures.push((nb, SingleAccountBindingSignature(sig)))
        }
        Ok(PoolOwnersSigned { signatures })
    }
}

impl DeserializeFromSlice for PoolSignature {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        match codec.get_u8()? {
            0 => {
                let sig = deserialize_signature(codec)?;
                Ok(PoolSignature::Operator(SingleAccountBindingSignature(sig)))
            }
            1 => PoolOwnersSigned::deserialize_from_slice(codec).map(PoolSignature::Owners),
            code => Err(ReadError::UnknownTag(code as u32)),
        }
    }
}

#[cfg(test)]
mod tests {

    use super::{PoolOwnersSigned, PoolPermissions};
    use crate::{
        chaintypes::HeaderId,
        date::BlockDate,
        key::EitherEd25519SecretKey,
        testing::{
            builders::{make_witness, StakePoolBuilder},
            data::AddressData,
            TestGen,
        },
        transaction::{
            Input, NoExtra, SingleAccountBindingSignature, TransactionSignDataHash, TxBuilder,
            Witness,
        },
        value::Value,
    };
    use chain_addr::Discrimination;
    use chain_crypto::{Ed25519, PublicKey, Verification};
    use std::iter;

    #[derive(Clone, Debug)]
    pub struct PoolOwnersWithSignatures {
        pub owners: Vec<AddressData>,
        pub signatories: Vec<(u8, AddressData)>,
    }

    impl PoolOwnersWithSignatures {
        pub fn new(owners: Vec<AddressData>, signatories: Vec<(u8, AddressData)>) -> Self {
            PoolOwnersWithSignatures {
                owners,
                signatories,
            }
        }

        pub fn owners_pks(&self) -> Vec<PublicKey<Ed25519>> {
            self.owners
                .iter()
                .cloned()
                .map(|x| x.public_key())
                .collect()
        }

        pub fn indexed_signatories_sks(&self) -> Vec<(u8, EitherEd25519SecretKey)> {
            self.signatories
                .iter()
                .cloned()
                .map(|(i, x)| (i, x.private_key()))
                .collect()
        }

        pub fn witnesses(
            &self,
            block0_hash: HeaderId,
            transaction_hash: &TransactionSignDataHash,
        ) -> Vec<Witness> {
            self.signatories
                .iter()
                .map(|(_, x)| make_witness(&block0_hash, x, transaction_hash))
                .collect()
        }

        pub fn inputs(&self) -> Vec<Input> {
            self.signatories
                .iter()
                .map(|(_, x)| x.make_input(Value::zero(), None))
                .collect()
        }
    }

    /// Signatures count is grater than pool permissions and signatories have correct indexes
    #[test]
    pub fn pool_owners_correct_signature() {
        let owners_len = 8;
        let pool_permissions = PoolPermissions::new(4);
        let owners: Vec<AddressData> =
            iter::from_fn(|| Some(AddressData::account(Discrimination::Test)))
                .take(owners_len)
                .collect();

        let signatories: Vec<(u8, AddressData)> = vec![
            (0u8, owners[0].clone()),
            (1u8, owners[1].clone()),
            (2u8, owners[2].clone()),
            (3u8, owners[3].clone()),
        ];

        let pool_owner_with_sign = PoolOwnersWithSignatures::new(owners, signatories);
        test_verify(
            pool_owner_with_sign,
            pool_permissions,
            true,
            "verification should succeed because signatories are larger than pool permissions",
        );
    }

    /// duplicated owner, which cause that signatures are less than owners
    #[test]
    pub fn pool_owners_signature_duplicated_owner() {
        let owners_len = 8;
        let pool_permissions = PoolPermissions::new(4);
        let owners: Vec<AddressData> =
            iter::from_fn(|| Some(AddressData::account(Discrimination::Test)))
                .take(owners_len)
                .collect();

        let signatories: Vec<(u8, AddressData)> = vec![
            (0u8, owners[0].clone()),
            (0u8, owners[0].clone()),
            (2u8, owners[2].clone()),
            (3u8, owners[3].clone()),
        ];

        let pool_owner_with_sign = PoolOwnersWithSignatures::new(owners, signatories);
        test_verify(pool_owner_with_sign, pool_permissions, false, "verification should fail because one of signature is duplicated which cause that we have only
        3 valid signatures, while 4 are needed");
    }

    /// duplicated owner, but signatures are ok
    #[test]
    pub fn pool_owners_signature_extra_duplicated_signature() {
        let owners_len = 8;
        let pool_permissions = PoolPermissions::new(4);
        let owners: Vec<AddressData> =
            iter::from_fn(|| Some(AddressData::account(Discrimination::Test)))
                .take(owners_len)
                .collect();

        let signatories: Vec<(u8, AddressData)> = vec![
            (0u8, owners[0].clone()),
            (0u8, owners[0].clone()),
            (1u8, owners[1].clone()),
            (2u8, owners[2].clone()),
            (3u8, owners[3].clone()),
        ];

        let pool_owner_with_sign = PoolOwnersWithSignatures::new(owners, signatories);
        test_verify(pool_owner_with_sign, pool_permissions, false, "verification should fail there is duplicated signature even if unique signatures count is larger than
        permission threshold");
    }

    #[test]
    pub fn pool_owners_signature_wrong_signature() {
        let owners_len = 2;
        let pool_permissions = PoolPermissions::new(1);
        let owners: Vec<AddressData> =
            iter::from_fn(|| Some(AddressData::account(Discrimination::Test)))
                .take(owners_len)
                .collect();

        let signatories: Vec<(u8, AddressData)> = vec![
            (0u8, AddressData::account(Discrimination::Test)),
            (1u8, owners[1].clone()),
        ];

        let pool_owner_with_sign = PoolOwnersWithSignatures::new(owners, signatories);
        test_verify(
            pool_owner_with_sign,
            pool_permissions,
            false,
            "verification should fail due to invalid signature",
        );
    }

    #[test]
    pub fn pool_owners_signature_too_many_signatures() {
        let owners_len = 2;
        let pool_permissions = PoolPermissions::new(1);
        let owners: Vec<AddressData> =
            iter::from_fn(|| Some(AddressData::account(Discrimination::Test)))
                .take(owners_len)
                .collect();

        let signatories: Vec<(u8, AddressData)> = vec![
            (0u8, owners[0].clone()),
            (1u8, owners[1].clone()),
            (2u8, AddressData::account(Discrimination::Test)),
        ];

        let pool_owner_with_sign = PoolOwnersWithSignatures::new(owners, signatories);
        test_verify(
            pool_owner_with_sign,
            pool_permissions,
            false,
            "verification should fail due to extraordinary signature",
        );
    }

    #[test]
    pub fn pool_owners_signature_too_few_signatures() {
        let owners_len = 4;
        let pool_permissions = PoolPermissions::new(2);
        let owners: Vec<AddressData> =
            iter::from_fn(|| Some(AddressData::account(Discrimination::Test)))
                .take(owners_len)
                .collect();

        let signatories: Vec<(u8, AddressData)> = vec![(0u8, owners[0].clone())];

        let pool_owner_with_sign = PoolOwnersWithSignatures::new(owners, signatories);
        test_verify(
            pool_owner_with_sign,
            pool_permissions,
            false,
            "verification should fail due to non sufficient signatures",
        );
    }

    #[test]
    pub fn pool_owners_signature_different_order() {
        let owners_len = 8;
        let pool_permissions = PoolPermissions::new(4);
        let owners: Vec<AddressData> =
            iter::from_fn(|| Some(AddressData::account(Discrimination::Test)))
                .take(owners_len)
                .collect();

        let signatories: Vec<(u8, AddressData)> = vec![
            (0u8, owners[4].clone()),
            (1u8, owners[3].clone()),
            (2u8, owners[1].clone()),
            (3u8, owners[2].clone()),
        ];

        let pool_owner_with_sign = PoolOwnersWithSignatures::new(owners, signatories);
        test_verify(
            pool_owner_with_sign,
            pool_permissions,
            false,
            "verification should fail due to incorrect signatures indexes",
        );
    }

    #[test]
    pub fn pool_owners_signature_no_owners() {
        let owners_len = 0;
        let pool_permissions = PoolPermissions::new(0);
        let owners: Vec<AddressData> =
            iter::from_fn(|| Some(AddressData::account(Discrimination::Test)))
                .take(owners_len)
                .collect();

        let signatories: Vec<(u8, AddressData)> = vec![];

        let pool_owner_with_sign = PoolOwnersWithSignatures::new(owners, signatories);
        test_verify(
            pool_owner_with_sign,
            pool_permissions,
            true,
            "verification should fail due no owners",
        );
    }

    #[test]
    pub fn pool_owners_signature_zero_permissions() {
        let owners_len = 8;
        let pool_permissions = PoolPermissions::new(0);
        let owners: Vec<AddressData> =
            iter::from_fn(|| Some(AddressData::account(Discrimination::Test)))
                .take(owners_len)
                .collect();

        let signatories: Vec<(u8, AddressData)> = vec![(0u8, owners[0].clone())];

        let pool_owner_with_sign = PoolOwnersWithSignatures::new(owners, signatories);
        test_verify(
            pool_owner_with_sign,
            pool_permissions,
            true,
            "verification should fail due to 0 limit for permissions",
        );
    }

    /// For given pool_owner_with_sign (which contains pool registration owners and subset of signatures derived from them)
    /// and given pool_permissions limit it tests verify method for PoolOwnersSigned struct
    fn test_verify(
        pool_owner_with_sign: PoolOwnersWithSignatures,
        pool_permissions: PoolPermissions,
        should_pass: bool,
        info: &str,
    ) {
        let stake_pool = StakePoolBuilder::new()
            .with_owners(pool_owner_with_sign.owners_pks())
            .with_pool_permissions(pool_permissions)
            .build();

        let builder = TxBuilder::new()
            .set_payload(&NoExtra)
            .set_expiry_date(BlockDate::first().next_epoch())
            .set_ios(&pool_owner_with_sign.inputs(), &[]);
        let auth_data_hash = builder.get_auth_data_for_witness().hash();
        let builder = builder
            .set_witnesses(&pool_owner_with_sign.witnesses(TestGen::hash(), &auth_data_hash));

        let auth_data = builder.get_auth_data();
        let mut sigs = Vec::new();
        for (i, key) in pool_owner_with_sign.indexed_signatories_sks() {
            let sig = SingleAccountBindingSignature::new(&auth_data, |d| key.sign_slice(d.0));
            sigs.push((i, sig))
        }
        let pool_owner_signed = PoolOwnersSigned { signatures: sigs };
        let verification = if should_pass {
            Verification::Success
        } else {
            Verification::Failed
        };
        assert_eq!(
            pool_owner_signed.verify(&stake_pool.info(), &builder.get_auth_data()),
            verification,
            "{}",
            info
        );
    }
}