Skip to content

Commit

Permalink
fix: revert "fix: remove totalDifficulty (lambdaclass#347)" (lambdacl…
Browse files Browse the repository at this point in the history
…ass#566)

**Motivation**

<!-- Why does this pull request exist? What are its goals? -->

While the rpc-compat tests can be made to work without the
totalDifficulty field by updating the execution api crate, the engine
tests panick as they still rely on it.
This PR aims to fix this issue (at least until the hive engine test
suite is updated) by reverting the removal of the field and using a
previous version of the execution api crate which still has this field

<!-- A clear and concise general description of the changes this PR
introduces -->
* Reverts lambdaclass#347 
* Updates hive rev to a version using a previous version of
execution-apis that still uses the totalDifficulty field
(lambdaclass/hive#6)

<!-- Link to issues: Resolves lambdaclass#111, Resolves lambdaclass#222 -->

Closes None, but is needed in order to run engine tests
  • Loading branch information
fmoletta authored Sep 26, 2024
1 parent 8ac547b commit fe6b3ea
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ stop-localnet-silent:
@kurtosis enclave stop lambdanet >/dev/null 2>&1 || true
@kurtosis enclave rm lambdanet --force >/dev/null 2>&1 || true

HIVE_REVISION := efcd74daee8edc6b5792fafbb1653ea665a02453
HIVE_REVISION := 3be4465a45c421651d765f4a28702962567b40e6
# Shallow clones can't specify a single revision, but at least we avoid working
# the whole history by making it shallow since a given date (one day before our
# target revision).
Expand Down
28 changes: 24 additions & 4 deletions crates/rpc/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ use crate::{
utils::RpcErr,
RpcHandler,
};
use ethereum_rust_core::types::{
calculate_base_fee_per_blob_gas, Block, BlockBody, BlockHash, BlockHeader, BlockNumber, Receipt,
use ethereum_rust_core::{
types::{
calculate_base_fee_per_blob_gas, Block, BlockBody, BlockHash, BlockHeader, BlockNumber,
Receipt,
},
U256,
};
use ethereum_rust_storage::Store;

Expand Down Expand Up @@ -76,7 +80,15 @@ impl RpcHandler for GetBlockByNumberRequest {
_ => return Ok(Value::Null),
};
let hash = header.compute_block_hash();
let block = RpcBlock::build(header, body, hash, self.hydrated);
// TODO (#307): Remove TotalDifficulty.
let total_difficulty = storage.get_block_total_difficulty(hash)?;
let block = RpcBlock::build(
header,
body,
hash,
self.hydrated,
total_difficulty.unwrap_or(U256::zero()),
);

serde_json::to_value(&block).map_err(|_| RpcErr::Internal)
}
Expand Down Expand Up @@ -107,7 +119,15 @@ impl RpcHandler for GetBlockByHashRequest {
_ => return Ok(Value::Null),
};
let hash = header.compute_block_hash();
let block = RpcBlock::build(header, body, hash, self.hydrated);
// TODO (#307): Remove TotalDifficulty.
let total_difficulty = storage.get_block_total_difficulty(hash)?;
let block = RpcBlock::build(
header,
body,
hash,
self.hydrated,
total_difficulty.unwrap_or(U256::zero()),
);
serde_json::to_value(&block).map_err(|_| RpcErr::Internal)
}
}
Expand Down
10 changes: 7 additions & 3 deletions crates/rpc/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::transaction::RpcTransaction;
use ethereum_rust_core::{
serde_utils,
types::{Block, BlockBody, BlockHash, BlockHeader, BlockNumber, Withdrawal},
H256,
H256, U256,
};
use ethereum_rust_rlp::encode::RLPEncode;

