mirror of
https://github.com/sigp/lighthouse.git
synced 2026-06-01 05:37:05 +00:00
There are certain crates which we re-export within `types` which creates a fragmented DevEx, where there are various ways to import the same crates.
```rust
// consensus/types/src/lib.rs
pub use bls::{
AggregatePublicKey, AggregateSignature, Error as BlsError, Keypair, PUBLIC_KEY_BYTES_LEN,
PublicKey, PublicKeyBytes, SIGNATURE_BYTES_LEN, SecretKey, Signature, SignatureBytes,
get_withdrawal_credentials,
};
pub use context_deserialize::{ContextDeserialize, context_deserialize};
pub use fixed_bytes::FixedBytesExtended;
pub use milhouse::{self, List, Vector};
pub use ssz_types::{BitList, BitVector, FixedVector, VariableList, typenum, typenum::Unsigned};
pub use superstruct::superstruct;
```
This PR removes these re-exports and makes it explicit that these types are imported from a non-`consensus/types` crate.
Co-Authored-By: Mac L <mjladson@pm.me>
69 lines
2.0 KiB
Rust
69 lines
2.0 KiB
Rust
use super::errors::{BlockOperationError, DepositInvalid};
|
|
use crate::per_block_processing::signature_sets::deposit_pubkey_signature_message;
|
|
use bls::PublicKeyBytes;
|
|
use merkle_proof::verify_merkle_proof;
|
|
use safe_arith::SafeArith;
|
|
use tree_hash::TreeHash;
|
|
use types::*;
|
|
|
|
type Result<T> = std::result::Result<T, BlockOperationError<DepositInvalid>>;
|
|
|
|
fn error(reason: DepositInvalid) -> BlockOperationError<DepositInvalid> {
|
|
BlockOperationError::invalid(reason)
|
|
}
|
|
|
|
/// Verify `Deposit.pubkey` signed `Deposit.signature`.
|
|
///
|
|
/// Spec v0.12.1
|
|
pub fn is_valid_deposit_signature(deposit_data: &DepositData, spec: &ChainSpec) -> Result<()> {
|
|
let (public_key, signature, msg) = deposit_pubkey_signature_message(deposit_data, spec)
|
|
.ok_or_else(|| error(DepositInvalid::BadBlsBytes))?;
|
|
|
|
verify!(
|
|
signature.verify(&public_key, msg),
|
|
DepositInvalid::BadSignature
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Returns a `Some(validator index)` if a pubkey already exists in the `validators`,
|
|
/// otherwise returns `None`.
|
|
///
|
|
/// Builds the pubkey cache if it is not already built.
|
|
pub fn get_existing_validator_index<E: EthSpec>(
|
|
state: &mut BeaconState<E>,
|
|
pub_key: &PublicKeyBytes,
|
|
) -> Result<Option<u64>> {
|
|
let validator_index = state.get_validator_index(pub_key)?;
|
|
Ok(validator_index.map(|idx| idx as u64))
|
|
}
|
|
|
|
/// Verify that a deposit is included in the state's eth1 deposit root.
|
|
///
|
|
/// The deposit index is provided as a parameter so we can check proofs
|
|
/// before they're due to be processed, and in parallel.
|
|
///
|
|
/// Spec v0.12.1
|
|
pub fn verify_deposit_merkle_proof<E: EthSpec>(
|
|
state: &BeaconState<E>,
|
|
deposit: &Deposit,
|
|
deposit_index: u64,
|
|
spec: &ChainSpec,
|
|
) -> Result<()> {
|
|
let leaf = deposit.data.tree_hash_root();
|
|
|
|
verify!(
|
|
verify_merkle_proof(
|
|
leaf,
|
|
&deposit.proof[..],
|
|
spec.deposit_contract_tree_depth.safe_add(1)? as usize,
|
|
deposit_index as usize,
|
|
state.eth1_data().deposit_root,
|
|
),
|
|
DepositInvalid::BadMerkleProof
|
|
);
|
|
|
|
Ok(())
|
|
}
|