Skip to content

Commit

Permalink
Lint and fix serde
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Aug 10, 2023
1 parent c9ae042 commit 7e045a5
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4004,7 +4004,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.read()
.get_slot::<T::EthSpec>(whisk_shuffling_decision_root, proposal_slot)
{
proposer.clone() as u64
*proposer as u64
} else {
// No known proposer for this fork and slot
return Ok(None);
Expand Down Expand Up @@ -5065,7 +5065,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
) -> Result<(), beacon_whisk_proposer_registry::Error> {
self.beacon_whisk_proposer_registry.write().insert(
Slot::new(data.proposer_slot),
data.whisk_shuffling_decision_root.clone(),
data.whisk_shuffling_decision_root,
data.validator_index as usize,
)
}
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2711,7 +2711,7 @@ pub fn serve<T: BeaconChainTypes>(
};

let whisk_proposer = WhiskProposer::from_query(&query)
.map_err(|e| warp_utils::reject::custom_bad_request(e))
.map_err(warp_utils::reject::custom_bad_request)
.unwrap();

let (block, _) = chain
Expand Down Expand Up @@ -2772,7 +2772,7 @@ pub fn serve<T: BeaconChainTypes>(
};

let whisk_proposer = WhiskProposer::from_query(&query)
.map_err(|e| warp_utils::reject::custom_bad_request(e))?;
.map_err(warp_utils::reject::custom_bad_request)?;

