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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
// Copyright 2021 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

//! Implementation of Transfers in the SAFE Network.

#![doc(
    html_logo_url = "https://raw.githubusercontent.com/maidsafe/QA/master/Images/maidsafe_logo.png",
    html_favicon_url = "https://maidsafe.net/img/favicon.ico",
    test(attr(forbid(warnings)))
)]
// For explanation of lint checks, run `rustc -W help`.
#![forbid(unsafe_code)]
#![warn(
    missing_docs,
    trivial_casts,
    trivial_numeric_casts,
    unused_extern_crates,
    unused_import_braces,
    unused_qualifications,
    unused_results
)]

mod actor;
mod error;
mod test_utils;
mod wallet;
mod wallet_replica;

pub use self::{
    actor::Actor as TransferActor, error::Error, wallet::Wallet, wallet_replica::WalletReplica,
};

use serde::{Deserialize, Serialize};
use sn_data_types::{
    CreditId, DebitId, PublicKey, SignedCredit, SignedDebit, Token, TransferAgreementProof,
    TransferValidated,
};
use std::collections::HashSet;

type Result<T> = std::result::Result<T, Error>;
type Outcome<T> = Result<Option<T>>;

trait TernaryResult<T> {
    fn success(item: T) -> Self;
    fn no_change() -> Self;
    fn rejected(error: Error) -> Self;
}

impl<T> TernaryResult<T> for Outcome<T> {
    fn success(item: T) -> Self {
        Ok(Some(item))
    }
    fn no_change() -> Self {
        Ok(None)
    }
    fn rejected(error: Error) -> Self {
        Err(error)
    }
}

// ------------------------------------------------------------
//                      Actor
// ------------------------------------------------------------

/// An implementation of the ReplicaValidator, should contain the logic from upper layers
/// for determining if a remote group of Replicas, represented by a PublicKey, is indeed valid.
/// This is logic from the membership part of the system, and thus handled by the upper layers
/// membership implementation.
pub trait ReplicaValidator {
    /// Determines if a remote group of Replicas, represented by a PublicKey, is indeed valid.
    fn is_valid(&self, section: PublicKey) -> bool;
}

/// Events raised by the Actor.
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
pub enum ActorEvent {
    /// Raised when a request to create
    /// a transfer validation cmd for Replicas,
    /// has been successful (valid on local state).
    TransferInitiated(TransferInitiated),
    /// Raised when an Actor receives a Replica transfer validation.
    TransferValidationReceived(TransferValidationReceived),
    /// Raised when the Actor has accumulated a
    /// quorum of validations, and produced a RegisterTransfer cmd
    /// for sending to Replicas.
    TransferRegistrationSent(TransferRegistrationSent),
    /// Raised when the Actor has received
    /// unknown credits on querying Replicas.
    TransfersSynched(TransfersSynched),
}

/// Raised when the Actor has received
/// f.ex. credits that its Replicas were holding upon
/// the propagation of them from a remote group of Replicas,
/// or unknown debits that its Replicas were holding
/// upon the registration of them from another
/// instance of the same Actor.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
pub struct TransfersSynched {
    id: PublicKey,
    balance: Token,
    debit_version: u64,
    credit_ids: HashSet<CreditId>,
}

/// This event is raised by the Actor after having
/// successfully created a transfer cmd to send to the
/// Replicas for validation.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
pub struct TransferInitiated {
    /// The debit signed by the initiating Actor.
    pub signed_debit: SignedDebit,
    /// The credit signed by the initiating Actor.
    pub signed_credit: SignedCredit,
}

impl TransferInitiated {
    /// Get the debit id
    pub fn id(&self) -> DebitId {
        self.signed_debit.id()
    }
}

/// Raised when a Replica responds with
/// a successful validation of a transfer.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
pub struct TransferValidationReceived {
    /// The event raised by a Replica.
    validation: TransferValidated,
    /// Added when quorum of validations
    /// have been received from Replicas.
    pub proof: Option<TransferAgreementProof>,
}

