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: add auxiliary deposit functions #378

Merged
merged 6 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
78 changes: 20 additions & 58 deletions ethereum-consensus/src/altair/block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
altair::{
beacon_block::BeaconBlock,
beacon_state::BeaconState,
compute_domain, compute_epoch_at_slot,
compute_epoch_at_slot,
constants::{
PARTICIPATION_FLAG_WEIGHTS, PROPOSER_WEIGHT, SYNC_REWARD_WEIGHT, WEIGHT_DENOMINATOR,
},
Expand All @@ -17,24 +17,16 @@ use crate::{
increase_balance, is_valid_indexed_attestation, process_block_header, process_eth1_data,
process_operations, process_randao,
sync::SyncAggregate,
Attestation, Deposit, DepositMessage,
Attestation, Bytes32, Gwei,
},
crypto::{eth_fast_aggregate_verify, verify_signature},
crypto::eth_fast_aggregate_verify,
domains::DomainType,
error::{
invalid_operation_error, InvalidAttestation, InvalidDeposit, InvalidOperation,
InvalidSyncAggregate,
},
phase0::constants::DEPOSIT_CONTRACT_TREE_DEPTH,
error::{invalid_operation_error, InvalidAttestation, InvalidOperation, InvalidSyncAggregate},
primitives::{BlsPublicKey, ParticipationFlags, ValidatorIndex},
signing::compute_signing_root,
ssz::prelude::*,
state_transition::{Context, Result},
};
use std::{
collections::{HashMap, HashSet},
iter::zip,
};
use std::{collections::HashMap, iter::zip};

pub fn process_attestation<
const SLOTS_PER_HISTORICAL_ROOT: usize,
Expand Down Expand Up @@ -160,7 +152,7 @@ pub fn process_attestation<
Ok(())
}

pub fn process_deposit<
pub fn add_validator_to_registry<
const SLOTS_PER_HISTORICAL_ROOT: usize,
const HISTORICAL_ROOTS_LIMIT: usize,
const ETH1_DATA_VOTES_BOUND: usize,
Expand All @@ -180,51 +172,21 @@ pub fn process_deposit<
MAX_VALIDATORS_PER_COMMITTEE,
SYNC_COMMITTEE_SIZE,
>,
deposit: &mut Deposit,
public_key: BlsPublicKey,
withdrawal_credentials: Bytes32,
amount: Gwei,
context: &Context,
) -> Result<()> {
let leaf = deposit.data.hash_tree_root()?;
let branch = &deposit.proof;
let depth = DEPOSIT_CONTRACT_TREE_DEPTH + 1;
let index = state.eth1_deposit_index as usize;
let root = state.eth1_data.deposit_root;
if is_valid_merkle_branch(leaf, branch, depth, index, root).is_err() {
return Err(invalid_operation_error(InvalidOperation::Deposit(
InvalidDeposit::InvalidProof { leaf, branch: branch.to_vec(), depth, index, root },
)))
}

state.eth1_deposit_index += 1;

let public_key = &deposit.data.public_key;
let amount = deposit.data.amount;
let validator_public_keys: HashSet<&BlsPublicKey> =
HashSet::from_iter(state.validators.iter().map(|v| &v.public_key));
if !validator_public_keys.contains(public_key) {
let mut deposit_message = DepositMessage {
public_key: public_key.clone(),
withdrawal_credentials: deposit.data.withdrawal_credentials.clone(),
amount,
};
let domain = compute_domain(DomainType::Deposit, None, None, context)?;
let signing_root = compute_signing_root(&mut deposit_message, domain)?;

if verify_signature(public_key, signing_root.as_ref(), &deposit.data.signature).is_err() {
// NOTE: explicitly return with no error and also no further mutations to `state`
return Ok(())
}
state.validators.push(get_validator_from_deposit(deposit, context));
state.balances.push(amount);
state.previous_epoch_participation.push(ParticipationFlags::default());
state.current_epoch_participation.push(ParticipationFlags::default());
state.inactivity_scores.push(0)
} else {
let index = state.validators.iter().position(|v| &v.public_key == public_key).unwrap();

increase_balance(state, index, amount);
}

Ok(())
) {
state.validators.push(get_validator_from_deposit(
public_key,
withdrawal_credentials,
amount,
context,
));
state.balances.push(amount);
state.previous_epoch_participation.push(ParticipationFlags::default());
state.current_epoch_participation.push(ParticipationFlags::default());
state.inactivity_scores.push(0);
}

