Skip to content

Commit

Permalink
reverted fee_recipient api parameter, added proposer preparation serv…
Browse files Browse the repository at this point in the history
…ice that generates proposer preparations for all known validators at the beginning of each epoch
  • Loading branch information
pk910 committed Jan 18, 2022
1 parent 51a1af6 commit 49e27f6
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 77 deletions.
6 changes: 1 addition & 5 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2869,7 +2869,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
randao_reveal: Signature,
slot: Slot,
validator_graffiti: Option<Graffiti>,
validator_fee_recipient: Option<Address>,
) -> Result<BeaconBlockAndState<T::EthSpec>, BlockProductionError> {
metrics::inc_counter(&metrics::BLOCK_PRODUCTION_REQUESTS);
let _complete_timer = metrics::start_timer(&metrics::BLOCK_PRODUCTION_TIMES);
Expand Down Expand Up @@ -2927,7 +2926,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
slot,
randao_reveal,
validator_graffiti,
validator_fee_recipient,
)
}

Expand All @@ -2950,7 +2948,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
produce_at_slot: Slot,
randao_reveal: Signature,
validator_graffiti: Option<Graffiti>,
validator_fee_recipient: Option<Address>,
) -> Result<BeaconBlockAndState<T::EthSpec>, BlockProductionError> {
let eth1_chain = self
.eth1_chain
Expand Down Expand Up @@ -3099,8 +3096,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
}
BeaconState::Merge(_) => {
let sync_aggregate = get_sync_aggregate()?;
let execution_payload =
get_execution_payload(self, &state, validator_fee_recipient)?;
let execution_payload = get_execution_payload(self, &state)?;
BeaconBlock::Merge(BeaconBlockMerge {
slot,
proposer_index,
Expand Down
10 changes: 2 additions & 8 deletions beacon_node/beacon_chain/src/execution_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,22 @@ pub fn validate_execution_payload_for_gossip<T: BeaconChainTypes>(
pub fn get_execution_payload<T: BeaconChainTypes>(
chain: &BeaconChain<T>,
state: &BeaconState<T::EthSpec>,
fee_recipient: Option<Address>,
) -> Result<ExecutionPayload<T::EthSpec>, BlockProductionError> {
Ok(prepare_execution_payload_blocking(chain, state, fee_recipient)?.unwrap_or_default())
Ok(prepare_execution_payload_blocking(chain, state)?.unwrap_or_default())
}

/// Wraps the async `prepare_execution_payload` function as a blocking task.
pub fn prepare_execution_payload_blocking<T: BeaconChainTypes>(
chain: &BeaconChain<T>,
state: &BeaconState<T::EthSpec>,
fee_recipient: Option<Address>,
) -> Result<Option<ExecutionPayload<T::EthSpec>>, BlockProductionError> {
let execution_layer = chain
.execution_layer
.as_ref()
.ok_or(BlockProductionError::ExecutionLayerMissing)?;

execution_layer
.block_on_generic(|_| async {
prepare_execution_payload(chain, state, fee_recipient).await
})
.block_on_generic(|_| async { prepare_execution_payload(chain, state).await })
.map_err(BlockProductionError::BlockingFailed)?
}

Expand All @@ -244,7 +240,6 @@ pub fn prepare_execution_payload_blocking<T: BeaconChainTypes>(
pub async fn prepare_execution_payload<T: BeaconChainTypes>(
chain: &BeaconChain<T>,
state: &BeaconState<T::EthSpec>,
fee_recipient: Option<Address>,
) -> Result<Option<ExecutionPayload<T::EthSpec>>, BlockProductionError> {
let spec = &chain.spec;
let execution_layer = chain
Expand Down Expand Up @@ -305,7 +300,6 @@ pub async fn prepare_execution_payload<T: BeaconChainTypes>(
timestamp,
random,
finalized_block_hash.unwrap_or_else(Hash256::zero),
fee_recipient,
)
.await
.map_err(BlockProductionError::GetPayloadFailed)?;
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ where

let (block, state) = self
.chain
.produce_block_on_state(state, None, slot, randao_reveal, Some(graffiti), None)
.produce_block_on_state(state, None, slot, randao_reveal, Some(graffiti))
.unwrap();

let signed_block = block.sign(
Expand Down Expand Up @@ -641,7 +641,7 @@ where

let (block, state) = self
.chain
.produce_block_on_state(state, None, slot, randao_reveal, Some(graffiti), None)
.produce_block_on_state(state, None, slot, randao_reveal, Some(graffiti))
.unwrap();

let signed_block = block.sign(
Expand Down
8 changes: 1 addition & 7 deletions beacon_node/execution_layer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,8 @@ impl ExecutionLayer {
timestamp: u64,
random: Hash256,
finalized_block_hash: Hash256,
validator_fee_recipient: Option<Address>,
) -> Result<ExecutionPayload<T>, Error> {
// Override the beacon node's suggested fee-recipient with fee-recipient from the validator, if present.
let suggested_fee_recipient = match validator_fee_recipient {
Some(fee_recipient) => fee_recipient,
None => self.suggested_fee_recipient()?,
};

let suggested_fee_recipient = self.suggested_fee_recipient()?;
debug!(
self.log(),
"Issuing engine_getPayload";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<T: EthSpec> MockExecutionLayer<T> {

let payload = self
.el
.get_payload::<T>(parent_hash, timestamp, random, finalized_block_hash, None)
.get_payload::<T>(parent_hash, timestamp, random, finalized_block_hash)
.await
.unwrap();
let block_hash = payload.block_hash;
Expand Down
7 changes: 1 addition & 6 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1869,12 +1869,7 @@ pub fn serve<T: BeaconChainTypes>(
})?;

let (block, _) = chain
.produce_block(
randao_reveal,
slot,
query.graffiti.map(Into::into),
query.fee_recipient,
)
.produce_block(randao_reveal, slot, query.graffiti.map(Into::into))
.map_err(warp_utils::reject::block_production_error)?;
let fork_name = block
.to_ref()
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ impl ApiTester {

let block = self
.client
.get_validator_blocks::<E>(slot, &randao_reveal, None, None)
.get_validator_blocks::<E>(slot, &randao_reveal, None)
.await
.unwrap()
.data;
Expand Down
23 changes: 17 additions & 6 deletions common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,23 @@ impl BeaconNodeHttpClient {
Ok(())
}

/// `POST validator/prepare_beacon_proposer`
pub async fn post_validator_prepare_beacon_proposer(
&self,
preparation_data: &[ProposerPreparationData],
) -> Result<(), Error> {
let mut path = self.eth_path(V1)?;

path.path_segments_mut()
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
.push("validator")
.push("prepare_beacon_proposer");

self.post(path, &preparation_data).await?;

Ok(())
}

/// `GET config/fork_schedule`
pub async fn get_config_fork_schedule(&self) -> Result<GenericResponse<Vec<Fork>>, Error> {
let mut path = self.eth_path(V1)?;
Expand Down Expand Up @@ -1127,7 +1144,6 @@ impl BeaconNodeHttpClient {
slot: Slot,
randao_reveal: &SignatureBytes,
graffiti: Option<&Graffiti>,
fee_recipient: Option<&Address>,
) -> Result<ForkVersionedResponse<BeaconBlock<T>>, Error> {
let mut path = self.eth_path(V2)?;

Expand All @@ -1145,11 +1161,6 @@ impl BeaconNodeHttpClient {
.append_pair("graffiti", &graffiti.to_string());
}

if let Some(fee_recipient) = fee_recipient {
path.query_pairs_mut()
.append_pair("fee_recipient", &format!("{:?}", fee_recipient));
}

self.get(path).await
}

Expand Down
1 change: 0 additions & 1 deletion common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,6 @@ pub struct ProposerData {
pub struct ValidatorBlocksQuery {
pub randao_reveal: SignatureBytes,
pub graffiti: Option<Graffiti>,
pub fee_recipient: Option<Address>,
}

#[derive(Clone, Serialize, Deserialize)]
Expand Down
2 changes: 2 additions & 0 deletions consensus/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub mod graffiti;
pub mod historical_batch;
pub mod indexed_attestation;
pub mod pending_attestation;
pub mod proposer_preparation_data;
pub mod proposer_slashing;
pub mod relative_epoch;
pub mod selection_proof;
Expand Down Expand Up @@ -126,6 +127,7 @@ pub use crate::participation_flags::ParticipationFlags;
pub use crate::participation_list::ParticipationList;
pub use crate::pending_attestation::PendingAttestation;
pub use crate::preset::{AltairPreset, BasePreset};
pub use crate::proposer_preparation_data::ProposerPreparationData;
pub use crate::proposer_slashing::ProposerSlashing;
pub use crate::relative_epoch::{Error as RelativeEpochError, RelativeEpoch};
pub use crate::selection_proof::SelectionProof;
Expand Down
12 changes: 12 additions & 0 deletions consensus/types/src/proposer_preparation_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::*;
use serde::{Deserialize, Serialize};

/// A proposer preparation, created when a validator prepares the beacon node for potential proposers
/// by supplying information required when proposing blocks for the given validators.
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct ProposerPreparationData {
/// The validators index.
pub validator_index: u64,
/// The fee-recipient address.
pub fee_recipient: Address,
}
40 changes: 2 additions & 38 deletions validator_client/src/block_service.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
beacon_node_fallback::{BeaconNodeFallback, RequireSynced},
fee_recipient_file::FeeRecipientFile,
graffiti_file::GraffitiFile,
};
use crate::{http_metrics::metrics, validator_store::ValidatorStore};
Expand All @@ -11,7 +10,7 @@ use slot_clock::SlotClock;
use std::ops::Deref;
use std::sync::Arc;
use tokio::sync::mpsc;
use types::{Address, EthSpec, PublicKeyBytes, Slot};
use types::{EthSpec, PublicKeyBytes, Slot};

/// Builds a `BlockService`.
pub struct BlockServiceBuilder<T, E: EthSpec> {
Expand All @@ -21,8 +20,6 @@ pub struct BlockServiceBuilder<T, E: EthSpec> {
context: Option<RuntimeContext<E>>,
graffiti: Option<Graffiti>,
graffiti_file: Option<GraffitiFile>,
fee_recipient: Option<Address>,
fee_recipient_file: Option<FeeRecipientFile>,
}

impl<T: SlotClock + 'static, E: EthSpec> BlockServiceBuilder<T, E> {
Expand All @@ -34,8 +31,6 @@ impl<T: SlotClock + 'static, E: EthSpec> BlockServiceBuilder<T, E> {
context: None,
graffiti: None,
graffiti_file: None,
fee_recipient: None,
fee_recipient_file: None,
}
}

Expand Down Expand Up @@ -69,16 +64,6 @@ impl<T: SlotClock + 'static, E: EthSpec> BlockServiceBuilder<T, E> {
self
}

pub fn fee_recipient(mut self, fee_recipient: Option<Address>) -> Self {
self.fee_recipient = fee_recipient;
self
}

pub fn fee_recipient_file(mut self, fee_recipient_file: Option<FeeRecipientFile>) -> Self {
self.fee_recipient_file = fee_recipient_file;
self
}

pub fn build(self) -> Result<BlockService<T, E>, String> {
Ok(BlockService {
inner: Arc::new(Inner {
Expand All @@ -96,8 +81,6 @@ impl<T: SlotClock + 'static, E: EthSpec> BlockServiceBuilder<T, E> {
.ok_or("Cannot build BlockService without runtime_context")?,
graffiti: self.graffiti,
graffiti_file: self.graffiti_file,
fee_recipient: self.fee_recipient,
fee_recipient_file: self.fee_recipient_file,
}),
})
}
Expand All @@ -111,8 +94,6 @@ pub struct Inner<T, E: EthSpec> {
context: RuntimeContext<E>,
graffiti: Option<Graffiti>,
graffiti_file: Option<GraffitiFile>,
fee_recipient: Option<Address>,
fee_recipient_file: Option<FeeRecipientFile>,
}

/// Attempts to produce attestations for any block producer(s) at the start of the epoch.
Expand Down Expand Up @@ -276,18 +257,6 @@ impl<T: SlotClock + 'static, E: EthSpec> BlockService<T, E> {
.or_else(|| self.validator_store.graffiti(&validator_pubkey))
.or(self.graffiti);

let fee_recipient = self
.fee_recipient_file
.clone()
.and_then(|mut g| match g.load_fee_recipient(&validator_pubkey) {
Ok(g) => g,
Err(e) => {
warn!(log, "Failed to read fee-recipient file"; "error" => ?e);
None
}
})
.or(self.fee_recipient);

let randao_reveal_ref = &randao_reveal;
let self_ref = &self;
let proposer_index = self.validator_store.validator_index(&validator_pubkey);
Expand All @@ -300,12 +269,7 @@ impl<T: SlotClock + 'static, E: EthSpec> BlockService<T, E> {
&[metrics::BEACON_BLOCK_HTTP_GET],
);
let block = beacon_node
.get_validator_blocks(
slot,
randao_reveal_ref,
graffiti.as_ref(),
fee_recipient.as_ref(),
)
.get_validator_blocks(slot, randao_reveal_ref, graffiti.as_ref())
.await
.map_err(|e| format!("Error from beacon node when producing block: {:?}", e))?
.data;
Expand Down
15 changes: 15 additions & 0 deletions validator_client/src/fee_recipient_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ impl FeeRecipientFile {
}
}

/// Returns the fee-recipient corresponding to the given public key if present, else returns the
/// default fee-recipient.
///
/// Returns an error if loading from the fee-recipient file fails.
pub fn get_fee_recipient(
&mut self,
public_key: &PublicKeyBytes,
) -> Result<Option<Address>, Error> {
Ok(self
.fee_recipients
.get(public_key)
.copied()
.or(self.default))
}

/// Loads the fee-recipient file and populates the default fee-recipient and `fee_recipients` hashmap.
/// Returns the fee-recipient corresponding to the given public key if present, else returns the
/// default fee-recipient.
Expand Down
Loading

0 comments on commit 49e27f6

Please sign in to comment.