snarkvm_synthesizer/vm/helpers/
committee.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright 2024 Aleo Network Foundation
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(clippy::redundant_closure)]

use console::{
    account::Address,
    network::Network,
    prelude::{cfg_into_iter, cfg_iter, cfg_reduce},
    program::{Identifier, Literal, Plaintext, Value},
    types::{Boolean, U8, U64},
};
use ledger_committee::Committee;

use anyhow::{Result, bail, ensure};
use indexmap::{IndexMap, indexmap};
use std::str::FromStr;

#[cfg(not(feature = "serial"))]
use rayon::prelude::*;

/// Returns the committee given the committee map from finalize storage.
pub fn committee_and_delegated_maps_into_committee<N: Network>(
    starting_round: u64,
    committee_map: Vec<(Plaintext<N>, Value<N>)>,
    delegated_map: Vec<(Plaintext<N>, Value<N>)>,
) -> Result<Committee<N>> {
    // Prepare the identifiers.
    let is_open_identifier: Identifier<N> = Identifier::from_str("is_open")?;
    let commission_identifier: Identifier<N> = Identifier::from_str("commission")?;

    // Extract the committee members.
    let committee_members: IndexMap<Address<N>, (u64, bool, u8)> = committee_map
        .iter()
        .map(|(key, value)| {
            // Extract the address from the key.
            let address = match key {
                Plaintext::Literal(Literal::Address(address), _) => address,
                _ => bail!("Invalid committee key (missing address) - {key}"),
            };

            // Extract the committee state from the value.
            let (is_open, commission) = match value {
                Value::Plaintext(Plaintext::Struct(state, _)) => {
                    // Extract the is_open flag from the value.
                    let is_open = match state.get(&is_open_identifier) {
                        Some(Plaintext::Literal(Literal::Boolean(is_open), _)) => **is_open,
                        _ => bail!("Invalid committee state (missing boolean) - {value}"),
                    };
                    // Extract the commission from the value.
                    let commission = match state.get(&commission_identifier) {
                        Some(Plaintext::Literal(Literal::U8(commission), _)) => **commission,
                        _ => bail!("Invalid committee state (missing commission) - {value}"),
                    };
                    // Return the committee state.
                    (is_open, commission)
                }
                _ => bail!("Invalid committee value (missing struct) - {value}"),
            };

            // Extract the microcredits for the address from the delegated map.
            let Some(microcredits) = delegated_map.iter().find_map(|(delegated_key, delegated_value)| {
                // Retrieve the delegated address.
                let delegated_address = match delegated_key {
                    Plaintext::Literal(Literal::Address(address), _) => Some(address),
                    _ => None,
                };
                // Check if the address matches.
                match delegated_address == Some(address) {
                    // Extract the microcredits from the value.
                    true => match delegated_value {
                        Value::Plaintext(Plaintext::Literal(Literal::U64(microcredits), _)) => Some(**microcredits),
                        _ => None,
                    },
                    false => None,
                }
            }) else {
                bail!("Missing microcredits for committee member - {address}");
            };

            // Return the committee member.
            Ok((*address, (microcredits, is_open, commission)))
        })
        .collect::<Result<IndexMap<_, _>>>()?;

    // Return the committee.
    Committee::new(starting_round, committee_members)
}

