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

[Merged by Bors] - Implement skip_randao_verification and blinded block rewards API #3540

Closed
Closed
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
4 changes: 2 additions & 2 deletions beacon_node/http_api/src/block_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lru::LruCache;
use slog::{debug, warn, Logger};
use state_processing::BlockReplayer;
use std::sync::Arc;
use types::BeaconBlock;
use types::BlindedBeaconBlock;
use warp_utils::reject::{
beacon_chain_error, beacon_state_error, custom_bad_request, custom_server_error,
};
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn get_block_rewards<T: BeaconChainTypes>(

/// Compute block rewards for blocks passed in as input.
pub fn compute_block_rewards<T: BeaconChainTypes>(
blocks: Vec<BeaconBlock<T::EthSpec>>,
blocks: Vec<BlindedBeaconBlock<T::EthSpec>>,
chain: Arc<BeaconChain<T>>,
log: Logger,
) -> Result<Vec<BlockReward>, warp::Rejection> {
Expand Down
92 changes: 41 additions & 51 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use beacon_chain::{
BeaconChainTypes, ProduceBlockVerification, WhenSlotSkipped,
};
pub use block_id::BlockId;
use eth2::types::{self as api_types, EndpointVersion, ValidatorId, ValidatorStatus};
use eth2::types::{
self as api_types, EndpointVersion, SkipRandaoVerification, ValidatorId, ValidatorStatus,
};
use lighthouse_network::{types::SyncState, EnrExt, NetworkGlobals, PeerId, PubsubMessage};
use lighthouse_version::version_with_platform;
use network::{NetworkMessage, NetworkSenders, ValidatorSubscriptionMessage};
Expand All @@ -35,7 +37,6 @@ use slot_clock::SlotClock;
use ssz::Encode;
pub use state_id::StateId;
use std::borrow::Cow;
use std::convert::TryInto;
use std::future::Future;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
Expand All @@ -46,7 +47,7 @@ use tokio_stream::{wrappers::BroadcastStream, StreamExt};
use types::{
Attestation, AttestationData, AttesterSlashing, BeaconStateError, BlindedPayload,
CommitteeCache, ConfigAndPreset, Epoch, EthSpec, ForkName, FullPayload,
ProposerPreparationData, ProposerSlashing, RelativeEpoch, Signature, SignedAggregateAndProof,
ProposerPreparationData, ProposerSlashing, RelativeEpoch, SignedAggregateAndProof,
SignedBeaconBlock, SignedBlindedBeaconBlock, SignedContributionAndProof,
SignedValidatorRegistrationData, SignedVoluntaryExit, Slot, SyncCommitteeMessage,
SyncContributionData,
Expand Down Expand Up @@ -2002,31 +2003,25 @@ pub fn serve<T: BeaconChainTypes>(
slot: Slot,
query: api_types::ValidatorBlocksQuery,
chain: Arc<BeaconChain<T>>| async move {
let randao_reveal = query.randao_reveal.as_ref().map_or_else(
|| {
if query.verify_randao {
Err(warp_utils::reject::custom_bad_request(
"randao_reveal is mandatory unless verify_randao=false".into(),
))
} else {
Ok(Signature::empty())
}
},
|sig_bytes| {
sig_bytes.try_into().map_err(|e| {
warp_utils::reject::custom_bad_request(format!(
"randao reveal is not a valid BLS signature: {:?}",
e
))
})
},
)?;
let randao_reveal = query.randao_reveal.decompress().map_err(|e| {
warp_utils::reject::custom_bad_request(format!(
"randao reveal is not a valid BLS signature: {:?}",
e
))
})?;

let randao_verification = if query.verify_randao {
ProduceBlockVerification::VerifyRandao
} else {
ProduceBlockVerification::NoVerification
};
let randao_verification =
if query.skip_randao_verification == SkipRandaoVerification::Yes {
if !randao_reveal.is_infinity() {
return Err(warp_utils::reject::custom_bad_request(
"randao_reveal must be point-at-infinity if verification is skipped"
.into(),
));
}
ProduceBlockVerification::NoVerification
} else {
ProduceBlockVerification::VerifyRandao
};

let (block, _) = chain
.produce_block_with_verification::<FullPayload<T::EthSpec>>(
Expand Down Expand Up @@ -2064,31 +2059,25 @@ pub fn serve<T: BeaconChainTypes>(
|slot: Slot,
query: api_types::ValidatorBlocksQuery,
chain: Arc<BeaconChain<T>>| async move {
let randao_reveal = query.randao_reveal.as_ref().map_or_else(
|| {
if query.verify_randao {
Err(warp_utils::reject::custom_bad_request(
"randao_reveal is mandatory unless verify_randao=false".into(),
))
} else {
Ok(Signature::empty())
}
},
|sig_bytes| {
sig_bytes.try_into().map_err(|e| {
warp_utils::reject::custom_bad_request(format!(
"randao reveal is not a valid BLS signature: {:?}",
e
))
})
},
)?;
let randao_reveal = query.randao_reveal.decompress().map_err(|e| {
warp_utils::reject::custom_bad_request(format!(
"randao reveal is not a valid BLS signature: {:?}",
e
))
})?;

let randao_verification = if query.verify_randao {
ProduceBlockVerification::VerifyRandao
} else {
ProduceBlockVerification::NoVerification
};
let randao_verification =
if query.skip_randao_verification == SkipRandaoVerification::Yes {
if !randao_reveal.is_infinity() {
return Err(warp_utils::reject::custom_bad_request(
"randao_reveal must be point-at-infinity if verification is skipped"
.into()
));
}
ProduceBlockVerification::NoVerification
} else {
ProduceBlockVerification::VerifyRandao
};

let (block, _) = chain
.produce_block_with_verification::<BlindedPayload<T::EthSpec>>(
Expand All @@ -2103,6 +2092,7 @@ pub fn serve<T: BeaconChainTypes>(
.to_ref()
.fork_name(&chain.spec)
.map_err(inconsistent_fork_rejection)?;

// Pose as a V2 endpoint so we return the fork `version`.
fork_versioned_response(V2, fork_name, block)
.map(|response| warp::reply::json(&response))
Expand Down
76 changes: 16 additions & 60 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1928,11 +1928,11 @@ impl ApiTester {

let block = self
.client
.get_validator_blocks_with_verify_randao::<E, FullPayload<E>>(
.get_validator_blocks_modular::<E, FullPayload<E>>(
slot,
&Signature::infinity().unwrap().into(),
None,
None,
Some(false),
SkipRandaoVerification::Yes,
)
.await
.unwrap()
Expand Down Expand Up @@ -1982,45 +1982,23 @@ impl ApiTester {
sk.sign(message).into()
};

// Check failure with no `verify_randao` passed.
// Check failure with no `skip_randao_verification` passed.
self.client
.get_validator_blocks::<E, FullPayload<E>>(slot, &bad_randao_reveal, None)
.await
.unwrap_err();

// Check failure with `verify_randao=true`.
// Check failure with `skip_randao_verification` (requires infinity sig).
self.client
.get_validator_blocks_with_verify_randao::<E, FullPayload<E>>(
.get_validator_blocks_modular::<E, FullPayload<E>>(
slot,
Some(&bad_randao_reveal),
&bad_randao_reveal,
None,
Some(true),
)
.await
.unwrap_err();

// Check failure with no randao reveal provided.
self.client
.get_validator_blocks_with_verify_randao::<E, FullPayload<E>>(
slot, None, None, None,
SkipRandaoVerification::Yes,
)
.await
.unwrap_err();

// Check success with `verify_randao=false`.
let block = self
.client
.get_validator_blocks_with_verify_randao::<E, FullPayload<E>>(
slot,
Some(&bad_randao_reveal),
None,
Some(false),
)
.await
.unwrap()
.data;

assert_eq!(block.slot(), slot);
self.chain.slot_clock.set_slot(slot.as_u64() + 1);
}

Expand Down Expand Up @@ -2095,11 +2073,11 @@ impl ApiTester {

let block = self
.client
.get_validator_blinded_blocks_with_verify_randao::<E, Payload>(
.get_validator_blinded_blocks_modular::<E, Payload>(
slot,
&Signature::infinity().unwrap().into(),
None,
None,
Some(false),
SkipRandaoVerification::Yes,
)
.await
.unwrap()
Expand Down Expand Up @@ -2151,45 +2129,23 @@ impl ApiTester {
sk.sign(message).into()
};

// Check failure with no `verify_randao` passed.
// Check failure with full randao verification enabled.
self.client
.get_validator_blinded_blocks::<E, Payload>(slot, &bad_randao_reveal, None)
.await
.unwrap_err();

// Check failure with `verify_randao=true`.
// Check failure with `skip_randao_verification` (requires infinity sig).
self.client
.get_validator_blinded_blocks_with_verify_randao::<E, Payload>(
.get_validator_blinded_blocks_modular::<E, Payload>(
slot,
Some(&bad_randao_reveal),
&bad_randao_reveal,
None,
Some(true),
)
.await
.unwrap_err();

// Check failure with no randao reveal provided.
self.client
.get_validator_blinded_blocks_with_verify_randao::<E, Payload>(
slot, None, None, None,
SkipRandaoVerification::Yes,
)
.await
.unwrap_err();

// Check success with `verify_randao=false`.
let block = self
.client
.get_validator_blinded_blocks_with_verify_randao::<E, Payload>(
slot,
Some(&bad_randao_reveal),
None,
Some(false),
)
.await
.unwrap()
.data;

assert_eq!(block.slot(), slot);
self.chain.slot_clock.set_slot(slot.as_u64() + 1);
}

Expand Down
43 changes: 18 additions & 25 deletions common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,17 +1228,17 @@ impl BeaconNodeHttpClient {
randao_reveal: &SignatureBytes,
graffiti: Option<&Graffiti>,
) -> Result<ForkVersionedResponse<BeaconBlock<T, Payload>>, Error> {
self.get_validator_blocks_with_verify_randao(slot, Some(randao_reveal), graffiti, None)
self.get_validator_blocks_modular(slot, randao_reveal, graffiti, SkipRandaoVerification::No)
.await
}

/// `GET v2/validator/blocks/{slot}`
pub async fn get_validator_blocks_with_verify_randao<T: EthSpec, Payload: ExecPayload<T>>(
pub async fn get_validator_blocks_modular<T: EthSpec, Payload: ExecPayload<T>>(
&self,
slot: Slot,
randao_reveal: Option<&SignatureBytes>,
randao_reveal: &SignatureBytes,
graffiti: Option<&Graffiti>,
verify_randao: Option<bool>,
skip_randao_verification: SkipRandaoVerification,
) -> Result<ForkVersionedResponse<BeaconBlock<T, Payload>>, Error> {
let mut path = self.eth_path(V2)?;

Expand All @@ -1248,19 +1248,17 @@ impl BeaconNodeHttpClient {
.push("blocks")
.push(&slot.to_string());

if let Some(randao_reveal) = randao_reveal {
path.query_pairs_mut()
.append_pair("randao_reveal", &randao_reveal.to_string());
}
path.query_pairs_mut()
.append_pair("randao_reveal", &randao_reveal.to_string());

if let Some(graffiti) = graffiti {
path.query_pairs_mut()
.append_pair("graffiti", &graffiti.to_string());
}

if let Some(verify_randao) = verify_randao {
if skip_randao_verification == SkipRandaoVerification::Yes {
path.query_pairs_mut()
.append_pair("verify_randao", &verify_randao.to_string());
.append_pair("skip_randao_verification", "");
}

self.get(path).await
Expand All @@ -1273,25 +1271,22 @@ impl BeaconNodeHttpClient {
randao_reveal: &SignatureBytes,
graffiti: Option<&Graffiti>,
) -> Result<ForkVersionedResponse<BeaconBlock<T, Payload>>, Error> {
self.get_validator_blinded_blocks_with_verify_randao(
self.get_validator_blinded_blocks_modular(
slot,
Some(randao_reveal),
randao_reveal,
graffiti,
None,
SkipRandaoVerification::No,
)
.await
}

/// `GET v1/validator/blinded_blocks/{slot}`
pub async fn get_validator_blinded_blocks_with_verify_randao<
T: EthSpec,
Payload: ExecPayload<T>,
>(
pub async fn get_validator_blinded_blocks_modular<T: EthSpec, Payload: ExecPayload<T>>(
&self,
slot: Slot,
randao_reveal: Option<&SignatureBytes>,
randao_reveal: &SignatureBytes,
graffiti: Option<&Graffiti>,
verify_randao: Option<bool>,
skip_randao_verification: SkipRandaoVerification,
) -> Result<ForkVersionedResponse<BeaconBlock<T, Payload>>, Error> {
let mut path = self.eth_path(V1)?;

Expand All @@ -1301,19 +1296,17 @@ impl BeaconNodeHttpClient {
.push("blinded_blocks")
.push(&slot.to_string());

if let Some(randao_reveal) = randao_reveal {
path.query_pairs_mut()
.append_pair("randao_reveal", &randao_reveal.to_string());
}
path.query_pairs_mut()
.append_pair("randao_reveal", &randao_reveal.to_string());

if let Some(graffiti) = graffiti {
path.query_pairs_mut()
.append_pair("graffiti", &graffiti.to_string());
}

if let Some(verify_randao) = verify_randao {
if skip_randao_verification == SkipRandaoVerification::Yes {
path.query_pairs_mut()
.append_pair("verify_randao", &verify_randao.to_string());
.append_key_only("skip_randao_verification");
}

self.get(path).await
Expand Down
Loading