refactor: add BlockWriter and BlockExecutionWriter (#3384)

This commit is contained in:
joshieDo
2023-06-27 10:02:17 +01:00
committed by GitHub
parent d3d44fd46f
commit 085a703d7c
16 changed files with 369 additions and 372 deletions

View File

@ -529,7 +529,7 @@ mod tests {
use std::{collections::BTreeMap, ops::Mul, str::FromStr};
fn insert_account<'a, TX: DbTxMut<'a>>(
tx: &mut TX,
tx: &TX,
address: Address,
account: Account,
storage: &BTreeMap<H256, U256>,
@ -540,7 +540,7 @@ mod tests {
}
fn insert_storage<'a, TX: DbTxMut<'a>>(
tx: &mut TX,
tx: &TX,
hashed_address: H256,
storage: &BTreeMap<H256, U256>,
) {
@ -556,7 +556,7 @@ mod tests {
fn incremental_vs_full_root(inputs: &[&str], modified: &str) {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let hashed_address = H256::from_low_u64_be(1);
let mut hashed_storage_cursor =
@ -592,7 +592,7 @@ mod tests {
// 3. Calculate the incremental root
let mut storage_changes = PrefixSet::default();
storage_changes.insert(Nibbles::unpack(modified_key));
let loader = StorageRoot::new_hashed(tx.tx_mut(), hashed_address)
let loader = StorageRoot::new_hashed(tx.tx_ref(), hashed_address)
.with_changed_prefixes(storage_changes);
let incremental_root = loader.root().unwrap();
@ -633,8 +633,8 @@ mod tests {
}
tx.commit().unwrap();
let mut tx = factory.provider_rw().unwrap();
let got = StorageRoot::new(tx.tx_mut(), address).root().unwrap();
let tx = factory.provider_rw().unwrap();
let got = StorageRoot::new(tx.tx_ref(), address).root().unwrap();
let expected = storage_root(storage.into_iter());
assert_eq!(expected, got);
});
@ -681,7 +681,7 @@ mod tests {
fn test_empty_storage_root() {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let address = Address::random();
let code = "el buen fla";
@ -690,11 +690,11 @@ mod tests {
balance: U256::from(414241124u32),
bytecode_hash: Some(keccak256(code)),
};
insert_account(tx.tx_mut(), address, account, &Default::default());
insert_account(tx.tx_ref(), address, account, &Default::default());
tx.commit().unwrap();
let mut tx = factory.provider_rw().unwrap();
let got = StorageRoot::new(tx.tx_mut(), address).root().unwrap();
let tx = factory.provider_rw().unwrap();
let got = StorageRoot::new(tx.tx_ref(), address).root().unwrap();
assert_eq!(got, EMPTY_ROOT);
}
@ -703,7 +703,7 @@ mod tests {
fn test_storage_root() {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let address = Address::random();
let storage = BTreeMap::from([
@ -718,11 +718,11 @@ mod tests {
bytecode_hash: Some(keccak256(code)),
};
insert_account(tx.tx_mut(), address, account, &storage);
insert_account(tx.tx_ref(), address, account, &storage);
tx.commit().unwrap();
let mut tx = factory.provider_rw().unwrap();
let got = StorageRoot::new(tx.tx_mut(), address).root().unwrap();
let tx = factory.provider_rw().unwrap();
let got = StorageRoot::new(tx.tx_ref(), address).root().unwrap();
assert_eq!(storage_root(storage.into_iter()), got);
}
@ -747,13 +747,13 @@ mod tests {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
for (address, (account, storage)) in &state {
insert_account(tx.tx_mut(), *address, *account, storage)
insert_account(tx.tx_ref(), *address, *account, storage)
}
tx.commit().unwrap();
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let expected = state_root(state.into_iter());
@ -763,7 +763,7 @@ mod tests {
let mut intermediate_state: Option<Box<IntermediateStateRootState>> = None;
while got.is_none() {
let calculator = StateRoot::new(tx.tx_mut())
let calculator = StateRoot::new(tx.tx_ref())
.with_threshold(threshold)
.with_intermediate_state(intermediate_state.take().map(|state| *state));
match calculator.root_with_progress().unwrap() {
@ -786,16 +786,16 @@ mod tests {
fn test_state_root_with_state(state: State) {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
for (address, (account, storage)) in &state {
insert_account(tx.tx_mut(), *address, *account, storage)
insert_account(tx.tx_ref(), *address, *account, storage)
}
tx.commit().unwrap();
let expected = state_root(state.into_iter());
let mut tx = factory.provider_rw().unwrap();
let got = StateRoot::new(tx.tx_mut()).root().unwrap();
let tx = factory.provider_rw().unwrap();
let got = StateRoot::new(tx.tx_ref()).root().unwrap();
assert_eq!(expected, got);
}
@ -836,9 +836,9 @@ mod tests {
hashed_storage_cursor.upsert(key3, StorageEntry { key: hashed_slot, value }).unwrap();
}
tx.commit().unwrap();
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let account3_storage_root = StorageRoot::new(tx.tx_mut(), address3).root().unwrap();
let account3_storage_root = StorageRoot::new(tx.tx_ref(), address3).root().unwrap();
let expected_root = storage_root_prehashed(storage.into_iter());
assert_eq!(expected_root, account3_storage_root);
}
@ -858,7 +858,7 @@ mod tests {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let mut hashed_account_cursor =
tx.tx_ref().cursor_write::<tables::HashedAccount>().unwrap();
@ -906,7 +906,7 @@ mod tests {
}
hashed_storage_cursor.upsert(key3, StorageEntry { key: hashed_slot, value }).unwrap();
}
let account3_storage_root = StorageRoot::new(tx.tx_mut(), address3).root().unwrap();
let account3_storage_root = StorageRoot::new(tx.tx_ref(), address3).root().unwrap();
hash_builder.add_leaf(
Nibbles::unpack(key3),
&encode_account(account3, Some(account3_storage_root)),
@ -1105,7 +1105,7 @@ mod tests {
drop(tx);
}
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
{
let mut hashed_account_cursor =
tx.tx_ref().cursor_write::<tables::HashedAccount>().unwrap();
@ -1129,7 +1129,7 @@ mod tests {
(key6, encode_account(account6, None)),
]);
let (root, trie_updates) = StateRoot::new(tx.tx_mut())
let (root, trie_updates) = StateRoot::new(tx.tx_ref())
.with_changed_account_prefixes(account_prefix_set)
.root_with_updates()
.unwrap();
@ -1165,11 +1165,11 @@ mod tests {
fn account_trie_around_extension_node() {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let expected = extension_node_trie(&mut tx);
let expected = extension_node_trie(&tx);
let (got, updates) = StateRoot::new(tx.tx_mut()).root_with_updates().unwrap();
let (got, updates) = StateRoot::new(tx.tx_ref()).root_with_updates().unwrap();
assert_eq!(expected, got);
// Check account trie
@ -1191,13 +1191,13 @@ mod tests {
fn account_trie_around_extension_node_with_dbtrie() {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let expected = extension_node_trie(&mut tx);
let expected = extension_node_trie(&tx);
let (got, updates) = StateRoot::new(tx.tx_mut()).root_with_updates().unwrap();
let (got, updates) = StateRoot::new(tx.tx_ref()).root_with_updates().unwrap();
assert_eq!(expected, got);
updates.flush(tx.tx_mut()).unwrap();
updates.flush(tx.tx_ref()).unwrap();
// read the account updates from the db
let mut accounts_trie = tx.tx_ref().cursor_read::<tables::AccountsTrie>().unwrap();
@ -1219,7 +1219,7 @@ mod tests {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let mut hashed_account_cursor = tx.tx_ref().cursor_write::<tables::HashedAccount>().unwrap();
let mut state = BTreeMap::default();
@ -1233,7 +1233,7 @@ mod tests {
}
}
let (state_root, trie_updates) = StateRoot::new(tx.tx_mut())
let (state_root, trie_updates) = StateRoot::new(tx.tx_ref())
.with_changed_account_prefixes(changes)
.root_with_updates()
.unwrap();
@ -1243,7 +1243,7 @@ mod tests {
state.clone().into_iter().map(|(key, balance)| (key, (Account { balance, ..Default::default() }, std::iter::empty())))
);
assert_eq!(expected_root, state_root);
trie_updates.flush(tx.tx_mut()).unwrap();
trie_updates.flush(tx.tx_ref()).unwrap();
}
});
}
@ -1253,14 +1253,13 @@ mod tests {
fn storage_trie_around_extension_node() {
let db = create_test_rw_db();
let factory = ProviderFactory::new(db.as_ref(), MAINNET.clone());
let mut tx = factory.provider_rw().unwrap();
let tx = factory.provider_rw().unwrap();
let hashed_address = H256::random();
let (expected_root, expected_updates) =
extension_node_storage_trie(&mut tx, hashed_address);
let (expected_root, expected_updates) = extension_node_storage_trie(&tx, hashed_address);
let (got, _, updates) =
StorageRoot::new_hashed(tx.tx_mut(), hashed_address).root_with_updates().unwrap();
StorageRoot::new_hashed(tx.tx_ref(), hashed_address).root_with_updates().unwrap();
assert_eq!(expected_root, got);
// Check account trie
@ -1279,7 +1278,7 @@ mod tests {
}
fn extension_node_storage_trie(
tx: &mut DatabaseProviderRW<'_, &Env<WriteMap>>,
tx: &DatabaseProviderRW<'_, &Env<WriteMap>>,
hashed_address: H256,
) -> (H256, HashMap<Nibbles, BranchNodeCompact>) {
let value = U256::from(1);
@ -1305,7 +1304,7 @@ mod tests {
(root, updates)
}
fn extension_node_trie(tx: &mut DatabaseProviderRW<'_, &Env<WriteMap>>) -> H256 {
fn extension_node_trie(tx: &DatabaseProviderRW<'_, &Env<WriteMap>>) -> H256 {
let a =
Account { nonce: 0, balance: U256::from(1u64), bytecode_hash: Some(H256::random()) };
let val = encode_account(a, None);