From 70b8c8e9ce3a0c5e14e84a3abb93026918106ef2 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Mon, 8 Apr 2024 12:22:52 -0400 Subject: [PATCH] =?UTF-8?q?mock-consensus:=20=F0=9F=AB=9B=20`two=5Fvalidat?= =?UTF-8?q?ors`=20adds=20two=20keys?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- crates/test/mock-consensus/src/builder.rs | 35 +++++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/crates/test/mock-consensus/src/builder.rs b/crates/test/mock-consensus/src/builder.rs index e0fc5bae0b..09c9a9a894 100644 --- a/crates/test/mock-consensus/src/builder.rs +++ b/crates/test/mock-consensus/src/builder.rs @@ -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 } } }