Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(l1): remove total_difficulty #2052

Merged
merged 5 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 0 additions & 53 deletions crates/blockchain/fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ pub fn apply_fork_choice(

let head = head_block.header;

total_difficulty_check(&head_hash, &head, store)?;

let latest = store.get_latest_block_number()?;

// If the head block is an already present head ancestor, skip the update.
Expand Down Expand Up @@ -197,54 +195,3 @@ fn find_link_with_canonical_chain(

Ok(None)
}

fn total_difficulty_check<'a>(
head_block_hash: &'a H256,
head_block: &'a BlockHeader,
storage: &'a Store,
) -> Result<(), InvalidForkChoice> {
// This check is performed only for genesis or for blocks with difficulty.
if head_block.difficulty.is_zero() && head_block.number != 0 {
return Ok(());
}

let total_difficulty = storage
.get_block_total_difficulty(*head_block_hash)?
.ok_or(StoreError::Custom(
"Block difficulty not found for head block".to_string(),
))?;

let terminal_total_difficulty = storage
.get_chain_config()?
.terminal_total_difficulty
.ok_or(StoreError::Custom(
"Terminal total difficulty not found in chain config".to_string(),
))?;

// Check that the header is post-merge.
if total_difficulty < terminal_total_difficulty.into() {
return Err(InvalidForkChoice::PreMergeBlock);
}

if head_block.number == 0 {
return Ok(());
}

// Non genesis checks

let parent_total_difficulty = storage
.get_block_total_difficulty(head_block.parent_hash)?
.ok_or(StoreError::Custom(
"Block difficulty not found for parent block".to_string(),
))?;

// TODO(#790): is this check necessary and correctly implemented?
if parent_total_difficulty >= terminal_total_difficulty.into() {
Err((StoreError::Custom(
"Parent block is already post terminal total difficulty".to_string(),
))
.into())
} else {
Ok(())
}
}
24 changes: 0 additions & 24 deletions crates/storage/store/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,6 @@ pub trait StoreEngine: Debug + Send + Sync + RefUnwindSafe {
/// Obtain block number for a given hash
fn get_block_number(&self, block_hash: BlockHash) -> Result<Option<BlockNumber>, StoreError>;

// TODO (#307): Remove TotalDifficulty.
/// Add block total difficulty
fn add_block_total_difficulty(
&self,
block_hash: BlockHash,
block_total_difficulty: U256,
) -> Result<(), StoreError>;

// TODO (#307): Remove TotalDifficulty.
/// Obtain block total difficulty
fn get_block_total_difficulty(&self, block_hash: BlockHash)
-> Result<Option<U256>, StoreError>;

/// Store transaction location (block number and index of the transaction within the block)
fn add_transaction_location(
&self,
Expand Down Expand Up @@ -199,17 +186,6 @@ pub trait StoreEngine: Debug + Send + Sync + RefUnwindSafe {
/// Obtain latest block number
fn get_latest_block_number(&self) -> Result<Option<BlockNumber>, StoreError>;

// TODO (#307): Remove TotalDifficulty.
/// Update latest total difficulty
fn update_latest_total_difficulty(
&self,
latest_total_difficulty: U256,
) -> Result<(), StoreError>;

// TODO (#307): Remove TotalDifficulty.
/// Obtain latest total difficulty
fn get_latest_total_difficulty(&self) -> Result<Option<U256>, StoreError>;

/// Update pending block number
fn update_pending_block_number(&self, block_number: BlockNumber) -> Result<(), StoreError>;

Expand Down
3 changes: 0 additions & 3 deletions crates/storage/store/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::fmt::Debug;
use std::marker::PhantomData;

use bytes::Bytes;
use ethereum_types::U256;
use ethrex_common::{
types::{AccountState, Block, BlockBody, BlockHash, BlockHeader, Receipt},
H256,
Expand All @@ -26,8 +25,6 @@ pub type BlockHashRLP = Rlp<BlockHash>;
pub type BlockHeaderRLP = Rlp<BlockHeader>;
pub type BlockBodyRLP = Rlp<BlockBody>;
pub type BlockRLP = Rlp<Block>;
// TODO (#307): Remove TotalDifficulty.
pub type BlockTotalDifficultyRLP = Rlp<U256>;

// Receipt types
pub type ReceiptRLP = Rlp<Receipt>;
Expand Down
31 changes: 1 addition & 30 deletions crates/storage/store/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,6 @@ impl Store {
self.engine.get_block_number(block_hash)
}

pub fn add_block_total_difficulty(
&self,
block_hash: BlockHash,
block_difficulty: U256,
) -> Result<(), StoreError> {
self.engine
.add_block_total_difficulty(block_hash, block_difficulty)
}

pub fn get_block_total_difficulty(
&self,
block_hash: BlockHash,
) -> Result<Option<U256>, StoreError> {
self.engine.get_block_total_difficulty(block_hash)
}

pub fn add_transaction_location(
&self,
transaction_hash: H256,
Expand Down Expand Up @@ -549,16 +533,11 @@ impl Store {
// TODO Maybe add both in a single tx?
let header = block.header;
let number = header.number;
let latest_total_difficulty = self.get_latest_total_difficulty()?;
let block_total_difficulty =
latest_total_difficulty.unwrap_or(U256::zero()) + header.difficulty;
let hash = header.compute_block_hash();
self.add_transaction_locations(&block.body.transactions, number, hash)?;
self.add_block_body(hash, block.body)?;
self.add_block_header(hash, header)?;
self.add_block_number(hash, number)?;
self.add_block_total_difficulty(hash, block_total_difficulty)?;
self.update_latest_total_difficulty(block_total_difficulty)
self.add_block_number(hash, number)
}

pub fn add_initial_state(&self, genesis: Genesis) -> Result<(), StoreError> {
Expand Down Expand Up @@ -698,14 +677,6 @@ impl Store {
.ok_or(StoreError::MissingLatestBlockNumber)
}

pub fn update_latest_total_difficulty(&self, block_difficulty: U256) -> Result<(), StoreError> {
self.engine.update_latest_total_difficulty(block_difficulty)
}

pub fn get_latest_total_difficulty(&self) -> Result<Option<U256>, StoreError> {
self.engine.get_latest_total_difficulty()
}

pub fn update_pending_block_number(&self, block_number: BlockNumber) -> Result<(), StoreError> {
self.engine.update_pending_block_number(block_number)
}
Expand Down
41 changes: 0 additions & 41 deletions crates/storage/store/store_db/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ struct StoreInner {
state_trie_nodes: NodeMap,
// A storage trie for each hashed account address
storage_trie_nodes: HashMap<H256, NodeMap>,
// TODO (#307): Remove TotalDifficulty.
block_total_difficulties: HashMap<BlockHash, U256>,
// Stores local blocks by payload id
payloads: HashMap<u64, (Block, U256, BlobsBundle, bool)>,
pending_blocks: HashMap<BlockHash, Block>,
Expand All @@ -56,8 +54,6 @@ struct ChainData {
finalized_block_number: Option<BlockNumber>,
safe_block_number: Option<BlockNumber>,
latest_block_number: Option<BlockNumber>,
// TODO (#307): Remove TotalDifficulty.
latest_total_difficulty: Option<U256>,
pending_block_number: Option<BlockNumber>,
is_synced: bool,
}
Expand Down Expand Up @@ -160,28 +156,6 @@ impl StoreEngine for Store {
Ok(self.inner().block_numbers.get(&block_hash).copied())
}

fn add_block_total_difficulty(
&self,
block_hash: BlockHash,
block_total_difficulty: U256,
) -> Result<(), StoreError> {
self.inner()
.block_total_difficulties
.insert(block_hash, block_total_difficulty);
Ok(())
}

fn get_block_total_difficulty(
&self,
block_hash: BlockHash,
) -> Result<Option<U256>, StoreError> {
Ok(self
.inner()
.block_total_difficulties
.get(&block_hash)
.copied())
}

fn add_transaction_location(
&self,
transaction_hash: H256,
Expand Down Expand Up @@ -303,17 +277,6 @@ impl StoreEngine for Store {
.replace(block_number);
Ok(())
}
fn update_latest_total_difficulty(
&self,
latest_total_difficulty: U256,
) -> Result<(), StoreError> {
self.inner()
.chain_data
.latest_total_difficulty
.replace(latest_total_difficulty);
Ok(())
}

fn get_latest_block_number(&self) -> Result<Option<BlockNumber>, StoreError> {
Ok(self.inner().chain_data.latest_block_number)
}
Expand All @@ -326,10 +289,6 @@ impl StoreEngine for Store {
Ok(())
}

fn get_latest_total_difficulty(&self) -> Result<Option<U256>, StoreError> {
Ok(self.inner().chain_data.latest_total_difficulty)
}

fn get_pending_block_number(&self) -> Result<Option<BlockNumber>, StoreError> {
Ok(self.inner().chain_data.pending_block_number)
}
Expand Down
46 changes: 1 addition & 45 deletions crates/storage/store/store_db/libmdbx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::api::StoreEngine;
use crate::error::StoreError;
use crate::rlp::{
AccountCodeHashRLP, AccountCodeRLP, AccountHashRLP, AccountStateRLP, BlockBodyRLP,
BlockHashRLP, BlockHeaderRLP, BlockRLP, BlockTotalDifficultyRLP, ReceiptRLP, Rlp,
TransactionHashRLP, TupleRLP,
BlockHashRLP, BlockHeaderRLP, BlockRLP, ReceiptRLP, Rlp, TransactionHashRLP, TupleRLP,
};
use crate::store::{MAX_SNAPSHOT_READS, STATE_TRIE_SEGMENTS};
use crate::trie_db::libmdbx::LibmdbxTrieDB;
Expand Down Expand Up @@ -158,22 +157,6 @@ impl StoreEngine for Store {
fn get_block_number(&self, block_hash: BlockHash) -> Result<Option<BlockNumber>, StoreError> {
self.read::<BlockNumbers>(block_hash.into())
}
fn add_block_total_difficulty(
&self,
block_hash: BlockHash,
block_total_difficulty: U256,
) -> Result<(), StoreError> {
self.write::<BlockTotalDifficulties>(block_hash.into(), block_total_difficulty.into())
}

fn get_block_total_difficulty(
&self,
block_hash: BlockHash,
) -> Result<Option<U256>, StoreError> {
Ok(self
.read::<BlockTotalDifficulties>(block_hash.into())?
.map(|b| b.to()))
}

fn add_account_code(&self, code_hash: H256, code: Bytes) -> Result<(), StoreError> {
self.write::<AccountCodes>(code_hash.into(), code.into())
Expand Down Expand Up @@ -320,25 +303,6 @@ impl StoreEngine for Store {
}
}

fn update_latest_total_difficulty(
&self,
latest_total_difficulty: U256,
) -> Result<(), StoreError> {
self.write::<ChainData>(
ChainDataIndex::LatestTotalDifficulty,
latest_total_difficulty.encode_to_vec(),
)
}

fn get_latest_total_difficulty(&self) -> Result<Option<U256>, StoreError> {
match self.read::<ChainData>(ChainDataIndex::LatestTotalDifficulty)? {
None => Ok(None),
Some(ref rlp) => RLPDecode::decode(rlp)
.map(Some)
.map_err(|_| StoreError::DecodeError),
}
}

fn update_pending_block_number(&self, block_number: BlockNumber) -> Result<(), StoreError> {
self.write::<ChainData>(
ChainDataIndex::PendingBlockNumber,
Expand Down Expand Up @@ -743,12 +707,6 @@ table!(
( BlockNumbers ) BlockHashRLP => BlockNumber
);

// TODO (#307): Remove TotalDifficulty.
table!(
/// Block hash to total difficulties table.
( BlockTotalDifficulties ) BlockHashRLP => BlockTotalDifficultyRLP
);

table!(
/// Block headers table.
( Headers ) BlockHashRLP => BlockHeaderRLP
Expand Down Expand Up @@ -895,8 +853,6 @@ impl Encodable for SnapStateIndex {
pub fn init_db(path: Option<impl AsRef<Path>>) -> Database {
let tables = [
table_info!(BlockNumbers),
// TODO (#307): Remove TotalDifficulty.
table_info!(BlockTotalDifficulties),
table_info!(Headers),
table_info!(Bodies),
table_info!(AccountCodes),
Expand Down
Loading