Skip to content

Commit 2b34b47

Browse files
authored
Merge pull request #2665 from kaimast/sample-batch-with-committee
Add more test-helpers for batch certificates
2 parents 7e69369 + 1cda7c6 commit 2b34b47

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

console/types/address/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub use snarkvm_console_types_boolean::Boolean;
3636
pub use snarkvm_console_types_field::Field;
3737
pub use snarkvm_console_types_group::Group;
3838

39+
/// The unique ID of an Aleo account, which is derived from its view key.
3940
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
4041
pub struct Address<E: Environment> {
4142
/// The underlying address.

ledger/committee/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ impl<N: Network> Committee<N> {
255255
#[cfg(any(test, feature = "test-helpers"))]
256256
pub mod test_helpers {
257257
use super::*;
258-
use console::{account::Address, prelude::TestRng};
258+
use console::{
259+
account::{Address, PrivateKey},
260+
prelude::TestRng,
261+
};
259262

260263
use indexmap::IndexMap;
261264
use rand_distr::{Distribution, Exp};
@@ -313,6 +316,28 @@ pub mod test_helpers {
313316
Committee::<CurrentNetwork>::new(round, members).unwrap()
314317
}
315318

319+
/// Generates a committee for the specified round, and private keys for its members.
320+
///
321+
/// This is slower than `sample_committee_for_round` or `sample_committee_for_round_and_size` due to the additional key generation.
322+
pub fn sample_committee_and_keys_for_round(
323+
round: u64,
324+
num_members: usize,
325+
rng: &mut TestRng,
326+
) -> (Committee<CurrentNetwork>, Vec<PrivateKey<CurrentNetwork>>) {
327+
// Sample the members.
328+
let mut members = IndexMap::with_capacity(num_members);
329+
let mut private_keys = Vec::with_capacity(num_members);
330+
for _ in 0..num_members {
331+
let private_key = PrivateKey::new(rng).unwrap();
332+
let address = Address::try_from(private_key).unwrap();
333+
let is_open = rng.gen();
334+
private_keys.push(private_key);
335+
members.insert(address, (2 * MIN_VALIDATOR_STAKE, is_open, 0));
336+
}
337+
// Return the committee and keys.
338+
(Committee::<CurrentNetwork>::new(round, members).unwrap(), private_keys)
339+
}
340+
316341
/// Samples a random committee for a given round and members.
317342
pub fn sample_committee_for_round_and_members(
318343
round: u64,

ledger/narwhal/batch-certificate/src/lib.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,25 +172,42 @@ pub mod test_helpers {
172172
sample_batch_certificate_for_round_with_previous_certificate_ids(round, certificate_ids, rng)
173173
}
174174

175-
/// Returns a sample batch certificate with a given round; the rest is sampled at random.
175+
/// Returns a sample batch certificate with a given round and the given certificate ids as predecessors; the rest is sampled at random.
176176
pub fn sample_batch_certificate_for_round_with_previous_certificate_ids(
177177
round: u64,
178178
previous_certificate_ids: IndexSet<Field<CurrentNetwork>>,
179179
rng: &mut TestRng,
180+
) -> BatchCertificate<CurrentNetwork> {
181+
let committee: Vec<_> = (0..5).map(|_| PrivateKey::new(rng).unwrap()).collect();
182+
sample_batch_certificate_for_round_with_committee(
183+
round,
184+
previous_certificate_ids,
185+
&committee[0],
186+
&committee[1..],
187+
rng,
188+
)
189+
}
190+
191+
/// Same as `sample_batch_certificate_for_round_with_previous_certificate_ids`, but also allows you to set the private keys that sign the certificate.
192+
pub fn sample_batch_certificate_for_round_with_committee(
193+
round: u64,
194+
previous_certificate_ids: IndexSet<Field<CurrentNetwork>>,
195+
author: &PrivateKey<CurrentNetwork>,
196+
signers: &[PrivateKey<CurrentNetwork>],
197+
rng: &mut TestRng,
180198
) -> BatchCertificate<CurrentNetwork> {
181199
// Sample a batch header.
182200
let batch_header =
183-
narwhal_batch_header::test_helpers::sample_batch_header_for_round_with_previous_certificate_ids(
201+
narwhal_batch_header::test_helpers::sample_batch_header_for_round_and_key_with_previous_certificate_ids(
184202
round,
203+
author,
185204
previous_certificate_ids,
186205
rng,
187206
);
188-
// Sample a list of signatures.
189-
let mut signatures = IndexSet::with_capacity(5);
190-
for _ in 0..5 {
191-
let private_key = PrivateKey::new(rng).unwrap();
192-
signatures.insert(private_key.sign(&[batch_header.batch_id()], rng).unwrap());
193-
}
207+
// Generate the endorsements.
208+
let signatures: IndexSet<_> =
209+
signers.iter().map(|private_key| private_key.sign(&[batch_header.batch_id()], rng).unwrap()).collect();
210+
194211
// Return the batch certificate.
195212
BatchCertificate::from(batch_header, signatures).unwrap()
196213
}

ledger/narwhal/batch-header/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,22 @@ pub mod test_helpers {
276276
) -> BatchHeader<CurrentNetwork> {
277277
// Sample a private key.
278278
let private_key = PrivateKey::new(rng).unwrap();
279+
// Generate a new certificated with the key as its author.
280+
sample_batch_header_for_round_and_key_with_previous_certificate_ids(
281+
round,
282+
&private_key,
283+
previous_certificate_ids,
284+
rng,
285+
)
286+
}
287+
288+
/// Returns a sample batch header with a given round, author key, and set of previous certificate IDs; the rest is sampled at random.
289+
pub fn sample_batch_header_for_round_and_key_with_previous_certificate_ids(
290+
round: u64,
291+
private_key: &PrivateKey<CurrentNetwork>,
292+
previous_certificate_ids: IndexSet<Field<CurrentNetwork>>,
293+
rng: &mut TestRng,
294+
) -> BatchHeader<CurrentNetwork> {
279295
// Sample the committee ID.
280296
let committee_id = Field::<CurrentNetwork>::rand(rng);
281297
// Sample transmission IDs.
@@ -284,7 +300,7 @@ pub mod test_helpers {
284300
// Checkpoint the timestamp for the batch.
285301
let timestamp = OffsetDateTime::now_utc().unix_timestamp();
286302
// Return the batch header.
287-
BatchHeader::new(&private_key, round, timestamp, committee_id, transmission_ids, previous_certificate_ids, rng)
303+
BatchHeader::new(private_key, round, timestamp, committee_id, transmission_ids, previous_certificate_ids, rng)
288304
.unwrap()
289305
}
290306

0 commit comments

Comments
 (0)