/// Raised when the Actor has accumulated a
/// quorum of validations, and produced a RegisterTransfer cmd
/// for sending to Replicas.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
pub struct TransferRegistrationSent {
    transfer_proof: TransferAgreementProof,
}

#[allow(unused)]
mod test {
    use crate::{
        actor::Actor, test_utils, test_utils::*, wallet, wallet_replica::WalletReplica, ActorEvent,
        Error, ReplicaValidator, Result, TransferInitiated, Wallet,
    };
    use crdts::{
        quickcheck::{quickcheck, TestResult},
        Dot,
    };
    use sn_data_types::{
        ActorHistory, Credit, CreditAgreementProof, CreditId, Debit, Keypair, OwnerType, PublicKey,
        ReplicaEvent, SignatureShare, SignedCredit, SignedDebit, SignedTransfer, Token, Transfer,
        TransferAgreementProof,
    };
    use std::collections::{HashMap, HashSet};
    use std::sync::Arc;
    use threshold_crypto::{PublicKeySet, PublicKeyShare, SecretKey, SecretKeySet, SecretKeyShare};

    macro_rules! hashmap {
        ($( $key: expr => $val: expr ),*) => {{
             let mut map = ::std::collections::HashMap::new();
             $( let _ = map.insert($key, $val); )*
             map
        }}
    }

    // ------------------------------------------------------------------------
    // ------------------------ Basic Transfer --------------------------------
    // ------------------------------------------------------------------------

    #[test]
    fn basic_transfer() {
        let _ = transfer_between_actors(100, 10, 3);
    }

    // #[allow(trivial_casts)]
    // #[test]
    // fn quickcheck_basic_transfer() {
    //     quickcheck(transfer_between_actors as fn(u64, u64, u8, u8, u8, u8) -> TestResult);
    // }

    #[test]
    fn synching() -> Result<()> {
        let section_count = 1;
        let replicas_per_section = 1;
        let section_configs = vec![vec![]];
        let Network {
            genesis_credit,
            mut sections,
            mut actors,
        } = setup_new_network(section_count, replicas_per_section, section_configs)?;

        let genesis_key = genesis_credit.recipient();
        let genesis_elder = &mut sections.remove(0).elders.remove(0);
        let past_key = Ok(PublicKey::Bls(
            genesis_elder.signing.replicas_pk_set().public_key(),
        ));
        let wallet_replica = match genesis_elder.replicas.get_mut(&genesis_key) {
            Some(w) => w,
            None => panic!("Failed the test; no such wallet."),
        };
        let _ = wallet_replica
            .genesis(&genesis_credit, || past_key)?
            .ok_or(Error::UnexpectedOutcome)?;

        let event = ReplicaEvent::TransferPropagated(sn_data_types::TransferPropagated {
            credit_proof: genesis_credit.clone(),
            crediting_replica_keys: PublicKey::Bls(
                genesis_elder.signing.replicas_pk_set().public_key(),
            ),
            crediting_replica_sig: genesis_elder.signing.sign_credit_proof(&genesis_credit)?,
        });
        wallet_replica.apply(event)?;

        println!("Finding genesis actor by id: {}", genesis_key);
        let mut actor_balance = None;
        for actor in actors.iter_mut() {
            println!("Actor id: {}", actor.actor.id());
            if actor.actor.id() == genesis_key {
                println!("Found actor!");
                if let Some(synched_event) = actor.actor.from_history(ActorHistory {
                    credits: vec![genesis_credit.clone()],
                    debits: vec![],
                })? {
                    actor
                        .actor
                        .apply(ActorEvent::TransfersSynched(synched_event))?;
                    actor_balance = Some(actor.actor.balance());
                }
                break;
            }
        }
        let balance = wallet_replica.balance();
        assert_eq!(genesis_credit.amount(), balance);
        assert_eq!(Some(balance), actor_balance);
        Ok(())
    }

    // ------------------------------------------------------------------------
    // ------------------------ Genesis --------------------------------
    // ------------------------------------------------------------------------

