-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
15 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use super::test_prelude::*; | ||
|
||
#[cfg(test)] | ||
mod validator_database_tests { | ||
use super::*; | ||
use types::Address; | ||
|
||
#[test] | ||
/// Test updating the fee recipient address | ||
fn test_update_fee_recipient() { | ||
let mut fixture = TestFixture::new(Some(1)); | ||
|
||
let validator_pubkey = fixture.cluster.validator_metadata.validator_pubkey; | ||
let updated_fee_recipient = Address::random(); | ||
let cluster_id = fixture.cluster.cluster_id; | ||
fixture | ||
.db | ||
.update_fee_recipient(cluster_id, validator_pubkey.clone(), updated_fee_recipient) | ||
.expect("Failed to update fee recipient"); | ||
|
||
// make sure the state store has changed, then check the db | ||
assert_eq!( | ||
updated_fee_recipient, | ||
fixture | ||
.db | ||
.get_fee_recipient(&cluster_id) | ||
.expect("Failed to get fee recipient") | ||
); | ||
assert_eq!( | ||
updated_fee_recipient.to_string(), | ||
queries::get_validator(&fixture.db, &(validator_pubkey.to_string())) | ||
.expect("Failed to fetch Validator") | ||
.3 | ||
); | ||
} | ||
|
||
#[test] | ||
/// Test setting the validator index | ||
fn test_set_validator_index() { | ||
let mut fixture = TestFixture::new(Some(1)); | ||
|
||
let validator_pubkey = fixture.cluster.validator_metadata.validator_pubkey; | ||
let updated_validator_index = ValidatorIndex(10); | ||
let cluster_id = fixture.cluster.cluster_id; | ||
fixture | ||
.db | ||
.set_validator_index( | ||
cluster_id, | ||
validator_pubkey.clone(), | ||
updated_validator_index, | ||
) | ||
.expect("Failed to update validator index"); | ||
|
||
// make sure the state store has changed, then check the db | ||
assert_eq!( | ||
updated_validator_index, | ||
fixture | ||
.db | ||
.get_validator_index(&cluster_id) | ||
.expect("Failed to get validator index") | ||
); | ||
assert_eq!( | ||
*updated_validator_index as i64, | ||
queries::get_validator(&fixture.db, &(validator_pubkey.to_string())) | ||
.expect("Failed to fetch Validator") | ||
.4 | ||
); | ||
} | ||
} |
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,39 +1,64 @@ | ||
use super::{DatabaseError, NetworkDatabase, SqlStatement, SQL}; | ||
use rusqlite::params; | ||
use ssv_types::{ClusterId, ValidatorIndex, ValidatorMetadata}; | ||
use ssv_types::{ClusterId, ValidatorIndex}; | ||
use types::{Address, PublicKey}; | ||
|
||
/// Implements all validator related db functionality | ||
impl NetworkDatabase { | ||
/// Populates or updates the fee recipient for the validator | ||
/// Update the fee recipient address for a validator | ||
pub fn update_fee_recipient( | ||
&mut self, | ||
cluster_id: ClusterId, | ||
validator_pubkey: PublicKey, | ||
fee_recipient: Address, | ||
) -> Result<(), DatabaseError> { | ||
// Make sure we are part of the cluster for this Validator | ||
if !self.state.clusters.contains(&cluster_id) { | ||
return Err(DatabaseError::NotFound(format!( | ||
"Validator for Cluster {} not in database", | ||
*cluster_id | ||
))); | ||
} | ||
|
||
let conn = self.connection()?; | ||
conn.prepare_cached(SQL[&SqlStatement::UpdateFeeRecipient])? | ||
.execute(params![ | ||
validator_pubkey.to_string(), | ||
fee_recipient.to_string() | ||
fee_recipient.to_string(), | ||
validator_pubkey.to_string() | ||
])?; | ||
let metadata = self | ||
.state | ||
.validator_metadata | ||
.get_mut(&cluster_id) | ||
.expect("Cluster should exist"); | ||
metadata.fee_recipient = fee_recipient; | ||
Ok(()) | ||
} | ||
|
||
/// Set the index of the validator | ||
pub fn set_validator_index( | ||
&mut self, | ||
cluster_id: ClusterId, | ||
validator_pubkey: PublicKey, | ||
index: ValidatorIndex, | ||
) -> Result<(), DatabaseError> { | ||
// Make sure we are part of the cluster for this validaor | ||
if !self.state.clusters.contains(&cluster_id) { | ||
return Err(DatabaseError::NotFound(format!( | ||
"Validator for Cluster {} not in database", | ||
*cluster_id | ||
))); | ||
} | ||
|
||
let conn = self.connection()?; | ||
conn.prepare_cached(SQL[&SqlStatement::SetValidatorIndex])? | ||
.execute(params![validator_pubkey.to_string(), *index])?; | ||
.execute(params![*index, validator_pubkey.to_string()])?; | ||
let metadata = self | ||
.state | ||
.validator_metadata | ||
.get_mut(&cluster_id) | ||
.expect("Cluster should exist"); | ||
metadata.validator_index = index; | ||
Ok(()) | ||
} | ||
|
||
/// Get the metatdata for the cluster | ||
pub fn get_validator_metadata(&self, id: &ClusterId) -> Option<&ValidatorMetadata> { | ||
self.state.validator_metadata.get(id) | ||
} | ||
} |