/// Returns the stakers given the bonded map from finalize storage.
pub fn bonded_map_into_stakers<N: Network>(
    bonded_map: Vec<(Plaintext<N>, Value<N>)>,
) -> Result<IndexMap<Address<N>, (Address<N>, u64)>> {
    // Prepare the identifiers.
    let validator_identifier = Identifier::from_str("validator")?;
    let microcredits_identifier = Identifier::from_str("microcredits")?;

    // Convert the given key and value into a staker entry.
    let convert = |key, value| {
        // Extract the staker from the key.
        let address = match key {
            Plaintext::Literal(Literal::Address(address), _) => address,
            _ => bail!("Invalid bonded key (missing staker) - {key}"),
        };
        // Extract the bonded state from the value.
        match &value {
            Value::Plaintext(Plaintext::Struct(state, _)) => {
                // Extract the validator from the value.
                let validator = match state.get(&validator_identifier) {
                    Some(Plaintext::Literal(Literal::Address(validator), _)) => *validator,
                    _ => bail!("Invalid bonded state (missing validator) - {value}"),
                };
                // Extract the microcredits from the value.
                let microcredits = match state.get(&microcredits_identifier) {
                    Some(Plaintext::Literal(Literal::U64(microcredits), _)) => **microcredits,
                    _ => bail!("Invalid bonded state (missing microcredits) - {value}"),
                };
                // Return the bonded state.
                Ok((address, (validator, microcredits)))
            }
            _ => bail!("Invalid bonded value (missing struct) - {value}"),
        }
    };

    // Convert the bonded map into stakers.
    bonded_map.into_iter().map(|(key, value)| convert(key, value)).collect::<Result<IndexMap<_, _>>>()
}

/// Checks that the given committee from committee storage matches the given stakers.
pub fn ensure_stakers_matches<N: Network>(
    committee: &Committee<N>,
    stakers: &IndexMap<Address<N>, (Address<N>, u64)>,
) -> Result<()> {
    // Construct the validator map.
    let validator_map: IndexMap<_, _> = cfg_reduce!(
        cfg_into_iter!(stakers)
            .map(|(_, (validator, microcredits))| {
                if committee.members().contains_key(validator) {
                    Some(indexmap! {*validator => *microcredits})
                } else {
                    None
                }
            })
            .flatten(),
        || IndexMap::new(),
        |mut acc, e| {
            for (validator, microcredits) in e {
                let entry: &mut u64 = acc.entry(validator).or_default();
                *entry = entry.saturating_add(microcredits);
            }
            acc
        }
    );

    // Compute the total microcredits.
    let total_microcredits =
        cfg_reduce!(cfg_iter!(validator_map).map(|(_, microcredits)| *microcredits), || 0u64, |a, b| {
            // Add the staker's microcredits to the total microcredits.
            a.saturating_add(b)
        });

    // Ensure the committee and committee map match.
    ensure!(committee.members().len() == validator_map.len(), "Committee and validator map length do not match");
    // Ensure the total microcredits match.
    ensure!(committee.total_stake() == total_microcredits, "Committee and validator map total stake do not match");

    // Iterate over the committee and ensure the committee and validators match.
    for (validator, (microcredits, _, _)) in committee.members() {
        let candidate_microcredits = validator_map.get(validator);
        ensure!(candidate_microcredits.is_some(), "A validator is missing in finalize storage");
        ensure!(
            *microcredits == *candidate_microcredits.unwrap(),
            "Committee contains an incorrect 'microcredits' amount from stakers"
        );
    }

    Ok(())
}

/// Returns the next committee, given the current committee and stakers.
pub fn to_next_committee<N: Network>(
    current_committee: &Committee<N>,
    next_round: u64,
    next_delegated: &IndexMap<Address<N>, u64>,
) -> Result<Committee<N>> {
    // Return the next committee.
    Committee::new(
        next_round,
        cfg_iter!(next_delegated)
            .flat_map(|(delegatee, microcredits)| {
                let Some((_, is_open, commission)) = current_committee.members().get(delegatee) else {
                    // Do nothing, as the delegatee is not part of the committee.
                    return None;
                };
                Some((*delegatee, (*microcredits, *is_open, *commission)))
            })
            .collect(),
    )
}