    #[test]
    fn can_start_with_genesis() -> Result<()> {
        let section_count = 1;
        let replicas_per_section = 1;
        let section_configs = vec![vec![u32::MAX as u64]];
        let Network {
            genesis_credit,
            mut sections,
            ..
        } = setup_new_network(section_count, replicas_per_section, section_configs)?;

        let genesis_key = genesis_credit.recipient();
        let genesis_elder = &mut sections.remove(0).elders.remove(0);
        let past_key = Ok(PublicKey::Bls(
            genesis_elder.signing.replicas_pk_set().public_key(),
        ));
        let wallet_replica = match genesis_elder.replicas.get_mut(&genesis_key) {
            Some(w) => w,
            None => panic!("Failed the test; no such wallet."),
        };
        let _ = wallet_replica
            .genesis(&genesis_credit, || past_key)?
            .ok_or(Error::UnexpectedOutcome)?;

        wallet_replica.apply(ReplicaEvent::TransferPropagated(
            sn_data_types::TransferPropagated {
                credit_proof: genesis_credit.clone(),
                crediting_replica_keys: PublicKey::Bls(
                    genesis_elder.signing.replicas_pk_set().public_key(),
                ),
                crediting_replica_sig: genesis_elder.signing.sign_credit_proof(&genesis_credit)?,
            },
        ))?;
        let balance = wallet_replica.balance();
        assert_eq!(genesis_credit.amount(), balance);
        Ok(())
    }

    #[test]
    fn genesis_can_only_be_the_first() -> Result<()> {
        let section_count = 1;
        let replica_count = 1;
        let section_configs = vec![vec![0]];

        let Network {
            genesis_credit,
            mut sections,
            ..
        } = setup_new_network(section_count, replica_count, section_configs)?;
        let genesis_elder = &mut sections.remove(0).elders.remove(0);
        let past_key = PublicKey::Bls(genesis_credit.replica_keys().public_key());
        let wallet_replica = match genesis_elder.replicas.get_mut(&genesis_credit.recipient()) {
            Some(w) => w,
            None => panic!("Failed the test; no such wallet."),
        };
        let _ = wallet_replica
            .genesis(&genesis_credit, || Ok(past_key))?
            .ok_or(Error::UnexpectedOutcome)?;

        wallet_replica.apply(ReplicaEvent::TransferPropagated(
            sn_data_types::TransferPropagated {
                credit_proof: genesis_credit.clone(),
                crediting_replica_keys: PublicKey::Bls(
                    genesis_elder.signing.replicas_pk_set().public_key(),
                ),
                crediting_replica_sig: genesis_elder.signing.sign_credit_proof(&genesis_credit)?,
            },
        ))?;

        // try genesis again..
        let result = wallet_replica.genesis(&genesis_credit, || Ok(past_key));
        match result {
            Ok(_) => panic!("Should not be able to genesis again."),
            Err(e) => assert_eq!(e, Error::InvalidOperation),
        }
        Ok(())
    }

    // ------------------------------------------------------------------------
    // ------------------------ Basic Transfer Body ---------------------------
    // ------------------------------------------------------------------------

    fn transfer_between_actors(
        sender_balance: u64,
        recipient_balance: u64,
        replica_count: u8,
    ) -> TestResult {
        match basic_transfer_between_actors(sender_balance, recipient_balance, replica_count) {
            Ok(Some(_)) => TestResult::passed(),
            Ok(None) => TestResult::discard(),
            Err(_) => TestResult::failed(),
        }
    }

