Don't create a execution payload with same timestamp as terminal block (#3331)

## Issue Addressed

Resolves #3316 

## Proposed Changes

This PR fixes an issue where lighthouse created a transition block with `block.execution_payload().timestamp == terminal_block.timestamp` if the terminal block was created at the slot boundary.
This commit is contained in:
Pawan Dhananjay
2022-07-18 23:15:41 +00:00
parent f9b9658711
commit e5e4e62758
9 changed files with 130 additions and 17 deletions

View File

@@ -106,6 +106,8 @@ pub struct ExecutionBlock {
pub block_number: u64,
pub parent_hash: ExecutionBlockHash,
pub total_difficulty: Uint256,
#[serde(with = "eth2_serde_utils::u64_hex_be")]
pub timestamp: u64,
}
/// Representation of an exection block with enough detail to reconstruct a payload.

View File

@@ -902,6 +902,7 @@ impl<T: EthSpec> ExecutionLayer<T> {
pub async fn get_terminal_pow_block_hash(
&self,
spec: &ChainSpec,
timestamp: u64,
) -> Result<Option<ExecutionBlockHash>, Error> {
let _timer = metrics::start_timer_vec(
&metrics::EXECUTION_LAYER_REQUEST_TIMES,
@@ -924,8 +925,19 @@ impl<T: EthSpec> ExecutionLayer<T> {
}
}
self.get_pow_block_hash_at_total_difficulty(engine, spec)
.await
let block = self.get_pow_block_at_total_difficulty(engine, spec).await?;
if let Some(pow_block) = block {
// If `terminal_block.timestamp == transition_block.timestamp`,
// we violate the invariant that a block's timestamp must be
// strictly greater than its parent's timestamp.
// The execution layer will reject a fcu call with such payload
// attributes leading to a missed block.
// Hence, we return `None` in such a case.
if pow_block.timestamp >= timestamp {
return Ok(None);
}
}
Ok(block.map(|b| b.block_hash))
})
.await
.map_err(Box::new)
@@ -953,11 +965,11 @@ impl<T: EthSpec> ExecutionLayer<T> {
/// `get_pow_block_at_terminal_total_difficulty`
///
/// https://github.com/ethereum/consensus-specs/blob/v1.1.5/specs/merge/validator.md
async fn get_pow_block_hash_at_total_difficulty(
async fn get_pow_block_at_total_difficulty(
&self,
engine: &Engine,
spec: &ChainSpec,
) -> Result<Option<ExecutionBlockHash>, ApiError> {
) -> Result<Option<ExecutionBlock>, ApiError> {
let mut block = engine
.api
.get_block_by_number(BlockByNumberQuery::Tag(LATEST_TAG))
@@ -970,7 +982,7 @@ impl<T: EthSpec> ExecutionLayer<T> {
let block_reached_ttd = block.total_difficulty >= spec.terminal_total_difficulty;
if block_reached_ttd {
if block.parent_hash == ExecutionBlockHash::zero() {
return Ok(Some(block.block_hash));
return Ok(Some(block));
}
let parent = self
.get_pow_block(engine, block.parent_hash)
@@ -979,7 +991,7 @@ impl<T: EthSpec> ExecutionLayer<T> {
let parent_reached_ttd = parent.total_difficulty >= spec.terminal_total_difficulty;
if block_reached_ttd && !parent_reached_ttd {
return Ok(Some(block.block_hash));
return Ok(Some(block));
} else {
block = parent;
}
@@ -1197,19 +1209,54 @@ mod test {
.move_to_block_prior_to_terminal_block()
.with_terminal_block(|spec, el, _| async move {
el.engine().upcheck().await;
assert_eq!(el.get_terminal_pow_block_hash(&spec).await.unwrap(), None)
assert_eq!(
el.get_terminal_pow_block_hash(&spec, timestamp_now())
.await
.unwrap(),
None
)
})
.await
.move_to_terminal_block()
.with_terminal_block(|spec, el, terminal_block| async move {
assert_eq!(
el.get_terminal_pow_block_hash(&spec).await.unwrap(),
el.get_terminal_pow_block_hash(&spec, timestamp_now())
.await
.unwrap(),
Some(terminal_block.unwrap().block_hash)
)
})
.await;
}
#[tokio::test]
async fn rejects_terminal_block_with_equal_timestamp() {
let runtime = TestRuntime::default();
MockExecutionLayer::default_params(runtime.task_executor.clone())
.move_to_block_prior_to_terminal_block()
.with_terminal_block(|spec, el, _| async move {
el.engine().upcheck().await;
assert_eq!(
el.get_terminal_pow_block_hash(&spec, timestamp_now())
.await
.unwrap(),
None
)
})
.await
.move_to_terminal_block()
.with_terminal_block(|spec, el, terminal_block| async move {
let timestamp = terminal_block.as_ref().map(|b| b.timestamp).unwrap();
assert_eq!(
el.get_terminal_pow_block_hash(&spec, timestamp)
.await
.unwrap(),
None
)
})
.await;
}
#[tokio::test]
async fn verifies_valid_terminal_block_hash() {
let runtime = TestRuntime::default();
@@ -1269,3 +1316,12 @@ mod test {
fn noop<T: EthSpec>(_: &ExecutionLayer<T>, _: &ExecutionPayload<T>) -> Option<ExecutionPayload<T>> {
None
}
#[cfg(test)]
/// Returns the duration since the unix epoch.
fn timestamp_now() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_else(|_| Duration::from_secs(0))
.as_secs()
}

View File

@@ -60,12 +60,14 @@ impl<T: EthSpec> Block<T> {
block_number: block.block_number,
parent_hash: block.parent_hash,
total_difficulty: block.total_difficulty,
timestamp: block.timestamp,
},
Block::PoS(payload) => ExecutionBlock {
block_hash: payload.block_hash,
block_number: payload.block_number,
parent_hash: payload.parent_hash,
total_difficulty,
timestamp: payload.timestamp,
},
}
}
@@ -100,6 +102,7 @@ pub struct PoWBlock {
pub block_hash: ExecutionBlockHash,
pub parent_hash: ExecutionBlockHash,
pub total_difficulty: Uint256,
pub timestamp: u64,
}
pub struct ExecutionBlockGenerator<T: EthSpec> {
@@ -266,6 +269,26 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
Ok(())
}
pub fn modify_last_block(&mut self, block_modifier: impl FnOnce(&mut Block<T>)) {
if let Some((last_block_hash, block_number)) =
self.block_hashes.keys().max().and_then(|block_number| {
self.block_hashes
.get(block_number)
.map(|block| (block, *block_number))
})
{
let mut block = self.blocks.remove(last_block_hash).unwrap();
block_modifier(&mut block);
// Update the block hash after modifying the block
match &mut block {
Block::PoW(b) => b.block_hash = ExecutionBlockHash::from_root(b.tree_hash_root()),
Block::PoS(b) => b.block_hash = ExecutionBlockHash::from_root(b.tree_hash_root()),
}
self.block_hashes.insert(block_number, block.block_hash());
self.blocks.insert(block.block_hash(), block);
}
}
pub fn get_payload(&mut self, id: &PayloadId) -> Option<ExecutionPayload<T>> {
self.payload_ids.get(id).cloned()
}
@@ -423,6 +446,7 @@ pub fn generate_pow_block(
block_hash: ExecutionBlockHash::zero(),
parent_hash,
total_difficulty,
timestamp: block_number,
};
block.block_hash = ExecutionBlockHash::from_root(block.tree_hash_root());

View File

@@ -6,7 +6,7 @@ use crate::engine_api::{
};
use bytes::Bytes;
use environment::null_logger;
use execution_block_generator::{Block, PoWBlock};
use execution_block_generator::PoWBlock;
use handle_rpc::handle_rpc;
use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
use serde::{Deserialize, Serialize};
@@ -21,7 +21,7 @@ use tokio::{runtime, sync::oneshot};
use types::{EthSpec, ExecutionBlockHash, Uint256};
use warp::{http::StatusCode, Filter, Rejection};
pub use execution_block_generator::{generate_pow_block, ExecutionBlockGenerator};
pub use execution_block_generator::{generate_pow_block, Block, ExecutionBlockGenerator};
pub use mock_execution_layer::MockExecutionLayer;
pub const DEFAULT_TERMINAL_DIFFICULTY: u64 = 6400;
@@ -334,6 +334,7 @@ impl<T: EthSpec> MockServer<T> {
block_hash,
parent_hash,
total_difficulty,
timestamp: block_number,
});
self.ctx