2021-09-29 22:14:15 +00:00
|
|
|
//! Provides generic behaviour for multiple execution engines, specifically fallback behaviour.
|
|
|
|
|
2022-02-17 21:47:06 +00:00
|
|
|
use crate::engine_api::{
|
2022-07-11 01:44:41 +00:00
|
|
|
Error as EngineApiError, ForkchoiceUpdatedResponse, PayloadAttributes, PayloadId,
|
2022-02-17 21:47:06 +00:00
|
|
|
};
|
2022-07-01 01:15:19 +00:00
|
|
|
use crate::HttpJsonRpc;
|
2021-11-15 06:13:38 +00:00
|
|
|
use lru::LruCache;
|
2022-07-13 20:31:39 +00:00
|
|
|
use slog::{debug, error, info, Logger};
|
2021-09-29 22:14:15 +00:00
|
|
|
use std::future::Future;
|
2022-07-13 20:31:39 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use task_executor::TaskExecutor;
|
2022-08-24 23:34:56 +00:00
|
|
|
use tokio::sync::{watch, Mutex, RwLock};
|
|
|
|
use tokio_stream::wrappers::WatchStream;
|
2022-02-28 22:07:48 +00:00
|
|
|
use types::{Address, ExecutionBlockHash, Hash256};
|
2021-11-15 06:13:38 +00:00
|
|
|
|
|
|
|
/// The number of payload IDs that will be stored for each `Engine`.
|
|
|
|
///
|
|
|
|
/// Since the size of each value is small (~100 bytes) a large number is used for safety.
|
|
|
|
const PAYLOAD_ID_LRU_CACHE_SIZE: usize = 512;
|
2021-09-29 22:14:15 +00:00
|
|
|
|
|
|
|
/// Stores the remembered state of a engine.
|
2022-08-24 23:34:56 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Eq, Default)]
|
|
|
|
enum EngineStateInternal {
|
2021-10-06 10:21:21 +00:00
|
|
|
Synced,
|
2022-08-24 23:34:56 +00:00
|
|
|
#[default]
|
2021-09-29 22:14:15 +00:00
|
|
|
Offline,
|
2021-10-06 10:21:21 +00:00
|
|
|
Syncing,
|
2022-03-08 06:46:24 +00:00
|
|
|
AuthFailed,
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 23:34:56 +00:00
|
|
|
/// A subset of the engine state to inform other services if the engine is online or offline.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
|
|
|
|
pub enum EngineState {
|
|
|
|
Online,
|
|
|
|
Offline,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<EngineStateInternal> for EngineState {
|
|
|
|
fn from(state: EngineStateInternal) -> Self {
|
|
|
|
match state {
|
|
|
|
EngineStateInternal::Synced | EngineStateInternal::Syncing => EngineState::Online,
|
|
|
|
EngineStateInternal::Offline | EngineStateInternal::AuthFailed => EngineState::Offline,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wrapper structure that ensures changes to the engine state are correctly reported to watchers.
|
|
|
|
struct State {
|
|
|
|
/// The actual engine state.
|
|
|
|
state: EngineStateInternal,
|
|
|
|
/// Notifier to watch the engine state.
|
|
|
|
notifier: watch::Sender<EngineState>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Deref for State {
|
|
|
|
type Target = EngineStateInternal;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
|
|
|
let state = EngineStateInternal::default();
|
|
|
|
let (notifier, _receiver) = watch::channel(state.into());
|
|
|
|
State { state, notifier }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
// Updates the state and notifies all watchers if the state has changed.
|
|
|
|
pub fn update(&mut self, new_state: EngineStateInternal) {
|
|
|
|
self.state = new_state;
|
|
|
|
self.notifier.send_if_modified(|last_state| {
|
|
|
|
let changed = *last_state != new_state.into(); // notify conditionally
|
|
|
|
*last_state = new_state.into(); // update the state unconditionally
|
|
|
|
changed
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gives access to a channel containing whether the last state is online.
|
|
|
|
///
|
|
|
|
/// This can be called several times.
|
|
|
|
pub fn watch(&self) -> WatchStream<EngineState> {
|
|
|
|
self.notifier.subscribe().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 10:21:21 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2021-11-15 06:13:38 +00:00
|
|
|
pub struct ForkChoiceState {
|
2022-02-28 22:07:48 +00:00
|
|
|
pub head_block_hash: ExecutionBlockHash,
|
|
|
|
pub safe_block_hash: ExecutionBlockHash,
|
|
|
|
pub finalized_block_hash: ExecutionBlockHash,
|
2021-10-06 10:21:21 +00:00
|
|
|
}
|
2021-09-29 22:14:15 +00:00
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
#[derive(Hash, PartialEq, std::cmp::Eq)]
|
|
|
|
struct PayloadIdCacheKey {
|
2022-02-28 22:07:48 +00:00
|
|
|
pub head_block_hash: ExecutionBlockHash,
|
2021-11-15 06:13:38 +00:00
|
|
|
pub timestamp: u64,
|
2022-03-03 02:10:57 +00:00
|
|
|
pub prev_randao: Hash256,
|
2021-12-12 09:04:21 +00:00
|
|
|
pub suggested_fee_recipient: Address,
|
2021-11-15 06:13:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 01:44:41 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum EngineError {
|
|
|
|
Offline,
|
|
|
|
Api { error: EngineApiError },
|
|
|
|
BuilderApi { error: EngineApiError },
|
|
|
|
Auth,
|
|
|
|
}
|
|
|
|
|
2021-09-29 22:14:15 +00:00
|
|
|
/// An execution engine.
|
2022-07-11 01:44:41 +00:00
|
|
|
pub struct Engine {
|
|
|
|
pub api: HttpJsonRpc,
|
2021-11-15 06:13:38 +00:00
|
|
|
payload_id_cache: Mutex<LruCache<PayloadIdCacheKey, PayloadId>>,
|
2022-08-24 23:34:56 +00:00
|
|
|
state: RwLock<State>,
|
|
|
|
latest_forkchoice_state: RwLock<Option<ForkChoiceState>>,
|
|
|
|
executor: TaskExecutor,
|
|
|
|
log: Logger,
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 01:44:41 +00:00
|
|
|
impl Engine {
|
2021-09-29 22:14:15 +00:00
|
|
|
/// Creates a new, offline engine.
|
2022-07-13 20:31:39 +00:00
|
|
|
pub fn new(api: HttpJsonRpc, executor: TaskExecutor, log: &Logger) -> Self {
|
2021-09-29 22:14:15 +00:00
|
|
|
Self {
|
|
|
|
api,
|
2021-11-15 06:13:38 +00:00
|
|
|
payload_id_cache: Mutex::new(LruCache::new(PAYLOAD_ID_LRU_CACHE_SIZE)),
|
2022-08-24 23:34:56 +00:00
|
|
|
state: Default::default(),
|
2022-07-11 01:44:41 +00:00
|
|
|
latest_forkchoice_state: Default::default(),
|
2022-07-13 20:31:39 +00:00
|
|
|
executor,
|
2022-07-11 01:44:41 +00:00
|
|
|
log: log.clone(),
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-15 06:13:38 +00:00
|
|
|
|
2022-08-24 23:34:56 +00:00
|
|
|
/// Gives access to a channel containing the last engine state.
|
|
|
|
///
|
|
|
|
/// This can be called several times.
|
|
|
|
pub async fn watch_state(&self) -> WatchStream<EngineState> {
|
|
|
|
self.state.read().await.watch()
|
|
|
|
}
|
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
pub async fn get_payload_id(
|
|
|
|
&self,
|
2022-02-28 22:07:48 +00:00
|
|
|
head_block_hash: ExecutionBlockHash,
|
2021-11-15 06:13:38 +00:00
|
|
|
timestamp: u64,
|
2022-03-03 02:10:57 +00:00
|
|
|
prev_randao: Hash256,
|
2021-12-12 09:04:21 +00:00
|
|
|
suggested_fee_recipient: Address,
|
2021-11-15 06:13:38 +00:00
|
|
|
) -> Option<PayloadId> {
|
|
|
|
self.payload_id_cache
|
|
|
|
.lock()
|
|
|
|
.await
|
|
|
|
.get(&PayloadIdCacheKey {
|
|
|
|
head_block_hash,
|
|
|
|
timestamp,
|
2022-03-03 02:10:57 +00:00
|
|
|
prev_randao,
|
2021-12-12 09:04:21 +00:00
|
|
|
suggested_fee_recipient,
|
2021-11-15 06:13:38 +00:00
|
|
|
})
|
|
|
|
.cloned()
|
|
|
|
}
|
|
|
|
|
2022-07-01 01:15:19 +00:00
|
|
|
pub async fn notify_forkchoice_updated(
|
2021-11-15 06:13:38 +00:00
|
|
|
&self,
|
|
|
|
forkchoice_state: ForkChoiceState,
|
|
|
|
payload_attributes: Option<PayloadAttributes>,
|
|
|
|
log: &Logger,
|
2022-02-17 21:47:06 +00:00
|
|
|
) -> Result<ForkchoiceUpdatedResponse, EngineApiError> {
|
2021-11-15 06:13:38 +00:00
|
|
|
let response = self
|
|
|
|
.api
|
|
|
|
.forkchoice_updated_v1(forkchoice_state, payload_attributes)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if let Some(payload_id) = response.payload_id {
|
|
|
|
if let Some(key) =
|
|
|
|
payload_attributes.map(|pa| PayloadIdCacheKey::new(&forkchoice_state, &pa))
|
|
|
|
{
|
|
|
|
self.payload_id_cache.lock().await.put(key, payload_id);
|
|
|
|
} else {
|
|
|
|
debug!(
|
|
|
|
log,
|
|
|
|
"Engine returned unexpected payload_id";
|
|
|
|
"payload_id" => ?payload_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 21:47:06 +00:00
|
|
|
Ok(response)
|
2021-11-15 06:13:38 +00:00
|
|
|
}
|
2021-09-29 22:14:15 +00:00
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
async fn get_latest_forkchoice_state(&self) -> Option<ForkChoiceState> {
|
|
|
|
*self.latest_forkchoice_state.read().await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn set_latest_forkchoice_state(&self, state: ForkChoiceState) {
|
|
|
|
*self.latest_forkchoice_state.write().await = Some(state);
|
2021-10-06 10:21:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 14:27:16 +00:00
|
|
|
async fn send_latest_forkchoice_state(&self) {
|
2021-11-15 06:13:38 +00:00
|
|
|
let latest_forkchoice_state = self.get_latest_forkchoice_state().await;
|
|
|
|
|
|
|
|
if let Some(forkchoice_state) = latest_forkchoice_state {
|
2022-03-09 00:42:05 +00:00
|
|
|
if forkchoice_state.head_block_hash == ExecutionBlockHash::zero() {
|
|
|
|
debug!(
|
|
|
|
self.log,
|
|
|
|
"No need to call forkchoiceUpdated";
|
|
|
|
"msg" => "head does not have execution enabled",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-06 10:21:21 +00:00
|
|
|
info!(
|
|
|
|
self.log,
|
|
|
|
"Issuing forkchoiceUpdated";
|
2021-11-15 06:13:38 +00:00
|
|
|
"forkchoice_state" => ?forkchoice_state,
|
2021-10-06 10:21:21 +00:00
|
|
|
);
|
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
// For simplicity, payload attributes are never included in this call. It may be
|
|
|
|
// reasonable to include them in the future.
|
2022-07-11 01:44:41 +00:00
|
|
|
if let Err(e) = self.api.forkchoice_updated_v1(forkchoice_state, None).await {
|
2021-10-06 13:34:17 +00:00
|
|
|
debug!(
|
2021-10-06 10:21:21 +00:00
|
|
|
self.log,
|
|
|
|
"Failed to issue latest head to engine";
|
|
|
|
"error" => ?e,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug!(
|
|
|
|
self.log,
|
|
|
|
"No head, not sending to engine";
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 14:27:16 +00:00
|
|
|
/// Returns `true` if the engine has a "synced" status.
|
|
|
|
pub async fn is_synced(&self) -> bool {
|
2022-08-24 23:34:56 +00:00
|
|
|
**self.state.read().await == EngineStateInternal::Synced
|
2021-10-06 10:21:21 +00:00
|
|
|
}
|
2022-07-13 20:31:39 +00:00
|
|
|
|
2022-06-22 14:27:16 +00:00
|
|
|
/// Run the `EngineApi::upcheck` function if the node's last known state is not synced. This
|
|
|
|
/// might be used to recover the node if offline.
|
2022-07-13 20:31:39 +00:00
|
|
|
pub async fn upcheck(&self) {
|
2022-08-24 23:34:56 +00:00
|
|
|
let state: EngineStateInternal = match self.api.upcheck().await {
|
2022-07-13 20:31:39 +00:00
|
|
|
Ok(()) => {
|
|
|
|
let mut state = self.state.write().await;
|
2022-08-24 23:34:56 +00:00
|
|
|
if **state != EngineStateInternal::Synced {
|
2022-07-13 20:31:39 +00:00
|
|
|
info!(
|
|
|
|
self.log,
|
|
|
|
"Execution engine online";
|
|
|
|
);
|
|
|
|
|
2022-06-22 14:27:16 +00:00
|
|
|
// Send the node our latest forkchoice_state.
|
|
|
|
self.send_latest_forkchoice_state().await;
|
2022-07-13 20:31:39 +00:00
|
|
|
} else {
|
|
|
|
debug!(
|
|
|
|
self.log,
|
|
|
|
"Execution engine online";
|
|
|
|
);
|
2022-06-22 14:27:16 +00:00
|
|
|
}
|
2022-08-24 23:34:56 +00:00
|
|
|
state.update(EngineStateInternal::Synced);
|
|
|
|
**state
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
2022-07-13 20:31:39 +00:00
|
|
|
Err(EngineApiError::IsSyncing) => {
|
|
|
|
let mut state = self.state.write().await;
|
2022-08-24 23:34:56 +00:00
|
|
|
state.update(EngineStateInternal::Syncing);
|
|
|
|
**state
|
2022-07-13 20:31:39 +00:00
|
|
|
}
|
|
|
|
Err(EngineApiError::Auth(err)) => {
|
|
|
|
error!(
|
|
|
|
self.log,
|
|
|
|
"Failed jwt authorization";
|
|
|
|
"error" => ?err,
|
|
|
|
);
|
2021-09-29 22:14:15 +00:00
|
|
|
|
2022-07-13 20:31:39 +00:00
|
|
|
let mut state = self.state.write().await;
|
2022-08-24 23:34:56 +00:00
|
|
|
state.update(EngineStateInternal::AuthFailed);
|
|
|
|
**state
|
2022-07-13 20:31:39 +00:00
|
|
|
}
|
2022-07-04 02:56:13 +00:00
|
|
|
Err(e) => {
|
2022-07-13 20:31:39 +00:00
|
|
|
error!(
|
|
|
|
self.log,
|
|
|
|
"Error during execution engine upcheck";
|
|
|
|
"error" => ?e,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut state = self.state.write().await;
|
2022-08-24 23:34:56 +00:00
|
|
|
state.update(EngineStateInternal::Offline);
|
|
|
|
**state
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
2022-07-13 20:31:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
self.log,
|
|
|
|
"Execution engine upcheck complete";
|
|
|
|
"state" => ?state,
|
|
|
|
);
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 20:31:39 +00:00
|
|
|
/// Run `func` on the node regardless of the node's current state.
|
|
|
|
///
|
|
|
|
/// ## Note
|
|
|
|
///
|
|
|
|
/// This function takes locks on `self.state`, holding a conflicting lock might cause a
|
|
|
|
/// deadlock.
|
|
|
|
pub async fn request<'a, F, G, H>(self: &'a Arc<Self>, func: F) -> Result<H, EngineError>
|
2021-09-29 22:14:15 +00:00
|
|
|
where
|
2022-07-11 01:44:41 +00:00
|
|
|
F: Fn(&'a Engine) -> G,
|
2021-09-29 22:14:15 +00:00
|
|
|
G: Future<Output = Result<H, EngineApiError>>,
|
|
|
|
{
|
2022-07-13 20:31:39 +00:00
|
|
|
match func(self).await {
|
|
|
|
Ok(result) => {
|
|
|
|
// Take a clone *without* holding the read-lock since the `upcheck` function will
|
|
|
|
// take a write-lock.
|
2022-08-24 23:34:56 +00:00
|
|
|
let state: EngineStateInternal = **self.state.read().await;
|
2022-07-13 20:31:39 +00:00
|
|
|
|
2022-08-24 23:34:56 +00:00
|
|
|
// Keep an up to date engine state.
|
|
|
|
if state != EngineStateInternal::Synced {
|
2022-07-13 20:31:39 +00:00
|
|
|
// Spawn the upcheck in another task to avoid slowing down this request.
|
|
|
|
let inner_self = self.clone();
|
|
|
|
self.executor.spawn(
|
|
|
|
async move { inner_self.upcheck().await },
|
|
|
|
"upcheck_after_success",
|
2022-06-22 14:27:16 +00:00
|
|
|
);
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 20:31:39 +00:00
|
|
|
Ok(result)
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
2022-07-13 20:31:39 +00:00
|
|
|
Err(error) => {
|
|
|
|
error!(
|
|
|
|
self.log,
|
|
|
|
"Execution engine call failed";
|
|
|
|
"error" => ?error,
|
|
|
|
);
|
2021-09-29 22:14:15 +00:00
|
|
|
|
2022-07-13 20:31:39 +00:00
|
|
|
// The node just returned an error, run an upcheck so we can update the endpoint
|
|
|
|
// state.
|
|
|
|
//
|
|
|
|
// Spawn the upcheck in another task to avoid slowing down this request.
|
|
|
|
let inner_self = self.clone();
|
|
|
|
self.executor.spawn(
|
|
|
|
async move { inner_self.upcheck().await },
|
|
|
|
"upcheck_after_error",
|
|
|
|
);
|
|
|
|
|
|
|
|
Err(EngineError::Api { error })
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
2022-06-22 14:27:16 +00:00
|
|
|
}
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-15 06:13:38 +00:00
|
|
|
|
|
|
|
impl PayloadIdCacheKey {
|
|
|
|
fn new(state: &ForkChoiceState, attributes: &PayloadAttributes) -> Self {
|
|
|
|
Self {
|
|
|
|
head_block_hash: state.head_block_hash,
|
|
|
|
timestamp: attributes.timestamp,
|
2022-03-03 02:10:57 +00:00
|
|
|
prev_randao: attributes.prev_randao,
|
2021-12-12 09:04:21 +00:00
|
|
|
suggested_fee_recipient: attributes.suggested_fee_recipient,
|
2021-11-15 06:13:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 23:34:56 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use tokio_stream::StreamExt;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_state_notifier() {
|
|
|
|
let mut state = State::default();
|
|
|
|
let initial_state: EngineState = state.state.into();
|
|
|
|
assert_eq!(initial_state, EngineState::Offline);
|
|
|
|
state.update(EngineStateInternal::Synced);
|
|
|
|
|
|
|
|
// a watcher that arrives after the first update.
|
|
|
|
let mut watcher = state.watch();
|
|
|
|
let new_state = watcher.next().await.expect("Last state is always present");
|
|
|
|
assert_eq!(new_state, EngineState::Online);
|
|
|
|
}
|
|
|
|
}
|