@@ -123,6 +123,7 @@ where
123
123
124
124
#[ cfg( test) ]
125
125
pub ( crate ) mod tests {
126
+ use std:: collections:: { BTreeMap , HashMap } ;
126
127
use std:: io:: { Read , Write } ;
127
128
use std:: net:: { SocketAddr , TcpListener } ;
128
129
@@ -138,20 +139,16 @@ pub(crate) mod tests {
138
139
use clarity:: vm:: costs:: ExecutionCost ;
139
140
use clarity:: vm:: types:: TupleData ;
140
141
use clarity:: vm:: Value as ClarityValue ;
141
- use hashbrown:: { HashMap , HashSet } ;
142
142
use libsigner:: SignerEntries ;
143
143
use rand:: distributions:: Standard ;
144
144
use rand:: { thread_rng, Rng } ;
145
- use rand_core:: { OsRng , RngCore } ;
145
+ use rand_core:: RngCore ;
146
146
use stacks_common:: types:: chainstate:: {
147
147
BlockHeaderHash , ConsensusHash , StacksAddress , StacksPrivateKey , StacksPublicKey ,
148
148
} ;
149
149
use stacks_common:: types:: { StacksEpochId , StacksPublicKeyBuffer } ;
150
150
use stacks_common:: util:: hash:: { Hash160 , Sha256Sum } ;
151
- use wsts:: curve:: ecdsa;
152
- use wsts:: curve:: point:: { Compressed , Point } ;
153
- use wsts:: curve:: scalar:: Scalar ;
154
- use wsts:: state_machine:: PublicKeys ;
151
+ use wsts:: curve:: point:: Point ;
155
152
156
153
use super :: * ;
157
154
use crate :: config:: { GlobalConfig , SignerConfig } ;
@@ -456,112 +453,59 @@ pub(crate) mod tests {
456
453
457
454
/// Generate a signer config with the given number of signers and keys where the first signer is
458
455
/// obtained from the provided global config
459
- pub fn generate_signer_config (
460
- config : & GlobalConfig ,
461
- num_signers : u32 ,
462
- num_keys : u32 ,
463
- ) -> SignerConfig {
456
+ pub fn generate_signer_config ( config : & GlobalConfig , num_signers : u32 ) -> SignerConfig {
464
457
assert ! (
465
458
num_signers > 0 ,
466
459
"Cannot generate 0 signers...Specify at least 1 signer."
467
460
) ;
468
- assert ! (
469
- num_keys > 0 ,
470
- "Cannot generate 0 keys for the provided signers...Specify at least 1 key."
471
- ) ;
472
- let mut public_keys = PublicKeys {
473
- signers : HashMap :: new ( ) ,
474
- key_ids : HashMap :: new ( ) ,
475
- } ;
461
+
462
+ let weight_per_signer = 100 / num_signers;
463
+ let mut remaining_weight = 100 % num_signers;
464
+
476
465
let reward_cycle = thread_rng ( ) . next_u64 ( ) ;
477
- let rng = & mut OsRng ;
478
- let num_keys = num_keys / num_signers;
479
- let remaining_keys = num_keys % num_signers;
480
- let mut coordinator_key_ids = HashMap :: new ( ) ;
481
- let mut signer_key_ids = HashMap :: new ( ) ;
482
- let mut signer_ids = HashMap :: new ( ) ;
483
- let mut start_key_id = 1u32 ;
484
- let mut end_key_id = start_key_id;
485
- let mut signer_public_keys = HashMap :: new ( ) ;
486
- let mut signer_slot_ids = vec ! [ ] ;
487
- let ecdsa_private_key = config. ecdsa_private_key ;
488
- let ecdsa_public_key =
489
- ecdsa:: PublicKey :: new ( & ecdsa_private_key) . expect ( "Failed to create ecdsa public key" ) ;
490
- // Key ids start from 1 hence the wrapping adds everywhere
466
+
467
+ let mut signer_pk_to_id = HashMap :: new ( ) ;
468
+ let mut signer_id_to_pk = HashMap :: new ( ) ;
469
+ let mut signer_addr_to_id = HashMap :: new ( ) ;
470
+ let mut signer_pks = Vec :: new ( ) ;
471
+ let mut signer_slot_ids = Vec :: new ( ) ;
472
+ let mut signer_id_to_addr = BTreeMap :: new ( ) ;
473
+ let mut signer_addr_to_weight = HashMap :: new ( ) ;
474
+ let mut signer_addresses = Vec :: new ( ) ;
475
+
491
476
for signer_id in 0 ..num_signers {
492
- end_key_id = if signer_id. wrapping_add ( 1 ) == num_signers {
493
- end_key_id . wrapping_add ( remaining_keys )
477
+ let private_key = if signer_id == 0 {
478
+ config . stacks_private_key
494
479
} else {
495
- end_key_id . wrapping_add ( num_keys )
480
+ StacksPrivateKey :: new ( )
496
481
} ;
497
- if signer_id == 0 {
498
- public_keys. signers . insert ( signer_id, ecdsa_public_key) ;
499
- let signer_public_key =
500
- Point :: try_from ( & Compressed :: from ( ecdsa_public_key. to_bytes ( ) ) ) . unwrap ( ) ;
501
- signer_public_keys. insert ( signer_id, signer_public_key) ;
502
- public_keys. signers . insert ( signer_id, ecdsa_public_key) ;
503
- for k in start_key_id..end_key_id {
504
- public_keys. key_ids . insert ( k, ecdsa_public_key) ;
505
- coordinator_key_ids
506
- . entry ( signer_id)
507
- . or_insert ( HashSet :: new ( ) )
508
- . insert ( k) ;
509
- signer_key_ids
510
- . entry ( signer_id)
511
- . or_insert ( Vec :: new ( ) )
512
- . push ( k) ;
513
- }
514
- start_key_id = end_key_id;
515
- let address = StacksAddress :: p2pkh (
516
- false ,
517
- & StacksPublicKey :: from_slice ( ecdsa_public_key. to_bytes ( ) . as_slice ( ) )
518
- . expect ( "Failed to create stacks public key" ) ,
519
- ) ;
520
- signer_slot_ids. push ( SignerSlotID ( signer_id) ) ;
521
- signer_ids. insert ( address, signer_id) ;
522
-
523
- continue ;
524
- }
525
- let private_key = Scalar :: random ( rng) ;
526
- let public_key = ecdsa:: PublicKey :: new ( & private_key) . unwrap ( ) ;
527
- let signer_public_key =
528
- Point :: try_from ( & Compressed :: from ( public_key. to_bytes ( ) ) ) . unwrap ( ) ;
529
- signer_public_keys. insert ( signer_id, signer_public_key) ;
530
- public_keys. signers . insert ( signer_id, public_key) ;
531
- for k in start_key_id..end_key_id {
532
- public_keys. key_ids . insert ( k, public_key) ;
533
- coordinator_key_ids
534
- . entry ( signer_id)
535
- . or_insert ( HashSet :: new ( ) )
536
- . insert ( k) ;
537
- signer_key_ids
538
- . entry ( signer_id)
539
- . or_insert ( Vec :: new ( ) )
540
- . push ( k) ;
541
- }
542
- let address = StacksAddress :: p2pkh (
543
- false ,
544
- & StacksPublicKey :: from_slice ( public_key. to_bytes ( ) . as_slice ( ) )
545
- . expect ( "Failed to create stacks public key" ) ,
546
- ) ;
482
+ let public_key = StacksPublicKey :: from_private ( & private_key) ;
483
+
484
+ signer_id_to_pk. insert ( signer_id, public_key) ;
485
+ signer_pk_to_id. insert ( public_key, signer_id) ;
486
+ let address = StacksAddress :: p2pkh ( false , & public_key) ;
487
+ signer_addr_to_id. insert ( address, signer_id) ;
488
+ signer_pks. push ( public_key) ;
547
489
signer_slot_ids. push ( SignerSlotID ( signer_id) ) ;
548
- signer_ids. insert ( address, signer_id) ;
549
- start_key_id = end_key_id;
490
+ signer_id_to_addr. insert ( signer_id, address) ;
491
+ signer_addr_to_weight. insert ( address, weight_per_signer + remaining_weight) ;
492
+ signer_addresses. push ( address) ;
493
+ remaining_weight = 0 ; // The first signer gets the extra weight if there is any. All other signers only get the weight_per_signer
550
494
}
551
495
SignerConfig {
552
496
reward_cycle,
553
497
signer_id : 0 ,
554
498
signer_slot_id : SignerSlotID ( rand:: thread_rng ( ) . gen_range ( 0 ..num_signers) ) , // Give a random signer slot id between 0 and num_signers
555
- key_ids : signer_key_ids. get ( & 0 ) . cloned ( ) . unwrap_or_default ( ) ,
556
499
signer_entries : SignerEntries {
557
- public_keys,
558
- coordinator_key_ids,
559
- signer_key_ids,
560
- signer_ids,
561
- signer_public_keys,
500
+ signer_addr_to_id,
501
+ signer_id_to_pk,
502
+ signer_pk_to_id,
503
+ signer_pks,
504
+ signer_id_to_addr,
505
+ signer_addr_to_weight,
506
+ signer_addresses,
562
507
} ,
563
508
signer_slot_ids,
564
- ecdsa_private_key : config. ecdsa_private_key ,
565
509
stacks_private_key : config. stacks_private_key ,
566
510
node_host : config. node_host . to_string ( ) ,
567
511
mainnet : config. network . is_mainnet ( ) ,
0 commit comments