pub fn to_next_delegated<N: Network>(
    next_stakers: &IndexMap<Address<N>, (Address<N>, u64)>,
) -> IndexMap<Address<N>, u64> {
    // Construct the delegated map.
    let delegated_map: IndexMap<Address<N>, u64> = cfg_reduce!(
        cfg_into_iter!(next_stakers).map(|(_, (delegatee, microcredits))| indexmap! {*delegatee => *microcredits}),
        || IndexMap::new(),
        |mut acc, e| {
            for (delegatee, microcredits) in e {
                let entry: &mut u64 = acc.entry(delegatee).or_default();
                *entry = entry.saturating_add(microcredits);
            }
            acc
        }
    );

    delegated_map
}

/// Returns the committee map, bonded map, and delegated map, given the committee and stakers.
pub fn to_next_committee_bonded_delegated_map<N: Network>(
    next_committee: &Committee<N>,
    next_stakers: &IndexMap<Address<N>, (Address<N>, u64)>,
    next_delegated: &IndexMap<Address<N>, u64>,
) -> (Vec<(Plaintext<N>, Value<N>)>, Vec<(Plaintext<N>, Value<N>)>, Vec<(Plaintext<N>, Value<N>)>) {
    // Prepare the identifiers.
    let validator_identifier = Identifier::from_str("validator").expect("Failed to parse 'validator'");
    let microcredits_identifier = Identifier::from_str("microcredits").expect("Failed to parse 'microcredits'");
    let is_open_identifier = Identifier::from_str("is_open").expect("Failed to parse 'is_open'");
    let commission_identifier = Identifier::from_str("commission").expect("Failed to parse 'commission'");

    // Construct the committee map.
    let committee_map = cfg_iter!(next_committee.members())
        .map(|(validator, (_, is_open, commission))| {
            // Construct the committee state.
            let committee_state = indexmap! {
                is_open_identifier => Plaintext::from(Literal::Boolean(Boolean::new(*is_open))),
                commission_identifier => Plaintext::from(Literal::U8(U8::new(*commission))),
            };
            // Return the committee state.
            (
                Plaintext::from(Literal::Address(*validator)),
                Value::Plaintext(Plaintext::Struct(committee_state, Default::default())),
            )
        })
        .collect::<Vec<_>>();

    // Construct the bonded map.
    let bonded_map = cfg_iter!(next_stakers)
        .map(|(staker, (validator, microcredits))| {
            // Construct the bonded state.
            let bonded_state = indexmap! {
                validator_identifier => Plaintext::from(Literal::Address(*validator)),
                microcredits_identifier => Plaintext::from(Literal::U64(U64::new(*microcredits))),
            };
            // Return the bonded state.
            (
                Plaintext::from(Literal::Address(*staker)),
                Value::Plaintext(Plaintext::Struct(bonded_state, Default::default())),
            )
        })
        .collect::<Vec<_>>();

    // Construct the delegated map.
    let delegated_map = cfg_iter!(next_delegated)
        .map(|(delegatee, microcredits)| {
            (
                Plaintext::from(Literal::Address(*delegatee)),
                Value::Plaintext(Plaintext::Literal(Literal::U64(U64::new(*microcredits)), Default::default())),
            )
        })
        .collect::<Vec<_>>();

    (committee_map, bonded_map, delegated_map)
}

/// Returns the withdraw map, given the withdrawal addresses.
pub fn to_next_withdraw_map<N: Network>(
    withdrawal_addresses: &IndexMap<Address<N>, Address<N>>,
) -> Vec<(Plaintext<N>, Value<N>)> {
    cfg_iter!(withdrawal_addresses)
        .map(|(staker, withdraw_address)| {
            (
                Plaintext::from(Literal::Address(*staker)),
                Value::Plaintext(Plaintext::Literal(Literal::Address(*withdraw_address), Default::default())),
            )
        })
        .collect::<Vec<_>>()
}

#[cfg(test)]
pub(crate) mod test_helpers {
    use super::*;
    use crate::vm::TestRng;
    use ledger_committee::{MIN_DELEGATOR_STAKE, MIN_VALIDATOR_STAKE};

    use rand::{CryptoRng, Rng};

