-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
fix(anvil): block dumps #8160
Merged
Merged
fix(anvil): block dumps #8160
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f186118
implemented latest_block dump/load
samlaf de9ae7a
update to dump/load all blocks instead of only latest
samlaf ad611a4
refactored state loading into storage.rs, and added load-dump cycle test
samlaf e495ffe
fix clippy errors for anvil
samlaf b42e047
remove SerializableHeader and use Header (now serializable)
samlaf 04e3827
clippy happy
mattsse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
//! In-memory blockchain storage | ||
use crate::eth::{ | ||
backend::{ | ||
db::{MaybeFullDatabase, StateDb}, | ||
db::{MaybeFullDatabase, SerializableBlock, StateDb}, | ||
mem::cache::DiskStateCache, | ||
}, | ||
pool::transactions::PoolTransaction, | ||
|
@@ -319,6 +319,21 @@ impl BlockchainStorage { | |
} | ||
} | ||
} | ||
|
||
pub fn serialized_blocks(&self) -> Vec<SerializableBlock> { | ||
self.blocks.values().map(|block| block.clone().into()).collect() | ||
} | ||
|
||
/// Deserialize and add all blocks data to the backend storage | ||
pub fn load_blocks(&mut self, serializable_blocks: Vec<SerializableBlock>) { | ||
for serializable_block in serializable_blocks.iter() { | ||
let block: Block = serializable_block.clone().into(); | ||
let block_hash = block.header.hash_slow(); | ||
let block_number = block.header.number; | ||
self.blocks.insert(block_hash, block); | ||
self.hashes.insert(U64::from(block_number), block_hash); | ||
} | ||
} | ||
} | ||
|
||
/// A simple in-memory blockchain | ||
|
@@ -427,7 +442,9 @@ pub struct MinedTransactionReceipt { | |
mod tests { | ||
use super::*; | ||
use crate::eth::backend::db::Db; | ||
use alloy_primitives::Address; | ||
use alloy_primitives::{hex, Address}; | ||
use alloy_rlp::Decodable; | ||
use anvil_core::eth::transaction::TypedTransaction; | ||
use foundry_evm::{ | ||
backend::MemDb, | ||
revm::{ | ||
|
@@ -499,4 +516,33 @@ mod tests { | |
assert_eq!(acc.balance, rU256::from(balance)); | ||
} | ||
} | ||
|
||
// verifies that blocks in BlockchainStorage remain the same when dumped and reloaded | ||
#[test] | ||
fn test_storage_dump_reload_cycle() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added this unit test for storage, but now that we are dumping and reloading both the db and storage, we might need an integration test in mod.rs for the dumping/reloading of the entire serialized_state? |
||
let mut dump_storage = BlockchainStorage::empty(); | ||
|
||
let partial_header = PartialHeader { gas_limit: 123456, ..Default::default() }; | ||
let bytes_first = &mut &hex::decode("f86b02843b9aca00830186a094d3e8763675e4c425df46cc3b5c0f6cbdac39604687038d7ea4c68000802ba00eb96ca19e8a77102767a41fc85a36afd5c61ccb09911cec5d3e86e193d9c5aea03a456401896b1b6055311536bf00a718568c744d8c1f9df59879e8350220ca18").unwrap()[..]; | ||
let tx: MaybeImpersonatedTransaction = | ||
TypedTransaction::decode(&mut &bytes_first[..]).unwrap().into(); | ||
let block = Block::new::<MaybeImpersonatedTransaction>( | ||
partial_header.clone(), | ||
vec![tx.clone()], | ||
vec![], | ||
); | ||
let block_hash = block.header.hash_slow(); | ||
dump_storage.blocks.insert(block_hash, block); | ||
|
||
let serialized_blocks = dump_storage.serialized_blocks(); | ||
|
||
let mut load_storage = BlockchainStorage::empty(); | ||
|
||
load_storage.load_blocks(serialized_blocks); | ||
|
||
let loaded_block = load_storage.blocks.get(&block_hash).unwrap(); | ||
assert_eq!(loaded_block.header.gas_limit, partial_header.gas_limit); | ||
let loaded_tx = loaded_block.transactions.first().unwrap(); | ||
assert_eq!(loaded_tx, &tx); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is the best implementation. Reading the blocks and then passing them to the dump_state function in db to also include them in the dumped json.
Will want to also dump events soon, so the db dump_state function might become cluttered and coupled. An alternative design is to pass mut references to a serialized_state to db, storage, event services, and have them add whatever information they are dumping, so that we can partially construct the serialized_state?