let (block, _) = chain
.produce_block_with_verification::<BlindedPayload<T::EthSpec>>(
Expand Down
9 changes: 2 additions & 7 deletions common/eth2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mime = "0.3.16"
pretty_reqwest_error = { path = "../../common/pretty_reqwest_error" }

[dev-dependencies]
serde_urlencoded = "0.7.1"
tokio = { version = "1.14.0", features = ["full"] }

[target.'cfg(target_os = "linux")'.dependencies]
Expand All @@ -39,10 +40,4 @@ procinfo = { version = "0.4.2", optional = true }

[features]
default = ["lighthouse"]
lighthouse = [
"proto_array",
"psutil",
"procinfo",
"store",
"slashing_protection",
]
lighthouse = ["proto_array", "psutil", "procinfo", "store", "slashing_protection"]
13 changes: 11 additions & 2 deletions common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ pub struct ValidatorBlocksQuery {
// TODO: Implement optional quoted_u64
// #[serde(with = "serde_utils::quoted_u64")]
pub proposer_index: Option<u64>,
pub k: Option<FieldElementBytes>,
// TODO WHISK: Use proper type that serializes as a string
pub k: Option<Graffiti>,
#[serde(default)]
pub dummy_whisk_proposer: bool,
}
Expand Down Expand Up @@ -727,7 +728,7 @@ impl WhiskProposer {
Ok(WhiskProposer::Dummy)
} else {
match (query.proposer_index, query.k) {
(Some(index), Some(k)) => Ok(WhiskProposer::PostWhisk { index, k }),
(Some(index), Some(k)) => Ok(WhiskProposer::PostWhisk { index, k: k.into() }),
(Some(_), None) => Err("proposer_index set without whisk k".into()),
(None, Some(_)) => Err("whisk_k set without proposer_index".into()),
(None, None) => Ok(WhiskProposer::PreWhisk),
Expand Down Expand Up @@ -1380,4 +1381,12 @@ mod tests {
Accept::Any
);
}

#[test]
fn test_deserialization() {
let query = "randao_reveal=0x93e04638aa8f0b71ff3ce950c9bb0f7a8fb6803fea887df8b88a0fc50bf21ad72639fde525744d703b92dd21fe3409d7026ba339e5fc173a955448311818e4526f7f87fdebaf9c6660c5185e6e0b3a6940fedb2689c9ec1fac837f38b572d729&proposer_index=213&k=0xfeffffff0000000001a40100fd5b42acfa275ef6f727c6ccb88262d6ac581261"; // Replace with your test string
let result: Result<ValidatorBlocksQuery, _> = serde_urlencoded::from_str(query);

assert!(result.is_ok(), "Failed to deserialize: {:?}", result.err());
}
}
6 changes: 5 additions & 1 deletion consensus/state_processing/src/per_block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ pub fn get_shuffle_indices<T: EthSpec>(randao_reveal: &Signature) -> Vec<usize>
// TODO: from_be_bytes truncates the output correctly?
// TODO: big or little endian?.
#[allow(clippy::expect_used)]
#[allow(clippy::arithmetic_side_effects)]
let shuffle_index = usize::from_be_bytes(
hash(&pre_image)
.get(0..8)
Expand Down Expand Up @@ -405,7 +406,9 @@ fn process_shuffled_trackers<T: EthSpec, Payload: AbstractExecPayload<T>>(
}

/// Return true if at `epoch` validator should shuffle candidate trackers
#[allow(clippy::arithmetic_side_effects)]
pub fn should_shuffle_trackers<T: EthSpec>(epoch: Epoch) -> bool {
// (clippy::arithmetic_side_effects) Will never divide by zero
epoch % T::whisk_epochs_per_shuffling_phase() + T::whisk_proposer_selection_gap() + 1
< T::whisk_epochs_per_shuffling_phase()
}
Expand Down Expand Up @@ -454,7 +457,8 @@ pub fn process_block_header<T: EthSpec, Payload: AbstractExecPayload<T>>(
state
.slot()
.as_usize()
.wrapping_rem(whisk_proposer_trackers.len()),
.safe_rem(T::whisk_proposer_trackers_count())
.expect("whisk_proposer_tracker_count is never 0"),
)
.expect("arr[x mod arr.len] always in bounds");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::per_epoch_processing::{
errors::EpochProcessingError,
resets::{process_eth1_data_reset, process_randao_mixes_reset, process_slashings_reset},
};
use safe_arith::SafeArith;
use types::{BeaconState, BeaconStateCapella, ChainSpec, Epoch, EthSpec, ForkName, RelativeEpoch};

use crate::common::update_progressive_balances_cache::{
Expand Down Expand Up @@ -91,7 +92,7 @@ fn process_whisk_updates<T: EthSpec>(
spec: &ChainSpec,
) -> Result<(), EpochProcessingError> {
let next_epoch = state.next_epoch()?;
if next_epoch % T::whisk_epochs_per_shuffling_phase() == 0 {
if next_epoch.safe_rem(T::whisk_epochs_per_shuffling_phase())? == 0 {
select_whisk_trackers(state, next_epoch, spec)?;
}
Ok(())
Expand Down
7 changes: 4 additions & 3 deletions consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,9 @@ impl<T: EthSpec> BeaconState<T> {
&self,
block_root: Hash256,
) -> Result<WhiskProposerShufflingRoot, Error> {
let shuffling_round_starting_epoch =
self.current_epoch() % T::whisk_epochs_per_shuffling_phase();
let shuffling_round_starting_epoch = self
.current_epoch()
.safe_rem(T::whisk_epochs_per_shuffling_phase())?;
let decision_slot = shuffling_round_starting_epoch
.start_slot(T::slots_per_epoch())
.saturating_sub(1_u64);
Expand Down Expand Up @@ -859,7 +860,7 @@ impl<T: EthSpec> BeaconState<T> {
spec: &ChainSpec,
) -> Result<Vec<usize>, Error> {
let proposer_seed = self.get_seed(
epoch - T::whisk_proposer_selection_gap(),
epoch.saturating_sub(T::whisk_proposer_selection_gap()),
Domain::WhiskProposerSelection,
spec,
)?;
Expand Down
4 changes: 2 additions & 2 deletions validator_client/src/duties_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,13 +1083,13 @@ async fn poll_beacon_proposers<T: SlotClock + 'static, E: EthSpec>(
// Notify beacon node that validator has duties for future blocks
// TODO WHISK: This routine should happen BEFORE the block to allow time to prepare
if let Ok((dependant_root, duties)) = response.as_ref() {
if duties.len() > 0 {
if !duties.is_empty() {
let preparation_data = duties
.iter()
.map(|duty| WhiskProposerPreparationData {
proposer_slot: duty.slot.into(),
validator_index: duty.validator_index,
whisk_shuffling_decision_root: dependant_root.clone(),
whisk_shuffling_decision_root: *dependant_root,
})
.collect::<Vec<_>>();
if let Err(e) = duties_service
Expand Down

0 comments on commit 7e045a5

Please sign in to comment.