@@ -17,6 +17,7 @@ use smallvec::SmallVec;
1717use state_processing:: state_advance:: partial_state_advance;
1818use std:: cmp:: Ordering ;
1919use std:: num:: NonZeroUsize ;
20+ use std:: sync:: Arc ;
2021use types:: non_zero_usize:: new_non_zero_usize;
2122use types:: {
2223 BeaconState , BeaconStateError , ChainSpec , Epoch , EthSpec , Fork , Hash256 , Slot , Unsigned ,
@@ -41,21 +42,21 @@ pub struct Proposer {
4142/// their signatures.
4243pub struct EpochBlockProposers {
4344 /// The epoch to which the proposers pertain.
44- epoch : Epoch ,
45+ pub epoch : Epoch ,
4546 /// The fork that should be used to verify proposer signatures.
46- fork : Fork ,
47+ pub fork : Fork ,
4748 /// A list of length `T::EthSpec::slots_per_epoch()`, representing the proposers for each slot
4849 /// in that epoch.
4950 ///
5051 /// E.g., if `self.epoch == 1`, then `self.proposers[0]` contains the proposer for slot `32`.
51- proposers : SmallVec < [ usize ; TYPICAL_SLOTS_PER_EPOCH ] > ,
52+ pub proposers : SmallVec < [ usize ; TYPICAL_SLOTS_PER_EPOCH ] > ,
5253}
5354
5455/// A cache to store the proposers for some epoch.
5556///
5657/// See the module-level documentation for more information.
5758pub struct BeaconProposerCache {
58- cache : LruCache < ( Epoch , Hash256 ) , OnceCell < EpochBlockProposers > > ,
59+ cache : LruCache < ( Epoch , Hash256 ) , Arc < OnceCell < EpochBlockProposers > > > ,
5960}
6061
6162impl Default for BeaconProposerCache {
@@ -111,6 +112,17 @@ impl BeaconProposerCache {
111112 . and_then ( |cache_once_cell| cache_once_cell. get ( ) . map ( |proposers| & proposers. proposers ) )
112113 }
113114
115+ pub fn get_or_insert_key (
116+ & mut self ,
117+ epoch : Epoch ,
118+ shuffling_decision_block : Hash256 ,
119+ ) -> Arc < OnceCell < EpochBlockProposers > > {
120+ let key = ( epoch, shuffling_decision_block) ;
121+ self . cache
122+ . get_or_insert ( key, || Arc :: new ( OnceCell :: new ( ) ) )
123+ . clone ( )
124+ }
125+
114126 /// Insert the proposers into the cache.
115127 ///
116128 /// See `Self::get` for a description of `shuffling_decision_block`.
@@ -125,61 +137,16 @@ impl BeaconProposerCache {
125137 ) -> Result < ( ) , BeaconStateError > {
126138 let key = ( epoch, shuffling_decision_block) ;
127139 if !self . cache . contains ( & key) {
128- self . cache . put (
129- key,
130- OnceCell :: with_value ( EpochBlockProposers {
131- epoch,
132- fork,
133- proposers : proposers. into ( ) ,
134- } ) ,
135- ) ;
136- }
137-
138- Ok ( ( ) )
139- }
140-
141- pub fn get_slot_or_insert_with < E : EthSpec , F , Err : BlockBlobError > (
142- & mut self ,
143- shuffling_decision_block : Hash256 ,
144- slot : Slot ,
145- compute_proposers_and_fork_fn : F ,
146- ) -> Result < Option < Proposer > , Err >
147- where
148- F : FnOnce ( ) -> Result < ( Vec < usize > , Fork ) , Err > ,
149- {
150- let epoch = slot. epoch ( E :: slots_per_epoch ( ) ) ;
151- let ( proposers, fork) = self . get_epoch_or_insert_with (
152- shuffling_decision_block,
153- epoch,
154- compute_proposers_and_fork_fn,
155- ) ?;
156-
157- Ok ( proposers
158- . get ( slot. as_usize ( ) % E :: SlotsPerEpoch :: to_usize ( ) )
159- . map ( |& index| Proposer { index, fork } ) )
160- }
161-
162- pub fn get_epoch_or_insert_with < F , Err : BlockBlobError > (
163- & mut self ,
164- shuffling_decision_block : Hash256 ,
165- epoch : Epoch ,
166- compute_proposers_and_fork_fn : F ,
167- ) -> Result < ( & SmallVec < [ usize ; TYPICAL_SLOTS_PER_EPOCH ] > , Fork ) , Err >
168- where
169- F : FnOnce ( ) -> Result < ( Vec < usize > , Fork ) , Err > ,
170- {
171- let key = ( epoch, shuffling_decision_block) ;
172- let once_cell = self . cache . get_or_insert_mut ( key, OnceCell :: new) ;
173- let epoch_block_proposers = once_cell. get_or_try_init ( || {
174- let ( proposers, fork) = compute_proposers_and_fork_fn ( ) ?;
175- Ok :: < EpochBlockProposers , Err > ( EpochBlockProposers {
140+ let epoch_proposers = EpochBlockProposers {
176141 epoch,
177142 fork,
178143 proposers : proposers. into ( ) ,
179- } )
180- } ) ?;
144+ } ;
145+ self . cache
146+ . put ( key, Arc :: new ( OnceCell :: with_value ( epoch_proposers) ) ) ;
147+ }
181148
182- Ok ( ( & epoch_block_proposers . proposers , epoch_block_proposers . fork ) )
149+ Ok ( ( ) )
183150 }
184151}
185152
0 commit comments