Files
lighthouse/beacon_node/execution_layer/src/execute_payload_handle.rs
Paul Hauner d8623cfc4f [Merge] Implement execution_layer (#2635)
* Checkout serde_utils from rayonism

* Make eth1::http functions pub

* Add bones of execution_layer

* Modify decoding

* Expose Transaction, cargo fmt

* Add executePayload

* Add all minimal spec endpoints

* Start adding json rpc wrapper

* Finish custom JSON response handler

* Switch to new rpc sending method

* Add first test

* Fix camelCase

* Finish adding tests

* Begin threading execution layer into BeaconChain

* Fix clippy lints

* Fix clippy lints

* Thread execution layer into ClientBuilder

* Add CLI flags

* Add block processing methods to ExecutionLayer

* Add block_on to execution_layer

* Integrate execute_payload

* Add extra_data field

* Begin implementing payload handle

* Send consensus valid/invalid messages

* Fix minor type in task_executor

* Call forkchoiceUpdated

* Add search for TTD block

* Thread TTD into execution layer

* Allow producing block with execution payload

* Add LRU cache for execution blocks

* Remove duplicate 0x on ssz_types serialization

* Add tests for block getter methods

* Add basic block generator impl

* Add is_valid_terminal_block to EL

* Verify merge block in block_verification

* Partially implement --terminal-block-hash-override

* Add terminal_block_hash to ChainSpec

* Remove Option from terminal_block_hash in EL

* Revert merge changes to consensus/fork_choice

* Remove commented-out code

* Add bones for handling RPC methods on test server

* Add first ExecutionLayer tests

* Add testing for finding terminal block

* Prevent infinite loops

* Add insert_merge_block to block gen

* Add block gen test for pos blocks

* Start adding payloads to block gen

* Fix clippy lints

* Add execution payload to block gen

* Add execute_payload to block_gen

* Refactor block gen

* Add all routes to mock server

* Use Uint256 for base_fee_per_gas

* Add working execution chain build

* Remove unused var

* Revert "Use Uint256 for base_fee_per_gas"

This reverts commit 6c88f19ac4.

* Fix base_fee_for_gas Uint256

* Update execute payload handle

* Improve testing, fix bugs

* Fix default fee-recipient

* Fix fee-recipient address (again)

* Add check for terminal block, add comments, tidy

* Apply suggestions from code review

Co-authored-by: realbigsean <seananderson33@GMAIL.com>

* Fix is_none on handle Drop

* Remove commented-out tests

Co-authored-by: realbigsean <seananderson33@GMAIL.com>
2021-12-02 14:26:51 +11:00

104 lines
3.7 KiB
Rust

use crate::{ConsensusStatus, ExecutionLayer};
use slog::{crit, error, Logger};
use types::Hash256;
/// Provides a "handle" which should be returned after an `engine_executePayload` call.
///
/// This handle allows the holder to send a valid or invalid message to the execution nodes to
/// indicate the consensus verification status of `self.block_hash`.
///
/// Most notably, this `handle` will send an "invalid" message when it is dropped unless it has
/// already sent a "valid" or "invalid" message. This is to help ensure that any accidental
/// dropping of this handle results in an "invalid" message. Such dropping would be expected when a
/// block verification returns early with an error.
pub struct ExecutePayloadHandle {
pub(crate) block_hash: Hash256,
pub(crate) execution_layer: Option<ExecutionLayer>,
pub(crate) log: Logger,
}
impl ExecutePayloadHandle {
/// Publish a "valid" message to all nodes for `self.block_hash`.
pub fn publish_consensus_valid(mut self) {
self.publish_blocking(ConsensusStatus::Valid)
}
/// Publish an "invalid" message to all nodes for `self.block_hash`.
pub fn publish_consensus_invalid(mut self) {
self.publish_blocking(ConsensusStatus::Invalid)
}
/// Publish the `status` message to all nodes for `self.block_hash`.
pub async fn publish_async(&mut self, status: ConsensusStatus) {
if let Some(execution_layer) = self.execution_layer() {
publish(&execution_layer, self.block_hash, status, &self.log).await
}
}
/// Publishes a message, suitable for running in a non-async context.
fn publish_blocking(&mut self, status: ConsensusStatus) {
if let Some(execution_layer) = self.execution_layer() {
let log = &self.log.clone();
let block_hash = self.block_hash;
if let Err(e) = execution_layer.block_on(|execution_layer| async move {
publish(execution_layer, block_hash, status, log).await;
Ok(())
}) {
error!(
self.log,
"Failed to spawn payload status task";
"error" => ?e,
"block_hash" => ?block_hash,
"status" => ?status,
);
}
}
}
/// Takes `self.execution_layer`, it cannot be used to send another duplicate or conflicting
/// message. Creates a log message if such an attempt is made.
fn execution_layer(&mut self) -> Option<ExecutionLayer> {
let execution_layer = self.execution_layer.take();
if execution_layer.is_none() {
crit!(
self.log,
"Double usage of ExecutePayloadHandle";
"block_hash" => ?self.block_hash,
);
}
execution_layer
}
}
/// Publish a `status`, creating a log message if it fails.
async fn publish(
execution_layer: &ExecutionLayer,
block_hash: Hash256,
status: ConsensusStatus,
log: &Logger,
) {
if let Err(e) = execution_layer
.consensus_validated(block_hash, status)
.await
{
// TODO(paul): consider how to recover when we are temporarily unable to tell a node
// that the block was valid.
crit!(
log,
"Failed to update execution consensus status";
"error" => ?e,
"block_hash" => ?block_hash,
"status" => ?status,
);
}
}
/// See the struct-level documentation for the reasoning for this `Drop` implementation.
impl Drop for ExecutePayloadHandle {
fn drop(&mut self) {
if self.execution_layer.is_some() {
self.publish_blocking(ConsensusStatus::Invalid)
}
}
}