    /// Returns the stakers, given the map of `(validator, (microcredits, is_open, commission))` entries.
    /// This method simulates the existence of delegators for the members.
    pub(crate) fn to_stakers<N: Network, R: Rng + CryptoRng>(
        members: &IndexMap<Address<N>, (u64, bool, u8)>,
        rng: &mut R,
    ) -> IndexMap<Address<N>, (Address<N>, u64)> {
        members
            .into_iter()
            .flat_map(|(validator, (microcredits, _, _))| {
                // Keep a tally of the remaining microcredits.
                let remaining_microcredits = microcredits.saturating_sub(MIN_VALIDATOR_STAKE);
                // Set the staker amount to `MIN_DELEGATOR_STAKE` microcredits.
                let staker_amount = MIN_DELEGATOR_STAKE;
                // Determine the number of iterations.
                let num_iterations = (remaining_microcredits / staker_amount).saturating_sub(1);

                // Construct the map of stakers.
                let rngs = (0..num_iterations).map(|_| TestRng::from_seed(rng.gen())).collect::<Vec<_>>();
                let mut stakers: IndexMap<_, _> = cfg_into_iter!(rngs)
                    .map(|mut rng| {
                        // Sample a random staker.
                        let staker = Address::<N>::new(rng.gen());
                        // Output the staker.
                        (staker, (*validator, staker_amount))
                    })
                    .collect();

                // Insert the validator.
                stakers.insert(*validator, (*validator, MIN_VALIDATOR_STAKE));

                // Insert the last staker.
                let final_amount = remaining_microcredits.saturating_sub(num_iterations * staker_amount);
                if final_amount > 0 {
                    let staker = Address::<N>::new(rng.gen());
                    stakers.insert(staker, (*validator, final_amount));
                }
                // Return the stakers.
                stakers
            })
            .collect()
    }

    /// Returns the validator delegation totals, given the map of `(validator, (microcredits, is_open, commission))` entries.
    /// This method simulates the existence of delegators for the members.
    pub(crate) fn to_delegations<N: Network>(
        members: &IndexMap<Address<N>, (u64, bool, u8)>,
    ) -> IndexMap<Address<N>, u64> {
        members.into_iter().map(|(validator, (microcredits, _, _))| (*validator, *microcredits)).collect()
    }

