diff --git a/crates/test/mock-consensus/src/builder.rs b/crates/test/mock-consensus/src/builder.rs index 8d583f6da2..e0fc5bae0b 100644 --- a/crates/test/mock-consensus/src/builder.rs +++ b/crates/test/mock-consensus/src/builder.rs @@ -6,8 +6,9 @@ mod init_chain; use { - crate::{keyring::Keyring, TestNode}, + crate::{Keyring, TestNode}, bytes::Bytes, + std::collections::BTreeMap, }; /// A buider, used to prepare and instantiate a new [`TestNode`]. @@ -57,9 +58,15 @@ impl Builder { ); } - Self { - keyring: Keyring::new_with_size(1), - ..self - } + // Generate a consensus key. + 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 } } } diff --git a/crates/test/mock-consensus/src/keyring.rs b/crates/test/mock-consensus/src/keyring.rs deleted file mode 100644 index 1258b664aa..0000000000 --- a/crates/test/mock-consensus/src/keyring.rs +++ /dev/null @@ -1,129 +0,0 @@ -//! Provides a [`Keyring`] for managing consensus keys. - -use crate::TestNode; - -use { - ed25519_consensus::{SigningKey, VerificationKey}, - rand_core::{CryptoRng, OsRng, RngCore}, - std::collections::btree_map::{self, BTreeMap}, -}; - -/// A keyring of [`VerificationKey`] and [`SigningKey`] consensus keys. -#[derive(Clone, Debug, Default)] -pub struct Keyring(BTreeMap); - -/// An entry in a [`Keyring`]. -pub type Entry = (VerificationKey, SigningKey); - -// === impl Keyring === - -impl Keyring { - /// Creates a new [`Keyring`]. - pub fn new() -> Self { - Self::default() - } - - /// Creates a new [`Keyring`] and fills with with `n` random entries. - pub fn new_with_size(n: usize) -> Self { - let gen = || Self::generate_key(OsRng); - std::iter::repeat_with(gen).take(n).collect() - } - - /// Returns the consensus signing key corresponding to the given verification key. - pub fn get(&self, verification_key: &VerificationKey) -> Option<&SigningKey> { - self.0.get(verification_key) - } - - /// Returns `true` if the keyring contains no elements. - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } - - /// Returns the number of entries in the keyring. - pub fn len(&self) -> usize { - self.0.len() - } - - /// Gets an iterator over the consensus verification keys in the keyring. - pub fn verification_keys(&self) -> impl Iterator { - self.0.keys() - } - - /// Gets an iterator over the consensus signing keys in the keyring. - pub fn signing_keys(&self) -> impl Iterator { - self.0.values() - } - - /// Generates a new key using the default [`OsRng`], and inserts it into the keyring. - /// - /// This returns the verification key associated with this new entry. - pub fn add_key(&mut self) -> VerificationKey { - self.add_key_with(OsRng) - } - - /// Generates a new key with the provided CSPRNG, and inserts it into the keyring. - /// - /// This returns the verification key associated with this new entry. - pub fn add_key_with(&mut self, rng: R) -> VerificationKey - where - R: RngCore + CryptoRng, - { - let (vk, sk) = Self::generate_key(rng); - self.0.insert(vk, sk); - vk - } - - /// Generates a new consensus key. - pub fn generate_key(rng: R) -> Entry - where - R: RngCore + CryptoRng, - { - let sk = ed25519_consensus::SigningKey::new(rng); - let vk = sk.verification_key(); - tracing::trace!(verification_key = ?vk, "generated consensus key"); - (vk, sk) - } -} - -type KeyringIter<'a> = btree_map::Iter<'a, VerificationKey, SigningKey>; -impl<'a> IntoIterator for &'a Keyring { - type Item = (&'a VerificationKey, &'a SigningKey); - type IntoIter = KeyringIter<'a>; - fn into_iter(self) -> KeyringIter<'a> { - self.0.iter() - } -} - -type KeyringIntoIter = btree_map::IntoIter; -impl IntoIterator for Keyring { - type Item = (VerificationKey, SigningKey); - type IntoIter = KeyringIntoIter; - fn into_iter(self) -> KeyringIntoIter { - self.0.into_iter() - } -} - -impl FromIterator for Keyring { - fn from_iter(iter: I) -> Keyring - where - I: IntoIterator, - { - let k = iter.into_iter().collect(); - Self(k) - } -} - -// === impl TestNode === - -/// Keyring-related interfaces for a test node. -impl TestNode { - /// Returns a reference to the test node's set of consensus keys. - pub fn keyring(&self) -> &Keyring { - &self.keyring - } - - /// Returns a mutable reference to the test node's set of consensus keys. - pub fn keyring_mut(&mut self) -> &mut Keyring { - &mut self.keyring - } -} diff --git a/crates/test/mock-consensus/src/lib.rs b/crates/test/mock-consensus/src/lib.rs index 0b0fa274ae..d28781899e 100644 --- a/crates/test/mock-consensus/src/lib.rs +++ b/crates/test/mock-consensus/src/lib.rs @@ -4,9 +4,13 @@ // // see penumbra-zone/penumbra#3588. +use { + ed25519_consensus::{SigningKey, VerificationKey}, + std::collections::BTreeMap, +}; + pub mod block; pub mod builder; -pub mod keyring; mod abci; @@ -23,9 +27,14 @@ pub struct TestNode { consensus: C, last_app_hash: Vec, height: tendermint::block::Height, - keyring: self::keyring::Keyring, + keyring: Keyring, } +/// An ordered map of consensus keys. +/// +/// Entries in this keyring consist of a [`VerificationKey`] and a [`SigningKey`]. +type Keyring = BTreeMap; + impl TestNode { pub const CHAIN_ID: &'static str = "penumbra-test-chain"; @@ -40,6 +49,16 @@ impl TestNode { // - https://doc.rust-lang.org/std/fmt/#formatting-traits format!("{:02X?}", self.last_app_hash) } + + /// Returns a reference to the test node's set of consensus keys. + pub fn keyring(&self) -> &Keyring { + &self.keyring + } + + /// Returns a mutable reference to the test node's set of consensus keys. + pub fn keyring_mut(&mut self) -> &mut Keyring { + &mut self.keyring + } } impl TestNode