Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
fix: extract validator pubkeys from genesis (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdplm authored Oct 6, 2023
1 parent 7d0416e commit 4ce1428
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions crates/topos/src/config/genesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ impl Genesis {
}

/// Parse the validators from the `extraData` field of the genesis file.
/// The `extraData` is patted with 32 bytes, and the validators are RLP encoded.
/// The `extraData` is padded with 32 bytes, and the validators are RLP encoded.
/// Each validator is 20 bytes, with a SEAL at the end of the whole list (8 bytes)
#[allow(dead_code)]
pub fn validators(&self) -> HashSet<ValidatorId> {
let extra_data = self.json["genesis"]["extraData"]
.as_str()
.unwrap()
.expect("The extraData field must be present. Bad genesis file?")
.to_string();

// Define constants for the prefix size and validator size
Expand All @@ -89,21 +88,22 @@ impl Genesis {

// Get the first Rlp item (index 0) and iterate over its items
let first_item = rlp.at(0).expect("Failed to get first RLP item");
let mut validator_public_keys = HashSet::new();

for i in 0..first_item.item_count().unwrap() {
let validator_data = first_item.at(i).expect("Failed to get RLP item").data();
if let Ok(validator_data) = validator_data {
let public_key = validator_data.to_vec();
let address = format!("0x{}", hex::encode(&public_key[1..=20]));
validator_public_keys.insert(
ValidatorId::try_from(address.as_str()).unwrap_or_else(|error| {
panic!("Failed to convert address to ValidatorId: {:?}", error)
}),
);
}
}

validator_public_keys
let item_count = first_item
.item_count()
.expect("Validators must be an RLP list. Bad genesis file?");
first_item.into_iter().fold(
HashSet::with_capacity(item_count),
|mut validator_public_keys, validator_rlp| {
if let Ok(public_key) = validator_rlp.data() {
let address = format!("0x{}", hex::encode(&public_key[1..=20]));
validator_public_keys.insert(
ValidatorId::try_from(address.as_str()).unwrap_or_else(|error| {
panic!("Failed to convert address to ValidatorId: {:?}", error)
}),
);
}
validator_public_keys
},
)
}
}

0 comments on commit 4ce1428

Please sign in to comment.