Skip to content

Commit

Permalink
Fix clippy lints on merge-f2f (#2626)
Browse files Browse the repository at this point in the history
* Remove unchecked arith from ssz_derive

* Address clippy lints in block_verfication

* Use safe math for is_valid_gas_limit
  • Loading branch information
paulhauner committed Oct 1, 2021
1 parent 6da6453 commit 82a1780
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
35 changes: 18 additions & 17 deletions beacon_node/beacon_chain/src/block_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,19 +1071,20 @@ impl<'a, T: BeaconChainTypes> FullyVerifiedBlock<'a, T> {
ExecutionPayloadError::NoEth1Connection,
))?;

if !eth1_chain
.on_payload(block.message().body().execution_payload().ok_or(
let payload_valid = eth1_chain
.on_payload(block.message().body().execution_payload().ok_or_else(|| {
BlockError::InconsistentFork(InconsistentFork {
fork_at_slot: eth2::types::ForkName::Merge,
object_fork: block.message().body().fork_name(),
}),
)?)
})
})?)
.map_err(|e| {
BlockError::ExecutionPayloadError(ExecutionPayloadError::Eth1VerificationError(
e,
))
})?
{
})?;

if !payload_valid {
return Err(BlockError::ExecutionPayloadError(
ExecutionPayloadError::RejectedByExecutionEngine,
));
Expand Down Expand Up @@ -1212,17 +1213,17 @@ fn validate_execution_payload<E: EthSpec>(
.execution_payload()
// TODO: this really should never error so maybe
// we should make this simpler..
.ok_or(BlockError::InconsistentFork(InconsistentFork {
fork_at_slot: eth2::types::ForkName::Merge,
object_fork: block.body().fork_name(),
}))?;

if is_merge_complete(state) {
if *execution_payload == <ExecutionPayload<E>>::default() {
return Err(BlockError::ExecutionPayloadError(
ExecutionPayloadError::PayloadEmpty,
));
}
.ok_or_else(|| {
BlockError::InconsistentFork(InconsistentFork {
fork_at_slot: eth2::types::ForkName::Merge,
object_fork: block.body().fork_name(),
})
})?;

if is_merge_complete(state) && *execution_payload == <ExecutionPayload<E>>::default() {
return Err(BlockError::ExecutionPayloadError(
ExecutionPayloadError::PayloadEmpty,
));
}

// TODO: finish these
Expand Down
26 changes: 17 additions & 9 deletions consensus/state_processing/src/per_block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,32 @@ pub fn get_new_eth1_data<T: EthSpec>(
pub fn is_valid_gas_limit<T: EthSpec>(
payload: &ExecutionPayload<T>,
parent: &ExecutionPayloadHeader<T>,
) -> bool {
) -> Result<bool, ArithError> {
// check if payload used too much gas
if payload.gas_used > payload.gas_limit {
return false;
return Ok(false);
}
// check if payload changed the gas limit too much
if payload.gas_limit >= parent.gas_limit + parent.gas_limit / T::gas_limit_denominator() {
return false;
if payload.gas_limit
>= parent
.gas_limit
.safe_add(parent.gas_limit.safe_div(T::gas_limit_denominator())?)?
{
return Ok(false);
}
if payload.gas_limit <= parent.gas_limit - parent.gas_limit / T::gas_limit_denominator() {
return false;
if payload.gas_limit
<= parent
.gas_limit
.safe_sub(parent.gas_limit.safe_div(T::gas_limit_denominator())?)?
{
return Ok(false);
}
// check if the gas limit is at least the minimum gas limit
if payload.gas_limit < T::min_gas_limit() {
return false;
return Ok(false);
}

return true;
Ok(true)
}

/// https://github.com/ethereum/consensus-specs/blob/dev/specs/merge/beacon-chain.md#process_execution_payload
Expand Down Expand Up @@ -344,7 +352,7 @@ pub fn process_execution_payload<T: EthSpec>(
}
);
block_verify!(
is_valid_gas_limit(payload, state.latest_execution_payload_header()?),
is_valid_gas_limit(payload, state.latest_execution_payload_header()?)?,
BlockProcessingError::ExecutionInvalidGasLimit {
used: payload.gas_used,
limit: payload.gas_limit,
Expand Down

0 comments on commit 82a1780

Please sign in to comment.