    /// Returns the withdrawal addresses, given the stakers.
    /// This method simulates the existence of unique withdrawal addresses for the stakers.
    pub(crate) fn to_withdraw_addresses<N: Network, R: Rng + CryptoRng>(
        stakers: &IndexMap<Address<N>, (Address<N>, u64)>,
        rng: &mut R,
    ) -> IndexMap<Address<N>, Address<N>> {
        stakers
            .into_iter()
            .map(|(staker, _)| {
                // Sample a random withdraw address.
                let withdraw_address = Address::<N>::new(rng.gen());
                // Return the withdraw address.
                (*staker, withdraw_address)
            })
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use console::prelude::TestRng;

    #[allow(unused_imports)]
    use rayon::prelude::*;
    use std::str::FromStr;

    /// Returns the committee map, given the map of `(validator, (microcredits, is_open, commission))` entries.
    fn to_committee_map<N: Network>(members: &IndexMap<Address<N>, (u64, bool, u8)>) -> Vec<(Plaintext<N>, Value<N>)> {
        members
            .par_iter()
            .map(|(validator, (_, is_open, commission))| {
                let is_open = Boolean::<N>::new(*is_open);
                let commission = U8::<N>::new(*commission);
                (
                    Plaintext::from(Literal::Address(*validator)),
                    Value::from_str(&format!("{{ is_open: {is_open}, commission: {commission} }}")).unwrap(),
                )
            })
            .collect()
    }

    /// Returns the delegated map, given the map of `(validator, (microcredits, is_open, commission))` entries.
    fn to_delegated_map<N: Network>(members: &IndexMap<Address<N>, (u64, bool, u8)>) -> Vec<(Plaintext<N>, Value<N>)> {
        members
            .par_iter()
            .map(|(validator, (microcredits, _, _))| {
                (
                    Plaintext::from(Literal::Address(*validator)),
                    Value::Plaintext(Plaintext::Literal(Literal::U64(U64::new(*microcredits)), Default::default())),
                )
            })
            .collect()
    }

    /// Returns the bonded map, given the staker, validator and microcredits.
    fn to_bonded_map<N: Network>(stakers: &IndexMap<Address<N>, (Address<N>, u64)>) -> Vec<(Plaintext<N>, Value<N>)> {
        // Prepare the identifiers.
        let validator_identifier = Identifier::from_str("validator").expect("Failed to parse 'validator'");
        let microcredits_identifier = Identifier::from_str("microcredits").expect("Failed to parse 'microcredits'");

        stakers
            .par_iter()
            .map(|(staker, (validator, microcredits))| {
                // Construct the bonded state.
                let bonded_state = indexmap! {
                    validator_identifier => Plaintext::from(Literal::Address(*validator)),
                    microcredits_identifier => Plaintext::from(Literal::U64(U64::new(*microcredits))),
                };
                // Return the bonded state.
                (
                    Plaintext::from(Literal::Address(*staker)),
                    Value::Plaintext(Plaintext::Struct(bonded_state, Default::default())),
                )
            })
            .collect()
    }

    /// Returns the withdrawal addresses given the withdraw map from finalize storage.
    pub fn withdraw_map_to_withdrawal_addresses<N: Network>(
        withdraw_map: Vec<(Plaintext<N>, Value<N>)>,
    ) -> Result<IndexMap<Address<N>, Address<N>>> {
        // Convert the given key and value into a staker entry.
        let convert = |key, value| {
            // Extract the staker from the key.
            let staker = match key {
                Plaintext::Literal(Literal::Address(address), _) => address,
                _ => bail!("Invalid withdraw key (missing staker) - {key}"),
            };

            // Extract the withdrawal address from the value.
            let withdrawal_address = match value {
                Value::Plaintext(Plaintext::Literal(Literal::Address(address), _)) => address,
                _ => bail!("Invalid withdraw value (missing address) - {key}"),
            };

            Ok((staker, withdrawal_address))
        };

        // Convert the withdraw map into withdrawal addresses.
        withdraw_map.into_iter().map(|(key, value)| convert(key, value)).collect::<Result<IndexMap<_, _>>>()
    }

    #[test]
    fn test_committee_and_delegated_maps_into_committee() {
        let rng = &mut TestRng::default();

        // Sample a committee.
        let committee = ledger_committee::test_helpers::sample_committee_for_round_and_size(1, 100, rng);

        // Initialize the committee map.
        let committee_map = to_committee_map(committee.members());

        // Initialize the delegated map.
        let delegated_map = to_delegated_map(committee.members());

        // Start a timer.
        let timer = std::time::Instant::now();
        // Convert the committee map into a committee.
        let candidate_committee =
            committee_and_delegated_maps_into_committee(committee.starting_round(), committee_map, delegated_map)
                .unwrap();
        println!("committee_and_delegated_maps_into_committee: {}ms", timer.elapsed().as_millis());
        assert_eq!(candidate_committee, committee);
    }

    #[test]
    fn test_bonded_map_into_stakers() {
        let rng = &mut TestRng::default();

        // Sample a committee.
        let committee = ledger_committee::test_helpers::sample_committee_for_round_and_size(1, 100, rng);
        // Convert the committee into stakers.
        let expected_stakers = crate::committee::test_helpers::to_stakers(committee.members(), rng);
        // Initialize the bonded map.
        let bonded_map = to_bonded_map(&expected_stakers);

        // Start a timer.
        let timer = std::time::Instant::now();
        // Convert the bonded map into stakers.
        let candidate_stakers = bonded_map_into_stakers(bonded_map).unwrap();
        println!("bonded_map_into_stakers: {}ms", timer.elapsed().as_millis());
        assert_eq!(candidate_stakers.len(), expected_stakers.len());
        assert_eq!(candidate_stakers, expected_stakers);
    }

    #[test]
    fn test_ensure_stakers_matches() {
        let rng = &mut TestRng::default();

        // Sample a committee.
        let committee = ledger_committee::test_helpers::sample_committee_for_round_and_size(1, 100, rng);
        // Convert the committee into stakers.
        let stakers = crate::committee::test_helpers::to_stakers(committee.members(), rng);

        // Start a timer.
        let timer = std::time::Instant::now();
        // Ensure the stakers matches.
        let result = ensure_stakers_matches(&committee, &stakers);
        println!("ensure_stakers_matches: {}ms", timer.elapsed().as_millis());
        assert!(result.is_ok());
    }

    #[test]
    fn test_to_next_committee() {
        let rng = &mut TestRng::default();

        // Sample a committee.
        let committee = ledger_committee::test_helpers::sample_committee_for_round_and_size(1, 100, rng);
        // Convert the committee into stakers.
        let _stakers = crate::committee::test_helpers::to_stakers(committee.members(), rng);
        // Convert the committee into delegations.
        let delegations = crate::committee::test_helpers::to_delegations(committee.members());

        // Start a timer.
        let timer = std::time::Instant::now();
        // Ensure the next committee matches the current committee.
        // Note: We can perform this check, in this specific case only, because we did not apply staking rewards.
        let next_committee = to_next_committee(&committee, committee.starting_round() + 1, &delegations).unwrap();
        println!("to_next_committee: {}ms", timer.elapsed().as_millis());
        assert_eq!(committee.starting_round() + 1, next_committee.starting_round());
        assert_eq!(committee.members(), next_committee.members());
    }

    #[test]
    fn test_to_next_committee_bonded_delegated_map() {
        let rng = &mut TestRng::default();

        // Sample a committee.
        let committee = ledger_committee::test_helpers::sample_committee(rng);
        // Convert the committee into stakers.
        let stakers: IndexMap<Address<console::network::MainnetV0>, (Address<console::network::MainnetV0>, u64)> =
            crate::committee::test_helpers::to_stakers(committee.members(), rng);
        // Convert the committee into delegations.
        let delegations = crate::committee::test_helpers::to_delegations(committee.members());

        // Start a timer.
        let timer = std::time::Instant::now();
        // Ensure the next committee matches the current committee.
        // Note: We can perform this check, in this specific case only, because we did not apply staking rewards.
        let (committee_map, bonded_map, _) = to_next_committee_bonded_delegated_map(&committee, &stakers, &delegations);
        println!("to_next_committee_bonded_delegated_map: {}ms", timer.elapsed().as_millis());
        assert_eq!(committee_map, to_committee_map(committee.members()));
        assert_eq!(bonded_map, to_bonded_map(&stakers));
    }

    #[test]
    fn test_to_withdraw_map() {
        let rng = &mut TestRng::default();

        // Sample a committee.
        let committee = ledger_committee::test_helpers::sample_committee(rng);
        // Convert the committee into stakers.
        let stakers = crate::committee::test_helpers::to_stakers(committee.members(), rng);

        // Construct the withdraw addresses.
        let withdrawal_addresses = crate::committee::test_helpers::to_withdraw_addresses(&stakers, rng);

        // Start a timer.
        let timer = std::time::Instant::now();
        // Ensure the withdrawal map is correct.
        let withdrawal_map = to_next_withdraw_map(&withdrawal_addresses);
        println!("to_next_withdraw_map: {}ms", timer.elapsed().as_millis());
        assert_eq!(withdrawal_addresses, withdraw_map_to_withdrawal_addresses(withdrawal_map).unwrap());
    }
}