@@ -544,23 +544,30 @@ decl_module!{
544544 #[ weight = 10000 ]
545545 fn register_attestor( origin) {
546546 let account = ensure_signed( origin) ?;
547- let mut att_count = <AttestorsCount >:: get( ) ;
548- let user_index_option = att_count. pop( ) ;
549- let current_user_index = match user_index_option {
550- Some ( x) => x,
551- None => 0 ,
552- } ;
553- let i = current_user_index. saturating_add( 1 ) ;
554- <Attestors <T >>:: insert( & i, & account) ;
555- let mut atts = <AttestorsCount >:: get( ) ;
556- atts. push( i) ;
557- <AttestorsCount >:: put( atts) ;
558- let mut att_vec = <ActiveAttestors >:: get( ) ;
559- att_vec. push( i) ;
560- att_vec. sort_unstable( ) ;
561- att_vec. dedup( ) ;
562- native:: info!( "registerAttestor; att_vec: {:#?}" , att_vec) ;
563- <ActiveAttestors >:: put( att_vec) ;
547+ // if already active, stop here. (if any active attestor matches. shortcircuit)
548+ // super inefficient worstcase, needs refactor at some point.
549+ let mut all_attestors = <Attestors <T >>:: iter( ) . map( |( _, y) | y) ;
550+ if !<ActiveAttestors >:: get( ) . iter( ) . any( |_|{
551+ all_attestors. any( |y| y == account)
552+ } ) {
553+ let mut att_count = <AttestorsCount >:: get( ) ;
554+ let user_index_option = att_count. pop( ) ;
555+ let current_user_index = match user_index_option {
556+ Some ( x) => x,
557+ None => 0 ,
558+ } ;
559+ let i = current_user_index. saturating_add( 1 ) ;
560+ <Attestors <T >>:: insert( & i, & account) ;
561+ let mut atts = <AttestorsCount >:: get( ) ;
562+ atts. push( i) ;
563+ <AttestorsCount >:: put( atts) ;
564+ let mut att_vec = <ActiveAttestors >:: get( ) ;
565+ att_vec. push( i) ;
566+ att_vec. sort_unstable( ) ;
567+ att_vec. dedup( ) ;
568+ native:: info!( "registerAttestor; att_vec: {:#?}" , att_vec) ;
569+ <ActiveAttestors >:: put( att_vec) ;
570+ }
564571 }
565572
566573 #[ weight = 10000 ]
@@ -634,7 +641,7 @@ decl_module!{
634641 native:: info!( "Last Dat Index: {:#?}" , last) ;
635642 match last {
636643 Some ( last_index) => {
637- let nonce = < Nonce > :: get ( ) ;
644+ let nonce = Self :: unique_nonce ( ) ;
638645 let new_random = ( T :: Randomness :: random( b"dat_verify_register" ) , & nonce, & account)
639646 . using_encoded( |mut b| u64 :: decode( & mut b) )
640647 . expect( "hash must be of correct size; Qed" ) ;
@@ -650,7 +657,6 @@ decl_module!{
650657 dat_hosters. dedup( ) ;
651658 <DatHosters <T >>:: insert( & dat_pubkey, & dat_hosters) ;
652659 <UsersStorage <T >>:: insert( & account, & current_user_dats) ;
653- <Nonce >:: mutate( |m| * m += 1 ) ;
654660 let user_index;
655661 if ( current_user_dats. len( ) == 1 ) {
656662 let user_index_option = <UsersCount >:: get( ) . pop( ) ;
@@ -720,7 +726,7 @@ decl_module!{
720726 #[ weight = ( 10000 , Operational ) ] //todo weight
721727 fn submit_challenge( origin, selected_user: u64 , dat_id: u64 ) {
722728 let challenge_index = <ChallengeIndex >:: get( ) ;
723- let nonce = < Nonce > :: get ( ) ;
729+ let nonce = Self :: unique_nonce ( ) ;
724730 let new_random = ( T :: Randomness :: random( b"dat_verify_init" ) , nonce)
725731 . using_encoded( |mut b| u64 :: decode( & mut b) )
726732 . expect( "hash must be of correct size; Qed" ) ;
@@ -748,7 +754,6 @@ decl_module!{
748754 <SelectedChallenges <T >>:: insert( & challenge_index, ( dat_pubkey, random_leaf, future_block) ) ;
749755 <SelectedUsers <T >>:: insert( & y, & selected_user_key) ;
750756 <ChallengeMap >:: insert( challenge_index, y) ;
751- <Nonce >:: put( <Nonce >:: get( ) + 1 ) ;
752757 <ChallengeIndex >:: put( <ChallengeIndex >:: get( ) + 1 ) ;
753758 Self :: deposit_event( RawEvent :: Challenge ( selected_user_key, future_block) ) ;
754759 }
@@ -824,7 +829,7 @@ impl<T: Trait> Module<T> {
824829 if attestors. expected_attestors . len ( ) > 0 {
825830 return attestors
826831 } else {
827- let mut expected_attestors = Self :: get_random_attestors ( ) ;
832+ let mut expected_attestors = Self :: get_random_attestors ( challenge_index ) ;
828833 expected_attestors. sort_unstable ( ) ;
829834 expected_attestors. dedup ( ) ;
830835 return ChallengeAttestations {
@@ -968,22 +973,27 @@ impl<T: Trait> Module<T> {
968973 }
969974 // ---
970975
971- fn get_random_attestors ( ) -> Vec < u64 > {
976+ fn unique_nonce ( ) -> u64 {
977+ let nonce = <Nonce >:: get ( ) ;
978+ <Nonce >:: put ( nonce+1 ) ;
979+ nonce
980+ }
981+
982+ fn get_random_attestors ( challenge_index : u64 ) -> Vec < u64 > {
972983 let members : Vec < u64 > = <ActiveAttestors >:: get ( ) ;
973984 match members. len ( ) {
974985 0 => members,
975986 _ => {
976- let nonce = < Nonce > :: get ( ) ;
987+ let nonce : u64 = Self :: unique_nonce ( ) ;
977988 let mut random_select: Vec < u64 > = Vec :: new ( ) ;
978- let seed = ( T :: Randomness :: random ( b"dat_random_attestors" ) , nonce )
989+ let seed = ( nonce , challenge_index , T :: Randomness :: random ( b"dat_random_attestors" ) )
979990 . using_encoded ( |b| <[ u8 ; 32 ] >:: decode ( & mut TrailingZeroInput :: new ( b) ) )
980991 . expect ( "input is padded with zeroes; qed" ) ;
981992 let mut rng = ChaChaRng :: from_seed ( seed) ;
982993 let pick_attestor = |_| Self :: pick_item ( & mut rng, & members[ ..] ) . expect ( "exited if members empty; qed" ) ;
983994 for attestor in ( 0 ..T :: AttestorsPerChallenge :: get ( ) ) . map ( pick_attestor) {
984995 random_select. push ( * attestor) ;
985996 }
986- <Nonce >:: put ( nonce+1 ) ;
987997 random_select
988998 } ,
989999 }
0 commit comments