26
26
thiserror:: Error ,
27
27
} ;
28
28
29
+ mod serde_stakes;
30
+ pub ( crate ) use serde_stakes:: serde_stakes_enum_compat;
31
+
29
32
#[ derive( Debug , Error ) ]
30
33
pub enum Error {
31
34
#[ error( "Invalid delegation: {0}" ) ]
@@ -267,7 +270,7 @@ impl Stakes<StakeAccount> {
267
270
let stake_account = StakeAccount :: try_from ( stake_account) ?;
268
271
// Sanity check that the delegation is consistent with what is
269
272
// stored in the account.
270
- if stake_account. delegation ( ) == * delegation {
273
+ if stake_account. delegation ( ) == delegation {
271
274
map. insert ( * pubkey, stake_account) ;
272
275
Ok ( map)
273
276
} else {
@@ -499,12 +502,15 @@ impl StakesEnum {
499
502
}
500
503
}
501
504
505
+ /// This conversion is very memory intensive so should only be used in
506
+ /// development contexts.
507
+ #[ cfg( feature = "dev-context-only-utils" ) ]
502
508
impl From < Stakes < StakeAccount > > for Stakes < Delegation > {
503
509
fn from ( stakes : Stakes < StakeAccount > ) -> Self {
504
510
let stake_delegations = stakes
505
511
. stake_delegations
506
512
. into_iter ( )
507
- . map ( |( pubkey, stake_account) | ( pubkey, stake_account. delegation ( ) ) )
513
+ . map ( |( pubkey, stake_account) | ( pubkey, * stake_account. delegation ( ) ) )
508
514
. collect ( ) ;
509
515
Self {
510
516
vote_accounts : stakes. vote_accounts ,
@@ -516,6 +522,9 @@ impl From<Stakes<StakeAccount>> for Stakes<Delegation> {
516
522
}
517
523
}
518
524
525
+ /// This conversion is memory intensive so should only be used in development
526
+ /// contexts.
527
+ #[ cfg( feature = "dev-context-only-utils" ) ]
519
528
impl From < Stakes < Stake > > for Stakes < Delegation > {
520
529
fn from ( stakes : Stakes < Stake > ) -> Self {
521
530
let stake_delegations = stakes
@@ -533,6 +542,9 @@ impl From<Stakes<Stake>> for Stakes<Delegation> {
533
542
}
534
543
}
535
544
545
+ /// This conversion is memory intensive so should only be used in development
546
+ /// contexts.
547
+ #[ cfg( feature = "dev-context-only-utils" ) ]
536
548
impl From < StakesEnum > for Stakes < Delegation > {
537
549
fn from ( stakes : StakesEnum ) -> Self {
538
550
match stakes {
@@ -576,36 +588,6 @@ impl PartialEq<StakesEnum> for StakesEnum {
576
588
}
577
589
}
578
590
579
- // In order to maintain backward compatibility, the StakesEnum in EpochStakes
580
- // and SerializableVersionedBank should be serialized as Stakes<Delegation>.
581
- pub ( crate ) mod serde_stakes_enum_compat {
582
- use {
583
- super :: * ,
584
- serde:: { Deserialize , Deserializer , Serialize , Serializer } ,
585
- } ;
586
-
587
- pub ( crate ) fn serialize < S > ( stakes : & StakesEnum , serializer : S ) -> Result < S :: Ok , S :: Error >
588
- where
589
- S : Serializer ,
590
- {
591
- match stakes {
592
- StakesEnum :: Delegations ( stakes) => stakes. serialize ( serializer) ,
593
- stakes => {
594
- let stakes = Stakes :: < Delegation > :: from ( stakes. clone ( ) ) ;
595
- stakes. serialize ( serializer)
596
- }
597
- }
598
- }
599
-
600
- pub ( crate ) fn deserialize < ' de , D > ( deserializer : D ) -> Result < Arc < StakesEnum > , D :: Error >
601
- where
602
- D : Deserializer < ' de > ,
603
- {
604
- let stakes = Stakes :: < Delegation > :: deserialize ( deserializer) ?;
605
- Ok ( Arc :: new ( StakesEnum :: Delegations ( stakes) ) )
606
- }
607
- }
608
-
609
591
fn refresh_vote_accounts (
610
592
thread_pool : & ThreadPool ,
611
593
epoch : Epoch ,
@@ -651,7 +633,6 @@ fn refresh_vote_accounts(
651
633
pub ( crate ) mod tests {
652
634
use {
653
635
super :: * ,
654
- rand:: Rng ,
655
636
rayon:: ThreadPoolBuilder ,
656
637
solana_sdk:: { account:: WritableAccount , pubkey:: Pubkey , rent:: Rent , stake} ,
657
638
solana_stake_program:: stake_state,
@@ -1078,63 +1059,4 @@ pub(crate) mod tests {
1078
1059
) ;
1079
1060
}
1080
1061
}
1081
-
1082
- #[ test]
1083
- fn test_serde_stakes_enum_compat ( ) {
1084
- #[ derive( Debug , PartialEq , Deserialize , Serialize ) ]
1085
- struct Dummy {
1086
- head : String ,
1087
- #[ serde( with = "serde_stakes_enum_compat" ) ]
1088
- stakes : Arc < StakesEnum > ,
1089
- tail : String ,
1090
- }
1091
- let mut rng = rand:: thread_rng ( ) ;
1092
- let stakes_cache = StakesCache :: new ( Stakes {
1093
- unused : rng. gen ( ) ,
1094
- epoch : rng. gen ( ) ,
1095
- ..Stakes :: default ( )
1096
- } ) ;
1097
- for _ in 0 ..rng. gen_range ( 5usize ..10 ) {
1098
- let vote_pubkey = solana_sdk:: pubkey:: new_rand ( ) ;
1099
- let vote_account = vote_state:: create_account (
1100
- & vote_pubkey,
1101
- & solana_sdk:: pubkey:: new_rand ( ) , // node_pubkey
1102
- rng. gen_range ( 0 ..101 ) , // commission
1103
- rng. gen_range ( 0 ..1_000_000 ) , // lamports
1104
- ) ;
1105
- stakes_cache. check_and_store ( & vote_pubkey, & vote_account, None ) ;
1106
- for _ in 0 ..rng. gen_range ( 10usize ..20 ) {
1107
- let stake_pubkey = solana_sdk:: pubkey:: new_rand ( ) ;
1108
- let rent = Rent :: with_slots_per_epoch ( rng. gen ( ) ) ;
1109
- let stake_account = stake_state:: create_account (
1110
- & stake_pubkey, // authorized
1111
- & vote_pubkey,
1112
- & vote_account,
1113
- & rent,
1114
- rng. gen_range ( 0 ..1_000_000 ) , // lamports
1115
- ) ;
1116
- stakes_cache. check_and_store ( & stake_pubkey, & stake_account, None ) ;
1117
- }
1118
- }
1119
- let stakes: Stakes < StakeAccount > = stakes_cache. stakes ( ) . clone ( ) ;
1120
- assert ! ( stakes. vote_accounts. as_ref( ) . len( ) >= 5 ) ;
1121
- assert ! ( stakes. stake_delegations. len( ) >= 50 ) ;
1122
- let dummy = Dummy {
1123
- head : String :: from ( "dummy-head" ) ,
1124
- stakes : Arc :: new ( StakesEnum :: from ( stakes. clone ( ) ) ) ,
1125
- tail : String :: from ( "dummy-tail" ) ,
1126
- } ;
1127
- assert ! ( dummy. stakes. vote_accounts( ) . as_ref( ) . len( ) >= 5 ) ;
1128
- let data = bincode:: serialize ( & dummy) . unwrap ( ) ;
1129
- let other: Dummy = bincode:: deserialize ( & data) . unwrap ( ) ;
1130
- assert_eq ! ( other, dummy) ;
1131
- let stakes = Stakes :: < Delegation > :: from ( stakes) ;
1132
- assert ! ( stakes. vote_accounts. as_ref( ) . len( ) >= 5 ) ;
1133
- assert ! ( stakes. stake_delegations. len( ) >= 50 ) ;
1134
- let other = match & * other. stakes {
1135
- StakesEnum :: Accounts ( _) | StakesEnum :: Stakes ( _) => panic ! ( "wrong type!" ) ,
1136
- StakesEnum :: Delegations ( delegations) => delegations,
1137
- } ;
1138
- assert_eq ! ( other, & stakes)
1139
- }
1140
1062
}
0 commit comments