Skip to content

Commit

Permalink
Merge pull request #251 from samtvlabs/feat/process_bls_to_execution_…
Browse files Browse the repository at this point in the history
…change

feat: implements process bls to execution change
  • Loading branch information
ralexstokes authored Sep 28, 2023
2 parents 7c8311b + 7e75cdf commit 16947e7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
64 changes: 53 additions & 11 deletions ethereum-consensus/src/capella/block_processing.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use crate::{
capella::{
compute_timestamp_at_slot, decrease_balance, get_current_epoch, get_randao_mix,
is_fully_withdrawable_validator, is_partially_withdrawable_validator, process_attestation,
process_attester_slashing, process_block_header, process_deposit, process_eth1_data,
process_proposer_slashing, process_randao, process_sync_aggregate, process_voluntary_exit,
BeaconBlock, BeaconBlockBody, BeaconState, ExecutionAddress, ExecutionEngine,
compute_domain, compute_signing_root, compute_timestamp_at_slot, decrease_balance,
get_current_epoch, get_randao_mix, is_fully_withdrawable_validator,
is_partially_withdrawable_validator, process_attestation, process_attester_slashing,
process_block_header, process_deposit, process_eth1_data, process_proposer_slashing,
process_randao, process_sync_aggregate, process_voluntary_exit, BeaconBlock,
BeaconBlockBody, BeaconState, DomainType, ExecutionAddress, ExecutionEngine,
ExecutionPayload, ExecutionPayloadHeader, NewPayloadRequest, SignedBlsToExecutionChange,
Withdrawal,
},
crypto::{hash, verify_signature},
primitives::{BLS_WITHDRAWAL_PREFIX, ETH1_ADDRESS_WITHDRAWAL_PREFIX},
ssz::prelude::*,
state_transition::{
invalid_operation_error, Context, InvalidDeposit, InvalidExecutionPayload,
InvalidOperation, InvalidWithdrawals, Result,
invalid_operation_error, Context, InvalidBlsToExecutionChange, InvalidDeposit,
InvalidExecutionPayload, InvalidOperation, InvalidWithdrawals, Result,
},
};

Expand All @@ -27,7 +30,7 @@ pub fn process_bls_to_execution_change<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
>(
_state: &mut BeaconState<
state: &mut BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
Expand All @@ -39,10 +42,49 @@ pub fn process_bls_to_execution_change<
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
>,
_signed_address_change: &mut SignedBlsToExecutionChange,
_context: &Context,
signed_address_change: &mut SignedBlsToExecutionChange,
context: &Context,
) -> Result<()> {
unimplemented!()
let address_change = &mut signed_address_change.message;
let signature = &signed_address_change.signature;

if address_change.validator_index >= state.validators.len() {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::ValidatorIndexOutOfBounds(address_change.validator_index),
)))
}

let withdrawal_credentials =
&mut state.validators[address_change.validator_index].withdrawal_credentials;
if withdrawal_credentials[0] != BLS_WITHDRAWAL_PREFIX {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::WithdrawalCredentialsPrefix(withdrawal_credentials[0]),
)))
}

// NOTE: compute `signing_root` ahead of the public key check to satisfy borrow check
let domain = compute_domain(
DomainType::BlsToExecutionChange,
None,
Some(state.genesis_validators_root),
context,
)?;
let signing_root = compute_signing_root(address_change, domain)?;

let public_key = &address_change.from_bls_public_key;
if withdrawal_credentials[1..] != hash(public_key.as_ref())[1..] {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::PublicKeyMismatch(public_key.clone()),
)))
}

verify_signature(public_key, signing_root.as_ref(), signature)?;

withdrawal_credentials[0] = ETH1_ADDRESS_WITHDRAWAL_PREFIX;
withdrawal_credentials[1..12].fill(0);
withdrawal_credentials[12..].copy_from_slice(address_change.to_execution_address.as_ref());

Ok(())
}

pub fn process_operations<
Expand Down
4 changes: 2 additions & 2 deletions ethereum-consensus/src/capella/bls_to_execution_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub struct BlsToExecutionChange {
#[derive(Default, Debug, Clone, SimpleSerialize, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SignedBlsToExecutionChange {
message: BlsToExecutionChange,
signature: BlsSignature,
pub message: BlsToExecutionChange,
pub signature: BlsSignature,
}
14 changes: 13 additions & 1 deletion ethereum-consensus/src/state_transition/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
capella::Withdrawal,
crypto::Error as CryptoError,
phase0::{AttestationData, BeaconBlockHeader, Checkpoint},
primitives::{BlsSignature, Bytes32, Epoch, Hash32, Root, Slot, ValidatorIndex},
primitives::{BlsPublicKey, BlsSignature, Bytes32, Epoch, Hash32, Root, Slot, ValidatorIndex},
ssz::prelude::*,
state_transition::Forks,
};
Expand Down Expand Up @@ -87,6 +87,8 @@ pub enum InvalidOperation {
ExecutionPayload(#[from] InvalidExecutionPayload),
#[error("invalid withdrawals: {0}")]
Withdrawal(#[from] InvalidWithdrawals),
#[error("invalid BLS signature to execution change: {0}")]
BlsToExecutionChange(#[from] InvalidBlsToExecutionChange),
}

#[derive(Debug, Error)]
Expand Down Expand Up @@ -189,6 +191,16 @@ pub enum InvalidWithdrawals {
IncorrectWithdrawals { provided: Vec<Withdrawal>, expected: Vec<Withdrawal> },
}

#[derive(Debug, Error)]
pub enum InvalidBlsToExecutionChange {
#[error("validator index {0} is out of bounds")]
ValidatorIndexOutOfBounds(usize),
#[error("invalid withdrawal credentials prefix: {0}")]
WithdrawalCredentialsPrefix(u8),
#[error("operation's public key did not match the registered key: {0:?}")]
PublicKeyMismatch(BlsPublicKey),
}

#[derive(Debug, Error)]
pub enum InvalidSyncAggregate {
#[error("invalid sync committee aggregate signature {signature} signing over previous slot block root {root}")]
Expand Down

0 comments on commit 16947e7

Please sign in to comment.