    fn basic_transfer_between_actors(
        sender_balance: u64,
        recipient_balance: u64,
        replica_count: u8,
    ) -> Result<Option<()>> {
        // --- Filter ---
        if 0 == sender_balance || 2 >= replica_count {
            return Ok(None);
        }

        // --- Arrange ---
        let section_count = 2;
        let sender_index = 0;
        let recipient_index = 1;
        let recipient_final = sender_balance + recipient_balance;
        let section_configs = vec![vec![sender_balance], vec![recipient_balance]];
        let Network {
            mut actors,
            mut sections,
            ..
        } = setup_new_network(section_count, replica_count, section_configs)?;
        let mut sender_section = sections.remove(0);
        let mut recipient_section = sections.remove(0);
        let mut sender = actors.remove(0);
        let mut recipient = actors.remove(0);

        // --- Act ---
        // 1. Init transfer at Sender Actor.
        let transfer = init_transfer(&mut sender, recipient.actor.id())?;
        // 2. Validate at Sender Replicas.
        let debit_proof =
            validate_at_sender_replicas(transfer, &mut sender)?.ok_or(Error::UnexpectedOutcome)?;
        // 3. Register at Sender Replicas.
        register_at_debiting_replicas(&debit_proof, &mut sender_section)?;
        // 4. Propagate to Recipient Replicas.
        let events =
            propagate_to_crediting_replicas(debit_proof.credit_proof(), &mut recipient_section);
        // 5. Synch at Recipient Actor.
        synch(&mut recipient)?;

        // --- Assert ---
        // Actor and Replicas have the correct balance.
        assert_balance(sender, Token::zero());
        assert_balance(recipient, Token::from_nano(recipient_final));
        Ok(Some(()))
    }

    fn assert_balance(actor: TestActor, amount: Token) {
        assert!(actor.actor.balance() == amount);
        actor.section.elders.iter().for_each(|elder| {
            let wallet = match elder.replicas.get(&actor.actor.id()) {
                Some(w) => w,
                None => panic!("Failed the test; no such wallet."),
            };
            assert_eq!(wallet.balance(), amount)
        });
    }

    // ------------------------------------------------------------------------
    // ------------------------ AT2 Steps -------------------------------------
    // ------------------------------------------------------------------------

    // 1. Init debit at Sender Actor.
    fn init_transfer(sender: &mut TestActor, to: PublicKey) -> Result<TransferInitiated> {
        let transfer = sender
            .actor
            .transfer(sender.actor.balance(), to, "asdf".to_string())?
            .ok_or(Error::UnexpectedOutcome)?;

        sender
            .actor
            .apply(ActorEvent::TransferInitiated(transfer.clone()))?;

        Ok(transfer)
    }

    // 2. Validate debit at Sender Replicas.
    fn validate_at_sender_replicas(
        transfer: TransferInitiated,
        sender: &mut TestActor,
    ) -> Result<Option<TransferAgreementProof>> {
        for elder in &mut sender.section.elders {
            let wallet_replica = match elder.replicas.get_mut(&sender.actor.id()) {
                Some(w) => w,
                None => panic!("Failed the test; no such wallet."),
            };
            let _ = wallet_replica
                .validate(&transfer.signed_debit, &transfer.signed_credit)?
                .ok_or(Error::UnexpectedOutcome)?;

            let signed_transfer = SignedTransfer {
                debit: transfer.signed_debit.clone(),
                credit: transfer.signed_credit.clone(),
            };
            let (replica_debit_sig, replica_credit_sig) =
                elder.signing.sign_transfer(&signed_transfer)?;
            let validation = sn_data_types::TransferValidated {
                signed_credit: signed_transfer.credit,
                signed_debit: signed_transfer.debit,
                replica_debit_sig,
                replica_credit_sig,
                replicas: sender.section.id.clone(),
            };
            // then apply to inmem state
            wallet_replica.apply(ReplicaEvent::TransferValidated(validation.clone()))?;

            let validation_received = sender
                .actor
                .receive(validation)?
                .ok_or(Error::UnexpectedOutcome)?;
            sender.actor.apply(ActorEvent::TransferValidationReceived(
                validation_received.clone(),
            ))?;
            if let Some(proof) = validation_received.proof {
                let registered = sender
                    .actor
                    .register(proof.clone())?
                    .ok_or(Error::UnexpectedOutcome)?;
                sender
                    .actor
                    .apply(ActorEvent::TransferRegistrationSent(registered))?;
                return Ok(Some(proof));
            }
        }
        Ok(None)
    }

