2020-11-26 01:10:51 +00:00
|
|
|
use crate::{http_metrics::metrics, validator_store::ValidatorStore};
|
2019-11-25 04:48:24 +00:00
|
|
|
use environment::RuntimeContext;
|
2020-09-29 03:46:54 +00:00
|
|
|
use eth2::{types::Graffiti, BeaconNodeHttpClient};
|
2020-07-07 04:03:21 +00:00
|
|
|
use futures::channel::mpsc::Receiver;
|
2020-06-04 11:48:05 +00:00
|
|
|
use futures::{StreamExt, TryFutureExt};
|
2020-07-07 04:03:21 +00:00
|
|
|
use slog::{crit, debug, error, info, trace, warn};
|
2019-11-25 04:48:24 +00:00
|
|
|
use slot_clock::SlotClock;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
2020-09-29 03:46:54 +00:00
|
|
|
use types::{EthSpec, PublicKey, Slot};
|
2019-11-25 04:48:24 +00:00
|
|
|
|
|
|
|
/// Builds a `BlockService`.
|
|
|
|
pub struct BlockServiceBuilder<T, E: EthSpec> {
|
|
|
|
validator_store: Option<ValidatorStore<T, E>>,
|
|
|
|
slot_clock: Option<Arc<T>>,
|
2020-09-29 03:46:54 +00:00
|
|
|
beacon_node: Option<BeaconNodeHttpClient>,
|
2019-11-25 04:48:24 +00:00
|
|
|
context: Option<RuntimeContext<E>>,
|
2020-08-11 02:16:29 +00:00
|
|
|
graffiti: Option<Graffiti>,
|
2019-11-25 04:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: SlotClock + 'static, E: EthSpec> BlockServiceBuilder<T, E> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
validator_store: None,
|
|
|
|
slot_clock: None,
|
|
|
|
beacon_node: None,
|
|
|
|
context: None,
|
2020-08-11 02:16:29 +00:00
|
|
|
graffiti: None,
|
2019-11-25 04:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn validator_store(mut self, store: ValidatorStore<T, E>) -> Self {
|
|
|
|
self.validator_store = Some(store);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn slot_clock(mut self, slot_clock: T) -> Self {
|
|
|
|
self.slot_clock = Some(Arc::new(slot_clock));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-29 03:46:54 +00:00
|
|
|
pub fn beacon_node(mut self, beacon_node: BeaconNodeHttpClient) -> Self {
|
2019-11-25 04:48:24 +00:00
|
|
|
self.beacon_node = Some(beacon_node);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn runtime_context(mut self, context: RuntimeContext<E>) -> Self {
|
|
|
|
self.context = Some(context);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:16:29 +00:00
|
|
|
pub fn graffiti(mut self, graffiti: Option<Graffiti>) -> Self {
|
|
|
|
self.graffiti = graffiti;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
pub fn build(self) -> Result<BlockService<T, E>, String> {
|
|
|
|
Ok(BlockService {
|
|
|
|
inner: Arc::new(Inner {
|
|
|
|
validator_store: self
|
|
|
|
.validator_store
|
|
|
|
.ok_or_else(|| "Cannot build BlockService without validator_store")?,
|
|
|
|
slot_clock: self
|
|
|
|
.slot_clock
|
|
|
|
.ok_or_else(|| "Cannot build BlockService without slot_clock")?,
|
|
|
|
beacon_node: self
|
|
|
|
.beacon_node
|
|
|
|
.ok_or_else(|| "Cannot build BlockService without beacon_node")?,
|
|
|
|
context: self
|
|
|
|
.context
|
|
|
|
.ok_or_else(|| "Cannot build BlockService without runtime_context")?,
|
2020-08-11 02:16:29 +00:00
|
|
|
graffiti: self.graffiti,
|
2019-11-25 04:48:24 +00:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper to minimise `Arc` usage.
|
|
|
|
pub struct Inner<T, E: EthSpec> {
|
|
|
|
validator_store: ValidatorStore<T, E>,
|
|
|
|
slot_clock: Arc<T>,
|
2020-09-29 03:46:54 +00:00
|
|
|
beacon_node: BeaconNodeHttpClient,
|
2019-11-25 04:48:24 +00:00
|
|
|
context: RuntimeContext<E>,
|
2020-08-11 02:16:29 +00:00
|
|
|
graffiti: Option<Graffiti>,
|
2019-11-25 04:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to produce attestations for any block producer(s) at the start of the epoch.
|
|
|
|
pub struct BlockService<T, E: EthSpec> {
|
|
|
|
inner: Arc<Inner<T, E>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E: EthSpec> Clone for BlockService<T, E> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E: EthSpec> Deref for BlockService<T, E> {
|
|
|
|
type Target = Inner<T, E>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
self.inner.deref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:03:21 +00:00
|
|
|
/// Notification from the duties service that we should try to produce a block.
|
|
|
|
pub struct BlockServiceNotification {
|
|
|
|
pub slot: Slot,
|
|
|
|
pub block_proposers: Vec<PublicKey>,
|
|
|
|
}
|
|
|
|
|
2019-11-25 04:48:24 +00:00
|
|
|
impl<T: SlotClock + 'static, E: EthSpec> BlockService<T, E> {
|
2020-07-07 04:03:21 +00:00
|
|
|
pub fn start_update_service(
|
|
|
|
self,
|
|
|
|
notification_rx: Receiver<BlockServiceNotification>,
|
|
|
|
) -> Result<(), String> {
|
2020-06-04 11:48:05 +00:00
|
|
|
let log = self.context.log().clone();
|
2019-11-25 04:48:24 +00:00
|
|
|
|
2020-07-07 04:03:21 +00:00
|
|
|
info!(log, "Block production service started");
|
2019-11-25 04:48:24 +00:00
|
|
|
|
2020-06-04 11:48:05 +00:00
|
|
|
let executor = self.inner.context.executor.clone();
|
2020-05-17 11:16:48 +00:00
|
|
|
|
2020-07-07 04:03:21 +00:00
|
|
|
let block_service_fut = notification_rx.for_each(move |notif| {
|
|
|
|
let service = self.clone();
|
|
|
|
async move {
|
|
|
|
service.do_update(notif).await.ok();
|
2020-05-17 11:16:48 +00:00
|
|
|
}
|
2020-07-07 04:03:21 +00:00
|
|
|
});
|
2020-05-17 11:16:48 +00:00
|
|
|
|
2020-07-07 04:03:21 +00:00
|
|
|
executor.spawn(block_service_fut, "block_service");
|
2020-05-17 11:16:48 +00:00
|
|
|
|
2020-06-04 11:48:05 +00:00
|
|
|
Ok(())
|
2019-11-25 04:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to produce a block for any block producers in the `ValidatorStore`.
|
2020-07-07 04:03:21 +00:00
|
|
|
async fn do_update(&self, notification: BlockServiceNotification) -> Result<(), ()> {
|
2020-06-04 11:48:05 +00:00
|
|
|
let log = self.context.log();
|
2020-11-26 01:10:51 +00:00
|
|
|
let _timer =
|
|
|
|
metrics::start_timer_vec(&metrics::BLOCK_SERVICE_TIMES, &[metrics::FULL_UPDATE]);
|
2020-05-17 11:16:48 +00:00
|
|
|
|
|
|
|
let slot = self.slot_clock.now().ok_or_else(move || {
|
|
|
|
crit!(log, "Duties manager failed to read slot clock");
|
|
|
|
})?;
|
|
|
|
|
2020-07-07 04:03:21 +00:00
|
|
|
if notification.slot != slot {
|
|
|
|
warn!(
|
|
|
|
log,
|
|
|
|
"Skipping block production for expired slot";
|
|
|
|
"current_slot" => slot.as_u64(),
|
|
|
|
"notification_slot" => notification.slot.as_u64(),
|
|
|
|
"info" => "Your machine could be overloaded"
|
|
|
|
);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if slot == self.context.eth2_config.spec.genesis_slot {
|
|
|
|
debug!(
|
|
|
|
log,
|
|
|
|
"Not producing block at genesis slot";
|
|
|
|
"proposers" => format!("{:?}", notification.block_proposers),
|
|
|
|
);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-05-17 11:16:48 +00:00
|
|
|
trace!(
|
|
|
|
log,
|
|
|
|
"Block service update started";
|
|
|
|
"slot" => slot.as_u64()
|
|
|
|
);
|
|
|
|
|
2020-07-07 04:03:21 +00:00
|
|
|
let proposers = notification.block_proposers;
|
2020-05-17 11:16:48 +00:00
|
|
|
|
2020-07-07 04:03:21 +00:00
|
|
|
if proposers.is_empty() {
|
2020-05-17 11:16:48 +00:00
|
|
|
trace!(
|
|
|
|
log,
|
|
|
|
"No local block proposers for this slot";
|
|
|
|
"slot" => slot.as_u64()
|
|
|
|
)
|
2020-07-07 04:03:21 +00:00
|
|
|
} else if proposers.len() > 1 {
|
2020-05-17 11:16:48 +00:00
|
|
|
error!(
|
|
|
|
log,
|
|
|
|
"Multiple block proposers for this slot";
|
|
|
|
"action" => "producing blocks for all proposers",
|
2020-07-07 04:03:21 +00:00
|
|
|
"num_proposers" => proposers.len(),
|
2020-05-17 11:16:48 +00:00
|
|
|
"slot" => slot.as_u64(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-28 05:30:57 +00:00
|
|
|
for validator_pubkey in proposers {
|
2020-05-17 11:16:48 +00:00
|
|
|
let service = self.clone();
|
|
|
|
let log = log.clone();
|
2020-11-28 05:30:57 +00:00
|
|
|
self.inner.context.executor.spawn(
|
2020-05-17 11:16:48 +00:00
|
|
|
service
|
|
|
|
.publish_block(slot, validator_pubkey)
|
2020-11-28 05:30:57 +00:00
|
|
|
.unwrap_or_else(move |e| {
|
2020-05-17 11:16:48 +00:00
|
|
|
crit!(
|
|
|
|
log,
|
|
|
|
"Error whilst producing block";
|
|
|
|
"message" => e
|
2020-11-28 05:30:57 +00:00
|
|
|
);
|
2020-05-17 11:16:48 +00:00
|
|
|
}),
|
2020-11-28 05:30:57 +00:00
|
|
|
"block service",
|
2020-05-17 11:16:48 +00:00
|
|
|
);
|
2020-11-28 05:30:57 +00:00
|
|
|
}
|
2020-05-17 11:16:48 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce a block at the given slot for validator_pubkey
|
|
|
|
async fn publish_block(self, slot: Slot, validator_pubkey: PublicKey) -> Result<(), String> {
|
2020-06-04 11:48:05 +00:00
|
|
|
let log = self.context.log();
|
2020-11-26 01:10:51 +00:00
|
|
|
let _timer =
|
|
|
|
metrics::start_timer_vec(&metrics::BLOCK_SERVICE_TIMES, &[metrics::BEACON_BLOCK]);
|
2020-05-17 11:16:48 +00:00
|
|
|
|
2020-05-18 06:25:16 +00:00
|
|
|
let current_slot = self
|
|
|
|
.slot_clock
|
|
|
|
.now()
|
|
|
|
.ok_or_else(|| "Unable to determine current slot from clock".to_string())?;
|
|
|
|
|
2020-05-17 11:16:48 +00:00
|
|
|
let randao_reveal = self
|
|
|
|
.validator_store
|
|
|
|
.randao_reveal(&validator_pubkey, slot.epoch(E::slots_per_epoch()))
|
|
|
|
.ok_or_else(|| "Unable to produce randao reveal".to_string())?;
|
|
|
|
|
|
|
|
let block = self
|
|
|
|
.beacon_node
|
2020-09-29 03:46:54 +00:00
|
|
|
.get_validator_blocks(slot, randao_reveal.into(), self.graffiti.as_ref())
|
2020-05-17 11:16:48 +00:00
|
|
|
.await
|
2020-09-29 03:46:54 +00:00
|
|
|
.map_err(|e| format!("Error from beacon node when producing block: {:?}", e))?
|
|
|
|
.data;
|
2020-05-17 11:16:48 +00:00
|
|
|
|
|
|
|
let signed_block = self
|
|
|
|
.validator_store
|
2020-05-18 06:25:16 +00:00
|
|
|
.sign_block(&validator_pubkey, block, current_slot)
|
2020-05-17 11:16:48 +00:00
|
|
|
.ok_or_else(|| "Unable to sign block".to_string())?;
|
|
|
|
|
2020-09-29 03:46:54 +00:00
|
|
|
self.beacon_node
|
|
|
|
.post_beacon_blocks(&signed_block)
|
2020-05-17 11:16:48 +00:00
|
|
|
.await
|
|
|
|
.map_err(|e| format!("Error from beacon node when publishing block: {:?}", e))?;
|
|
|
|
|
2020-09-29 03:46:54 +00:00
|
|
|
info!(
|
|
|
|
log,
|
|
|
|
"Successfully published block";
|
|
|
|
"deposits" => signed_block.message.body.deposits.len(),
|
|
|
|
"attestations" => signed_block.message.body.attestations.len(),
|
|
|
|
"slot" => signed_block.slot().as_u64(),
|
|
|
|
);
|
2020-05-17 11:16:48 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-11-25 04:48:24 +00:00
|
|
|
}
|
|
|
|
}
|