Skip to content

Commit

Permalink
mock-consensus: 🫛 two_validators adds two keys
Browse files Browse the repository at this point in the history
fixes #3937.

* #3937
* #3588

this adds a `two_validators` method to the test node builder, so that
tests may set up a test node that has two validator keys.
  • Loading branch information
cratelyn committed Apr 8, 2024
1 parent 4fce953 commit 70b8c8e
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions crates/test/mock-consensus/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,38 @@ impl Builder {
);
}

// Generate a consensus key.
// Generate a key and place it in the keyring.
let mut keyring = Keyring::new();
Self::add_key(&mut keyring);

Self { keyring, ..self }
}

/// Generates a pair of validator keys.
pub fn two_validators(self) -> Self {
let Self { keyring: prev, .. } = self;

// Log a warning if we are about to overwrite any existing keys.
if !prev.is_empty() {
tracing::warn!(
count = %prev.len(),
"builder overwriting entries in keyring, this may be a bug!"
);
}

// Generate two keys and place them in the keyring.
let mut keyring = Keyring::new();
Self::add_key(&mut keyring);
Self::add_key(&mut keyring);

Self { keyring, ..self }
}

/// Generates consensus keys and places them in the provided keyring.
fn add_key(keyring: &mut Keyring) {
let sk = ed25519_consensus::SigningKey::new(rand_core::OsRng);
let vk = sk.verification_key();
tracing::trace!(verification_key = ?vk, "generated consensus key");

// Place it into the keyring.
let mut keyring = BTreeMap::new();
keyring.insert(vk, sk);

Self { keyring, ..self }
}
}

0 comments on commit 70b8c8e

Please sign in to comment.