1
- use std:: collections:: HashMap ;
2
- use std:: sync:: Arc ;
3
-
4
1
use async_trait:: async_trait;
5
2
use chrono:: prelude:: * ;
6
3
use hex:: ToHex ;
7
4
use slog_scope:: { debug, trace, warn} ;
5
+ use std:: { collections:: HashMap , sync:: Arc } ;
8
6
use thiserror:: Error ;
9
7
10
- use mithril_common:: crypto_helper:: {
11
- key_decode_hex, key_encode_hex, ProtocolAggregateVerificationKey , ProtocolAggregationError ,
12
- ProtocolClerk , ProtocolKeyRegistration , ProtocolMultiSignature , ProtocolParameters ,
13
- ProtocolPartyId , ProtocolRegistrationError , ProtocolSignerVerificationKey ,
14
- ProtocolSingleSignature , ProtocolStakeDistribution ,
15
- } ;
16
- use mithril_common:: entities:: { self , SignerWithStake } ;
17
- use mithril_common:: store:: { StakeStore , StakeStorer , StoreError } ;
18
8
use mithril_common:: {
19
- NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET , SIGNER_EPOCH_RECORDING_OFFSET ,
20
- SIGNER_EPOCH_RETRIEVAL_OFFSET ,
9
+ crypto_helper:: {
10
+ key_decode_hex, key_encode_hex, ProtocolAggregateVerificationKey , ProtocolAggregationError ,
11
+ ProtocolClerk , ProtocolKeyRegistration , ProtocolMultiSignature , ProtocolParameters ,
12
+ ProtocolPartyId , ProtocolRegistrationError , ProtocolSignerVerificationKey ,
13
+ ProtocolSingleSignature , ProtocolStakeDistribution ,
14
+ } ,
15
+ entities:: { self , Epoch , SignerWithStake } ,
16
+ store:: { StakeStore , StakeStorer , StoreError } ,
21
17
} ;
22
18
23
- use crate :: store:: { SingleSignatureStorer , VerificationKeyStorer } ;
24
19
use crate :: {
20
+ store:: { SingleSignatureStorer , VerificationKeyStorer } ,
25
21
ProtocolParametersStore , ProtocolParametersStorer , SingleSignatureStore , VerificationKeyStore ,
26
22
} ;
27
23
@@ -191,13 +187,10 @@ pub trait MultiSigner: Sync + Send {
191
187
}
192
188
193
189
/// Get signers with stake
194
- async fn get_signers_with_stake ( & self )
195
- -> Result < Vec < entities:: SignerWithStake > , ProtocolError > ;
190
+ async fn get_signers_with_stake ( & self ) -> Result < Vec < SignerWithStake > , ProtocolError > ;
196
191
197
192
/// Get signers for the next epoch with their stake
198
- async fn get_next_signers_with_stake (
199
- & self ,
200
- ) -> Result < Vec < entities:: SignerWithStake > , ProtocolError > ;
193
+ async fn get_next_signers_with_stake ( & self ) -> Result < Vec < SignerWithStake > , ProtocolError > ;
201
194
202
195
/// Registers a single signature
203
196
async fn register_single_signature (
@@ -322,18 +315,12 @@ impl MultiSignerImpl {
322
315
}
323
316
}
324
317
325
- /// Get stake distribution with epoch offset
326
- async fn get_stake_distribution_with_epoch_offset (
318
+ /// Get the [ stake distribution][ProtocolStakeDistribution] for the given `epoch`
319
+ async fn get_stake_distribution_at_epoch (
327
320
& self ,
328
- epoch_offset : i64 ,
321
+ epoch : Epoch ,
329
322
) -> Result < ProtocolStakeDistribution , ProtocolError > {
330
- debug ! ( "Get stake distribution with epoch offset" ; "epoch_offset" =>epoch_offset) ;
331
- let epoch = self
332
- . current_beacon
333
- . as_ref ( )
334
- . ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
335
- . epoch
336
- . offset_by ( epoch_offset) ?;
323
+ debug ! ( "Get stake distribution at epoch" ; "epoch" => #?epoch) ;
337
324
338
325
let stakes = self
339
326
. stake_store
@@ -343,18 +330,12 @@ impl MultiSignerImpl {
343
330
Ok ( stakes. into_iter ( ) . collect :: < ProtocolStakeDistribution > ( ) )
344
331
}
345
332
346
- /// Get protocol parameters with epoch offset
347
- async fn get_protocol_parameters_with_epoch_offset (
333
+ /// Get the [ protocol parameters][ProtocolParameters] for the given `epoch`
334
+ async fn get_protocol_parameters_at_epoch (
348
335
& self ,
349
- epoch_offset : i64 ,
336
+ epoch : Epoch ,
350
337
) -> Result < Option < ProtocolParameters > , ProtocolError > {
351
- debug ! ( "Get protocol parameters with epoch offset" ; "epoch_offset" =>epoch_offset) ;
352
- let epoch = self
353
- . current_beacon
354
- . as_ref ( )
355
- . ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
356
- . epoch
357
- . offset_by ( epoch_offset) ?;
338
+ debug ! ( "Get protocol parameters at epoch" ; "epoch" => #?epoch) ;
358
339
359
340
match self
360
341
. protocol_parameters_store
@@ -431,8 +412,13 @@ impl MultiSigner for MultiSignerImpl {
431
412
/// Get protocol parameters
432
413
async fn get_protocol_parameters ( & self ) -> Result < Option < ProtocolParameters > , ProtocolError > {
433
414
debug ! ( "Get protocol parameters" ) ;
434
- self . get_protocol_parameters_with_epoch_offset ( SIGNER_EPOCH_RETRIEVAL_OFFSET )
435
- . await
415
+ let epoch = self
416
+ . current_beacon
417
+ . as_ref ( )
418
+ . ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
419
+ . epoch
420
+ . offset_to_signer_retrieval_epoch ( ) ?;
421
+ self . get_protocol_parameters_at_epoch ( epoch) . await
436
422
}
437
423
438
424
/// Update protocol parameters
@@ -446,7 +432,7 @@ impl MultiSigner for MultiSignerImpl {
446
432
. as_ref ( )
447
433
. ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
448
434
. epoch
449
- . offset_by ( SIGNER_EPOCH_RECORDING_OFFSET ) ? ;
435
+ . offset_to_recording_epoch ( ) ;
450
436
451
437
self . protocol_parameters_store
452
438
. save_protocol_parameters ( epoch, protocol_parameters. to_owned ( ) . into ( ) )
@@ -459,24 +445,39 @@ impl MultiSigner for MultiSignerImpl {
459
445
& self ,
460
446
) -> Result < Option < ProtocolParameters > , ProtocolError > {
461
447
debug ! ( "Get next protocol parameters" ) ;
462
- self . get_protocol_parameters_with_epoch_offset ( NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET )
463
- . await
448
+ let epoch = self
449
+ . current_beacon
450
+ . as_ref ( )
451
+ . ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
452
+ . epoch
453
+ . offset_to_next_signer_retrieval_epoch ( ) ;
454
+ self . get_protocol_parameters_at_epoch ( epoch) . await
464
455
}
465
456
466
457
/// Get stake distribution
467
458
async fn get_stake_distribution ( & self ) -> Result < ProtocolStakeDistribution , ProtocolError > {
468
459
debug ! ( "Get stake distribution" ) ;
469
- self . get_stake_distribution_with_epoch_offset ( SIGNER_EPOCH_RETRIEVAL_OFFSET )
470
- . await
460
+ let epoch = self
461
+ . current_beacon
462
+ . as_ref ( )
463
+ . ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
464
+ . epoch
465
+ . offset_to_signer_retrieval_epoch ( ) ?;
466
+ self . get_stake_distribution_at_epoch ( epoch) . await
471
467
}
472
468
473
469
/// Get next stake distribution
474
470
async fn get_next_stake_distribution (
475
471
& self ,
476
472
) -> Result < ProtocolStakeDistribution , ProtocolError > {
477
473
debug ! ( "Get next stake distribution" ) ;
478
- self . get_stake_distribution_with_epoch_offset ( NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET )
479
- . await
474
+ let epoch = self
475
+ . current_beacon
476
+ . as_ref ( )
477
+ . ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
478
+ . epoch
479
+ . offset_to_next_signer_retrieval_epoch ( ) ;
480
+ self . get_stake_distribution_at_epoch ( epoch) . await
480
481
}
481
482
482
483
/// Update stake distribution
@@ -490,7 +491,7 @@ impl MultiSigner for MultiSignerImpl {
490
491
. as_ref ( )
491
492
. ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
492
493
. epoch
493
- . offset_by ( SIGNER_EPOCH_RECORDING_OFFSET ) ? ;
494
+ . offset_to_recording_epoch ( ) ;
494
495
let stakes = HashMap :: from_iter ( stakes. iter ( ) . cloned ( ) ) ;
495
496
self . stake_store . save_stakes ( epoch, stakes) . await ?;
496
497
@@ -523,7 +524,7 @@ impl MultiSigner for MultiSignerImpl {
523
524
. as_ref ( )
524
525
. ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
525
526
. epoch
526
- . offset_by ( SIGNER_EPOCH_RETRIEVAL_OFFSET ) ?;
527
+ . offset_to_signer_retrieval_epoch ( ) ?;
527
528
let signers = self
528
529
. verification_key_store
529
530
. get_verification_keys ( epoch)
@@ -537,16 +538,14 @@ impl MultiSigner for MultiSignerImpl {
537
538
}
538
539
}
539
540
540
- async fn get_signers_with_stake (
541
- & self ,
542
- ) -> Result < Vec < entities:: SignerWithStake > , ProtocolError > {
541
+ async fn get_signers_with_stake ( & self ) -> Result < Vec < SignerWithStake > , ProtocolError > {
543
542
debug ! ( "Get signers with stake" ) ;
544
543
let epoch = self
545
544
. current_beacon
546
545
. as_ref ( )
547
546
. ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
548
547
. epoch
549
- . offset_by ( SIGNER_EPOCH_RETRIEVAL_OFFSET ) ?;
548
+ . offset_to_signer_retrieval_epoch ( ) ?;
550
549
let signers = self
551
550
. verification_key_store
552
551
. get_verification_keys ( epoch)
@@ -558,7 +557,7 @@ impl MultiSigner for MultiSignerImpl {
558
557
. iter ( )
559
558
. filter_map ( |( party_id, stake) | {
560
559
signers. get ( party_id) . map ( |signer| {
561
- entities :: SignerWithStake :: new (
560
+ SignerWithStake :: new (
562
561
party_id. to_owned ( ) ,
563
562
signer. verification_key . to_owned ( ) ,
564
563
signer. verification_key_signature . to_owned ( ) ,
@@ -571,16 +570,14 @@ impl MultiSigner for MultiSignerImpl {
571
570
. collect ( ) )
572
571
}
573
572
574
- async fn get_next_signers_with_stake (
575
- & self ,
576
- ) -> Result < Vec < entities:: SignerWithStake > , ProtocolError > {
573
+ async fn get_next_signers_with_stake ( & self ) -> Result < Vec < SignerWithStake > , ProtocolError > {
577
574
debug ! ( "Get next signers with stake" ) ;
578
575
let epoch = self
579
576
. current_beacon
580
577
. as_ref ( )
581
578
. ok_or_else ( ProtocolError :: UnavailableBeacon ) ?
582
579
. epoch
583
- . offset_by ( NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET ) ? ;
580
+ . offset_to_next_signer_retrieval_epoch ( ) ;
584
581
let signers = self
585
582
. verification_key_store
586
583
. get_verification_keys ( epoch)
@@ -592,7 +589,7 @@ impl MultiSigner for MultiSignerImpl {
592
589
. iter ( )
593
590
. filter_map ( |( party_id, stake) | {
594
591
signers. get ( party_id) . map ( |signer| {
595
- entities :: SignerWithStake :: new (
592
+ SignerWithStake :: new (
596
593
party_id. to_owned ( ) ,
597
594
signer. verification_key . to_owned ( ) ,
598
595
signer. verification_key_signature . to_owned ( ) ,
@@ -724,31 +721,30 @@ impl MultiSigner for MultiSignerImpl {
724
721
#[ cfg( test) ]
725
722
mod tests {
726
723
use super :: * ;
727
- use crate :: store :: { SingleSignatureStore , VerificationKeyStore } ;
728
- use crate :: ProtocolParametersStore ;
729
-
730
- use mithril_common :: crypto_helper :: tests_setup :: * ;
731
- use mithril_common:: fake_data ;
732
- use mithril_common :: store :: adapter :: MemoryAdapter ;
733
- use mithril_common :: store :: StakeStore ;
734
-
735
- use std :: collections :: HashMap ;
736
- use std:: sync:: Arc ;
724
+ use crate :: {
725
+ store :: { SingleSignatureStore , VerificationKeyStore } ,
726
+ ProtocolParametersStore ,
727
+ } ;
728
+ use mithril_common:: {
729
+ crypto_helper :: tests_setup :: * ,
730
+ fake_data ,
731
+ store :: { adapter :: MemoryAdapter , StakeStore } ,
732
+ } ;
733
+ use std:: { collections :: HashMap , sync:: Arc } ;
737
734
738
735
async fn setup_multi_signer ( ) -> MultiSignerImpl {
739
736
let beacon = fake_data:: beacon ( ) ;
740
- let verification_key_store = VerificationKeyStore :: new ( Box :: new (
741
- MemoryAdapter :: < entities:: Epoch , HashMap < entities:: PartyId , entities:: Signer > > :: new (
742
- None ,
743
- )
744
- . unwrap ( ) ,
745
- ) , None ) ;
737
+ let verification_key_store = VerificationKeyStore :: new (
738
+ Box :: new (
739
+ MemoryAdapter :: < Epoch , HashMap < entities:: PartyId , entities:: Signer > > :: new ( None )
740
+ . unwrap ( ) ,
741
+ ) ,
742
+ None ,
743
+ ) ;
746
744
let stake_store = StakeStore :: new (
747
745
Box :: new (
748
- MemoryAdapter :: < entities:: Epoch , HashMap < entities:: PartyId , entities:: Stake > > :: new (
749
- None ,
750
- )
751
- . unwrap ( ) ,
746
+ MemoryAdapter :: < Epoch , HashMap < entities:: PartyId , entities:: Stake > > :: new ( None )
747
+ . unwrap ( ) ,
752
748
) ,
753
749
None ,
754
750
) ;
@@ -764,16 +760,13 @@ mod tests {
764
760
) ;
765
761
let protocol_parameters_store = ProtocolParametersStore :: new (
766
762
Box :: new (
767
- MemoryAdapter :: < entities :: Epoch , entities:: ProtocolParameters > :: new ( Some ( vec ! [
763
+ MemoryAdapter :: < Epoch , entities:: ProtocolParameters > :: new ( Some ( vec ! [
768
764
(
769
765
beacon. epoch. offset_to_signer_retrieval_epoch( ) . unwrap( ) ,
770
766
fake_data:: protocol_parameters( ) ,
771
767
) ,
772
768
(
773
- beacon
774
- . epoch
775
- . offset_to_next_signer_retrieval_epoch( )
776
- . unwrap( ) ,
769
+ beacon. epoch. offset_to_next_signer_retrieval_epoch( ) ,
777
770
fake_data:: protocol_parameters( ) ,
778
771
) ,
779
772
] ) )
@@ -851,7 +844,7 @@ mod tests {
851
844
852
845
offset_epoch (
853
846
& mut multi_signer,
854
- SIGNER_EPOCH_RECORDING_OFFSET - SIGNER_EPOCH_RETRIEVAL_OFFSET ,
847
+ Epoch :: SIGNER_RECORDING_OFFSET as i64 - Epoch :: SIGNER_RETRIEVAL_OFFSET ,
855
848
)
856
849
. await ;
857
850
@@ -887,7 +880,7 @@ mod tests {
887
880
888
881
offset_epoch (
889
882
& mut multi_signer,
890
- SIGNER_EPOCH_RECORDING_OFFSET - SIGNER_EPOCH_RETRIEVAL_OFFSET ,
883
+ Epoch :: SIGNER_RECORDING_OFFSET as i64 - Epoch :: SIGNER_RETRIEVAL_OFFSET ,
891
884
)
892
885
. await ;
893
886
@@ -943,7 +936,7 @@ mod tests {
943
936
for ( signer_with_stake, _, _) in & signers {
944
937
verification_key_store
945
938
. save_verification_key (
946
- start_epoch. offset_to_recording_epoch ( ) . unwrap ( ) ,
939
+ start_epoch. offset_to_recording_epoch ( ) ,
947
940
signer_with_stake. to_owned ( ) . into ( ) ,
948
941
)
949
942
. await
@@ -952,7 +945,7 @@ mod tests {
952
945
953
946
offset_epoch (
954
947
& mut multi_signer,
955
- SIGNER_EPOCH_RECORDING_OFFSET - SIGNER_EPOCH_RETRIEVAL_OFFSET ,
948
+ Epoch :: SIGNER_RECORDING_OFFSET as i64 - Epoch :: SIGNER_RETRIEVAL_OFFSET ,
956
949
)
957
950
. await ;
958
951
// We have to update the current message AFTER we reached the epoch for
0 commit comments