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

refactor: remove block vrf verification from epoch manager #12447

Open
wants to merge 2 commits into
base: stefan/remove_verify_chunk_header
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2284,9 +2284,10 @@ impl Chain {
// Check the header is valid before we proceed with the full block.
self.validate_header(header, provenance, challenges)?;

self.epoch_manager.verify_block_vrf(
header.epoch_id(),
header.height(),
let validator =
self.epoch_manager.get_block_producer_info(header.epoch_id(), header.height())?;
crate::signature_verification::verify_block_vrf(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could we put verify_block_vrf as part of imports instead of fully qualifying?

validator,
&prev_random_value,
block.vrf_value(),
block.vrf_proof(),
Expand Down
16 changes: 16 additions & 0 deletions chain/chain/src/signature_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ use near_primitives::{
};
use std::sync::Arc;

pub fn verify_block_vrf(
validator: ValidatorStake,
prev_random_value: &CryptoHash,
vrf_value: &near_crypto::vrf::Value,
vrf_proof: &near_crypto::vrf::Proof,
) -> Result<(), Error> {
let public_key =
near_crypto::key_conversion::convert_public_key(validator.public_key().unwrap_as_ed25519())
.unwrap();

if !public_key.is_vrf_valid(&prev_random_value.as_ref(), vrf_value, vrf_proof) {
return Err(Error::InvalidRandomnessBeaconOutput);
}
Ok(())
}

/// Verify chunk header signature.
/// return false if the header signature does not match the key for the assigned chunk producer
/// for this chunk, or if the chunk producer has been slashed
Expand Down
21 changes: 9 additions & 12 deletions chain/chain/src/test_utils/kv_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,16 @@ impl EpochManagerAdapter for MockEpochManager {
epoch_id: &EpochId,
height: BlockHeight,
) -> Result<AccountId, EpochError> {
self.get_block_producer_info(epoch_id, height).map(|validator| validator.take_account_id())
}

fn get_block_producer_info(
&self,
epoch_id: &EpochId,
height: BlockHeight,
) -> Result<ValidatorStake, EpochError> {
let validators = self.get_block_producers(self.get_valset_for_epoch(epoch_id)?);
Ok(validators[(height as usize) % validators.len()].account_id().clone())
Ok(validators[(height as usize) % validators.len()].clone())
}

fn get_chunk_producer_info(
Expand Down Expand Up @@ -883,17 +891,6 @@ impl EpochManagerAdapter for MockEpochManager {
Ok(())
}

fn verify_block_vrf(
&self,
_epoch_id: &EpochId,
_block_height: BlockHeight,
_prev_random_value: &CryptoHash,
_vrf_value: &near_crypto::vrf::Value,
_vrf_proof: &near_crypto::vrf::Proof,
) -> Result<(), Error> {
Ok(())
}

fn verify_validator_signature(
&self,
_epoch_id: &EpochId,
Expand Down
47 changes: 16 additions & 31 deletions chain/epoch-manager/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ pub trait EpochManagerAdapter: Send + Sync {
height: BlockHeight,
) -> Result<AccountId, EpochError>;

/// Block producers and stake for given height for the main block. Return EpochError if outside of known boundaries.
fn get_block_producer_info(
&self,
epoch_id: &EpochId,
height: BlockHeight,
) -> Result<ValidatorStake, EpochError>;

/// Chunk producer info for given height for given shard. Return EpochError if outside of known boundaries.
fn get_chunk_producer_info(
&self,
Expand Down Expand Up @@ -324,15 +331,6 @@ pub trait EpochManagerAdapter: Send + Sync {
next_epoch_info: EpochInfo,
) -> Result<(), EpochError>;

fn verify_block_vrf(
&self,
epoch_id: &EpochId,
block_height: BlockHeight,
prev_random_value: &CryptoHash,
vrf_value: &near_crypto::vrf::Value,
vrf_proof: &near_crypto::vrf::Proof,
) -> Result<(), Error>;

/// Verify validator signature for the given epoch.
/// Note: doesn't account for slashed accounts within given epoch. USE WITH CAUTION.
fn verify_validator_signature(
Expand Down Expand Up @@ -691,8 +689,16 @@ impl EpochManagerAdapter for EpochManagerHandle {
epoch_id: &EpochId,
height: BlockHeight,
) -> Result<AccountId, EpochError> {
self.get_block_producer_info(epoch_id, height).map(|validator| validator.take_account_id())
}

fn get_block_producer_info(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pugachAG, how comfortable are we with exposing block producer info?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't see any issues with that, especially considering that we already expose chunk producer info this way

&self,
epoch_id: &EpochId,
height: BlockHeight,
) -> Result<ValidatorStake, EpochError> {
let epoch_manager = self.read();
Ok(epoch_manager.get_block_producer_info(epoch_id, height)?.take_account_id())
Ok(epoch_manager.get_block_producer_info(epoch_id, height)?)
}

fn get_chunk_producer_info(
Expand Down Expand Up @@ -796,27 +802,6 @@ impl EpochManagerAdapter for EpochManagerHandle {
)
}

fn verify_block_vrf(
&self,
epoch_id: &EpochId,
block_height: BlockHeight,
prev_random_value: &CryptoHash,
vrf_value: &near_crypto::vrf::Value,
vrf_proof: &near_crypto::vrf::Proof,
) -> Result<(), Error> {
let epoch_manager = self.read();
let validator = epoch_manager.get_block_producer_info(epoch_id, block_height)?;
let public_key = near_crypto::key_conversion::convert_public_key(
validator.public_key().unwrap_as_ed25519(),
)
.unwrap();

if !public_key.is_vrf_valid(&prev_random_value.as_ref(), vrf_value, vrf_proof) {
return Err(Error::InvalidRandomnessBeaconOutput);
}
Ok(())
}

fn verify_validator_signature(
&self,
epoch_id: &EpochId,
Expand Down
Loading