    // 3. Register debit at Sender Replicas.
    fn register_at_debiting_replicas(
        debit_proof: &TransferAgreementProof,
        section: &mut Section,
    ) -> Result<()> {
        for elder in &mut section.elders {
            let wallet_replica = match elder.replicas.get_mut(&debit_proof.sender()) {
                Some(w) => w,
                None => panic!("Failed the test; no such wallet."),
            };
            let past_key = Ok(PublicKey::Bls(debit_proof.replica_keys().public_key()));
            let registered = wallet_replica
                .register(debit_proof, || past_key)?
                .ok_or(Error::UnexpectedOutcome)?;
            wallet_replica.apply(ReplicaEvent::TransferRegistered(registered))?;
        }
        Ok(())
    }

    // 4. Propagate credit to Recipient Replicas.
    fn propagate_to_crediting_replicas(
        credit_proof: CreditAgreementProof,
        section: &mut Section,
    ) -> Vec<ReplicaEvent> {
        section
            .elders
            .iter_mut()
            .map(|replica| {
                let wallet_replica = match replica.replicas.get_mut(&credit_proof.recipient()) {
                    Some(w) => w,
                    None => panic!("Failed the test; no such wallet."),
                };
                let past_key = Ok(PublicKey::Bls(credit_proof.replica_keys().public_key()));
                let _ = wallet_replica
                    .receive_propagated(&credit_proof, || past_key)?
                    .ok_or(Error::UnexpectedOutcome)?;

                let crediting_replica_sig = replica.signing.sign_credit_proof(&credit_proof)?;
                let propagated = sn_data_types::TransferPropagated {
                    credit_proof: credit_proof.clone(),
                    crediting_replica_keys: PublicKey::Bls(
                        replica.signing.replicas_pk_set().public_key(),
                    ),
                    crediting_replica_sig,
                };
                // then apply to inmem state
                wallet_replica.apply(ReplicaEvent::TransferPropagated(propagated.clone()))?;
                Ok(ReplicaEvent::TransferPropagated(propagated))
            })
            .filter_map(|c: Result<ReplicaEvent>| match c {
                Ok(c) => Some(c),
                _ => None,
            })
            .collect()
    }

    // 5. Synch at Recipient Actor.
    fn synch(recipient: &mut TestActor) -> Result<()> {
        let section = &recipient.section;
        let wallet = section.elders[0]
            .replicas
            .get(&recipient.actor.id())
            .ok_or(Error::UnexpectedOutcome)?;
        let snapshot = wallet.wallet().ok_or(Error::UnexpectedOutcome)?;
        let transfers = recipient
            .actor
            .synch(
                snapshot.balance,
                snapshot.debit_version,
                snapshot.credit_ids,
            )?
            .ok_or(Error::UnexpectedOutcome)?;
        recipient
            .actor
            .apply(ActorEvent::TransfersSynched(transfers))
    }

    // ------------------------------------------------------------------------
    // ------------------------ Setup Helpers ---------------------------------
    // ------------------------------------------------------------------------

    fn get_random_pk() -> PublicKey {
        PublicKey::from(SecretKey::random().public_key())
    }

    fn find_group(index: u8, sections: &[Section]) -> Option<Section> {
        for section in sections {
            if section.index == index {
                return Some(section.clone());
            }
        }
        None
    }

    fn setup_random_wallet(balance: u64, section: u8) -> Result<TestWallet> {
        let mut rng = rand::thread_rng();
        let keypair = Keypair::new_ed25519(&mut rng);
        let recipient = keypair.public_key();
        let owner = OwnerType::Single(recipient);
        let mut wallet = Wallet::new(owner);
        setup_wallet(balance, section, keypair, wallet)
    }

