-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do the locking around Validator
state instead of on each member
#3163
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use std::sync::atomic::Ordering; | ||
use std::{mem, sync::Arc}; | ||
|
||
use async_trait::async_trait; | ||
use nimiq_bls::{KeyPair as BlsKeyPair, SecretKey as BlsSecretKey}; | ||
|
@@ -7,17 +7,18 @@ use nimiq_keys::Address; | |
use nimiq_network_libp2p::Network; | ||
use nimiq_rpc_interface::{types::RPCResult, validator::ValidatorInterface}; | ||
use nimiq_serde::{Deserialize, Serialize}; | ||
use nimiq_validator::validator::ValidatorProxy; | ||
use nimiq_validator::validator::ValidatorState; | ||
use parking_lot::RwLock; | ||
|
||
use crate::error::Error; | ||
|
||
pub struct ValidatorDispatcher { | ||
validator: ValidatorProxy, | ||
validator: Arc<RwLock<ValidatorState>>, | ||
consensus: ConsensusProxy<Network>, | ||
} | ||
|
||
impl ValidatorDispatcher { | ||
pub fn new(validator: ValidatorProxy, consensus: ConsensusProxy<Network>) -> Self { | ||
pub fn new(validator: Arc<RwLock<ValidatorState>>, consensus: ConsensusProxy<Network>) -> Self { | ||
ValidatorDispatcher { | ||
validator, | ||
consensus, | ||
|
@@ -31,18 +32,19 @@ impl ValidatorInterface for ValidatorDispatcher { | |
type Error = Error; | ||
|
||
async fn get_address(&mut self) -> RPCResult<Address, (), Self::Error> { | ||
Ok(self.validator.validator_address.read().clone().into()) | ||
Ok(self.validator.read().validator_address.clone().into()) | ||
} | ||
|
||
// TODO: why do we give out secret keys via RPC? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Action this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an issue: #3270. Theoretically a breaking change so I didn't want to do it here. |
||
async fn get_signing_key(&mut self) -> RPCResult<String, (), Self::Error> { | ||
Ok(hex::encode(self.validator.signing_key.read().private.serialize_to_vec()).into()) | ||
Ok(hex::encode(self.validator.read().signing_key.private.serialize_to_vec()).into()) | ||
} | ||
|
||
async fn get_voting_key(&mut self) -> RPCResult<String, (), Self::Error> { | ||
Ok(hex::encode( | ||
self.validator | ||
.voting_keys | ||
.read() | ||
.voting_keys | ||
.get_current_key() | ||
.secret_key | ||
.serialize_to_vec(), | ||
|
@@ -53,8 +55,8 @@ impl ValidatorInterface for ValidatorDispatcher { | |
async fn get_voting_keys(&mut self) -> RPCResult<Vec<String>, (), Self::Error> { | ||
Ok(self | ||
.validator | ||
.voting_keys | ||
.read() | ||
.voting_keys | ||
.get_keys() | ||
.into_iter() | ||
.map(|key| hex::encode(key.secret_key.serialize_to_vec())) | ||
|
@@ -63,7 +65,7 @@ impl ValidatorInterface for ValidatorDispatcher { | |
} | ||
|
||
async fn add_voting_key(&mut self, secret_key: String) -> RPCResult<(), (), Self::Error> { | ||
self.validator.voting_keys.write().add_key(BlsKeyPair::from( | ||
self.validator.write().voting_keys.add_key(BlsKeyPair::from( | ||
BlsSecretKey::deserialize_from_vec(&hex::decode(secret_key)?)?, | ||
)); | ||
Ok(().into()) | ||
|
@@ -73,16 +75,21 @@ impl ValidatorInterface for ValidatorDispatcher { | |
&mut self, | ||
automatic_reactivate: bool, | ||
) -> RPCResult<(), (), Self::Error> { | ||
self.validator | ||
.automatic_reactivate | ||
.store(automatic_reactivate, Ordering::Release); | ||
let old = mem::replace( | ||
&mut self.validator.write().automatic_reactivate, | ||
automatic_reactivate, | ||
); | ||
|
||
log::debug!("Automatic reactivation set to {}.", automatic_reactivate); | ||
log::debug!( | ||
"Automatic reactivation set to {} (from {}).", | ||
automatic_reactivate, | ||
old | ||
); | ||
Ok(().into()) | ||
} | ||
|
||
async fn is_validator_elected(&mut self) -> RPCResult<bool, (), Self::Error> { | ||
let is_elected = self.validator.slot_band.read().is_some(); | ||
let is_elected = self.validator.read().slot_band.is_some(); | ||
Ok(is_elected.into()) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
read()
is maybe not so nice, should I encapsulate that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not mind the
.read()
here all that much, so from my end a change would not be necessary.