Skip to content

Fix get_custody_groups not sorting the result #7711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion beacon_node/lighthouse_network/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use lru_cache::LRUTimeCache;
use peerdb::{BanOperation, BanResult, ScoreUpdateResult};
use rand::seq::SliceRandom;
use smallvec::SmallVec;
use std::collections::BTreeSet;
use std::{
sync::Arc,
time::{Duration, Instant},
Expand Down Expand Up @@ -1470,7 +1471,7 @@ impl<E: EthSpec> PeerManager<E> {
&self,
peer_id: &PeerId,
custody_group_count: u64,
) -> Result<HashSet<CustodyIndex>, String> {
) -> Result<BTreeSet<CustodyIndex>, String> {
// If we don't have a node id, we cannot compute the custody duties anyway
let node_id = peer_id_to_node_id(peer_id)?;
let spec = &self.network_globals.spec;
Expand Down
30 changes: 26 additions & 4 deletions consensus/types/src/data_column_custody_group.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{ChainSpec, ColumnIndex, DataColumnSubnetId};
use alloy_primitives::U256;
use itertools::Itertools;
use maplit::hashset;
use maplit::btreeset;
use safe_arith::{ArithError, SafeArith};
use std::collections::HashSet;
use std::collections::{BTreeSet, HashSet};

pub type CustodyIndex = u64;

Expand All @@ -24,14 +24,14 @@ pub fn get_custody_groups(
raw_node_id: [u8; 32],
custody_group_count: u64,
spec: &ChainSpec,
) -> Result<HashSet<CustodyIndex>, DataColumnCustodyGroupError> {
) -> Result<BTreeSet<CustodyIndex>, DataColumnCustodyGroupError> {
if custody_group_count > spec.number_of_custody_groups {
return Err(DataColumnCustodyGroupError::InvalidCustodyGroupCount(
custody_group_count,
));
}

let mut custody_groups: HashSet<u64> = hashset![];
let mut custody_groups: BTreeSet<u64> = btreeset![];
let mut current_id = U256::from_be_slice(&raw_node_id);
while custody_groups.len() < custody_group_count as usize {
let mut node_id_bytes = [0u8; 32];
Expand Down Expand Up @@ -141,4 +141,26 @@ mod test {
assert_eq!(subnets.len(), subnets_per_custody_group as usize);
}
}

#[test]
fn test_get_custody_groups_sorted() {
let mut spec = ChainSpec::mainnet();
spec.number_of_custody_groups = 64;
let custody_group_count = 32;
let node_id = [1u8; 32];

let custody_groups = get_custody_groups(node_id, custody_group_count, &spec)
.expect("should compute custody groups successfully");

let vec = custody_groups.iter().collect::<Vec<_>>();
let sorted_vec = {
let mut temp_vec = vec.clone();
temp_vec.sort();
temp_vec
};

for (a, b) in sorted_vec.iter().zip(vec.iter()) {
assert_eq!(a, b);
}
}
}
1 change: 0 additions & 1 deletion testing/ef_tests/src/cases/get_custody_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ impl<E: EthSpec> Case for GetCustodyGroups<E> {
let mut computed = get_custody_groups(raw_node_id, self.custody_group_count, &spec)
.map(|set| set.into_iter().collect::<Vec<_>>())
.expect("should compute custody groups");
computed.sort();

let expected = &self.result;
if computed == *expected {
Expand Down