    fn setup_wallet(
        balance: u64,
        section: u8,
        keypair: Keypair,
        wallet: Wallet,
    ) -> Result<TestWallet> {
        let mut wallet = wallet;
        if balance > 0 {
            let amount = Token::from_nano(balance);
            let sender = Dot::new(get_random_pk(), 0);
            let debit = Debit { id: sender, amount };
            let credit = Credit {
                id: debit.credit_id()?,
                recipient: wallet.id().public_key(),
                amount,
                msg: "".to_string(),
            };
            let _ = wallet.apply_credit(credit)?;
        }

        Ok(TestWallet {
            wallet,
            keypair,
            section,
        })
    }

    fn setup_actor(wallet: TestWallet, sections: &[Section]) -> Result<TestActor> {
        let section = find_group(wallet.section, sections).ok_or(Error::UnexpectedOutcome)?;

        let actor = Actor::from_snapshot(
            wallet.wallet,
            wallet.keypair,
            section.id.clone(),
            Validator {},
        );

        Ok(TestActor { actor, section })
    }

    // Create n replica groups, with k replicas in each
    fn setup_section_keys(group_count: u8, replica_count: u8) -> HashMap<u8, SectionKeys> {
        let mut rng = rand::thread_rng();
        let mut groups = HashMap::new();
        for i in 0..group_count {
            let threshold = std::cmp::max(1, 2 * replica_count / 3) - 1;
            let bls_secret_key = SecretKeySet::random(threshold as usize, &mut rng);
            let peers = bls_secret_key.public_keys();
            let mut shares = vec![];
            for j in 0..replica_count {
                let share = bls_secret_key.secret_key_share(j as usize);
                shares.push((share, j as usize));
            }
            let _ = groups.insert(
                i,
                SectionKeys {
                    index: i,
                    id: peers,
                    keys: shares,
                },
            );
        }
        groups
    }

    fn setup_new_network(
        section_count: u8,
        replicas_per_section: u8,
        section_configs: Vec<Vec<u64>>,
    ) -> Result<Network> {
        // setup genesis section
        let mut wallets_in_genesis_section = vec![];
        let genesis_index = 0;
        let mut section_configs = section_configs;
        let mut genesis_section_configs = section_configs.remove(genesis_index);
        for balance in genesis_section_configs {
            let wallet = setup_random_wallet(balance, genesis_index as u8)?;
            let _ = wallets_in_genesis_section.push(wallet);
        }

        let ((genesis_credit, genesis_wallet), genesis_section) =
            setup_genesis_section(replicas_per_section, wallets_in_genesis_section.clone())?;
        let _ = wallets_in_genesis_section.insert(0, genesis_wallet);

        let mut all_sections = vec![genesis_section];
        let mut other_section_wallets = vec![];
        if section_count > 1 {
            // setup rest of the sections
            for (section_index, wallet_configs) in section_configs.iter().enumerate() {
                let mut next_section_wallets = vec![];
                for balance in wallet_configs {
                    let wallet = setup_random_wallet(*balance, (section_index + 1) as u8)?;
                    let _ = next_section_wallets.push(wallet.clone());
                    let _ = other_section_wallets.push(wallet);
                }
                let section_keys = generate_section_keys(section_index as u8, replicas_per_section);
                let section =
                    setup_section(section_index as u8, section_keys, next_section_wallets);
                all_sections.push(section);
            }
        }

        let wallets = wallets_in_genesis_section
            .into_iter()
            .chain(other_section_wallets.into_iter())
            .collect();
        let actors = get_test_actors(wallets, all_sections.clone())?;

        Ok(Network {
            genesis_credit,
            sections: all_sections,
            actors,
        })
    }

    fn get_test_actors(wallets: Vec<TestWallet>, sections: Vec<Section>) -> Result<Vec<TestActor>> {
        let mut actors = vec![];
        for wallet in wallets {
            actors.push(setup_actor(wallet.clone(), &sections)?);
        }
        Ok(actors)
    }

