File tree Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -440,6 +440,16 @@ impl<T: EthSpec> ValidatorMonitor<T> {
440
440
. and_then ( |pubkey| self . validators . get ( pubkey) )
441
441
}
442
442
443
+ /// Return `true` if the `validator_index` has been registered with `self`.
444
+ pub fn contains_validator ( & self , validator_index : u64 ) -> bool {
445
+ self . indices . contains_key ( & validator_index)
446
+ }
447
+
448
+ /// Return `true` if the automatic validator registration is enabled.
449
+ pub fn auto_register_enabled ( & self ) -> bool {
450
+ self . auto_register
451
+ }
452
+
443
453
/// Returns the number of validators monitored by `self`.
444
454
pub fn num_validators ( & self ) -> usize {
445
455
self . validators . len ( )
Original file line number Diff line number Diff line change @@ -1877,10 +1877,26 @@ pub fn serve<T: BeaconChainTypes>(
1877
1877
chain : Arc < BeaconChain < T > > | {
1878
1878
blocking_json_task ( move || {
1879
1879
for subscription in & subscriptions {
1880
- chain
1881
- . validator_monitor
1882
- . write ( )
1883
- . auto_register_local_validator ( subscription. validator_index ) ;
1880
+ // Assign the result from the read to a separate variable, to avoid a Rust
1881
+ // quirk where a read-lock obtained inside an `if` statement is for the
1882
+ // execution inside it.
1883
+ //
1884
+ // Checking the read-lock first helps avoid lengthy waits for the
1885
+ // write-lock. Although another thread may add the validator between
1886
+ // dropping the read-lock and obtaining the write-lock, there's no harm in
1887
+ // registering the same validator twice.
1888
+ let should_register = {
1889
+ let validator_monitor = chain. validator_monitor . read ( ) ;
1890
+ validator_monitor. auto_register_enabled ( )
1891
+ && validator_monitor
1892
+ . contains_validator ( subscription. validator_index )
1893
+ } ;
1894
+ if should_register {
1895
+ chain
1896
+ . validator_monitor
1897
+ . write ( )
1898
+ . auto_register_local_validator ( subscription. validator_index ) ;
1899
+ }
1884
1900
1885
1901
let subscription = api_types:: ValidatorSubscription {
1886
1902
validator_index : subscription. validator_index ,
You can’t perform that action at this time.
0 commit comments