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:
parent
f9b9658711
commit
e5e4e62758
@ -3908,14 +3908,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
|||||||
ForkName::Base | ForkName::Altair => return Ok(()),
|
ForkName::Base | ForkName::Altair => return Ok(()),
|
||||||
_ => {
|
_ => {
|
||||||
// We are post-bellatrix
|
// We are post-bellatrix
|
||||||
if execution_layer
|
if let Some(payload_attributes) = execution_layer
|
||||||
.payload_attributes(next_slot, params.head_root)
|
.payload_attributes(next_slot, params.head_root)
|
||||||
.await
|
.await
|
||||||
.is_some()
|
|
||||||
{
|
{
|
||||||
// We are a proposer, check for terminal_pow_block_hash
|
// We are a proposer, check for terminal_pow_block_hash
|
||||||
if let Some(terminal_pow_block_hash) = execution_layer
|
if let Some(terminal_pow_block_hash) = execution_layer
|
||||||
.get_terminal_pow_block_hash(&self.spec)
|
.get_terminal_pow_block_hash(&self.spec, payload_attributes.timestamp)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::ForkchoiceUpdate)?
|
.map_err(Error::ForkchoiceUpdate)?
|
||||||
{
|
{
|
||||||
|
@ -393,7 +393,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
let terminal_pow_block_hash = execution_layer
|
let terminal_pow_block_hash = execution_layer
|
||||||
.get_terminal_pow_block_hash(spec)
|
.get_terminal_pow_block_hash(spec, timestamp)
|
||||||
.await
|
.await
|
||||||
.map_err(BlockProductionError::TerminalPoWBlockLookupFailed)?;
|
.map_err(BlockProductionError::TerminalPoWBlockLookupFailed)?;
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ use rayon::prelude::*;
|
|||||||
use sensitive_url::SensitiveUrl;
|
use sensitive_url::SensitiveUrl;
|
||||||
use slog::Logger;
|
use slog::Logger;
|
||||||
use slot_clock::TestingSlotClock;
|
use slot_clock::TestingSlotClock;
|
||||||
|
use state_processing::per_block_processing::compute_timestamp_at_slot;
|
||||||
use state_processing::{
|
use state_processing::{
|
||||||
state_advance::{complete_state_advance, partial_state_advance},
|
state_advance::{complete_state_advance, partial_state_advance},
|
||||||
StateRootStrategy,
|
StateRootStrategy,
|
||||||
@ -521,6 +522,11 @@ where
|
|||||||
self.chain.head_beacon_state_cloned()
|
self.chain.head_beacon_state_cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_timestamp_at_slot(&self) -> u64 {
|
||||||
|
let state = self.get_current_state();
|
||||||
|
compute_timestamp_at_slot(&state, &self.spec).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_current_state_and_root(&self) -> (BeaconState<E>, Hash256) {
|
pub fn get_current_state_and_root(&self) -> (BeaconState<E>, Hash256) {
|
||||||
let head = self.chain.head_snapshot();
|
let head = self.chain.head_snapshot();
|
||||||
let state_root = head.beacon_state_root();
|
let state_root = head.beacon_state_root();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#![cfg(not(debug_assertions))] // Tests run too slow in debug.
|
#![cfg(not(debug_assertions))] // Tests run too slow in debug.
|
||||||
|
|
||||||
use beacon_chain::test_utils::BeaconChainHarness;
|
use beacon_chain::test_utils::BeaconChainHarness;
|
||||||
use execution_layer::test_utils::{generate_pow_block, DEFAULT_TERMINAL_BLOCK};
|
use execution_layer::test_utils::{generate_pow_block, Block, DEFAULT_TERMINAL_BLOCK};
|
||||||
use types::*;
|
use types::*;
|
||||||
|
|
||||||
const VALIDATOR_COUNT: usize = 32;
|
const VALIDATOR_COUNT: usize = 32;
|
||||||
@ -22,6 +22,7 @@ fn verify_execution_payload_chain<T: EthSpec>(chain: &[FullPayload<T>]) {
|
|||||||
prev_ep.execution_payload.block_number + 1,
|
prev_ep.execution_payload.block_number + 1,
|
||||||
ep.execution_payload.block_number
|
ep.execution_payload.block_number
|
||||||
);
|
);
|
||||||
|
assert!(ep.execution_payload.timestamp > prev_ep.execution_payload.timestamp);
|
||||||
}
|
}
|
||||||
prev_ep = Some(ep.clone());
|
prev_ep = Some(ep.clone());
|
||||||
}
|
}
|
||||||
@ -169,6 +170,30 @@ async fn base_altair_merge_with_terminal_block_after_fork() {
|
|||||||
.move_to_terminal_block()
|
.move_to_terminal_block()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// Add a slot duration to get to the next slot
|
||||||
|
let timestamp = harness.get_timestamp_at_slot() + harness.spec.seconds_per_slot;
|
||||||
|
|
||||||
|
harness
|
||||||
|
.execution_block_generator()
|
||||||
|
.modify_last_block(|block| {
|
||||||
|
if let Block::PoW(terminal_block) = block {
|
||||||
|
terminal_block.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
harness.extend_slots(1).await;
|
||||||
|
|
||||||
|
let one_after_merge_head = &harness.chain.head_snapshot().beacon_block;
|
||||||
|
assert_eq!(
|
||||||
|
*one_after_merge_head
|
||||||
|
.message()
|
||||||
|
.body()
|
||||||
|
.execution_payload()
|
||||||
|
.unwrap(),
|
||||||
|
FullPayload::default()
|
||||||
|
);
|
||||||
|
assert_eq!(one_after_merge_head.slot(), merge_fork_slot + 2);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Next merge block should include an exec payload.
|
* Next merge block should include an exec payload.
|
||||||
*/
|
*/
|
||||||
|
@ -106,6 +106,8 @@ pub struct ExecutionBlock {
|
|||||||
pub block_number: u64,
|
pub block_number: u64,
|
||||||
pub parent_hash: ExecutionBlockHash,
|
pub parent_hash: ExecutionBlockHash,
|
||||||
pub total_difficulty: Uint256,
|
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.
|
/// Representation of an exection block with enough detail to reconstruct a payload.
|
||||||
|
@ -902,6 +902,7 @@ impl<T: EthSpec> ExecutionLayer<T> {
|
|||||||
pub async fn get_terminal_pow_block_hash(
|
pub async fn get_terminal_pow_block_hash(
|
||||||
&self,
|
&self,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
|
timestamp: u64,
|
||||||
) -> Result<Option<ExecutionBlockHash>, Error> {
|
) -> Result<Option<ExecutionBlockHash>, Error> {
|
||||||
let _timer = metrics::start_timer_vec(
|
let _timer = metrics::start_timer_vec(
|
||||||
&metrics::EXECUTION_LAYER_REQUEST_TIMES,
|
&metrics::EXECUTION_LAYER_REQUEST_TIMES,
|
||||||
@ -924,8 +925,19 @@ impl<T: EthSpec> ExecutionLayer<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.get_pow_block_hash_at_total_difficulty(engine, spec)
|
let block = self.get_pow_block_at_total_difficulty(engine, spec).await?;
|
||||||
.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
|
.await
|
||||||
.map_err(Box::new)
|
.map_err(Box::new)
|
||||||
@ -953,11 +965,11 @@ impl<T: EthSpec> ExecutionLayer<T> {
|
|||||||
/// `get_pow_block_at_terminal_total_difficulty`
|
/// `get_pow_block_at_terminal_total_difficulty`
|
||||||
///
|
///
|
||||||
/// https://github.com/ethereum/consensus-specs/blob/v1.1.5/specs/merge/validator.md
|
/// 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,
|
&self,
|
||||||
engine: &Engine,
|
engine: &Engine,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<Option<ExecutionBlockHash>, ApiError> {
|
) -> Result<Option<ExecutionBlock>, ApiError> {
|
||||||
let mut block = engine
|
let mut block = engine
|
||||||
.api
|
.api
|
||||||
.get_block_by_number(BlockByNumberQuery::Tag(LATEST_TAG))
|
.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;
|
let block_reached_ttd = block.total_difficulty >= spec.terminal_total_difficulty;
|
||||||
if block_reached_ttd {
|
if block_reached_ttd {
|
||||||
if block.parent_hash == ExecutionBlockHash::zero() {
|
if block.parent_hash == ExecutionBlockHash::zero() {
|
||||||
return Ok(Some(block.block_hash));
|
return Ok(Some(block));
|
||||||
}
|
}
|
||||||
let parent = self
|
let parent = self
|
||||||
.get_pow_block(engine, block.parent_hash)
|
.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;
|
let parent_reached_ttd = parent.total_difficulty >= spec.terminal_total_difficulty;
|
||||||
|
|
||||||
if block_reached_ttd && !parent_reached_ttd {
|
if block_reached_ttd && !parent_reached_ttd {
|
||||||
return Ok(Some(block.block_hash));
|
return Ok(Some(block));
|
||||||
} else {
|
} else {
|
||||||
block = parent;
|
block = parent;
|
||||||
}
|
}
|
||||||
@ -1197,19 +1209,54 @@ mod test {
|
|||||||
.move_to_block_prior_to_terminal_block()
|
.move_to_block_prior_to_terminal_block()
|
||||||
.with_terminal_block(|spec, el, _| async move {
|
.with_terminal_block(|spec, el, _| async move {
|
||||||
el.engine().upcheck().await;
|
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
|
.await
|
||||||
.move_to_terminal_block()
|
.move_to_terminal_block()
|
||||||
.with_terminal_block(|spec, el, terminal_block| async move {
|
.with_terminal_block(|spec, el, terminal_block| async move {
|
||||||
assert_eq!(
|
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)
|
Some(terminal_block.unwrap().block_hash)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.await;
|
.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]
|
#[tokio::test]
|
||||||
async fn verifies_valid_terminal_block_hash() {
|
async fn verifies_valid_terminal_block_hash() {
|
||||||
let runtime = TestRuntime::default();
|
let runtime = TestRuntime::default();
|
||||||
@ -1269,3 +1316,12 @@ mod test {
|
|||||||
fn noop<T: EthSpec>(_: &ExecutionLayer<T>, _: &ExecutionPayload<T>) -> Option<ExecutionPayload<T>> {
|
fn noop<T: EthSpec>(_: &ExecutionLayer<T>, _: &ExecutionPayload<T>) -> Option<ExecutionPayload<T>> {
|
||||||
None
|
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()
|
||||||
|
}
|
||||||
|
@ -60,12 +60,14 @@ impl<T: EthSpec> Block<T> {
|
|||||||
block_number: block.block_number,
|
block_number: block.block_number,
|
||||||
parent_hash: block.parent_hash,
|
parent_hash: block.parent_hash,
|
||||||
total_difficulty: block.total_difficulty,
|
total_difficulty: block.total_difficulty,
|
||||||
|
timestamp: block.timestamp,
|
||||||
},
|
},
|
||||||
Block::PoS(payload) => ExecutionBlock {
|
Block::PoS(payload) => ExecutionBlock {
|
||||||
block_hash: payload.block_hash,
|
block_hash: payload.block_hash,
|
||||||
block_number: payload.block_number,
|
block_number: payload.block_number,
|
||||||
parent_hash: payload.parent_hash,
|
parent_hash: payload.parent_hash,
|
||||||
total_difficulty,
|
total_difficulty,
|
||||||
|
timestamp: payload.timestamp,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,6 +102,7 @@ pub struct PoWBlock {
|
|||||||
pub block_hash: ExecutionBlockHash,
|
pub block_hash: ExecutionBlockHash,
|
||||||
pub parent_hash: ExecutionBlockHash,
|
pub parent_hash: ExecutionBlockHash,
|
||||||
pub total_difficulty: Uint256,
|
pub total_difficulty: Uint256,
|
||||||
|
pub timestamp: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ExecutionBlockGenerator<T: EthSpec> {
|
pub struct ExecutionBlockGenerator<T: EthSpec> {
|
||||||
@ -266,6 +269,26 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
|
|||||||
Ok(())
|
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>> {
|
pub fn get_payload(&mut self, id: &PayloadId) -> Option<ExecutionPayload<T>> {
|
||||||
self.payload_ids.get(id).cloned()
|
self.payload_ids.get(id).cloned()
|
||||||
}
|
}
|
||||||
@ -423,6 +446,7 @@ pub fn generate_pow_block(
|
|||||||
block_hash: ExecutionBlockHash::zero(),
|
block_hash: ExecutionBlockHash::zero(),
|
||||||
parent_hash,
|
parent_hash,
|
||||||
total_difficulty,
|
total_difficulty,
|
||||||
|
timestamp: block_number,
|
||||||
};
|
};
|
||||||
|
|
||||||
block.block_hash = ExecutionBlockHash::from_root(block.tree_hash_root());
|
block.block_hash = ExecutionBlockHash::from_root(block.tree_hash_root());
|
||||||
|
@ -6,7 +6,7 @@ use crate::engine_api::{
|
|||||||
};
|
};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use environment::null_logger;
|
use environment::null_logger;
|
||||||
use execution_block_generator::{Block, PoWBlock};
|
use execution_block_generator::PoWBlock;
|
||||||
use handle_rpc::handle_rpc;
|
use handle_rpc::handle_rpc;
|
||||||
use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
|
use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@ -21,7 +21,7 @@ use tokio::{runtime, sync::oneshot};
|
|||||||
use types::{EthSpec, ExecutionBlockHash, Uint256};
|
use types::{EthSpec, ExecutionBlockHash, Uint256};
|
||||||
use warp::{http::StatusCode, Filter, Rejection};
|
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 use mock_execution_layer::MockExecutionLayer;
|
||||||
|
|
||||||
pub const DEFAULT_TERMINAL_DIFFICULTY: u64 = 6400;
|
pub const DEFAULT_TERMINAL_DIFFICULTY: u64 = 6400;
|
||||||
@ -334,6 +334,7 @@ impl<T: EthSpec> MockServer<T> {
|
|||||||
block_hash,
|
block_hash,
|
||||||
parent_hash,
|
parent_hash,
|
||||||
total_difficulty,
|
total_difficulty,
|
||||||
|
timestamp: block_number,
|
||||||
});
|
});
|
||||||
|
|
||||||
self.ctx
|
self.ctx
|
||||||
|
@ -219,7 +219,7 @@ impl<E: GenericExecutionEngine> TestRig<E> {
|
|||||||
let terminal_pow_block_hash = self
|
let terminal_pow_block_hash = self
|
||||||
.ee_a
|
.ee_a
|
||||||
.execution_layer
|
.execution_layer
|
||||||
.get_terminal_pow_block_hash(&self.spec)
|
.get_terminal_pow_block_hash(&self.spec, timestamp_now())
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -228,7 +228,7 @@ impl<E: GenericExecutionEngine> TestRig<E> {
|
|||||||
terminal_pow_block_hash,
|
terminal_pow_block_hash,
|
||||||
self.ee_b
|
self.ee_b
|
||||||
.execution_layer
|
.execution_layer
|
||||||
.get_terminal_pow_block_hash(&self.spec)
|
.get_terminal_pow_block_hash(&self.spec, timestamp_now())
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
Loading…
Reference in New Issue
Block a user