    fn generate_section_keys(section_index: u8, replica_count: u8) -> SectionKeys {
        //
        let mut rng = rand::thread_rng();
        let threshold = std::cmp::max(1, 2 * replica_count / 3) - 1;
        let bls_secret_key = SecretKeySet::random(threshold as usize, &mut rng);
        let peers = bls_secret_key.public_keys();
        let mut shares = vec![];
        for j in 0..replica_count {
            let share = bls_secret_key.secret_key_share(j as usize);
            shares.push((share, j as usize));
        }
        SectionKeys {
            index: section_index,
            id: peers,
            keys: shares,
        }
    }

    fn setup_section(
        section_index: u8,
        section_keys: SectionKeys,
        wallets: Vec<TestWallet>,
    ) -> Section {
        /// ----
        let mut elders = vec![];
        let peer_replicas = section_keys.id.clone();
        for (secret_key, key_index) in &section_keys.keys {
            let wallets = wallets.clone();
            let mut wallet_replicas = hashmap![];
            for wallet in wallets.into_iter() {
                let wallet_id = wallet.wallet.id();
                let pending_proposals = Default::default();
                let wallet_replica = WalletReplica::from_snapshot(
                    wallet_id.clone(),
                    secret_key.public_key_share(),
                    *key_index,
                    peer_replicas.clone(),
                    wallet.wallet.clone(),
                    pending_proposals,
                    None,
                );
                let _ = wallet_replicas.insert(wallet_id.public_key(), wallet_replica);
            }
            elders.push(Elder {
                id: secret_key.public_key_share(),
                replicas: wallet_replicas,
                signing: ReplicaSigning::new(secret_key.clone(), *key_index, peer_replicas.clone()),
            });
        }
        Section {
            index: section_index,
            id: peer_replicas,
            elders,
        }
    }

    fn setup_genesis_section(
        replica_count: u8,
        wallets: Vec<TestWallet>,
    ) -> Result<((CreditAgreementProof, TestWallet), Section)> {
        // setup genesis wallet
        let threshold = (replica_count - 1) as usize;
        let balance = u32::MAX as u64 * 1_000_000_000;
        let mut rng = rand::thread_rng();
        let bls_secret_key = SecretKeySet::random(threshold, &mut rng);
        let peer_replicas = bls_secret_key.public_keys();
        let id = PublicKey::Bls(peer_replicas.public_key());
        let keypair = sn_data_types::Keypair::new_bls_share(
            0,
            bls_secret_key.secret_key_share(0),
            peer_replicas.clone(),
        );
        let owner = OwnerType::Multi(peer_replicas.clone());
        let empty_genesis_wallet = setup_wallet(0, 0, keypair, Wallet::new(owner))?;
        let genesis_credit = get_multi_genesis(balance, id, bls_secret_key.clone())?;

        let mut wallets = wallets;
        wallets.insert(0, empty_genesis_wallet.clone());

        // setup genesis replicas
        let mut elders = vec![];
        for key_index in 0..replica_count as usize {
            // copy over all wallets to the replicas
            for wallet in &wallets {
                let secret_key = bls_secret_key.secret_key_share(key_index);
                let peer_replicas = bls_secret_key.public_keys();
                let mut wallet_replicas = hashmap![];
                let wallet_id = wallet.wallet.id();
                let pending_proposals = Default::default();
                let wallet_replica = WalletReplica::from_snapshot(
                    wallet_id.clone(),
                    secret_key.public_key_share(),
                    key_index,
                    peer_replicas.clone(),
                    wallet.wallet.clone(),
                    pending_proposals,
                    None,
                );
                let _ = wallet_replicas.insert(wallet_id.public_key(), wallet_replica);
                elders.push(Elder {
                    id: secret_key.public_key_share(),
                    replicas: wallet_replicas,
                    signing: ReplicaSigning::new(
                        secret_key.clone(),
                        key_index,
                        peer_replicas.clone(),
                    ),
                });
            }
        }
        Ok((
            (genesis_credit, empty_genesis_wallet),
            Section {
                index: 0,
                id: peer_replicas,
                elders,
            },
        ))
    }

    // ------------------------------------------------------------------------
    // ------------------------ Structs ---------------------------------------
    // ------------------------------------------------------------------------
}