-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export slashing protection per validator (#2674)
## Issue Addressed Part of #2557 ## Proposed Changes Refactor the slashing protection export so that it can export data for a subset of validators. This is the last remaining building block required for supporting the standard validator API (which I'll start to build atop this branch) ## Additional Info Built on and requires #2598
- Loading branch information
1 parent
e75ce53
commit 06e310c
Showing
5 changed files
with
230 additions
and
73 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
75 changes: 75 additions & 0 deletions
75
validator_client/slashing_protection/src/extra_interchange_tests.rs
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,75 @@ | ||
#![cfg(test)] | ||
|
||
use crate::test_utils::pubkey; | ||
use crate::*; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn export_non_existent_key() { | ||
let dir = tempdir().unwrap(); | ||
let slashing_db_file = dir.path().join("slashing_protection.sqlite"); | ||
let slashing_db = SlashingDatabase::create(&slashing_db_file).unwrap(); | ||
|
||
let key1 = pubkey(1); | ||
let key2 = pubkey(2); | ||
|
||
// Exporting two non-existent keys should fail on the first one. | ||
let err = slashing_db | ||
.export_interchange_info(Hash256::zero(), Some(&[key1, key2])) | ||
.unwrap_err(); | ||
assert!(matches!( | ||
err, | ||
InterchangeError::NotSafe(NotSafe::UnregisteredValidator(k)) if k == key1 | ||
)); | ||
|
||
slashing_db.register_validator(key1).unwrap(); | ||
|
||
// Exporting one key that exists and one that doesn't should fail on the one that doesn't. | ||
let err = slashing_db | ||
.export_interchange_info(Hash256::zero(), Some(&[key1, key2])) | ||
.unwrap_err(); | ||
assert!(matches!( | ||
err, | ||
InterchangeError::NotSafe(NotSafe::UnregisteredValidator(k)) if k == key2 | ||
)); | ||
|
||
// Exporting only keys that exist should work. | ||
let interchange = slashing_db | ||
.export_interchange_info(Hash256::zero(), Some(&[key1])) | ||
.unwrap(); | ||
assert_eq!(interchange.data.len(), 1); | ||
assert_eq!(interchange.data[0].pubkey, key1); | ||
} | ||
|
||
#[test] | ||
fn export_same_key_twice() { | ||
let dir = tempdir().unwrap(); | ||
let slashing_db_file = dir.path().join("slashing_protection.sqlite"); | ||
let slashing_db = SlashingDatabase::create(&slashing_db_file).unwrap(); | ||
|
||
let key1 = pubkey(1); | ||
|
||
slashing_db.register_validator(key1).unwrap(); | ||
|
||
let export_single = slashing_db | ||
.export_interchange_info(Hash256::zero(), Some(&[key1])) | ||
.unwrap(); | ||
let export_double = slashing_db | ||
.export_interchange_info(Hash256::zero(), Some(&[key1, key1])) | ||
.unwrap(); | ||
|
||
assert_eq!(export_single.data.len(), 1); | ||
|
||
// Allow the same data to be exported twice, this is harmless, albeit slightly inefficient. | ||
assert_eq!(export_double.data.len(), 2); | ||
assert_eq!(export_double.data[0], export_double.data[1]); | ||
|
||
// The data should be identical to the single export. | ||
assert_eq!(export_double.data[0], export_single.data[0]); | ||
|
||
// The minified versions should be equal too. | ||
assert_eq!( | ||
export_single.minify().unwrap(), | ||
export_double.minify().unwrap() | ||
); | ||
} |
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
Oops, something went wrong.