|
| 1 | +use crate::{ |
| 2 | + hot::{HotKvRead, HotKvWrite}, |
| 3 | + tables::hot::{self as tables, AccountStorageKey}, |
| 4 | +}; |
| 5 | +use alloy::primitives::{Address, B256, U256}; |
| 6 | +use reth::primitives::{Account, Bytecode, Header, SealedHeader, StorageEntry}; |
| 7 | +use std::borrow::Cow; |
| 8 | + |
| 9 | +/// Trait for database read operations. |
| 10 | +pub trait HotDbReader: sealed::Sealed { |
| 11 | + /// The error type for read operations |
| 12 | + type Error: std::error::Error + Send + Sync + 'static + From<crate::ser::DeserError>; |
| 13 | + |
| 14 | + /// Read a block header by its number. |
| 15 | + fn get_header(&self, number: u64) -> Result<Option<Header>, Self::Error>; |
| 16 | + |
| 17 | + /// Read a block number by its hash. |
| 18 | + fn get_header_number(&self, hash: &B256) -> Result<Option<u64>, Self::Error>; |
| 19 | + |
| 20 | + /// Read the canonical hash by block number. |
| 21 | + fn get_canonical_hash(&self, number: u64) -> Result<Option<B256>, Self::Error>; |
| 22 | + |
| 23 | + /// Read contract Bytecode by its hash. |
| 24 | + fn get_bytecode(&self, code_hash: &B256) -> Result<Option<Bytecode>, Self::Error>; |
| 25 | + |
| 26 | + /// Read an account by its address. |
| 27 | + fn get_account(&self, address: &Address) -> Result<Option<Account>, Self::Error>; |
| 28 | + |
| 29 | + /// Read a storage slot by its address and key. |
| 30 | + fn get_storage(&self, address: &Address, key: &B256) -> Result<Option<U256>, Self::Error>; |
| 31 | + |
| 32 | + /// Read a [`StorageEntry`] by its address and key. |
| 33 | + fn get_storage_entry( |
| 34 | + &self, |
| 35 | + address: &Address, |
| 36 | + key: &B256, |
| 37 | + ) -> Result<Option<StorageEntry>, Self::Error> { |
| 38 | + let opt = self.get_storage(address, key)?; |
| 39 | + Ok(opt.map(|value| StorageEntry { key: *key, value })) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl<T> HotDbReader for T |
| 44 | +where |
| 45 | + T: HotKvRead, |
| 46 | +{ |
| 47 | + type Error = <T as HotKvRead>::Error; |
| 48 | + |
| 49 | + fn get_header(&self, number: u64) -> Result<Option<Header>, Self::Error> { |
| 50 | + self.get::<tables::Headers>(&number) |
| 51 | + } |
| 52 | + |
| 53 | + fn get_header_number(&self, hash: &B256) -> Result<Option<u64>, Self::Error> { |
| 54 | + self.get::<tables::HeaderNumbers>(hash) |
| 55 | + } |
| 56 | + |
| 57 | + fn get_canonical_hash(&self, number: u64) -> Result<Option<B256>, Self::Error> { |
| 58 | + self.get::<tables::CanonicalHeaders>(&number) |
| 59 | + } |
| 60 | + |
| 61 | + fn get_bytecode(&self, code_hash: &B256) -> Result<Option<Bytecode>, Self::Error> { |
| 62 | + self.get::<tables::Bytecodes>(code_hash) |
| 63 | + } |
| 64 | + |
| 65 | + fn get_account(&self, address: &Address) -> Result<Option<Account>, Self::Error> { |
| 66 | + self.get::<tables::PlainAccountState>(address) |
| 67 | + } |
| 68 | + |
| 69 | + fn get_storage(&self, address: &Address, key: &B256) -> Result<Option<U256>, Self::Error> { |
| 70 | + let storage_key = AccountStorageKey { |
| 71 | + address: std::borrow::Cow::Borrowed(address), |
| 72 | + key: std::borrow::Cow::Borrowed(key), |
| 73 | + }; |
| 74 | + let key = storage_key.encode_key(); |
| 75 | + self.get::<tables::PlainStorageState>(&key) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// Trait for database write operations. |
| 80 | +pub trait HotDbWriter: sealed::Sealed { |
| 81 | + /// The error type for write operations |
| 82 | + type Error: std::error::Error + Send + Sync + 'static + From<crate::ser::DeserError>; |
| 83 | + |
| 84 | + /// Read the latest block header. |
| 85 | + fn put_header(&mut self, header: &Header) -> Result<(), Self::Error>; |
| 86 | + |
| 87 | + /// Write a block number by its hash. |
| 88 | + fn put_header_number(&mut self, hash: &B256, number: u64) -> Result<(), Self::Error>; |
| 89 | + |
| 90 | + /// Write the canonical hash by block number. |
| 91 | + fn put_canonical_hash(&mut self, number: u64, hash: &B256) -> Result<(), Self::Error>; |
| 92 | + |
| 93 | + /// Write contract Bytecode by its hash. |
| 94 | + fn put_bytecode(&mut self, code_hash: &B256, bytecode: &Bytecode) -> Result<(), Self::Error>; |
| 95 | + |
| 96 | + /// Write an account by its address. |
| 97 | + fn put_account(&mut self, address: &Address, account: &Account) -> Result<(), Self::Error>; |
| 98 | + |
| 99 | + /// Write a storage entry by its address and key. |
| 100 | + fn put_storage( |
| 101 | + &mut self, |
| 102 | + address: &Address, |
| 103 | + key: &B256, |
| 104 | + entry: &U256, |
| 105 | + ) -> Result<(), Self::Error>; |
| 106 | + |
| 107 | + /// Commit the write transaction. |
| 108 | + fn commit(self) -> Result<(), Self::Error>; |
| 109 | + |
| 110 | + /// Write a canonical header (header, number mapping, and canonical hash). |
| 111 | + fn put_canonical(&mut self, header: &SealedHeader) -> Result<(), Self::Error> { |
| 112 | + self.put_header(header)?; |
| 113 | + self.put_header_number(&header.hash(), header.number)?; |
| 114 | + self.put_canonical_hash(header.number, &header.hash()) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl<T> HotDbWriter for T |
| 119 | +where |
| 120 | + T: HotKvWrite, |
| 121 | +{ |
| 122 | + type Error = <T as HotKvRead>::Error; |
| 123 | + |
| 124 | + fn put_header(&mut self, header: &Header) -> Result<(), Self::Error> { |
| 125 | + self.queue_put::<tables::Headers>(&header.number, header) |
| 126 | + } |
| 127 | + |
| 128 | + fn put_header_number(&mut self, hash: &B256, number: u64) -> Result<(), Self::Error> { |
| 129 | + self.queue_put::<tables::HeaderNumbers>(hash, &number) |
| 130 | + } |
| 131 | + |
| 132 | + fn put_canonical_hash(&mut self, number: u64, hash: &B256) -> Result<(), Self::Error> { |
| 133 | + self.queue_put::<tables::CanonicalHeaders>(&number, hash) |
| 134 | + } |
| 135 | + |
| 136 | + fn put_bytecode(&mut self, code_hash: &B256, bytecode: &Bytecode) -> Result<(), Self::Error> { |
| 137 | + self.queue_put::<tables::Bytecodes>(code_hash, bytecode) |
| 138 | + } |
| 139 | + |
| 140 | + fn put_account(&mut self, address: &Address, account: &Account) -> Result<(), Self::Error> { |
| 141 | + self.queue_put::<tables::PlainAccountState>(address, account) |
| 142 | + } |
| 143 | + |
| 144 | + fn put_storage( |
| 145 | + &mut self, |
| 146 | + address: &Address, |
| 147 | + key: &B256, |
| 148 | + entry: &U256, |
| 149 | + ) -> Result<(), Self::Error> { |
| 150 | + let storage_key = |
| 151 | + AccountStorageKey { address: Cow::Borrowed(address), key: Cow::Borrowed(key) }; |
| 152 | + self.queue_put::<tables::PlainStorageState>(&storage_key.encode_key(), entry) |
| 153 | + } |
| 154 | + |
| 155 | + fn commit(self) -> Result<(), Self::Error> { |
| 156 | + HotKvWrite::raw_commit(self) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +mod sealed { |
| 161 | + use crate::hot::HotKvRead; |
| 162 | + |
| 163 | + /// Sealed trait to prevent external implementations of HotDbReader and HotDbWriter. |
| 164 | + #[allow(dead_code, unreachable_pub)] |
| 165 | + pub trait Sealed {} |
| 166 | + impl<T> Sealed for T where T: HotKvRead {} |
| 167 | +} |
0 commit comments