diff --git a/crates/blockchain/fork_choice.rs b/crates/blockchain/fork_choice.rs index a0daa6de7..0bf65b6f3 100644 --- a/crates/blockchain/fork_choice.rs +++ b/crates/blockchain/fork_choice.rs @@ -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. @@ -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(()) - } -} diff --git a/crates/storage/store/api.rs b/crates/storage/store/api.rs index a0f2a075e..324a78f79 100644 --- a/crates/storage/store/api.rs +++ b/crates/storage/store/api.rs @@ -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, 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, StoreError>; - /// Store transaction location (block number and index of the transaction within the block) fn add_transaction_location( &self, @@ -199,17 +186,6 @@ pub trait StoreEngine: Debug + Send + Sync + RefUnwindSafe { /// Obtain latest block number fn get_latest_block_number(&self) -> Result, 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, StoreError>; - /// Update pending block number fn update_pending_block_number(&self, block_number: BlockNumber) -> Result<(), StoreError>; diff --git a/crates/storage/store/rlp.rs b/crates/storage/store/rlp.rs index 3822abc0a..1632b570f 100644 --- a/crates/storage/store/rlp.rs +++ b/crates/storage/store/rlp.rs @@ -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, @@ -26,8 +25,6 @@ pub type BlockHashRLP = Rlp; pub type BlockHeaderRLP = Rlp; pub type BlockBodyRLP = Rlp; pub type BlockRLP = Rlp; -// TODO (#307): Remove TotalDifficulty. -pub type BlockTotalDifficultyRLP = Rlp; // Receipt types pub type ReceiptRLP = Rlp; diff --git a/crates/storage/store/store.rs b/crates/storage/store/store.rs index 5f9ce6634..a3cf19b6c 100644 --- a/crates/storage/store/store.rs +++ b/crates/storage/store/store.rs @@ -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, StoreError> { - self.engine.get_block_total_difficulty(block_hash) - } - pub fn add_transaction_location( &self, transaction_hash: H256, @@ -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> { @@ -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, 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) } diff --git a/crates/storage/store/store_db/in_memory.rs b/crates/storage/store/store_db/in_memory.rs index 081120ee7..641f4c427 100644 --- a/crates/storage/store/store_db/in_memory.rs +++ b/crates/storage/store/store_db/in_memory.rs @@ -36,8 +36,6 @@ struct StoreInner { state_trie_nodes: NodeMap, // A storage trie for each hashed account address storage_trie_nodes: HashMap, - // TODO (#307): Remove TotalDifficulty. - block_total_difficulties: HashMap, // Stores local blocks by payload id payloads: HashMap, pending_blocks: HashMap, @@ -56,8 +54,6 @@ struct ChainData { finalized_block_number: Option, safe_block_number: Option, latest_block_number: Option, - // TODO (#307): Remove TotalDifficulty. - latest_total_difficulty: Option, pending_block_number: Option, is_synced: bool, } @@ -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, StoreError> { - Ok(self - .inner() - .block_total_difficulties - .get(&block_hash) - .copied()) - } - fn add_transaction_location( &self, transaction_hash: H256, @@ -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, StoreError> { Ok(self.inner().chain_data.latest_block_number) } @@ -326,10 +289,6 @@ impl StoreEngine for Store { Ok(()) } - fn get_latest_total_difficulty(&self) -> Result, StoreError> { - Ok(self.inner().chain_data.latest_total_difficulty) - } - fn get_pending_block_number(&self) -> Result, StoreError> { Ok(self.inner().chain_data.pending_block_number) } diff --git a/crates/storage/store/store_db/libmdbx.rs b/crates/storage/store/store_db/libmdbx.rs index 96c1342e6..cd3869a00 100644 --- a/crates/storage/store/store_db/libmdbx.rs +++ b/crates/storage/store/store_db/libmdbx.rs @@ -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; @@ -158,22 +157,6 @@ impl StoreEngine for Store { fn get_block_number(&self, block_hash: BlockHash) -> Result, StoreError> { self.read::(block_hash.into()) } - fn add_block_total_difficulty( - &self, - block_hash: BlockHash, - block_total_difficulty: U256, - ) -> Result<(), StoreError> { - self.write::(block_hash.into(), block_total_difficulty.into()) - } - - fn get_block_total_difficulty( - &self, - block_hash: BlockHash, - ) -> Result, StoreError> { - Ok(self - .read::(block_hash.into())? - .map(|b| b.to())) - } fn add_account_code(&self, code_hash: H256, code: Bytes) -> Result<(), StoreError> { self.write::(code_hash.into(), code.into()) @@ -320,25 +303,6 @@ impl StoreEngine for Store { } } - fn update_latest_total_difficulty( - &self, - latest_total_difficulty: U256, - ) -> Result<(), StoreError> { - self.write::( - ChainDataIndex::LatestTotalDifficulty, - latest_total_difficulty.encode_to_vec(), - ) - } - - fn get_latest_total_difficulty(&self) -> Result, StoreError> { - match self.read::(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::( ChainDataIndex::PendingBlockNumber, @@ -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 @@ -895,8 +853,6 @@ impl Encodable for SnapStateIndex { pub fn init_db(path: Option>) -> Database { let tables = [ table_info!(BlockNumbers), - // TODO (#307): Remove TotalDifficulty. - table_info!(BlockTotalDifficulties), table_info!(Headers), table_info!(Bodies), table_info!(AccountCodes), diff --git a/crates/storage/store/store_db/redb.rs b/crates/storage/store/store_db/redb.rs index 464d01be1..b5862957a 100644 --- a/crates/storage/store/store_db/redb.rs +++ b/crates/storage/store/store_db/redb.rs @@ -1,8 +1,6 @@ use std::{borrow::Borrow, panic::RefUnwindSafe, sync::Arc}; -use crate::rlp::{ - AccountHashRLP, AccountStateRLP, BlockRLP, BlockTotalDifficultyRLP, Rlp, TransactionHashRLP, -}; +use crate::rlp::{AccountHashRLP, AccountStateRLP, BlockRLP, Rlp, TransactionHashRLP}; use crate::store::MAX_SNAPSHOT_READS; use crate::trie_db::{redb::RedBTrie, redb_multitable::RedBMultiTableTrieDB}; use crate::{ @@ -30,8 +28,6 @@ const STATE_TRIE_NODES_TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("StateTrieNodes"); const BLOCK_NUMBERS_TABLE: TableDefinition = TableDefinition::new("BlockNumbers"); -const BLOCK_TOTAL_DIFFICULTIES_TABLE: TableDefinition = - TableDefinition::new("BlockTotalDifficulties"); const HEADERS_TABLE: TableDefinition = TableDefinition::new("Headers"); const BLOCK_BODIES_TABLE: TableDefinition = @@ -324,31 +320,6 @@ impl StoreEngine for RedBStore { .map(|b| b.value())) } - fn add_block_total_difficulty( - &self, - block_hash: BlockHash, - block_total_difficulty: ethrex_common::U256, - ) -> Result<(), StoreError> { - // self.write::(block_hash.into(), block_total_difficulty.into()) - self.write( - BLOCK_TOTAL_DIFFICULTIES_TABLE, - >::into(block_hash), - >>::into(block_total_difficulty), - ) - } - - fn get_block_total_difficulty( - &self, - block_hash: BlockHash, - ) -> Result, StoreError> { - Ok(self - .read( - BLOCK_TOTAL_DIFFICULTIES_TABLE, - >::into(block_hash), - )? - .map(|b| b.value().to())) - } - fn add_transaction_location( &self, transaction_hash: ethrex_common::H256, @@ -535,26 +506,6 @@ impl StoreEngine for RedBStore { } } - fn update_latest_total_difficulty( - &self, - latest_total_difficulty: ethrex_common::U256, - ) -> Result<(), StoreError> { - self.write( - CHAIN_DATA_TABLE, - ChainDataIndex::LatestTotalDifficulty, - latest_total_difficulty.encode_to_vec(), - ) - } - - fn get_latest_total_difficulty(&self) -> Result, StoreError> { - match self.read(CHAIN_DATA_TABLE, ChainDataIndex::LatestTotalDifficulty)? { - None => Ok(None), - Some(ref rlp) => RLPDecode::decode(&rlp.value()) - .map(Some) - .map_err(|_| StoreError::DecodeError), - } - } - fn update_pending_block_number(&self, block_number: BlockNumber) -> Result<(), StoreError> { self.write( CHAIN_DATA_TABLE, @@ -1018,7 +969,6 @@ pub fn init_db() -> Result { let table_creation_txn = db.begin_write()?; table_creation_txn.open_table(STATE_TRIE_NODES_TABLE)?; table_creation_txn.open_table(BLOCK_NUMBERS_TABLE)?; - table_creation_txn.open_table(BLOCK_TOTAL_DIFFICULTIES_TABLE)?; table_creation_txn.open_table(CANONICAL_BLOCK_HASHES_TABLE)?; table_creation_txn.open_table(RECEIPTS_TABLE)?; table_creation_txn.open_multimap_table(STORAGE_TRIE_NODES_TABLE)?; diff --git a/crates/storage/store/utils.rs b/crates/storage/store/utils.rs index e8fea33b7..72d35c685 100644 --- a/crates/storage/store/utils.rs +++ b/crates/storage/store/utils.rs @@ -8,9 +8,7 @@ pub enum ChainDataIndex { SafeBlockNumber = 3, LatestBlockNumber = 4, PendingBlockNumber = 5, - // TODO (#307): Remove TotalDifficulty. - LatestTotalDifficulty = 6, - IsSynced = 7, + IsSynced = 6, } impl From for ChainDataIndex { @@ -28,9 +26,6 @@ impl From for ChainDataIndex { x if x == ChainDataIndex::PendingBlockNumber as u8 => { ChainDataIndex::PendingBlockNumber } - x if x == ChainDataIndex::LatestTotalDifficulty as u8 => { - ChainDataIndex::LatestTotalDifficulty - } x if x == ChainDataIndex::IsSynced as u8 => ChainDataIndex::IsSynced, _ => panic!("Invalid value when casting to ChainDataIndex: {}", value), }