Skip to content

Commit d642125

Browse files
db actions
1 parent f358add commit d642125

1 file changed

Lines changed: 36 additions & 13 deletions

File tree

fuzz/fuzz_targets/standard/fuzz_database_states.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use ssv_types::{
99
Cluster, ClusterId, OperatorId, Share, ValidatorIndex, ValidatorMetadata, ENCRYPTED_KEY_LENGTH,
1010
};
1111
use ssz::Decode;
12-
use tempfile::NamedTempFile;
1312
use std::{path::Path, sync::OnceLock};
13+
use tempfile::NamedTempFile;
1414
use types::{Address, Graffiti, PublicKeyBytes};
1515

1616
const TESTING_KEY: &str = "-----BEGIN PRIVATE KEY-----
@@ -44,7 +44,15 @@ SPdvWXU4osCd7vgiJvAP4ek=
4444

4545
static RSA_KEY: OnceLock<Rsa<openssl::pkey::Public>> = OnceLock::new();
4646

47-
#[derive(Arbitrary)]
47+
#[derive(Arbitrary, Debug)]
48+
enum DbAction {
49+
InsertValidator,
50+
UpdateStatus(bool),
51+
DeleteValidator,
52+
BumpNonce,
53+
}
54+
55+
#[derive(Arbitrary, Debug)]
4856
struct FuzzData {
4957
cluster_id: [u8; 32],
5058
owner: [u8; 20],
@@ -56,6 +64,7 @@ struct FuzzData {
5664
encrypted_private_key: [u8; ENCRYPTED_KEY_LENGTH],
5765
validator_index: usize,
5866
graffiti: [u8; 32],
67+
actions: Vec<DbAction>,
5968
}
6069

6170
fn fuzz_with_data(fuzz: FuzzData) {
@@ -117,22 +126,37 @@ fn fuzz_with_data(fuzz: FuzzData) {
117126
encrypted_private_key: fuzz.encrypted_private_key,
118127
};
119128

120-
// Test insert_validator
121-
let Ok(conn) = old.connection() else {
122-
return;
123-
};
124-
let Ok(tx) = conn.unchecked_transaction() else {
125-
return;
126-
};
129+
for action in fuzz.actions {
130+
let Ok(conn) = old.connection() else {
131+
return;
132+
};
133+
let Ok(tx) = conn.unchecked_transaction() else {
134+
return;
135+
};
127136

128-
let _ = old.insert_validator(cluster, &validator, vec![share], &tx);
129-
let _ = tx.commit();
137+
match action {
138+
DbAction::InsertValidator => {
139+
let _ = old.insert_validator(cluster.clone(), &validator, vec![share.clone()], &tx);
140+
}
141+
DbAction::UpdateStatus(status) => {
142+
let _ = old.update_status(ClusterId(fuzz.cluster_id), status, &tx);
143+
}
144+
DbAction::DeleteValidator => {
145+
let _ = old.delete_validator(&validator.public_key, &tx);
146+
}
147+
DbAction::BumpNonce => {
148+
let _ = old.bump_and_get_nonce(&cluster.owner, &tx);
149+
}
150+
}
151+
152+
let _ = tx.commit();
153+
}
130154

131155
let Ok(new) = NetworkDatabase::new(Path::new(&db_path), rsa_key, Default::default()) else {
132156
return;
133157
};
134158

135-
// Compare states. I'm not sure about this part.
159+
// Compare states
136160
let old_state = old.state();
137161
let new_state = new.state();
138162

@@ -152,7 +176,6 @@ fn fuzz_with_data(fuzz: FuzzData) {
152176
let old_clusters: Vec<_> = old_state.clusters().values().cloned().collect();
153177
let new_clusters: Vec<_> = new_state.clusters().values().cloned().collect();
154178
assert_eq!(old_clusters, new_clusters, "clusters differ");
155-
156179
}
157180

158181
fn main() {

0 commit comments

Comments
 (0)