Comments (6)
@shekhirin assign me
from reth.
@malik672 this one is more tricky, I recommend you checking other issues for adding tests to BlockchainProvider2
that are similar to the ones that I've already addressed
from reth.
@shekhirin sounds like a challenge......, I will take it
from reth.
@shekhirin came up with this, is it valid
`
#[cfg(test)]
mod tests {
use super::*;
use reth_db::{mock::DatabaseMock, DatabaseError};
use reth_primitives::{Address, StorageKey};
use std::sync::Arc;
fn setup_provider() -> BlockchainProvider2<DatabaseMock> {
let db = DatabaseMock::default();
let provider_factory = ProviderFactory::new(db.clone(), Arc::new(ChainSpec::default()), StaticFileProvider::default());
BlockchainProvider2::new(provider_factory).unwrap()
}
#[test]
fn test_latest() {
let provider = setup_provider();
let latest_state = provider.latest();
assert!(latest_state.is_ok());
}
#[test]
fn test_history_by_block_number() {
let provider = setup_provider();
let block_number = 100;
let state = provider.history_by_block_number(block_number);
assert!(state.is_ok());
}
#[test]
fn test_history_by_block_hash() {
let provider = setup_provider();
let block_hash = B256::random();
let state = provider.history_by_block_hash(block_hash);
assert!(state.is_ok());
}
#[test]
fn test_state_by_block_hash() {
let provider = setup_provider();
let block_hash = B256::random();
let state = provider.state_by_block_hash(block_hash);
assert!(state.is_ok());
}
#[test]
fn test_pending() {
let provider = setup_provider();
let pending_state = provider.pending();
assert!(pending_state.is_ok());
}
#[test]
fn test_pending_state_by_hash() {
let provider = setup_provider();
let block_hash = B256::random();
let pending_state = provider.pending_state_by_hash(block_hash).unwrap();
assert!(pending_state.is_some() || pending_state.is_none());
}
#[test]
fn test_state_by_block_number_or_tag() {
let provider = setup_provider();
let latest_state = provider.state_by_block_number_or_tag(BlockNumberOrTag::Latest);
assert!(latest_state.is_ok());
let finalized_state = provider.state_by_block_number_or_tag(BlockNumberOrTag::Finalized);
assert!(finalized_state.is_ok());
let safe_state = provider.state_by_block_number_or_tag(BlockNumberOrTag::Safe);
assert!(safe_state.is_ok());
let earliest_state = provider.state_by_block_number_or_tag(BlockNumberOrTag::Earliest);
assert!(earliest_state.is_ok());
let pending_state = provider.state_by_block_number_or_tag(BlockNumberOrTag::Pending);
assert!(pending_state.is_ok());
let number_state = provider.state_by_block_number_or_tag(BlockNumberOrTag::Number(100));
assert!(number_state.is_ok());
}
#[test]
fn test_state_provider_methods() {
let provider = setup_provider();
let state_provider = provider.latest().unwrap();
let address = Address::random();
let account = state_provider.basic_account(address).unwrap();
assert!(account.is_some() || account.is_none());
let storage = state_provider.storage(address, StorageKey::random()).unwrap();
assert!(storage.is_some() || storage.is_none());
let code = state_provider.bytecode_by_hash(B256::random()).unwrap();
assert!(code.is_some() || code.is_none());
}
#[test]
fn test_error_handling() {
let provider = setup_provider();
let result = provider.history_by_block_number(u64::MAX);
assert!(result.is_err());
let result = provider.history_by_block_hash(B256::random());
assert!(result.is_err());
}
}
`
from reth.
#[cfg(test)]
mod tests {
use std::sync::Arc;
use reth_chain_state::{ExecutedBlock, NewCanonicalChain};
use reth_primitives::{Account, Address, BlockNumberOrTag, B256, U256};
use super::StateProviderFactory;
use reth_testing_utils::generators::{self, random_block_range};
use crate::{providers::BlockchainProvider2, test_utils::create_test_provider_factory, BlockWriter};
#[test]
fn test_state_provider_factory() -> Result<(), ()> {
let mut rng = generators::rng();
let factory = create_test_provider_factory();
let blocks = random_block_range(&mut rng, 0..=10, B256::ZERO, 0..1);
let database_blocks = blocks[0..5].to_vec();
let in_memory_blocks = blocks[5..].to_vec();
let mut blocks_iter = blocks.into_iter();
let provider_rw = factory.provider_rw().unwrap();
for block in (0..5).map_while(|_| blocks_iter.next()) {
let sealed_block = block.seal_with_senders().expect("failed to seal block with senders");
provider_rw.insert_historical_block(sealed_block.clone()).unwrap();
let address = Address::random();
let account = Account::default();
provider_rw.insert_block(block.seal_with_senders().unwrap());
}
provider_rw.commit().unwrap();
let provider = BlockchainProvider2::new(factory).unwrap();
let chain = NewCanonicalChain::Commit {
new: blocks_iter
.map(|block| {
let senders = block.senders().expect("failed to recover senders");
ExecutedBlock::new(
Arc::new(block),
Arc::new(senders),
Default::default(),
Default::default(),
Default::default(),
)
})
.collect(),
};
provider.canonical_in_memory_state.update_chain(chain);
provider
.canonical_in_memory_state
.set_canonical_head(in_memory_blocks.last().unwrap().clone().header);
let latest_state = provider.latest().unwrap();
assert_eq!(memory_state.block_hash(memory_block.number).unwrap().unwrap(), memory_block.hash());
let db_block = database_blocks.last().unwrap();
let historical_state = provider.history_by_block_number(db_block.number);
assert!(historical_state.is_ok());
let historical_state = historical_state.unwrap();
assert_eq!(memory_state.block_hash(memory_block.number).unwrap().unwrap(), memory_block.hash());
let memory_block = in_memory_blocks.first().unwrap();
let memory_state = provider.history_by_block_hash(memory_block.hash());
assert!(memory_state.is_ok());
let memory_state = memory_state.unwrap();
assert_eq!(memory_state.block_hash(memory_block.number).unwrap().unwrap(), memory_block.hash());
let latest_state = provider.state_by_block_number_or_tag(BlockNumberOrTag::Latest).unwrap();
assert_eq!(memory_state.block_hash(memory_block.number).unwrap().unwrap(), memory_block.hash());
let earliest_state = provider.state_by_block_number_or_tag(BlockNumberOrTag::Earliest).unwrap();
let pending_state = provider.pending().unwrap();
assert_eq!(memory_state.block_hash(memory_block.number).unwrap().unwrap(), memory_block.hash());
Ok(())
}
}
from reth.
@malik672 yeah, this is the right direction, but you also need to query retrieved states and verify that they return correct values
from reth.
Related Issues (20)
- Refactor `random_blocks` function
- Refactor range functions on `BlockchainProvider2`
- op-reth outputs bad data for "eth_getTransactionReceipt" method
- op-reth outputs bad data for "eth_getTransactionByBlockHashAndIndex" method HOT 1
- op-reth outputs less information than op-geth regarding requests HOT 4
- Make `reth_chainspec::ChainSpec` generic over hard fork HOT 4
- Remove special module handling for eth_callBundle HOT 1
- Properly handle restart edge cases between ExEx and new engine
- Use TransactionInfo type for rpc transaction construction HOT 1
- Improve rpc error for nonce too low HOT 3
- reth interrupted by SIGSEGV HOT 1
- Move optimism rpc types to op-alloy HOT 2
- Define OP chain spec type
- Add builder for `NodeTypes::ChainSpec`
- Move function `reth_primitives::proofs::calculate_receipt_root_optimism` to `reth-optimism-ptimitves`.
- Remove `optimism` feature from `reth-primitives` HOT 2
- feat: debug engine reorg depth
- Move `OptimismHardfork` to `reth-optimism-chainspec` HOT 2
- some metrics are missing or incompatible when switching to new engine HOT 2
- impl From alloy-consensus::TxType for reth_primitives::TxType
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reth.