pub fn process_sync_aggregate<
Expand Down
6 changes: 3 additions & 3 deletions ethereum-consensus/src/altair/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
altair::{
beacon_block::BeaconBlockBody, beacon_state::BeaconState,
block_processing::process_deposit, helpers::get_next_sync_committee, BeaconBlockHeader,
Deposit, DepositData, Eth1Data, Fork, DEPOSIT_DATA_LIST_BOUND,
beacon_block::BeaconBlockBody, beacon_state::BeaconState, helpers::get_next_sync_committee,
process_deposit, BeaconBlockHeader, Deposit, DepositData, Eth1Data, Fork,
DEPOSIT_DATA_LIST_BOUND,
},
primitives::{Gwei, Hash32, GENESIS_EPOCH},
ssz::prelude::*,
Expand Down
92 changes: 91 additions & 1 deletion ethereum-consensus/src/altair/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use crate::{
beacon_block::{BeaconBlock, BeaconBlockBody, SignedBeaconBlock},
beacon_state::BeaconState,
block_processing::{
process_attestation, process_block, process_deposit, process_sync_aggregate,
add_validator_to_registry, process_attestation, process_block, process_sync_aggregate,
},
constants::{
PARTICIPATION_FLAG_WEIGHTS, PROPOSER_WEIGHT, SYNC_COMMITTEE_SUBNET_COUNT,
Expand Down Expand Up @@ -195,6 +195,96 @@ pub fn process_attester_slashing<
Ok(())
}
}
pub fn apply_deposit<
const SLOTS_PER_HISTORICAL_ROOT: usize,
const HISTORICAL_ROOTS_LIMIT: usize,
const ETH1_DATA_VOTES_BOUND: usize,
const VALIDATOR_REGISTRY_LIMIT: usize,
const EPOCHS_PER_HISTORICAL_VECTOR: usize,
const EPOCHS_PER_SLASHINGS_VECTOR: usize,
const MAX_VALIDATORS_PER_COMMITTEE: usize,
const SYNC_COMMITTEE_SIZE: usize,
>(
state: &mut BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
VALIDATOR_REGISTRY_LIMIT,
EPOCHS_PER_HISTORICAL_VECTOR,
EPOCHS_PER_SLASHINGS_VECTOR,
MAX_VALIDATORS_PER_COMMITTEE,
SYNC_COMMITTEE_SIZE,
>,
public_key: &BlsPublicKey,
withdrawal_credentials: &Bytes32,
amount: Gwei,
signature: &BlsSignature,
context: &Context,
) -> Result<()> {
let index = state.validators.iter().position(|v| v.public_key == *public_key);
if let Some(index) = index {
increase_balance(state, index, amount);
return Ok(());
}
let mut deposit_message = DepositMessage {
public_key: public_key.clone(),
withdrawal_credentials: withdrawal_credentials.clone(),
amount,
};
let domain = compute_domain(DomainType::Deposit, None, None, context)?;
let signing_root = compute_signing_root(&mut deposit_message, domain)?;
if verify_signature(public_key, signing_root.as_ref(), signature).is_err() {
return Ok(());
}
add_validator_to_registry(
state,
deposit_message.public_key,
deposit_message.withdrawal_credentials,
amount,
context,
);
Ok(())
}
pub fn process_deposit<
const SLOTS_PER_HISTORICAL_ROOT: usize,
const HISTORICAL_ROOTS_LIMIT: usize,
const ETH1_DATA_VOTES_BOUND: usize,
const VALIDATOR_REGISTRY_LIMIT: usize,
const EPOCHS_PER_HISTORICAL_VECTOR: usize,
const EPOCHS_PER_SLASHINGS_VECTOR: usize,
const MAX_VALIDATORS_PER_COMMITTEE: usize,
const SYNC_COMMITTEE_SIZE: usize,
>(
state: &mut BeaconState<
SLOTS_PER_HISTORICAL_ROOT,
HISTORICAL_ROOTS_LIMIT,
ETH1_DATA_VOTES_BOUND,
VALIDATOR_REGISTRY_LIMIT,
EPOCHS_PER_HISTORICAL_VECTOR,
EPOCHS_PER_SLASHINGS_VECTOR,
MAX_VALIDATORS_PER_COMMITTEE,
SYNC_COMMITTEE_SIZE,
>,
deposit: &mut Deposit,
context: &Context,
) -> Result<()> {
let leaf = deposit.data.hash_tree_root()?;
let branch = &deposit.proof;
let depth = DEPOSIT_CONTRACT_TREE_DEPTH + 1;
let index = state.eth1_deposit_index as usize;
let root = state.eth1_data.deposit_root;
if is_valid_merkle_branch(leaf, branch, depth, index, root).is_err() {
return Err(invalid_operation_error(InvalidOperation::Deposit(
InvalidDeposit::InvalidProof { leaf, branch: branch.to_vec(), depth, index, root },
)));
}
state.eth1_deposit_index += 1;
let public_key = &deposit.data.public_key;
let withdrawal_credentials = &deposit.data.withdrawal_credentials;
let amount = deposit.data.amount;
let signature = &deposit.data.signature;
apply_deposit(state, public_key, withdrawal_credentials, amount, signature, context)
}
pub fn process_voluntary_exit<
const SLOTS_PER_HISTORICAL_ROOT: usize,
const HISTORICAL_ROOTS_LIMIT: usize,
Expand Down
Loading
Loading