Skip to content
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

feat: implements process bls to execution change #251

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9a1e484
feat: implements process bls to execution change
distributedstatemachine Sep 18, 2023
156ee94
chore: lint
distributedstatemachine Sep 19, 2023
f596fda
chore: PR comments
distributedstatemachine Sep 20, 2023
2c9d9f7
chore: lint
distributedstatemachine Sep 20, 2023
c689eeb
Update ethereum-consensus/src/capella/block_processing.rs
distributedstatemachine Sep 20, 2023
9567d59
fix: PR comments
distributedstatemachine Sep 25, 2023
2e38cba
fix: pr comments - errors
distributedstatemachine Sep 26, 2023
77d2224
fix: PR comments - block processing
distributedstatemachine Sep 26, 2023
358ba32
chore: remove clone from validator
distributedstatemachine Sep 26, 2023
5d1cb02
Merge branch 'main' into feat/process_bls_to_execution_change
distributedstatemachine Sep 26, 2023
2f01095
Merge branch 'main' into feat/process_bls_to_execution_change
distributedstatemachine Sep 26, 2023
14930ea
Apply suggestions from code review
ralexstokes Sep 26, 2023
11c3978
Update ethereum-consensus/src/capella/block_processing.rs
ralexstokes Sep 26, 2023
dd54459
chore: lint
distributedstatemachine Sep 26, 2023
169bef9
fix: pr comments
distributedstatemachine Sep 26, 2023
f8e0eee
Apply suggestions from code review
ralexstokes Sep 28, 2023
5ff439f
Merge branch 'main' into feat/process_bls_to_execution_change
ralexstokes Sep 28, 2023
6f6ca89
Update ethereum-consensus/src/capella/block_processing.rs
ralexstokes Sep 28, 2023
1cf7cf8
Update ethereum-consensus/src/capella/block_processing.rs
ralexstokes Sep 28, 2023
7e75cdf
Merge branch 'main' into feat/process_bls_to_execution_change
ralexstokes Sep 28, 2023
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
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 validator = &mut state.validators[address_change.validator_index];
let withdrawal_credentials = &mut validator.withdrawal_credentials;
ralexstokes marked this conversation as resolved.
Show resolved Hide resolved
if withdrawal_credentials[0] != BLS_WITHDRAWAL_PREFIX {
return Err(invalid_operation_error(InvalidOperation::BlsToExecutionChange(
InvalidBlsToExecutionChange::WithdrawalCredentialsPrefix(withdrawal_credentials[0]),
)))
}

ralexstokes marked this conversation as resolved.
Show resolved Hide resolved
// 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)?;
ralexstokes marked this conversation as resolved.
Show resolved Hide resolved
ralexstokes marked this conversation as resolved.
Show resolved Hide resolved
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,
ralexstokes marked this conversation as resolved.
Show resolved Hide resolved
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}")]
ralexstokes marked this conversation as resolved.
Show resolved Hide resolved
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
Loading