|
| 1 | +use crate::db::{ |
| 2 | + AccountTrieHistory, HashedAccountHistory, HashedStorageHistory, HashedStorageKey, MaybeDeleted, |
| 3 | + StorageTrieHistory, StorageTrieKey, StorageValue, VersionedValue, |
| 4 | +}; |
| 5 | +use alloy_primitives::B256; |
| 6 | +use reth_db::table::{DupSort, Table}; |
| 7 | +use reth_primitives_traits::Account; |
| 8 | +use reth_trie::{BranchNodeCompact, Nibbles, StoredNibbles}; |
| 9 | + |
| 10 | +/// Helper to convert inputs into a table key or kv pair. |
| 11 | +pub trait IntoKV<Tab: Table + DupSort> { |
| 12 | + /// Convert `self` into the table key. |
| 13 | + fn into_key(self) -> Tab::Key; |
| 14 | + |
| 15 | + /// Convert `self` into kv for the given `block_number`. |
| 16 | + fn into_kv(self, block_number: u64) -> (Tab::Key, Tab::Value); |
| 17 | +} |
| 18 | + |
| 19 | +impl IntoKV<AccountTrieHistory> for (Nibbles, Option<BranchNodeCompact>) { |
| 20 | + fn into_key(self) -> StoredNibbles { |
| 21 | + StoredNibbles::from(self.0) |
| 22 | + } |
| 23 | + |
| 24 | + fn into_kv(self, block_number: u64) -> (StoredNibbles, VersionedValue<BranchNodeCompact>) { |
| 25 | + let (path, node) = self; |
| 26 | + (StoredNibbles::from(path), VersionedValue { block_number, value: MaybeDeleted(node) }) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl IntoKV<StorageTrieHistory> for (B256, Nibbles, Option<BranchNodeCompact>) { |
| 31 | + fn into_key(self) -> StorageTrieKey { |
| 32 | + let (hashed_address, path, _) = self; |
| 33 | + StorageTrieKey::new(hashed_address, StoredNibbles::from(path)) |
| 34 | + } |
| 35 | + fn into_kv(self, block_number: u64) -> (StorageTrieKey, VersionedValue<BranchNodeCompact>) { |
| 36 | + let (hashed_address, path, node) = self; |
| 37 | + ( |
| 38 | + StorageTrieKey::new(hashed_address, StoredNibbles::from(path)), |
| 39 | + VersionedValue { block_number, value: MaybeDeleted(node) }, |
| 40 | + ) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl IntoKV<HashedAccountHistory> for (B256, Option<Account>) { |
| 45 | + fn into_key(self) -> B256 { |
| 46 | + self.0 |
| 47 | + } |
| 48 | + fn into_kv(self, block_number: u64) -> (B256, VersionedValue<Account>) { |
| 49 | + let (hashed_address, account) = self; |
| 50 | + (hashed_address, VersionedValue { block_number, value: MaybeDeleted(account) }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl IntoKV<HashedStorageHistory> for (B256, B256, Option<StorageValue>) { |
| 55 | + fn into_key(self) -> HashedStorageKey { |
| 56 | + let (hashed_address, hashed_storage_key, _) = self; |
| 57 | + HashedStorageKey::new(hashed_address, hashed_storage_key) |
| 58 | + } |
| 59 | + fn into_kv(self, block_number: u64) -> (HashedStorageKey, VersionedValue<StorageValue>) { |
| 60 | + let (hashed_address, hashed_storage_key, value) = self; |
| 61 | + ( |
| 62 | + HashedStorageKey::new(hashed_address, hashed_storage_key), |
| 63 | + VersionedValue { block_number, value: MaybeDeleted(value) }, |
| 64 | + ) |
| 65 | + } |
| 66 | +} |
0 commit comments