mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
Discussion draft: change DB Writer to take value references (#13672)
This commit is contained in:
@ -158,7 +158,7 @@ where
|
||||
if let Some(node) = maybe_updated {
|
||||
self.cursor.upsert(
|
||||
self.hashed_address,
|
||||
StorageTrieEntry { nibbles, node: node.clone() },
|
||||
&StorageTrieEntry { nibbles, node: node.clone() },
|
||||
)?;
|
||||
}
|
||||
}
|
||||
@ -229,7 +229,7 @@ mod tests {
|
||||
cursor
|
||||
.upsert(
|
||||
key.into(),
|
||||
BranchNodeCompact::new(
|
||||
&BranchNodeCompact::new(
|
||||
0b0000_0010_0000_0001,
|
||||
0b0000_0010_0000_0001,
|
||||
0,
|
||||
@ -264,7 +264,7 @@ mod tests {
|
||||
let value = BranchNodeCompact::new(1, 1, 1, vec![B256::random()], None);
|
||||
|
||||
cursor
|
||||
.upsert(hashed_address, StorageTrieEntry { nibbles: key.clone(), node: value.clone() })
|
||||
.upsert(hashed_address, &StorageTrieEntry { nibbles: key.clone(), node: value.clone() })
|
||||
.unwrap();
|
||||
|
||||
let mut cursor = DatabaseStorageTrieCursor::new(cursor, hashed_address);
|
||||
|
||||
@ -31,7 +31,7 @@ proptest! {
|
||||
|
||||
// Insert init state into database
|
||||
for (hashed_address, balance) in init_state.clone() {
|
||||
hashed_account_cursor.upsert(hashed_address, Account { balance, ..Default::default() }).unwrap();
|
||||
hashed_account_cursor.upsert(hashed_address, &Account { balance, ..Default::default() }).unwrap();
|
||||
}
|
||||
|
||||
// Compute initial root and updates
|
||||
@ -46,7 +46,7 @@ proptest! {
|
||||
for (hashed_address, balance) in state_update {
|
||||
if let Some(balance) = balance {
|
||||
let account = Account { balance, ..Default::default() };
|
||||
hashed_account_cursor.upsert(hashed_address, account).unwrap();
|
||||
hashed_account_cursor.upsert(hashed_address, &account).unwrap();
|
||||
hashed_state.accounts.insert(hashed_address, Some(account));
|
||||
state.insert(hashed_address, balance);
|
||||
} else {
|
||||
@ -85,7 +85,7 @@ proptest! {
|
||||
// Insert init state into database
|
||||
for (hashed_slot, value) in init_storage.clone() {
|
||||
hashed_storage_cursor
|
||||
.upsert(hashed_address, StorageEntry { key: hashed_slot, value })
|
||||
.upsert(hashed_address, &StorageEntry { key: hashed_slot, value })
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@ -102,7 +102,7 @@ proptest! {
|
||||
let mut hashed_storage = HashedStorage::new(is_deleted);
|
||||
for (hashed_slot, value) in storage_update.clone() {
|
||||
hashed_storage_cursor
|
||||
.upsert(hashed_address, StorageEntry { key: hashed_slot, value })
|
||||
.upsert(hashed_address, &StorageEntry { key: hashed_slot, value })
|
||||
.unwrap();
|
||||
hashed_storage.storage.insert(hashed_slot, value);
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ fn incremental_vs_full_root(inputs: &[&str], modified: &str) {
|
||||
let data = inputs.iter().map(|x| B256::from_str(x).unwrap());
|
||||
let value = U256::from(0);
|
||||
for key in data {
|
||||
hashed_storage_cursor.upsert(hashed_address, StorageEntry { key, value }).unwrap();
|
||||
hashed_storage_cursor.upsert(hashed_address, &StorageEntry { key, value }).unwrap();
|
||||
}
|
||||
|
||||
// Generate the intermediate nodes on the receiving end of the channel
|
||||
@ -71,7 +71,7 @@ fn incremental_vs_full_root(inputs: &[&str], modified: &str) {
|
||||
hashed_storage_cursor.delete_current().unwrap();
|
||||
}
|
||||
hashed_storage_cursor
|
||||
.upsert(hashed_address, StorageEntry { key: modified_key, value })
|
||||
.upsert(hashed_address, &StorageEntry { key: modified_key, value })
|
||||
.unwrap();
|
||||
|
||||
// 2. Calculate full merkle root
|
||||
@ -313,7 +313,7 @@ fn storage_root_regression() {
|
||||
let mut hashed_storage_cursor =
|
||||
tx.tx_ref().cursor_dup_write::<tables::HashedStorages>().unwrap();
|
||||
for (hashed_slot, value) in storage.clone() {
|
||||
hashed_storage_cursor.upsert(key3, StorageEntry { key: hashed_slot, value }).unwrap();
|
||||
hashed_storage_cursor.upsert(key3, &StorageEntry { key: hashed_slot, value }).unwrap();
|
||||
}
|
||||
tx.commit().unwrap();
|
||||
let tx = factory.provider_rw().unwrap();
|
||||
@ -349,7 +349,7 @@ fn account_and_storage_trie() {
|
||||
let key1 =
|
||||
B256::from_str("b000000000000000000000000000000000000000000000000000000000000000").unwrap();
|
||||
let account1 = Account { nonce: 0, balance: U256::from(3).mul(ether), bytecode_hash: None };
|
||||
hashed_account_cursor.upsert(key1, account1).unwrap();
|
||||
hashed_account_cursor.upsert(key1, &account1).unwrap();
|
||||
hash_builder.add_leaf(Nibbles::unpack(key1), &encode_account(account1, None));
|
||||
|
||||
// Some address whose hash starts with 0xB040
|
||||
@ -358,7 +358,7 @@ fn account_and_storage_trie() {
|
||||
assert_eq!(key2[0], 0xB0);
|
||||
assert_eq!(key2[1], 0x40);
|
||||
let account2 = Account { nonce: 0, balance: ether, ..Default::default() };
|
||||
hashed_account_cursor.upsert(key2, account2).unwrap();
|
||||
hashed_account_cursor.upsert(key2, &account2).unwrap();
|
||||
hash_builder.add_leaf(Nibbles::unpack(key2), &encode_account(account2, None));
|
||||
|
||||
// Some address whose hash starts with 0xB041
|
||||
@ -370,7 +370,7 @@ fn account_and_storage_trie() {
|
||||
B256::from_str("5be74cad16203c4905c068b012a2e9fb6d19d036c410f16fd177f337541440dd").unwrap();
|
||||
let account3 =
|
||||
Account { nonce: 0, balance: U256::from(2).mul(ether), bytecode_hash: Some(code_hash) };
|
||||
hashed_account_cursor.upsert(key3, account3).unwrap();
|
||||
hashed_account_cursor.upsert(key3, &account3).unwrap();
|
||||
for (hashed_slot, value) in storage {
|
||||
if hashed_storage_cursor
|
||||
.seek_by_key_subkey(key3, hashed_slot)
|
||||
@ -380,7 +380,7 @@ fn account_and_storage_trie() {
|
||||
{
|
||||
hashed_storage_cursor.delete_current().unwrap();
|
||||
}
|
||||
hashed_storage_cursor.upsert(key3, StorageEntry { key: hashed_slot, value }).unwrap();
|
||||
hashed_storage_cursor.upsert(key3, &StorageEntry { key: hashed_slot, value }).unwrap();
|
||||
}
|
||||
let account3_storage_root = StorageRoot::from_tx(tx.tx_ref(), address3).root().unwrap();
|
||||
hash_builder
|
||||
@ -389,19 +389,19 @@ fn account_and_storage_trie() {
|
||||
let key4a =
|
||||
B256::from_str("B1A0000000000000000000000000000000000000000000000000000000000000").unwrap();
|
||||
let account4a = Account { nonce: 0, balance: U256::from(4).mul(ether), ..Default::default() };
|
||||
hashed_account_cursor.upsert(key4a, account4a).unwrap();
|
||||
hashed_account_cursor.upsert(key4a, &account4a).unwrap();
|
||||
hash_builder.add_leaf(Nibbles::unpack(key4a), &encode_account(account4a, None));
|
||||
|
||||
let key5 =
|
||||
B256::from_str("B310000000000000000000000000000000000000000000000000000000000000").unwrap();
|
||||
let account5 = Account { nonce: 0, balance: U256::from(8).mul(ether), ..Default::default() };
|
||||
hashed_account_cursor.upsert(key5, account5).unwrap();
|
||||
hashed_account_cursor.upsert(key5, &account5).unwrap();
|
||||
hash_builder.add_leaf(Nibbles::unpack(key5), &encode_account(account5, None));
|
||||
|
||||
let key6 =
|
||||
B256::from_str("B340000000000000000000000000000000000000000000000000000000000000").unwrap();
|
||||
let account6 = Account { nonce: 0, balance: U256::from(1).mul(ether), ..Default::default() };
|
||||
hashed_account_cursor.upsert(key6, account6).unwrap();
|
||||
hashed_account_cursor.upsert(key6, &account6).unwrap();
|
||||
hash_builder.add_leaf(Nibbles::unpack(key6), &encode_account(account6, None));
|
||||
|
||||
// Populate account & storage trie DB tables
|
||||
@ -452,7 +452,7 @@ fn account_and_storage_trie() {
|
||||
let key4b = keccak256(address4b);
|
||||
assert_eq!(key4b.0[0], key4a.0[0]);
|
||||
let account4b = Account { nonce: 0, balance: U256::from(5).mul(ether), bytecode_hash: None };
|
||||
hashed_account_cursor.upsert(key4b, account4b).unwrap();
|
||||
hashed_account_cursor.upsert(key4b, &account4b).unwrap();
|
||||
|
||||
let mut prefix_set = PrefixSetMut::default();
|
||||
prefix_set.insert(Nibbles::unpack(key4b));
|
||||
@ -649,7 +649,7 @@ proptest! {
|
||||
let should_generate_changeset = !state.is_empty();
|
||||
let mut changes = PrefixSetMut::default();
|
||||
for (hashed_address, balance) in accounts.clone() {
|
||||
hashed_account_cursor.upsert(hashed_address, Account { balance, ..Default::default() }).unwrap();
|
||||
hashed_account_cursor.upsert(hashed_address, &Account { balance, ..Default::default() }).unwrap();
|
||||
if should_generate_changeset {
|
||||
changes.insert(Nibbles::unpack(hashed_address));
|
||||
}
|
||||
@ -703,7 +703,9 @@ fn extension_node_storage_trie<N: ProviderNodeTypes>(
|
||||
hex!("30af8f0000000000000000000000000000000000000000000000000000000000"),
|
||||
hex!("3100000000000000000000000000000000000000000000000000000000000000"),
|
||||
] {
|
||||
hashed_storage.upsert(hashed_address, StorageEntry { key: B256::new(key), value }).unwrap();
|
||||
hashed_storage
|
||||
.upsert(hashed_address, &StorageEntry { key: B256::new(key), value })
|
||||
.unwrap();
|
||||
hb.add_leaf(Nibbles::unpack(key), &alloy_rlp::encode_fixed_size(&value));
|
||||
}
|
||||
|
||||
@ -730,7 +732,7 @@ fn extension_node_trie<N: ProviderNodeTypes>(
|
||||
hex!("30af8f0000000000000000000000000000000000000000000000000000000000"),
|
||||
hex!("3100000000000000000000000000000000000000000000000000000000000000"),
|
||||
] {
|
||||
hashed_accounts.upsert(B256::new(key), a).unwrap();
|
||||
hashed_accounts.upsert(B256::new(key), &a).unwrap();
|
||||
hb.add_leaf(Nibbles::unpack(key), &val);
|
||||
}
|
||||
|
||||
|
||||
@ -38,7 +38,7 @@ fn walk_nodes_with_common_prefix() {
|
||||
|
||||
let mut account_cursor = tx.tx_ref().cursor_write::<tables::AccountsTrie>().unwrap();
|
||||
for (k, v) in &inputs {
|
||||
account_cursor.upsert(k.clone().into(), v.clone()).unwrap();
|
||||
account_cursor.upsert(k.clone().into(), &v.clone()).unwrap();
|
||||
}
|
||||
let account_trie = DatabaseAccountTrieCursor::new(account_cursor);
|
||||
test_cursor(account_trie, &expected);
|
||||
@ -47,7 +47,10 @@ fn walk_nodes_with_common_prefix() {
|
||||
let mut storage_cursor = tx.tx_ref().cursor_dup_write::<tables::StoragesTrie>().unwrap();
|
||||
for (k, v) in &inputs {
|
||||
storage_cursor
|
||||
.upsert(hashed_address, StorageTrieEntry { nibbles: k.clone().into(), node: v.clone() })
|
||||
.upsert(
|
||||
hashed_address,
|
||||
&StorageTrieEntry { nibbles: k.clone().into(), node: v.clone() },
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let storage_trie = DatabaseStorageTrieCursor::new(storage_cursor, hashed_address);
|
||||
@ -106,7 +109,7 @@ fn cursor_rootnode_with_changesets() {
|
||||
|
||||
let hashed_address = B256::random();
|
||||
for (k, v) in nodes {
|
||||
cursor.upsert(hashed_address, StorageTrieEntry { nibbles: k.into(), node: v }).unwrap();
|
||||
cursor.upsert(hashed_address, &StorageTrieEntry { nibbles: k.into(), node: v }).unwrap();
|
||||
}
|
||||
|
||||
let mut trie = DatabaseStorageTrieCursor::new(cursor, hashed_address);
|
||||
|
||||
@ -114,10 +114,10 @@ fn correctly_decodes_branch_node_values() {
|
||||
let mut hashed_storage_cursor =
|
||||
provider.tx_ref().cursor_dup_write::<tables::HashedStorages>().unwrap();
|
||||
hashed_storage_cursor
|
||||
.upsert(hashed_address, StorageEntry { key: hashed_slot1, value: U256::from(1) })
|
||||
.upsert(hashed_address, &StorageEntry { key: hashed_slot1, value: U256::from(1) })
|
||||
.unwrap();
|
||||
hashed_storage_cursor
|
||||
.upsert(hashed_address, StorageEntry { key: hashed_slot2, value: U256::from(1) })
|
||||
.upsert(hashed_address, &StorageEntry { key: hashed_slot2, value: U256::from(1) })
|
||||
.unwrap();
|
||||
|
||||
let state_root = StateRoot::from_tx(provider.tx_ref()).root().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user