forked from anoma/namada-trusted-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.rs
969 lines (836 loc) · 31.5 KB
/
environment.rs
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
use crate::{authentication::KeyPair, objects::Participant, storage::Disk};
pub use phase2::{helpers::CurveKind, ContributionMode, ProvingSystem};
use setup_utils::{CheckForCorrectness, UseCompression};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
type BatchSize = usize;
type ChunkSize = usize;
type NumberOfChunks = usize;
type Power = usize;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
pub contribution_mode: ContributionMode,
pub proving_system: ProvingSystem,
pub curve: CurveKind,
pub power: Power,
pub batch_size: BatchSize,
pub chunk_size: ChunkSize,
}
impl Settings {
/// Creates a new `Settings`
///
/// + `batch_size` - will panic if this is set to `0`.
pub fn new(
contribution_mode: ContributionMode,
proving_system: ProvingSystem,
curve: CurveKind,
power: Power,
batch_size: BatchSize,
chunk_size: ChunkSize,
) -> Self {
if batch_size == 0 {
panic!("batch_size cannot be equal to zero");
}
Self {
contribution_mode,
proving_system,
curve,
power,
batch_size,
chunk_size,
}
}
pub fn contribution_mode(&self) -> ContributionMode {
self.contribution_mode
}
pub fn proving_system(&self) -> ProvingSystem {
self.proving_system
}
pub fn curve(&self) -> CurveKind {
self.curve
}
pub fn power(&self) -> Power {
self.power
}
pub fn batch_size(&self) -> BatchSize {
self.batch_size
}
pub fn chunk_size(&self) -> ChunkSize {
self.chunk_size
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Deployment {
Testing,
Development,
Production,
}
#[derive(Debug, Clone)]
pub enum Parameters {
AleoInner,
AleoOuter,
AleoUniversal,
Custom(Settings),
Test3Chunks,
Test8Chunks,
TestChunks {
number_of_chunks: usize,
},
TestCustom {
number_of_chunks: usize,
power: usize,
batch_size: usize,
},
Namada {
number_of_chunks: usize,
power: usize,
batch_size: usize,
},
}
impl Parameters {
/// Returns the corresponding settings for each parameter type.
fn to_settings(&self) -> Settings {
match self {
Parameters::AleoInner => Self::aleo_inner(),
Parameters::AleoOuter => Self::aleo_outer(),
Parameters::AleoUniversal => Self::aleo_universal(),
Parameters::Custom(settings) => settings.clone(),
Parameters::Test3Chunks => Self::test_3_chunks(),
Parameters::Test8Chunks => Self::test_8_chunks(),
Parameters::TestChunks { number_of_chunks } => Self::test_chunks(number_of_chunks),
Parameters::TestCustom {
number_of_chunks,
power,
batch_size,
} => Self::test_custom(number_of_chunks, power, batch_size),
Parameters::Namada {
number_of_chunks,
power,
batch_size,
} => Self::namada(number_of_chunks, power, batch_size),
}
}
fn aleo_inner() -> Settings {
Settings::new(
ContributionMode::Chunked,
ProvingSystem::Groth16,
CurveKind::Bls12_377,
Power::from(19_usize),
BatchSize::from(2097152_usize),
ChunkSize::from(32768_usize),
)
}
fn aleo_outer() -> Settings {
Settings::new(
ContributionMode::Chunked,
ProvingSystem::Groth16,
CurveKind::BW6,
Power::from(20_usize),
BatchSize::from(2097152_usize),
ChunkSize::from(65536_usize),
)
}
fn aleo_universal() -> Settings {
Settings::new(
ContributionMode::Chunked,
ProvingSystem::Marlin,
CurveKind::Bls12_377,
Power::from(28_usize),
BatchSize::from(2097152_usize),
ChunkSize::from(65536_usize),
)
}
fn test_3_chunks() -> Settings {
Settings::new(
ContributionMode::Chunked,
ProvingSystem::Groth16,
CurveKind::Bls12_377,
Power::from(8_usize),
BatchSize::from(64_usize),
ChunkSize::from(172_usize),
)
}
fn test_8_chunks() -> Settings {
Settings::new(
ContributionMode::Chunked,
ProvingSystem::Groth16,
CurveKind::Bls12_377,
Power::from(14_usize),
BatchSize::from(64_usize),
ChunkSize::from(4096_usize),
)
}
fn test_chunks(number_of_chunks: &NumberOfChunks) -> Settings {
let proving_system = ProvingSystem::Groth16;
let power = 14_usize;
let batch_size = 128_usize;
Settings::new(
ContributionMode::Chunked,
proving_system,
CurveKind::Bls12_377,
Power::from(power),
BatchSize::from(batch_size),
chunk_size!(number_of_chunks, proving_system, power),
)
}
fn test_custom(number_of_chunks: &NumberOfChunks, power: &Power, batch_size: &BatchSize) -> Settings {
let proving_system = ProvingSystem::Groth16;
Settings::new(
ContributionMode::Full,
proving_system,
CurveKind::Bls12_381,
*power,
*batch_size,
chunk_size!(number_of_chunks, proving_system, power),
)
}
fn namada(number_of_chunks: &NumberOfChunks, power: &Power, batch_size: &BatchSize) -> Settings {
let proving_system = ProvingSystem::Groth16;
Settings::new(
ContributionMode::Full,
proving_system,
CurveKind::Bls12_381,
*power,
*batch_size,
chunk_size!(number_of_chunks, proving_system, power),
)
}
}
#[serde_with::serde_as]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Environment {
/// The parameter settings of this coordinator.
parameters: Settings,
/// The compressed input setting of the coordinator.
compressed_inputs: UseCompression,
/// The compressed output setting of the coordinator.
compressed_outputs: UseCompression,
/// The input correctness check preference of the coordinator.
check_input_for_correctness: CheckForCorrectness,
/// The minimum number of contributors permitted to participate in a round.
minimum_contributors_per_round: usize,
/// The maximum number of contributors permitted to participate in a round.
maximum_contributors_per_round: usize,
/// The minimum number of verifiers permitted to participate in a round.
minimum_verifiers_per_round: usize,
/// The maximum number of verifiers permitted to participate in a round.
maximum_verifiers_per_round: usize,
/// The number of chunks a contributor is authorized to lock in tandem in a round.
contributor_lock_chunk_limit: usize,
/// The number of chunks a verifier is authorized to lock in tandem in a round.
verifier_lock_chunk_limit: usize,
/// Returns the maximum duration a contributor can go without
/// being seen by the coordinator before it will be dropped from
/// the ceremony by the coordinator.
contributor_seen_timeout: time::Duration,
/// The maximum duration a verifier can go without being seen by
/// the coordinator before it will be dropped from the ceremony by
/// the coordinator.
verifier_seen_timeout: time::Duration,
/// The maximum duration a lock can be held by a participant
/// before it will be dropped from the ceremony by the
/// coordinator.
participant_lock_timeout: time::Duration,
/// The maximum duration a queued contributor can go without a heartbeat.
queue_seen_timeout: time::Duration,
/// The number of drops tolerated by a participant before banning them from future rounds.
participant_ban_threshold: u16,
/// The setting to allow current contributors to join the queue for the next round.
allow_current_contributors_in_queue: bool,
/// The setting to allow current verifiers to join the queue for the next round.
allow_current_verifiers_in_queue: bool,
/// The minimum number of seconds to wait after aggregation before starting the next round.
queue_wait_time: u64,
/// The contributors managed by the coordinator.
coordinator_contributors: Vec<Participant>,
/// The verifiers managed by the coordinator.
coordinator_verifiers: Vec<Participant>,
/// The signing key used by the default coordinator's verifier
default_verifier_signing_key: String,
/// The software version number of the coordinator.
software_version: u64,
/// The deployment environment of this coordinator.
deployment: Deployment,
/// The base directory for disk storage of this coordinator.
local_base_directory: String,
disable_reliability_zeroing: bool,
}
impl Environment {
///
/// Returns the parameter settings of the coordinator.
///
pub fn parameters(&self) -> Settings {
self.parameters.clone()
}
///
/// Returns the compressed input setting of the coordinator.
///
/// The default choice should be `UseCompression::No` to minimize time
/// spent by contributors on decompressing inputs.
///
pub const fn compressed_inputs(&self) -> UseCompression {
self.compressed_inputs
}
///
/// Returns the compressed output setting of the coordinator.
///
/// The default choice should be `UseCompression::Yes` to minimize time
/// spent by the coordinator and contributors on uploading chunks.
///
pub const fn compressed_outputs(&self) -> UseCompression {
self.compressed_outputs
}
///
/// Returns the input correctness check preference of the coordinator.
///
/// The default choice should be `CheckForCorrectness::No` to minimize time
/// spent by the contributors on reading chunks.
///
pub fn check_input_for_correctness(&self) -> CheckForCorrectness {
self.check_input_for_correctness
}
///
/// Returns the minimum number of contributors permitted to
/// participate in a round.
///
pub const fn minimum_contributors_per_round(&self) -> usize {
self.minimum_contributors_per_round
}
///
/// Returns the maximum number of contributors permitted to
/// participate in a round.
///
pub const fn maximum_contributors_per_round(&self) -> usize {
self.maximum_contributors_per_round
}
///
/// Returns the number of chunks a contributor is
/// authorized to lock in tandem at any point during a round.
///
pub const fn contributor_lock_chunk_limit(&self) -> usize {
self.contributor_lock_chunk_limit
}
///
/// Returns the number of chunks a verifier is
/// authorized to lock in tandem at any point during a round.
///
pub const fn verifier_lock_chunk_limit(&self) -> usize {
self.verifier_lock_chunk_limit
}
///
/// Returns the maximum duration a contributor can go without
/// being seen by the coordinator before it will be dropped from
/// the ceremony by the coordinator.
///
pub const fn contributor_seen_timeout(&self) -> time::Duration {
self.contributor_seen_timeout
}
///
/// Returns the maximum duration a verifier can go without being
/// seen by the coordinator before it will be dropped from the
/// ceremony by the coordinator.
///
pub const fn verifier_seen_timeout(&self) -> time::Duration {
self.verifier_seen_timeout
}
///
/// Returns the maximum duration that a participant can hold a
/// lock before being dropped from the ceremony by the
/// coordinator.
///
pub const fn participant_lock_timeout(&self) -> time::Duration {
self.participant_lock_timeout
}
///
/// Returns the maximum duration that a queued contributor can go
/// without a heartbeat.
///
pub const fn queue_seen_timeout(&self) -> time::Duration {
self.queue_seen_timeout
}
///
/// Returns the number of times the coordinator tolerates
/// a dropped participant before banning them from future rounds.
///
pub const fn participant_ban_threshold(&self) -> u16 {
self.participant_ban_threshold
}
///
/// Returns the setting to allow current contributors to
/// join the queue for the next round.
///
pub const fn allow_current_contributors_in_queue(&self) -> bool {
self.allow_current_contributors_in_queue
}
///
/// Returns the setting to allow current verifiers to
/// join the queue for the next round.
///
pub const fn allow_current_verifiers_in_queue(&self) -> bool {
self.allow_current_verifiers_in_queue
}
///
/// Returns the minimum number of seconds to wait after aggregation
/// before starting the next round.
///
pub const fn queue_wait_time(&self) -> u64 {
self.queue_wait_time
}
///
/// Returns the contributors managed by the coordinator.
///
/// The primary purpose of this is to establish an identity for the coordinator
/// when running initialization of each round.
///
/// This can also be purposed for completing contributions of participants
/// who may have dropped off and handed over control of their session.
///
pub const fn coordinator_contributors(&self) -> &Vec<Participant> {
&self.coordinator_contributors
}
/// Returns the verifiers managed by the coordinator.
pub const fn coordinator_verifiers(&self) -> &Vec<Participant> {
&self.coordinator_verifiers
}
/// Returns the default verifiers' signing key.
pub fn default_verifier_signing_key(&self) -> String {
self.default_verifier_signing_key.clone()
}
///
/// Returns the software version number of the coordinator.
///
pub const fn software_version(&self) -> u64 {
self.software_version
}
///
/// Returns the deployment environment of the coordinator.
///
pub const fn deployment(&self) -> &Deployment {
&self.deployment
}
///
/// Returns the base directory for disk storage of this coordinator.
///
pub fn local_base_directory(&self) -> &str {
&self.local_base_directory
}
///
/// Returns the appropriate number of chunks for the coordinator
/// to run given a proof system, power and chunk size.
///
pub fn number_of_chunks(&self) -> u64 {
// let proving_system = &self.parameters.proving_system;
// let power = self.parameters.power;
// let chunk_size = self.parameters.chunk_size;
// (total_size_in_g1!(proving_system, power) + chunk_size as u64 - 1) / chunk_size as u64
1
}
/// Returns the storage system of the coordinator.
pub(crate) fn storage(&self) -> anyhow::Result<Disk> {
Ok(Disk::load(self)?)
}
}
impl From<Testing> for Environment {
fn from(deployment: Testing) -> Environment {
deployment.environment
}
}
impl From<Development> for Environment {
fn from(deployment: Development) -> Environment {
deployment.environment
}
}
impl From<Production> for Environment {
fn from(deployment: Production) -> Environment {
deployment.environment
}
}
// TODO (howardwu): Convert the implementation to a procedural macro.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Testing {
pub environment: Environment,
}
impl Testing {
pub fn minimum_contributors_per_round(mut self, minimum: usize) -> Self {
self.environment.minimum_contributors_per_round = minimum;
self
}
pub fn maximum_contributors_per_round(mut self, maximum: usize) -> Self {
self.environment.maximum_contributors_per_round = maximum;
self
}
pub fn disable_reliability_zeroing(mut self, disable_zeroing: bool) -> Self {
self.environment.disable_reliability_zeroing = disable_zeroing;
self
}
#[inline]
pub fn coordinator_contributors(&self, contributors: &[Participant]) -> Self {
// Check that all participants are contributors.
if contributors.into_par_iter().filter(|p| !p.is_contributor()).count() > 0 {
panic!("Specifying to environment a list of coordinator contributors with non-contributors.")
}
let mut deployment = self.clone();
deployment.environment.coordinator_contributors = contributors.to_vec();
deployment
}
#[inline]
pub fn coordinator_verifiers(&self, verifiers: &[Participant]) -> Self {
// Check that all participants are verifiers.
if verifiers.into_par_iter().filter(|p| !p.is_verifier()).count() > 0 {
panic!("Specifying to environment a list of coordinator verifiers with non-verifiers.")
}
let mut deployment = self.clone();
deployment.environment.coordinator_verifiers = verifiers.to_vec();
deployment
}
pub fn contributor_seen_timeout(&self, contributor_timeout: time::Duration) -> Self {
let mut deployment = self.clone();
deployment.environment.contributor_seen_timeout = contributor_timeout;
deployment
}
pub fn participant_lock_timeout(&self, participant_lock_timeout: time::Duration) -> Self {
let mut deployment = self.clone();
deployment.environment.participant_lock_timeout = participant_lock_timeout;
deployment
}
pub fn queue_seen_timeout(&self, queue_seen_timeout: time::Duration) -> Self {
let mut deployment = self.clone();
deployment.environment.queue_seen_timeout = queue_seen_timeout;
deployment
}
fn generate_namada_env(keypair: &KeyPair) -> Self {
Self {
environment: Environment {
parameters: Parameters::Namada {
number_of_chunks: 1,
power: 6,
batch_size: 16,
}
.to_settings(),
compressed_inputs: UseCompression::No,
compressed_outputs: UseCompression::Yes,
check_input_for_correctness: CheckForCorrectness::No,
minimum_contributors_per_round: 1,
maximum_contributors_per_round: 1,
minimum_verifiers_per_round: 1,
maximum_verifiers_per_round: 5,
contributor_lock_chunk_limit: 1,
verifier_lock_chunk_limit: 5,
contributor_seen_timeout: time::Duration::minutes(5),
verifier_seen_timeout: time::Duration::minutes(15),
participant_lock_timeout: time::Duration::minutes(20),
queue_seen_timeout: time::Duration::minutes(5),
participant_ban_threshold: 5,
allow_current_contributors_in_queue: true,
allow_current_verifiers_in_queue: true,
queue_wait_time: 0,
coordinator_contributors: vec![Participant::new_contributor("testing-coordinator-contributor")],
coordinator_verifiers: vec![Participant::new_verifier(keypair.pubkey())],
default_verifier_signing_key: keypair.sigkey().to_owned(),
software_version: 1,
deployment: Deployment::Testing,
local_base_directory: "./transcript/testing".to_string(),
disable_reliability_zeroing: false,
},
}
}
/// Generate a new Testing env with [`Parameters::Namada`] parameters
pub fn new(keypair: &KeyPair) -> Self {
Self::generate_namada_env(keypair)
}
}
impl From<Parameters> for Testing {
fn from(parameters: Parameters) -> Self {
let mut testing = Self::default();
testing.environment.parameters = parameters.to_settings();
testing
}
}
impl std::ops::Deref for Testing {
type Target = Environment;
fn deref(&self) -> &Self::Target {
&self.environment
}
}
impl std::default::Default for Testing {
fn default() -> Self {
let keypair = KeyPair::new();
Self::generate_namada_env(&keypair)
}
}
// TODO (howardwu): Convert the implementation to a procedural macro.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Development {
environment: Environment,
}
impl Development {
pub fn minimum_contributors_per_round(mut self, minimum: usize) -> Self {
self.environment.minimum_contributors_per_round = minimum;
self
}
pub fn maximum_contributors_per_round(mut self, maximum: usize) -> Self {
self.environment.maximum_contributors_per_round = maximum;
self
}
pub fn contributor_seen_timeout(mut self, timeout: time::Duration) -> Self {
self.environment.contributor_seen_timeout = timeout;
self
}
pub fn participant_lock_timeout(mut self, timeout: time::Duration) -> Self {
self.environment.participant_lock_timeout = timeout;
self
}
pub fn disable_reliability_zeroing(mut self, disable_zeroing: bool) -> Self {
self.environment.disable_reliability_zeroing = disable_zeroing;
self
}
#[inline]
pub fn coordinator_contributors(&self, contributors: &[Participant]) -> Self {
// Check that all participants are contributors.
if contributors.into_par_iter().filter(|p| !p.is_contributor()).count() > 0 {
panic!("Specifying to environment a list of coordinator contributors with non-contributors.")
}
let mut deployment = self.clone();
deployment.environment.coordinator_contributors = contributors.to_vec();
deployment
}
#[inline]
pub fn coordinator_verifiers(&self, verifiers: &[Participant]) -> Self {
// Check that all participants are verifiers.
if verifiers.into_par_iter().filter(|p| !p.is_verifier()).count() > 0 {
panic!("Specifying to environment a list of coordinator verifiers with non-verifiers.")
}
let mut deployment = self.clone();
deployment.environment.coordinator_verifiers = verifiers.to_vec();
deployment
}
fn generate_namada_env(keypair: &KeyPair) -> Self {
Self {
environment: Environment {
parameters: Parameters::Namada {
number_of_chunks: 1,
power: 6,
batch_size: 16,
}
.to_settings(),
compressed_inputs: UseCompression::No,
compressed_outputs: UseCompression::Yes,
check_input_for_correctness: CheckForCorrectness::No,
minimum_contributors_per_round: 1,
maximum_contributors_per_round: 1,
minimum_verifiers_per_round: 1,
maximum_verifiers_per_round: 5,
contributor_lock_chunk_limit: 1,
verifier_lock_chunk_limit: 5,
contributor_seen_timeout: time::Duration::minutes(1),
verifier_seen_timeout: time::Duration::minutes(15),
participant_lock_timeout: time::Duration::minutes(20),
queue_seen_timeout: time::Duration::minutes(10),
participant_ban_threshold: 5,
allow_current_contributors_in_queue: true,
allow_current_verifiers_in_queue: true,
queue_wait_time: 60,
coordinator_contributors: vec![Participant::new_contributor("development-coordinator-contributor")],
coordinator_verifiers: vec![Participant::new_verifier(keypair.pubkey())],
default_verifier_signing_key: keypair.sigkey().to_owned(),
software_version: 1,
deployment: Deployment::Development,
local_base_directory: "./transcript/development".to_string(),
disable_reliability_zeroing: false,
},
}
}
/// Generate a new Development env with [`Parameters::Namada`] parameters
pub fn new(keypair: &KeyPair) -> Self {
Self::generate_namada_env(keypair)
}
}
impl From<Parameters> for Development {
fn from(parameters: Parameters) -> Self {
let mut testing = Self::default();
testing.environment.parameters = parameters.to_settings();
testing
}
}
impl std::ops::Deref for Development {
type Target = Environment;
fn deref(&self) -> &Self::Target {
&self.environment
}
}
impl std::ops::DerefMut for Development {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.environment
}
}
impl std::default::Default for Development {
fn default() -> Self {
let keypair = KeyPair::new();
Self::generate_namada_env(&keypair)
}
}
// TODO (howardwu): Convert the implementation to a procedural macro.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Production {
environment: Environment,
}
impl Production {
pub fn minimum_contributors_per_round(mut self, minimum: usize) -> Self {
self.environment.minimum_contributors_per_round = minimum;
self
}
pub fn maximum_contributors_per_round(mut self, maximum: usize) -> Self {
self.environment.maximum_contributors_per_round = maximum;
self
}
pub fn contributor_seen_timeout(mut self, timeout: time::Duration) -> Self {
self.environment.contributor_seen_timeout = timeout;
self
}
pub fn participant_lock_timeout(mut self, timeout: time::Duration) -> Self {
self.environment.participant_lock_timeout = timeout;
self
}
pub fn queue_seen_timeout(mut self, timeout: time::Duration) -> Self {
self.environment.queue_seen_timeout = timeout;
self
}
pub fn disable_reliability_zeroing(mut self, disable_zeroing: bool) -> Self {
self.environment.disable_reliability_zeroing = disable_zeroing;
self
}
#[inline]
pub fn coordinator_contributors(&self, contributors: &[Participant]) -> Self {
// Check that all participants are contributors.
if contributors.into_par_iter().filter(|p| !p.is_contributor()).count() > 0 {
panic!("Specifying to environment a list of coordinator contributors with non-contributors.")
}
let mut deployment = self.clone();
deployment.environment.coordinator_contributors = contributors.to_vec();
deployment
}
#[inline]
pub fn coordinator_verifiers(&self, verifiers: &[Participant]) -> Self {
// Check that all participants are verifiers.
if verifiers.into_par_iter().filter(|p| !p.is_verifier()).count() > 0 {
panic!("Specifying to environment a list of coordinator verifiers with non-verifiers.")
}
let mut deployment = self.clone();
deployment.environment.coordinator_verifiers = verifiers.to_vec();
deployment
}
fn generate_namada_env(keypair: &KeyPair) -> Self {
let test_timeout = match std::env::var("NAMADA_MPC_TIMEOUT_SECONDS") {
Ok(t) => Some(time::Duration::seconds(t.parse::<i64>().unwrap())),
Err(_) => None,
};
Self {
environment: Environment {
parameters: Parameters::Namada {
number_of_chunks: 1,
power: 6,
batch_size: 16,
}
.to_settings(),
compressed_inputs: UseCompression::No,
compressed_outputs: UseCompression::Yes,
check_input_for_correctness: CheckForCorrectness::No,
minimum_contributors_per_round: 1,
maximum_contributors_per_round: 1,
minimum_verifiers_per_round: 1,
maximum_verifiers_per_round: 5,
contributor_lock_chunk_limit: 1,
verifier_lock_chunk_limit: 5,
contributor_seen_timeout: test_timeout.unwrap_or_else(|| time::Duration::seconds(150)),
verifier_seen_timeout: test_timeout.unwrap_or_else(|| time::Duration::days(7)),
participant_lock_timeout: test_timeout.unwrap_or_else(|| time::Duration::minutes(20)),
queue_seen_timeout: test_timeout.unwrap_or_else(|| time::Duration::seconds(150)),
participant_ban_threshold: 5,
allow_current_contributors_in_queue: false,
allow_current_verifiers_in_queue: true,
queue_wait_time: 5,
coordinator_contributors: vec![Participant::new_contributor("coordinator-contributor")],
coordinator_verifiers: vec![Participant::new_verifier(keypair.pubkey())],
default_verifier_signing_key: keypair.sigkey().to_owned(),
software_version: 1,
deployment: Deployment::Production,
local_base_directory: "./transcript".to_string(),
disable_reliability_zeroing: false,
},
}
}
/// Generate a new Production env with [`Parameters::Namada`] parameters
pub fn new(keypair: &KeyPair) -> Self {
Self::generate_namada_env(keypair)
}
}
impl From<Parameters> for Production {
fn from(parameters: Parameters) -> Self {
let mut testing = Self::default();
testing.environment.parameters = parameters.to_settings();
testing
}
}
impl std::ops::Deref for Production {
type Target = Environment;
fn deref(&self) -> &Self::Target {
&self.environment
}
}
impl std::default::Default for Production {
fn default() -> Self {
let keypair = KeyPair::new();
Self::generate_namada_env(&keypair)
}
}
#[cfg(test)]
mod tests {
use crate::environment::*;
#[test]
fn test_aleo_test_3_chunks() {
let parameters = Parameters::Test3Chunks;
let power = parameters.to_settings().power;
assert_eq!(Power::from(8_usize), power);
assert_eq!(3, Testing::from(parameters).number_of_chunks());
}
#[test]
fn test_aleo_test_8_chunks() {
let parameters = Parameters::Test8Chunks;
let power = parameters.to_settings().power;
assert_eq!(Power::from(14_usize), power);
assert_eq!(8, Testing::from(parameters).number_of_chunks());
}
#[test]
fn test_custom_chunk_3() {
let number_of_chunks = 3;
let parameters = Parameters::TestChunks { number_of_chunks };
let settings = parameters.to_settings();
let power = settings.power;
let chunk_size = settings.chunk_size;
assert_eq!(Power::from(14_usize), power);
assert_eq!(ChunkSize::from(10923_usize), chunk_size);
assert_eq!(number_of_chunks as u64, Testing::from(parameters).number_of_chunks());
}
#[test]
fn test_custom_chunk_8() {
let number_of_chunks = 8;
let parameters = Parameters::TestChunks { number_of_chunks };
let settings = parameters.to_settings();
let power = settings.power;
let chunk_size = settings.chunk_size;
assert_eq!(Power::from(14_usize), power);
assert_eq!(ChunkSize::from(4096_usize), chunk_size);
assert_eq!(number_of_chunks as u64, Testing::from(parameters).number_of_chunks());
}
#[test]
fn test_custom_chunk_20() {
let number_of_chunks = 20;
let parameters = Parameters::TestChunks { number_of_chunks };
let settings = parameters.to_settings();
let power = settings.power;
let chunk_size = settings.chunk_size;
assert_eq!(Power::from(14_usize), power);
assert_eq!(ChunkSize::from(1639_usize), chunk_size);
assert_eq!(number_of_chunks as u64, Testing::from(parameters).number_of_chunks());
}
}