Expand All @@ -14,6 +14,8 @@ pub struct RpcBlock {
hash: H256,
#[serde(with = "serde_utils::u64::hex_str")]
size: u64,
// TODO (#307): Remove TotalDifficulty.
total_difficulty: U256,
#[serde(flatten)]
header: BlockHeader,
#[serde(flatten)]
Expand Down Expand Up @@ -48,6 +50,7 @@ impl RpcBlock {
body: BlockBody,
hash: H256,
full_transactions: bool,
total_difficulty: U256,
) -> RpcBlock {
let size = Block {
header: header.clone(),
Expand All @@ -67,6 +70,7 @@ impl RpcBlock {

RpcBlock {
hash,
total_difficulty,
size: size as u64,
header,
body: body_wrapper,
Expand Down Expand Up @@ -190,8 +194,8 @@ mod test {
};
let hash = block_header.compute_block_hash();

let block = RpcBlock::build(block_header, block_body, hash, true);
let expected_block = r#"{"hash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","size":"0x2d6","parentHash":"0x1ac1bf1eef97dc6b03daba5af3b89881b7ae4bc1600dc434f450a9ec34d44999","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba","stateRoot":"0x9de6f95cb4ff4ef22a73705d6ba38c4b927c7bca9887ef5d24a734bb863218d9","transactionsRoot":"0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d","receiptsRoot":"0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x1","gasLimit":"0x16345785d8a0000","gasUsed":"0xa8de","timestamp":"0x3e8","extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","baseFeePerGas":"0x7","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","blobGasUsed":"0x0","excessBlobGas":"0x0","parentBeaconBlockRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactions":[{"type":"0x2","nonce":"0x0","to":"0x6177843db3138ae69679a54b95cf345ed759450d","gas":"0xf618","value":"0xaa87bee538000","input":"0x307831353638","maxPriorityFeePerGas":"0x11","maxFeePerGas":"0x4e","gasPrice":"0x4e","accessList":[{"address":"0x6177843db3138ae69679a54b95cf345ed759450d","storageKeys":[]}],"chainId":"0x301824","yParity":"0x0","v":"0x0","r":"0x151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65d","s":"0x64c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4","blockNumber":"0x1","blockHash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","from":"0x35af8ea983a3ba94c655e19b82b932a30d6b9558","hash":"0x0b8c8f37731d9493916b06d666c3fd5dee2c3bbda06dfe866160d717e00dda91","transactionIndex":"0x0"}],"uncles":[],"withdrawals":[]}"#;
let block = RpcBlock::build(block_header, block_body, hash, true, U256::zero());
let expected_block = r#"{"hash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","size":"0x2d6","totalDifficulty":"0x0","parentHash":"0x1ac1bf1eef97dc6b03daba5af3b89881b7ae4bc1600dc434f450a9ec34d44999","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba","stateRoot":"0x9de6f95cb4ff4ef22a73705d6ba38c4b927c7bca9887ef5d24a734bb863218d9","transactionsRoot":"0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d","receiptsRoot":"0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x1","gasLimit":"0x16345785d8a0000","gasUsed":"0xa8de","timestamp":"0x3e8","extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","baseFeePerGas":"0x7","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","blobGasUsed":"0x0","excessBlobGas":"0x0","parentBeaconBlockRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactions":[{"type":"0x2","nonce":"0x0","to":"0x6177843db3138ae69679a54b95cf345ed759450d","gas":"0xf618","value":"0xaa87bee538000","input":"0x307831353638","maxPriorityFeePerGas":"0x11","maxFeePerGas":"0x4e","gasPrice":"0x4e","accessList":[{"address":"0x6177843db3138ae69679a54b95cf345ed759450d","storageKeys":[]}],"chainId":"0x301824","yParity":"0x0","v":"0x0","r":"0x151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65d","s":"0x64c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4","blockNumber":"0x1","blockHash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","from":"0x35af8ea983a3ba94c655e19b82b932a30d6b9558","hash":"0x0b8c8f37731d9493916b06d666c3fd5dee2c3bbda06dfe866160d717e00dda91","transactionIndex":"0x0"}],"uncles":[],"withdrawals":[]}"#;
assert_eq!(serde_json::to_string(&block).unwrap(), expected_block)
}
}
26 changes: 25 additions & 1 deletion crates/storage/engines/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::Bytes;
use ethereum_rust_core::types::{
Block, BlockBody, BlockHash, BlockHeader, BlockNumber, ChainConfig, Index, Receipt, Transaction,
};
use ethereum_types::{Address, H256};
use ethereum_types::{Address, H256, U256};
use std::{fmt::Debug, panic::RefUnwindSafe};

use crate::error::StoreError;
Expand Down Expand Up @@ -53,6 +53,19 @@ 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 @@ -174,6 +187,17 @@ 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
42 changes: 41 additions & 1 deletion crates/storage/engines/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ethereum_rust_core::types::{
BlockBody, BlockHash, BlockHeader, BlockNumber, ChainConfig, Index, Receipt, Transaction,
};
use ethereum_rust_trie::{InMemoryTrieDB, Trie};
use ethereum_types::{Address, H256};
use ethereum_types::{Address, H256, U256};
use std::{
collections::HashMap,
fmt::Debug,
Expand Down Expand Up @@ -34,6 +34,8 @@ struct StoreInner {
receipts: HashMap<BlockHash, HashMap<Index, Receipt>>,
state_trie_nodes: NodeMap,
storage_trie_nodes: HashMap<Address, NodeMap>,
// TODO (#307): Remove TotalDifficulty.
block_total_difficulties: HashMap<BlockHash, U256>,
}

#[derive(Default, Debug)]
Expand All @@ -43,6 +45,8 @@ 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>,
}

Expand Down Expand Up @@ -105,6 +109,28 @@ 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 @@ -239,6 +265,16 @@ 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 @@ -252,6 +288,10 @@ 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
49 changes: 47 additions & 2 deletions crates/storage/engines/libmdbx.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::api::StoreEngine;
use crate::error::StoreError;
use crate::rlp::{
AccountCodeHashRLP, AccountCodeRLP, BlockBodyRLP, BlockHashRLP, BlockHeaderRLP, ReceiptRLP,
Rlp, TransactionHashRLP, TransactionRLP, TupleRLP,
AccountCodeHashRLP, AccountCodeRLP, BlockBodyRLP, BlockHashRLP, BlockHeaderRLP,
BlockTotalDifficultyRLP, ReceiptRLP, Rlp, TransactionHashRLP, TransactionRLP, TupleRLP,
};
use anyhow::Result;
use bytes::Bytes;
Expand Down Expand Up @@ -127,6 +127,22 @@ impl StoreEngine for Store {
) -> std::result::Result<Option<BlockNumber>, StoreError> {
self.read::<BlockNumbers>(block_hash.into())
}
fn add_block_total_difficulty(
&self,
block_hash: BlockHash,
block_total_difficulty: U256,
) -> std::result::Result<(), StoreError> {
self.write::<BlockTotalDifficulties>(block_hash.into(), block_total_difficulty.into())
}

fn get_block_total_difficulty(
&self,
block_hash: BlockHash,
) -> std::result::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 @@ -286,6 +302,25 @@ impl StoreEngine for Store {
}
}

fn update_latest_total_difficulty(
&self,
latest_total_difficulty: U256,
) -> std::result::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 @@ -356,6 +391,12 @@ 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 @@ -471,6 +512,8 @@ pub enum ChainDataIndex {
SafeBlockNumber = 3,
LatestBlockNumber = 4,
PendingBlockNumber = 5,
// TODO (#307): Remove TotalDifficulty.
LatestTotalDifficulty = 6,
}

impl Encodable for ChainDataIndex {
Expand All @@ -486,6 +529,8 @@ impl Encodable for ChainDataIndex {
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
3 changes: 3 additions & 0 deletions crates/storage/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ethereum_rust_core::{
H256,
};
use ethereum_rust_rlp::{decode::RLPDecode, encode::RLPEncode};
use ethereum_types::U256;
#[cfg(feature = "libmdbx")]
use libmdbx::orm::{Decodable, Encodable};

Expand All @@ -17,6 +18,8 @@ pub type AccountCodeRLP = Rlp<Bytes>;
pub type BlockHashRLP = Rlp<BlockHash>;
pub type BlockHeaderRLP = Rlp<BlockHeader>;
pub type BlockBodyRLP = Rlp<BlockBody>;
// TODO (#307): Remove TotalDifficulty.
pub type BlockTotalDifficultyRLP = Rlp<U256>;

// Receipt types
pub type ReceiptRLP = Rlp<Receipt>;
Expand Down
Loading

0 comments on commit fe6b3ea

Please sign in to comment.