reth_db/implementation/mdbx/
mod.rs

1//! Module that interacts with MDBX.
2
3use crate::{
4    lockfile::StorageLock,
5    metrics::DatabaseEnvMetrics,
6    tables::{self, Tables},
7    utils::default_page_size,
8    DatabaseError, TableSet,
9};
10use eyre::Context;
11use metrics::{gauge, Label};
12use reth_db_api::{
13    cursor::{DbCursorRO, DbCursorRW},
14    database::Database,
15    database_metrics::DatabaseMetrics,
16    models::ClientVersion,
17    transaction::{DbTx, DbTxMut},
18};
19use reth_libmdbx::{
20    ffi, DatabaseFlags, Environment, EnvironmentFlags, Geometry, HandleSlowReadersReturnCode,
21    MaxReadTransactionDuration, Mode, PageSize, SyncMode, RO, RW,
22};
23use reth_storage_errors::db::LogLevel;
24use reth_tracing::tracing::error;
25use std::{
26    ops::{Deref, Range},
27    path::Path,
28    sync::Arc,
29    time::{SystemTime, UNIX_EPOCH},
30};
31use tx::Tx;
32
33pub mod cursor;
34pub mod tx;
35
36mod utils;
37
38/// 1 KB in bytes
39pub const KILOBYTE: usize = 1024;
40/// 1 MB in bytes
41pub const MEGABYTE: usize = KILOBYTE * 1024;
42/// 1 GB in bytes
43pub const GIGABYTE: usize = MEGABYTE * 1024;
44/// 1 TB in bytes
45pub const TERABYTE: usize = GIGABYTE * 1024;
46
47/// MDBX allows up to 32767 readers (`MDBX_READERS_LIMIT`), but we limit it to slightly below that
48const DEFAULT_MAX_READERS: u64 = 32_000;
49
50/// Space that a read-only transaction can occupy until the warning is emitted.
51/// See [`reth_libmdbx::EnvironmentBuilder::set_handle_slow_readers`] for more information.
52const MAX_SAFE_READER_SPACE: usize = 10 * GIGABYTE;
53
54/// Environment used when opening a MDBX environment. RO/RW.
55#[derive(Clone, Copy, Debug, Eq, PartialEq)]
56pub enum DatabaseEnvKind {
57    /// Read-only MDBX environment.
58    RO,
59    /// Read-write MDBX environment.
60    RW,
61}
62
63impl DatabaseEnvKind {
64    /// Returns `true` if the environment is read-write.
65    pub const fn is_rw(&self) -> bool {
66        matches!(self, Self::RW)
67    }
68}
69
70/// Arguments for database initialization.
71#[derive(Clone, Debug)]
72pub struct DatabaseArguments {
73    /// Client version that accesses the database.
74    client_version: ClientVersion,
75    /// Database geometry settings.
76    geometry: Geometry<Range<usize>>,
77    /// Database log level. If [None], the default value is used.
78    log_level: Option<LogLevel>,
79    /// Maximum duration of a read transaction. If [None], the default value is used.
80    max_read_transaction_duration: Option<MaxReadTransactionDuration>,
81    /// Open environment in exclusive/monopolistic mode. If [None], the default value is used.
82    ///
83    /// This can be used as a replacement for `MDB_NOLOCK`, which don't supported by MDBX. In this
84    /// way, you can get the minimal overhead, but with the correct multi-process and multi-thread
85    /// locking.
86    ///
87    /// If `true` = open environment in exclusive/monopolistic mode or return `MDBX_BUSY` if
88    /// environment already used by other process. The main feature of the exclusive mode is the
89    /// ability to open the environment placed on a network share.
90    ///
91    /// If `false` = open environment in cooperative mode, i.e. for multi-process
92    /// access/interaction/cooperation. The main requirements of the cooperative mode are:
93    /// - Data files MUST be placed in the LOCAL file system, but NOT on a network share.
94    /// - Environment MUST be opened only by LOCAL processes, but NOT over a network.
95    /// - OS kernel (i.e. file system and memory mapping implementation) and all processes that
96    ///   open the given environment MUST be running in the physically single RAM with
97    ///   cache-coherency. The only exception for cache-consistency requirement is Linux on MIPS
98    ///   architecture, but this case has not been tested for a long time).
99    ///
100    /// This flag affects only at environment opening but can't be changed after.
101    exclusive: Option<bool>,
102}
103
104impl Default for DatabaseArguments {
105    fn default() -> Self {
106        Self::new(ClientVersion::default())
107    }
108}
109
110impl DatabaseArguments {
111    /// Create new database arguments with given client version.
112    pub fn new(client_version: ClientVersion) -> Self {
113        Self {
114            client_version,
115            geometry: Geometry {
116                size: Some(0..(4 * TERABYTE)),
117                growth_step: Some(4 * GIGABYTE as isize),
118                shrink_threshold: Some(0),
119                page_size: Some(PageSize::Set(default_page_size())),
120            },
121            log_level: None,
122            max_read_transaction_duration: None,
123            exclusive: None,
124        }
125    }
126
127    /// Sets the upper size limit of the db environment, the maximum database size in bytes.
128    pub const fn with_geometry_max_size(mut self, max_size: Option<usize>) -> Self {
129        if let Some(max_size) = max_size {
130            self.geometry.size = Some(0..max_size);
131        }
132        self
133    }
134
135    /// Configures the database growth step in bytes.
136    pub const fn with_growth_step(mut self, growth_step: Option<usize>) -> Self {
137        if let Some(growth_step) = growth_step {
138            self.geometry.growth_step = Some(growth_step as isize);
139        }
140        self
141    }
142
143    /// Set the log level.
144    pub const fn with_log_level(mut self, log_level: Option<LogLevel>) -> Self {
145        self.log_level = log_level;
146        self
147    }
148
149    /// Set the maximum duration of a read transaction.
150    pub const fn with_max_read_transaction_duration(
151        mut self,
152        max_read_transaction_duration: Option<MaxReadTransactionDuration>,
153    ) -> Self {
154        self.max_read_transaction_duration = max_read_transaction_duration;
155        self
156    }
157
158    /// Set the mdbx exclusive flag.
159    pub const fn with_exclusive(mut self, exclusive: Option<bool>) -> Self {
160        self.exclusive = exclusive;
161        self
162    }
163
164    /// Returns the client version if any.
165    pub const fn client_version(&self) -> &ClientVersion {
166        &self.client_version
167    }
168}
169
170/// Wrapper for the libmdbx environment: [Environment]
171#[derive(Debug)]
172pub struct DatabaseEnv {
173    /// Libmdbx-sys environment.
174    inner: Environment,
175    /// Cache for metric handles. If `None`, metrics are not recorded.
176    metrics: Option<Arc<DatabaseEnvMetrics>>,
177    /// Write lock for when dealing with a read-write environment.
178    _lock_file: Option<StorageLock>,
179}
180
181impl Database for DatabaseEnv {
182    type TX = tx::Tx<RO>;
183    type TXMut = tx::Tx<RW>;
184
185    fn tx(&self) -> Result<Self::TX, DatabaseError> {
186        Tx::new_with_metrics(
187            self.inner.begin_ro_txn().map_err(|e| DatabaseError::InitTx(e.into()))?,
188            self.metrics.clone(),
189        )
190        .map_err(|e| DatabaseError::InitTx(e.into()))
191    }
192
193    fn tx_mut(&self) -> Result<Self::TXMut, DatabaseError> {
194        Tx::new_with_metrics(
195            self.inner.begin_rw_txn().map_err(|e| DatabaseError::InitTx(e.into()))?,
196            self.metrics.clone(),
197        )
198        .map_err(|e| DatabaseError::InitTx(e.into()))
199    }
200}
201
202impl DatabaseMetrics for DatabaseEnv {
203    fn report_metrics(&self) {
204        for (name, value, labels) in self.gauge_metrics() {
205            gauge!(name, labels).set(value);
206        }
207    }
208
209    fn gauge_metrics(&self) -> Vec<(&'static str, f64, Vec<Label>)> {
210        let mut metrics = Vec::new();
211
212        let _ = self
213            .view(|tx| {
214                for table in Tables::ALL.iter().map(Tables::name) {
215                    let table_db = tx.inner.open_db(Some(table)).wrap_err("Could not open db.")?;
216
217                    let stats = tx
218                        .inner
219                        .db_stat(&table_db)
220                        .wrap_err(format!("Could not find table: {table}"))?;
221
222                    let page_size = stats.page_size() as usize;
223                    let leaf_pages = stats.leaf_pages();
224                    let branch_pages = stats.branch_pages();
225                    let overflow_pages = stats.overflow_pages();
226                    let num_pages = leaf_pages + branch_pages + overflow_pages;
227                    let table_size = page_size * num_pages;
228                    let entries = stats.entries();
229
230                    metrics.push((
231                        "db.table_size",
232                        table_size as f64,
233                        vec![Label::new("table", table)],
234                    ));
235                    metrics.push((
236                        "db.table_pages",
237                        leaf_pages as f64,
238                        vec![Label::new("table", table), Label::new("type", "leaf")],
239                    ));
240                    metrics.push((
241                        "db.table_pages",
242                        branch_pages as f64,
243                        vec![Label::new("table", table), Label::new("type", "branch")],
244                    ));
245                    metrics.push((
246                        "db.table_pages",
247                        overflow_pages as f64,
248                        vec![Label::new("table", table), Label::new("type", "overflow")],
249                    ));
250                    metrics.push((
251                        "db.table_entries",
252                        entries as f64,
253                        vec![Label::new("table", table)],
254                    ));
255                }
256
257                Ok::<(), eyre::Report>(())
258            })
259            .map_err(|error| error!(%error, "Failed to read db table stats"));
260
261        if let Ok(freelist) =
262            self.freelist().map_err(|error| error!(%error, "Failed to read db.freelist"))
263        {
264            metrics.push(("db.freelist", freelist as f64, vec![]));
265        }
266
267        if let Ok(stat) = self.stat().map_err(|error| error!(%error, "Failed to read db.stat")) {
268            metrics.push(("db.page_size", stat.page_size() as f64, vec![]));
269        }
270
271        metrics.push((
272            "db.timed_out_not_aborted_transactions",
273            self.timed_out_not_aborted_transactions() as f64,
274            vec![],
275        ));
276
277        metrics
278    }
279}
280
281impl DatabaseEnv {
282    /// Opens the database at the specified path with the given `EnvKind`.
283    ///
284    /// It does not create the tables, for that call [`DatabaseEnv::create_tables`].
285    pub fn open(
286        path: &Path,
287        kind: DatabaseEnvKind,
288        args: DatabaseArguments,
289    ) -> Result<Self, DatabaseError> {
290        let _lock_file = if kind.is_rw() {
291            StorageLock::try_acquire(path)
292                .map_err(|err| DatabaseError::Other(err.to_string()))?
293                .into()
294        } else {
295            None
296        };
297
298        let mut inner_env = Environment::builder();
299
300        let mode = match kind {
301            DatabaseEnvKind::RO => Mode::ReadOnly,
302            DatabaseEnvKind::RW => {
303                // enable writemap mode in RW mode
304                inner_env.write_map();
305                Mode::ReadWrite { sync_mode: SyncMode::Durable }
306            }
307        };
308
309        // Note: We set max dbs to 256 here to allow for custom tables. This needs to be set on
310        // environment creation.
311        debug_assert!(Tables::ALL.len() <= 256, "number of tables exceed max dbs");
312        inner_env.set_max_dbs(256);
313        inner_env.set_geometry(args.geometry);
314
315        fn is_current_process(id: u32) -> bool {
316            #[cfg(unix)]
317            {
318                id == std::os::unix::process::parent_id() || id == std::process::id()
319            }
320
321            #[cfg(not(unix))]
322            {
323                id == std::process::id()
324            }
325        }
326
327        extern "C" fn handle_slow_readers(
328            _env: *const ffi::MDBX_env,
329            _txn: *const ffi::MDBX_txn,
330            process_id: ffi::mdbx_pid_t,
331            thread_id: ffi::mdbx_tid_t,
332            read_txn_id: u64,
333            gap: std::ffi::c_uint,
334            space: usize,
335            retry: std::ffi::c_int,
336        ) -> HandleSlowReadersReturnCode {
337            if space > MAX_SAFE_READER_SPACE {
338                let message = if is_current_process(process_id as u32) {
339                    "Current process has a long-lived database transaction that grows the database file."
340                } else {
341                    "External process has a long-lived database transaction that grows the database file. \
342                     Use shorter-lived read transactions or shut down the node."
343                };
344                reth_tracing::tracing::warn!(
345                    target: "storage::db::mdbx",
346                    ?process_id,
347                    ?thread_id,
348                    ?read_txn_id,
349                    ?gap,
350                    ?space,
351                    ?retry,
352                    "{message}"
353                )
354            }
355
356            reth_libmdbx::HandleSlowReadersReturnCode::ProceedWithoutKillingReader
357        }
358        inner_env.set_handle_slow_readers(handle_slow_readers);
359
360        inner_env.set_flags(EnvironmentFlags {
361            mode,
362            // We disable readahead because it improves performance for linear scans, but
363            // worsens it for random access (which is our access pattern outside of sync)
364            no_rdahead: true,
365            coalesce: true,
366            exclusive: args.exclusive.unwrap_or_default(),
367            ..Default::default()
368        });
369        // Configure more readers
370        inner_env.set_max_readers(DEFAULT_MAX_READERS);
371        // This parameter sets the maximum size of the "reclaimed list", and the unit of measurement
372        // is "pages". Reclaimed list is the list of freed pages that's populated during the
373        // lifetime of DB transaction, and through which MDBX searches when it needs to insert new
374        // record with overflow pages. The flow is roughly the following:
375        // 0. We need to insert a record that requires N number of overflow pages (in consecutive
376        //    sequence inside the DB file).
377        // 1. Get some pages from the freelist, put them into the reclaimed list.
378        // 2. Search through the reclaimed list for the sequence of size N.
379        // 3. a. If found, return the sequence.
380        // 3. b. If not found, repeat steps 1-3. If the reclaimed list size is larger than
381        //    the `rp augment limit`, stop the search and allocate new pages at the end of the file:
382        //    https://github.com/paradigmxyz/reth/blob/2a4c78759178f66e30c8976ec5d243b53102fc9a/crates/storage/libmdbx-rs/mdbx-sys/libmdbx/mdbx.c#L11479-L11480.
383        //
384        // Basically, this parameter controls for how long do we search through the freelist before
385        // trying to allocate new pages. Smaller value will make MDBX to fallback to
386        // allocation faster, higher value will force MDBX to search through the freelist
387        // longer until the sequence of pages is found.
388        //
389        // The default value of this parameter is set depending on the DB size. The bigger the
390        // database, the larger is `rp augment limit`.
391        // https://github.com/paradigmxyz/reth/blob/2a4c78759178f66e30c8976ec5d243b53102fc9a/crates/storage/libmdbx-rs/mdbx-sys/libmdbx/mdbx.c#L10018-L10024.
392        //
393        // Previously, MDBX set this value as `256 * 1024` constant. Let's fallback to this,
394        // because we want to prioritize freelist lookup speed over database growth.
395        // https://github.com/paradigmxyz/reth/blob/fa2b9b685ed9787636d962f4366caf34a9186e66/crates/storage/libmdbx-rs/mdbx-sys/libmdbx/mdbx.c#L16017.
396        inner_env.set_rp_augment_limit(256 * 1024);
397
398        if let Some(log_level) = args.log_level {
399            // Levels higher than [LogLevel::Notice] require libmdbx built with `MDBX_DEBUG` option.
400            let is_log_level_available = if cfg!(debug_assertions) {
401                true
402            } else {
403                matches!(
404                    log_level,
405                    LogLevel::Fatal | LogLevel::Error | LogLevel::Warn | LogLevel::Notice
406                )
407            };
408            if is_log_level_available {
409                inner_env.set_log_level(match log_level {
410                    LogLevel::Fatal => 0,
411                    LogLevel::Error => 1,
412                    LogLevel::Warn => 2,
413                    LogLevel::Notice => 3,
414                    LogLevel::Verbose => 4,
415                    LogLevel::Debug => 5,
416                    LogLevel::Trace => 6,
417                    LogLevel::Extra => 7,
418                });
419            } else {
420                return Err(DatabaseError::LogLevelUnavailable(log_level))
421            }
422        }
423
424        if let Some(max_read_transaction_duration) = args.max_read_transaction_duration {
425            inner_env.set_max_read_transaction_duration(max_read_transaction_duration);
426        }
427
428        let env = Self {
429            inner: inner_env.open(path).map_err(|e| DatabaseError::Open(e.into()))?,
430            metrics: None,
431            _lock_file,
432        };
433
434        Ok(env)
435    }
436
437    /// Enables metrics on the database.
438    pub fn with_metrics(mut self) -> Self {
439        self.metrics = Some(DatabaseEnvMetrics::new().into());
440        self
441    }
442
443    /// Creates all the tables defined in [`Tables`], if necessary.
444    pub fn create_tables(&self) -> Result<(), DatabaseError> {
445        self.create_tables_for::<Tables>()
446    }
447
448    /// Creates all the tables defined in the given [`TableSet`], if necessary.
449    pub fn create_tables_for<TS: TableSet>(&self) -> Result<(), DatabaseError> {
450        let tx = self.inner.begin_rw_txn().map_err(|e| DatabaseError::InitTx(e.into()))?;
451
452        for table in TS::tables() {
453            let flags =
454                if table.is_dupsort() { DatabaseFlags::DUP_SORT } else { DatabaseFlags::default() };
455
456            tx.create_db(Some(table.name()), flags)
457                .map_err(|e| DatabaseError::CreateTable(e.into()))?;
458        }
459
460        tx.commit().map_err(|e| DatabaseError::Commit(e.into()))?;
461
462        Ok(())
463    }
464
465    /// Records version that accesses the database with write privileges.
466    pub fn record_client_version(&self, version: ClientVersion) -> Result<(), DatabaseError> {
467        if version.is_empty() {
468            return Ok(())
469        }
470
471        let tx = self.tx_mut()?;
472        let mut version_cursor = tx.cursor_write::<tables::VersionHistory>()?;
473
474        let last_version = version_cursor.last()?.map(|(_, v)| v);
475        if Some(&version) != last_version.as_ref() {
476            version_cursor.upsert(
477                SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs(),
478                &version,
479            )?;
480            tx.commit()?;
481        }
482
483        Ok(())
484    }
485}
486
487impl Deref for DatabaseEnv {
488    type Target = Environment;
489
490    fn deref(&self) -> &Self::Target {
491        &self.inner
492    }
493}
494
495#[cfg(test)]
496mod tests {
497    use super::*;
498    use crate::{
499        tables::{
500            AccountsHistory, CanonicalHeaders, Headers, PlainAccountState, PlainStorageState,
501        },
502        test_utils::*,
503        AccountChangeSets,
504    };
505    use alloy_consensus::Header;
506    use alloy_primitives::{address, Address, B256, U256};
507    use reth_db_api::{
508        cursor::{DbDupCursorRO, DbDupCursorRW, ReverseWalker, Walker},
509        models::{AccountBeforeTx, IntegerList, ShardedKey},
510        table::{Encode, Table},
511    };
512    use reth_libmdbx::Error;
513    use reth_primitives_traits::{Account, StorageEntry};
514    use reth_storage_errors::db::{DatabaseWriteError, DatabaseWriteOperation};
515    use std::str::FromStr;
516    use tempfile::TempDir;
517
518    /// Create database for testing
519    fn create_test_db(kind: DatabaseEnvKind) -> Arc<DatabaseEnv> {
520        Arc::new(create_test_db_with_path(
521            kind,
522            &tempfile::TempDir::new().expect(ERROR_TEMPDIR).into_path(),
523        ))
524    }
525
526    /// Create database for testing with specified path
527    fn create_test_db_with_path(kind: DatabaseEnvKind, path: &Path) -> DatabaseEnv {
528        let env = DatabaseEnv::open(path, kind, DatabaseArguments::new(ClientVersion::default()))
529            .expect(ERROR_DB_CREATION);
530        env.create_tables().expect(ERROR_TABLE_CREATION);
531        env
532    }
533
534    const ERROR_DB_CREATION: &str = "Not able to create the mdbx file.";
535    const ERROR_PUT: &str = "Not able to insert value into table.";
536    const ERROR_APPEND: &str = "Not able to append the value to the table.";
537    const ERROR_UPSERT: &str = "Not able to upsert the value to the table.";
538    const ERROR_GET: &str = "Not able to get value from table.";
539    const ERROR_DEL: &str = "Not able to delete from table.";
540    const ERROR_COMMIT: &str = "Not able to commit transaction.";
541    const ERROR_RETURN_VALUE: &str = "Mismatching result.";
542    const ERROR_INIT_TX: &str = "Failed to create a MDBX transaction.";
543    const ERROR_ETH_ADDRESS: &str = "Invalid address.";
544
545    #[test]
546    fn db_creation() {
547        create_test_db(DatabaseEnvKind::RW);
548    }
549
550    #[test]
551    fn db_manual_put_get() {
552        let env = create_test_db(DatabaseEnvKind::RW);
553
554        let value = Header::default();
555        let key = 1u64;
556
557        // PUT
558        let tx = env.tx_mut().expect(ERROR_INIT_TX);
559        tx.put::<Headers>(key, value.clone()).expect(ERROR_PUT);
560        tx.commit().expect(ERROR_COMMIT);
561
562        // GET
563        let tx = env.tx().expect(ERROR_INIT_TX);
564        let result = tx.get::<Headers>(key).expect(ERROR_GET);
565        assert_eq!(result.expect(ERROR_RETURN_VALUE), value);
566        tx.commit().expect(ERROR_COMMIT);
567    }
568
569    #[test]
570    fn db_dup_cursor_delete_first() {
571        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
572        let tx = db.tx_mut().expect(ERROR_INIT_TX);
573
574        let mut dup_cursor = tx.cursor_dup_write::<PlainStorageState>().unwrap();
575
576        let entry_0 = StorageEntry { key: B256::with_last_byte(1), value: U256::from(0) };
577        let entry_1 = StorageEntry { key: B256::with_last_byte(1), value: U256::from(1) };
578
579        dup_cursor.upsert(Address::with_last_byte(1), &entry_0).expect(ERROR_UPSERT);
580        dup_cursor.upsert(Address::with_last_byte(1), &entry_1).expect(ERROR_UPSERT);
581
582        assert_eq!(
583            dup_cursor.walk(None).unwrap().collect::<Result<Vec<_>, _>>(),
584            Ok(vec![(Address::with_last_byte(1), entry_0), (Address::with_last_byte(1), entry_1),])
585        );
586
587        let mut walker = dup_cursor.walk(None).unwrap();
588        walker.delete_current().expect(ERROR_DEL);
589
590        assert_eq!(walker.next(), Some(Ok((Address::with_last_byte(1), entry_1))));
591
592        // Check the tx view - it correctly holds entry_1
593        assert_eq!(
594            tx.cursor_dup_read::<PlainStorageState>()
595                .unwrap()
596                .walk(None)
597                .unwrap()
598                .collect::<Result<Vec<_>, _>>(),
599            Ok(vec![
600                (Address::with_last_byte(1), entry_1), // This is ok - we removed entry_0
601            ])
602        );
603
604        // Check the remainder of walker
605        assert_eq!(walker.next(), None);
606    }
607
608    #[test]
609    fn db_cursor_walk() {
610        let env = create_test_db(DatabaseEnvKind::RW);
611
612        let value = Header::default();
613        let key = 1u64;
614
615        // PUT
616        let tx = env.tx_mut().expect(ERROR_INIT_TX);
617        tx.put::<Headers>(key, value.clone()).expect(ERROR_PUT);
618        tx.commit().expect(ERROR_COMMIT);
619
620        // Cursor
621        let tx = env.tx().expect(ERROR_INIT_TX);
622        let mut cursor = tx.cursor_read::<Headers>().unwrap();
623
624        let first = cursor.first().unwrap();
625        assert!(first.is_some(), "First should be our put");
626
627        // Walk
628        let walk = cursor.walk(Some(key)).unwrap();
629        let first = walk.into_iter().next().unwrap().unwrap();
630        assert_eq!(first.1, value, "First next should be put value");
631    }
632
633    #[test]
634    fn db_cursor_walk_range() {
635        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
636
637        // PUT (0, 0), (1, 0), (2, 0), (3, 0)
638        let tx = db.tx_mut().expect(ERROR_INIT_TX);
639        vec![0, 1, 2, 3]
640            .into_iter()
641            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
642            .expect(ERROR_PUT);
643        tx.commit().expect(ERROR_COMMIT);
644
645        let tx = db.tx().expect(ERROR_INIT_TX);
646        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
647
648        // [1, 3)
649        let mut walker = cursor.walk_range(1..3).unwrap();
650        assert_eq!(walker.next(), Some(Ok((1, B256::ZERO))));
651        assert_eq!(walker.next(), Some(Ok((2, B256::ZERO))));
652        assert_eq!(walker.next(), None);
653        // next() returns None after walker is done
654        assert_eq!(walker.next(), None);
655
656        // [1, 2]
657        let mut walker = cursor.walk_range(1..=2).unwrap();
658        assert_eq!(walker.next(), Some(Ok((1, B256::ZERO))));
659        assert_eq!(walker.next(), Some(Ok((2, B256::ZERO))));
660        // next() returns None after walker is done
661        assert_eq!(walker.next(), None);
662
663        // [1, ∞)
664        let mut walker = cursor.walk_range(1..).unwrap();
665        assert_eq!(walker.next(), Some(Ok((1, B256::ZERO))));
666        assert_eq!(walker.next(), Some(Ok((2, B256::ZERO))));
667        assert_eq!(walker.next(), Some(Ok((3, B256::ZERO))));
668        // next() returns None after walker is done
669        assert_eq!(walker.next(), None);
670
671        // [2, 4)
672        let mut walker = cursor.walk_range(2..4).unwrap();
673        assert_eq!(walker.next(), Some(Ok((2, B256::ZERO))));
674        assert_eq!(walker.next(), Some(Ok((3, B256::ZERO))));
675        assert_eq!(walker.next(), None);
676        // next() returns None after walker is done
677        assert_eq!(walker.next(), None);
678
679        // (∞, 3)
680        let mut walker = cursor.walk_range(..3).unwrap();
681        assert_eq!(walker.next(), Some(Ok((0, B256::ZERO))));
682        assert_eq!(walker.next(), Some(Ok((1, B256::ZERO))));
683        assert_eq!(walker.next(), Some(Ok((2, B256::ZERO))));
684        // next() returns None after walker is done
685        assert_eq!(walker.next(), None);
686
687        // (∞, ∞)
688        let mut walker = cursor.walk_range(..).unwrap();
689        assert_eq!(walker.next(), Some(Ok((0, B256::ZERO))));
690        assert_eq!(walker.next(), Some(Ok((1, B256::ZERO))));
691        assert_eq!(walker.next(), Some(Ok((2, B256::ZERO))));
692        assert_eq!(walker.next(), Some(Ok((3, B256::ZERO))));
693        // next() returns None after walker is done
694        assert_eq!(walker.next(), None);
695    }
696
697    #[test]
698    fn db_cursor_walk_range_on_dup_table() {
699        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
700
701        let address0 = Address::ZERO;
702        let address1 = Address::with_last_byte(1);
703        let address2 = Address::with_last_byte(2);
704
705        let tx = db.tx_mut().expect(ERROR_INIT_TX);
706        tx.put::<AccountChangeSets>(0, AccountBeforeTx { address: address0, info: None })
707            .expect(ERROR_PUT);
708        tx.put::<AccountChangeSets>(0, AccountBeforeTx { address: address1, info: None })
709            .expect(ERROR_PUT);
710        tx.put::<AccountChangeSets>(0, AccountBeforeTx { address: address2, info: None })
711            .expect(ERROR_PUT);
712        tx.put::<AccountChangeSets>(1, AccountBeforeTx { address: address0, info: None })
713            .expect(ERROR_PUT);
714        tx.put::<AccountChangeSets>(1, AccountBeforeTx { address: address1, info: None })
715            .expect(ERROR_PUT);
716        tx.put::<AccountChangeSets>(1, AccountBeforeTx { address: address2, info: None })
717            .expect(ERROR_PUT);
718        tx.put::<AccountChangeSets>(2, AccountBeforeTx { address: address0, info: None }) // <- should not be returned by the walker
719            .expect(ERROR_PUT);
720        tx.commit().expect(ERROR_COMMIT);
721
722        let tx = db.tx().expect(ERROR_INIT_TX);
723        let mut cursor = tx.cursor_read::<AccountChangeSets>().unwrap();
724
725        let entries = cursor.walk_range(..).unwrap().collect::<Result<Vec<_>, _>>().unwrap();
726        assert_eq!(entries.len(), 7);
727
728        let mut walker = cursor.walk_range(0..=1).unwrap();
729        assert_eq!(walker.next(), Some(Ok((0, AccountBeforeTx { address: address0, info: None }))));
730        assert_eq!(walker.next(), Some(Ok((0, AccountBeforeTx { address: address1, info: None }))));
731        assert_eq!(walker.next(), Some(Ok((0, AccountBeforeTx { address: address2, info: None }))));
732        assert_eq!(walker.next(), Some(Ok((1, AccountBeforeTx { address: address0, info: None }))));
733        assert_eq!(walker.next(), Some(Ok((1, AccountBeforeTx { address: address1, info: None }))));
734        assert_eq!(walker.next(), Some(Ok((1, AccountBeforeTx { address: address2, info: None }))));
735        assert_eq!(walker.next(), None);
736    }
737
738    #[allow(clippy::reversed_empty_ranges)]
739    #[test]
740    fn db_cursor_walk_range_invalid() {
741        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
742
743        // PUT (0, 0), (1, 0), (2, 0), (3, 0)
744        let tx = db.tx_mut().expect(ERROR_INIT_TX);
745        vec![0, 1, 2, 3]
746            .into_iter()
747            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
748            .expect(ERROR_PUT);
749        tx.commit().expect(ERROR_COMMIT);
750
751        let tx = db.tx().expect(ERROR_INIT_TX);
752        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
753
754        // start bound greater than end bound
755        let mut res = cursor.walk_range(3..1).unwrap();
756        assert_eq!(res.next(), None);
757
758        // start bound greater than end bound
759        let mut res = cursor.walk_range(15..=2).unwrap();
760        assert_eq!(res.next(), None);
761
762        // returning nothing
763        let mut walker = cursor.walk_range(1..1).unwrap();
764        assert_eq!(walker.next(), None);
765    }
766
767    #[test]
768    fn db_walker() {
769        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
770
771        // PUT (0, 0), (1, 0), (3, 0)
772        let tx = db.tx_mut().expect(ERROR_INIT_TX);
773        vec![0, 1, 3]
774            .into_iter()
775            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
776            .expect(ERROR_PUT);
777        tx.commit().expect(ERROR_COMMIT);
778
779        let tx = db.tx().expect(ERROR_INIT_TX);
780        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
781
782        let mut walker = Walker::new(&mut cursor, None);
783
784        assert_eq!(walker.next(), Some(Ok((0, B256::ZERO))));
785        assert_eq!(walker.next(), Some(Ok((1, B256::ZERO))));
786        assert_eq!(walker.next(), Some(Ok((3, B256::ZERO))));
787        assert_eq!(walker.next(), None);
788
789        // transform to ReverseWalker
790        let mut reverse_walker = walker.rev();
791        assert_eq!(reverse_walker.next(), Some(Ok((3, B256::ZERO))));
792        assert_eq!(reverse_walker.next(), Some(Ok((1, B256::ZERO))));
793        assert_eq!(reverse_walker.next(), Some(Ok((0, B256::ZERO))));
794        assert_eq!(reverse_walker.next(), None);
795    }
796
797    #[test]
798    fn db_reverse_walker() {
799        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
800
801        // PUT (0, 0), (1, 0), (3, 0)
802        let tx = db.tx_mut().expect(ERROR_INIT_TX);
803        vec![0, 1, 3]
804            .into_iter()
805            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
806            .expect(ERROR_PUT);
807        tx.commit().expect(ERROR_COMMIT);
808
809        let tx = db.tx().expect(ERROR_INIT_TX);
810        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
811
812        let mut reverse_walker = ReverseWalker::new(&mut cursor, None);
813
814        assert_eq!(reverse_walker.next(), Some(Ok((3, B256::ZERO))));
815        assert_eq!(reverse_walker.next(), Some(Ok((1, B256::ZERO))));
816        assert_eq!(reverse_walker.next(), Some(Ok((0, B256::ZERO))));
817        assert_eq!(reverse_walker.next(), None);
818
819        // transform to Walker
820        let mut walker = reverse_walker.forward();
821        assert_eq!(walker.next(), Some(Ok((0, B256::ZERO))));
822        assert_eq!(walker.next(), Some(Ok((1, B256::ZERO))));
823        assert_eq!(walker.next(), Some(Ok((3, B256::ZERO))));
824        assert_eq!(walker.next(), None);
825    }
826
827    #[test]
828    fn db_walk_back() {
829        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
830
831        // PUT (0, 0), (1, 0), (3, 0)
832        let tx = db.tx_mut().expect(ERROR_INIT_TX);
833        vec![0, 1, 3]
834            .into_iter()
835            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
836            .expect(ERROR_PUT);
837        tx.commit().expect(ERROR_COMMIT);
838
839        let tx = db.tx().expect(ERROR_INIT_TX);
840        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
841
842        let mut reverse_walker = cursor.walk_back(Some(1)).unwrap();
843        assert_eq!(reverse_walker.next(), Some(Ok((1, B256::ZERO))));
844        assert_eq!(reverse_walker.next(), Some(Ok((0, B256::ZERO))));
845        assert_eq!(reverse_walker.next(), None);
846
847        let mut reverse_walker = cursor.walk_back(Some(2)).unwrap();
848        assert_eq!(reverse_walker.next(), Some(Ok((3, B256::ZERO))));
849        assert_eq!(reverse_walker.next(), Some(Ok((1, B256::ZERO))));
850        assert_eq!(reverse_walker.next(), Some(Ok((0, B256::ZERO))));
851        assert_eq!(reverse_walker.next(), None);
852
853        let mut reverse_walker = cursor.walk_back(Some(4)).unwrap();
854        assert_eq!(reverse_walker.next(), Some(Ok((3, B256::ZERO))));
855        assert_eq!(reverse_walker.next(), Some(Ok((1, B256::ZERO))));
856        assert_eq!(reverse_walker.next(), Some(Ok((0, B256::ZERO))));
857        assert_eq!(reverse_walker.next(), None);
858
859        let mut reverse_walker = cursor.walk_back(None).unwrap();
860        assert_eq!(reverse_walker.next(), Some(Ok((3, B256::ZERO))));
861        assert_eq!(reverse_walker.next(), Some(Ok((1, B256::ZERO))));
862        assert_eq!(reverse_walker.next(), Some(Ok((0, B256::ZERO))));
863        assert_eq!(reverse_walker.next(), None);
864    }
865
866    #[test]
867    fn db_cursor_seek_exact_or_previous_key() {
868        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
869
870        // PUT
871        let tx = db.tx_mut().expect(ERROR_INIT_TX);
872        vec![0, 1, 3]
873            .into_iter()
874            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
875            .expect(ERROR_PUT);
876        tx.commit().expect(ERROR_COMMIT);
877
878        // Cursor
879        let missing_key = 2;
880        let tx = db.tx().expect(ERROR_INIT_TX);
881        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
882        assert_eq!(cursor.current(), Ok(None));
883
884        // Seek exact
885        let exact = cursor.seek_exact(missing_key).unwrap();
886        assert_eq!(exact, None);
887        assert_eq!(cursor.current(), Ok(Some((missing_key + 1, B256::ZERO))));
888        assert_eq!(cursor.prev(), Ok(Some((missing_key - 1, B256::ZERO))));
889        assert_eq!(cursor.prev(), Ok(Some((missing_key - 2, B256::ZERO))));
890    }
891
892    #[test]
893    fn db_cursor_insert() {
894        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
895
896        // PUT
897        let tx = db.tx_mut().expect(ERROR_INIT_TX);
898        vec![0, 1, 3, 4, 5]
899            .into_iter()
900            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
901            .expect(ERROR_PUT);
902        tx.commit().expect(ERROR_COMMIT);
903
904        let key_to_insert = 2;
905        let tx = db.tx_mut().expect(ERROR_INIT_TX);
906        let mut cursor = tx.cursor_write::<CanonicalHeaders>().unwrap();
907
908        // INSERT
909        assert_eq!(cursor.insert(key_to_insert, &B256::ZERO), Ok(()));
910        assert_eq!(cursor.current(), Ok(Some((key_to_insert, B256::ZERO))));
911
912        // INSERT (failure)
913        assert_eq!(
914            cursor.insert(key_to_insert, &B256::ZERO),
915            Err(DatabaseWriteError {
916                info: Error::KeyExist.into(),
917                operation: DatabaseWriteOperation::CursorInsert,
918                table_name: CanonicalHeaders::NAME,
919                key: key_to_insert.encode().into(),
920            }
921            .into())
922        );
923        assert_eq!(cursor.current(), Ok(Some((key_to_insert, B256::ZERO))));
924
925        tx.commit().expect(ERROR_COMMIT);
926
927        // Confirm the result
928        let tx = db.tx().expect(ERROR_INIT_TX);
929        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
930        let res = cursor.walk(None).unwrap().map(|res| res.unwrap().0).collect::<Vec<_>>();
931        assert_eq!(res, vec![0, 1, 2, 3, 4, 5]);
932        tx.commit().expect(ERROR_COMMIT);
933    }
934
935    #[test]
936    fn db_cursor_insert_dup() {
937        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
938        let tx = db.tx_mut().expect(ERROR_INIT_TX);
939
940        let mut dup_cursor = tx.cursor_dup_write::<PlainStorageState>().unwrap();
941        let key = Address::random();
942        let subkey1 = B256::random();
943        let subkey2 = B256::random();
944
945        let entry1 = StorageEntry { key: subkey1, value: U256::ZERO };
946        assert!(dup_cursor.insert(key, &entry1).is_ok());
947
948        // Can't insert
949        let entry2 = StorageEntry { key: subkey2, value: U256::ZERO };
950        assert!(dup_cursor.insert(key, &entry2).is_err());
951    }
952
953    #[test]
954    fn db_cursor_delete_current_non_existent() {
955        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
956        let tx = db.tx_mut().expect(ERROR_INIT_TX);
957
958        let key1 = Address::with_last_byte(1);
959        let key2 = Address::with_last_byte(2);
960        let key3 = Address::with_last_byte(3);
961        let mut cursor = tx.cursor_write::<PlainAccountState>().unwrap();
962
963        assert!(cursor.insert(key1, &Account::default()).is_ok());
964        assert!(cursor.insert(key2, &Account::default()).is_ok());
965        assert!(cursor.insert(key3, &Account::default()).is_ok());
966
967        // Seek & delete key2
968        cursor.seek_exact(key2).unwrap();
969        assert_eq!(cursor.delete_current(), Ok(()));
970        assert_eq!(cursor.seek_exact(key2), Ok(None));
971
972        // Seek & delete key2 again
973        assert_eq!(cursor.seek_exact(key2), Ok(None));
974        assert_eq!(cursor.delete_current(), Ok(()));
975        // Assert that key1 is still there
976        assert_eq!(cursor.seek_exact(key1), Ok(Some((key1, Account::default()))));
977        // Assert that key3 was deleted
978        assert_eq!(cursor.seek_exact(key3), Ok(None));
979    }
980
981    #[test]
982    fn db_cursor_insert_wherever_cursor_is() {
983        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
984        let tx = db.tx_mut().expect(ERROR_INIT_TX);
985
986        // PUT
987        vec![0, 1, 3, 5, 7, 9]
988            .into_iter()
989            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
990            .expect(ERROR_PUT);
991        tx.commit().expect(ERROR_COMMIT);
992
993        let tx = db.tx_mut().expect(ERROR_INIT_TX);
994        let mut cursor = tx.cursor_write::<CanonicalHeaders>().unwrap();
995
996        // INSERT (cursor starts at last)
997        cursor.last().unwrap();
998        assert_eq!(cursor.current(), Ok(Some((9, B256::ZERO))));
999
1000        for pos in (2..=8).step_by(2) {
1001            assert_eq!(cursor.insert(pos, &B256::ZERO), Ok(()));
1002            assert_eq!(cursor.current(), Ok(Some((pos, B256::ZERO))));
1003        }
1004        tx.commit().expect(ERROR_COMMIT);
1005
1006        // Confirm the result
1007        let tx = db.tx().expect(ERROR_INIT_TX);
1008        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
1009        let res = cursor.walk(None).unwrap().map(|res| res.unwrap().0).collect::<Vec<_>>();
1010        assert_eq!(res, vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
1011        tx.commit().expect(ERROR_COMMIT);
1012    }
1013
1014    #[test]
1015    fn db_cursor_append() {
1016        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
1017
1018        // PUT
1019        let tx = db.tx_mut().expect(ERROR_INIT_TX);
1020        vec![0, 1, 2, 3, 4]
1021            .into_iter()
1022            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
1023            .expect(ERROR_PUT);
1024        tx.commit().expect(ERROR_COMMIT);
1025
1026        // APPEND
1027        let key_to_append = 5;
1028        let tx = db.tx_mut().expect(ERROR_INIT_TX);
1029        let mut cursor = tx.cursor_write::<CanonicalHeaders>().unwrap();
1030        assert_eq!(cursor.append(key_to_append, &B256::ZERO), Ok(()));
1031        tx.commit().expect(ERROR_COMMIT);
1032
1033        // Confirm the result
1034        let tx = db.tx().expect(ERROR_INIT_TX);
1035        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
1036        let res = cursor.walk(None).unwrap().map(|res| res.unwrap().0).collect::<Vec<_>>();
1037        assert_eq!(res, vec![0, 1, 2, 3, 4, 5]);
1038        tx.commit().expect(ERROR_COMMIT);
1039    }
1040
1041    #[test]
1042    fn db_cursor_append_failure() {
1043        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
1044
1045        // PUT
1046        let tx = db.tx_mut().expect(ERROR_INIT_TX);
1047        vec![0, 1, 3, 4, 5]
1048            .into_iter()
1049            .try_for_each(|key| tx.put::<CanonicalHeaders>(key, B256::ZERO))
1050            .expect(ERROR_PUT);
1051        tx.commit().expect(ERROR_COMMIT);
1052
1053        // APPEND
1054        let key_to_append = 2;
1055        let tx = db.tx_mut().expect(ERROR_INIT_TX);
1056        let mut cursor = tx.cursor_write::<CanonicalHeaders>().unwrap();
1057        assert_eq!(
1058            cursor.append(key_to_append, &B256::ZERO),
1059            Err(DatabaseWriteError {
1060                info: Error::KeyMismatch.into(),
1061                operation: DatabaseWriteOperation::CursorAppend,
1062                table_name: CanonicalHeaders::NAME,
1063                key: key_to_append.encode().into(),
1064            }
1065            .into())
1066        );
1067        assert_eq!(cursor.current(), Ok(Some((5, B256::ZERO)))); // the end of table
1068        tx.commit().expect(ERROR_COMMIT);
1069
1070        // Confirm the result
1071        let tx = db.tx().expect(ERROR_INIT_TX);
1072        let mut cursor = tx.cursor_read::<CanonicalHeaders>().unwrap();
1073        let res = cursor.walk(None).unwrap().map(|res| res.unwrap().0).collect::<Vec<_>>();
1074        assert_eq!(res, vec![0, 1, 3, 4, 5]);
1075        tx.commit().expect(ERROR_COMMIT);
1076    }
1077
1078    #[test]
1079    fn db_cursor_upsert() {
1080        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
1081        let tx = db.tx_mut().expect(ERROR_INIT_TX);
1082
1083        let mut cursor = tx.cursor_write::<PlainAccountState>().unwrap();
1084        let key = Address::random();
1085
1086        let account = Account::default();
1087        cursor.upsert(key, &account).expect(ERROR_UPSERT);
1088        assert_eq!(cursor.seek_exact(key), Ok(Some((key, account))));
1089
1090        let account = Account { nonce: 1, ..Default::default() };
1091        cursor.upsert(key, &account).expect(ERROR_UPSERT);
1092        assert_eq!(cursor.seek_exact(key), Ok(Some((key, account))));
1093
1094        let account = Account { nonce: 2, ..Default::default() };
1095        cursor.upsert(key, &account).expect(ERROR_UPSERT);
1096        assert_eq!(cursor.seek_exact(key), Ok(Some((key, account))));
1097
1098        let mut dup_cursor = tx.cursor_dup_write::<PlainStorageState>().unwrap();
1099        let subkey = B256::random();
1100
1101        let value = U256::from(1);
1102        let entry1 = StorageEntry { key: subkey, value };
1103        dup_cursor.upsert(key, &entry1).expect(ERROR_UPSERT);
1104        assert_eq!(dup_cursor.seek_by_key_subkey(key, subkey), Ok(Some(entry1)));
1105
1106        let value = U256::from(2);
1107        let entry2 = StorageEntry { key: subkey, value };
1108        dup_cursor.upsert(key, &entry2).expect(ERROR_UPSERT);
1109        assert_eq!(dup_cursor.seek_by_key_subkey(key, subkey), Ok(Some(entry1)));
1110        assert_eq!(dup_cursor.next_dup_val(), Ok(Some(entry2)));
1111    }
1112
1113    #[test]
1114    fn db_cursor_dupsort_append() {
1115        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
1116
1117        let transition_id = 2;
1118
1119        let tx = db.tx_mut().expect(ERROR_INIT_TX);
1120        let mut cursor = tx.cursor_write::<AccountChangeSets>().unwrap();
1121        vec![0, 1, 3, 4, 5]
1122            .into_iter()
1123            .try_for_each(|val| {
1124                cursor.append(
1125                    transition_id,
1126                    &AccountBeforeTx { address: Address::with_last_byte(val), info: None },
1127                )
1128            })
1129            .expect(ERROR_APPEND);
1130        tx.commit().expect(ERROR_COMMIT);
1131
1132        // APPEND DUP & APPEND
1133        let subkey_to_append = 2;
1134        let tx = db.tx_mut().expect(ERROR_INIT_TX);
1135        let mut cursor = tx.cursor_write::<AccountChangeSets>().unwrap();
1136        assert_eq!(
1137            cursor.append_dup(
1138                transition_id,
1139                AccountBeforeTx { address: Address::with_last_byte(subkey_to_append), info: None }
1140            ),
1141            Err(DatabaseWriteError {
1142                info: Error::KeyMismatch.into(),
1143                operation: DatabaseWriteOperation::CursorAppendDup,
1144                table_name: AccountChangeSets::NAME,
1145                key: transition_id.encode().into(),
1146            }
1147            .into())
1148        );
1149        assert_eq!(
1150            cursor.append(
1151                transition_id - 1,
1152                &AccountBeforeTx { address: Address::with_last_byte(subkey_to_append), info: None }
1153            ),
1154            Err(DatabaseWriteError {
1155                info: Error::KeyMismatch.into(),
1156                operation: DatabaseWriteOperation::CursorAppend,
1157                table_name: AccountChangeSets::NAME,
1158                key: (transition_id - 1).encode().into(),
1159            }
1160            .into())
1161        );
1162        assert_eq!(
1163            cursor.append(
1164                transition_id,
1165                &AccountBeforeTx { address: Address::with_last_byte(subkey_to_append), info: None }
1166            ),
1167            Ok(())
1168        );
1169    }
1170
1171    #[test]
1172    fn db_closure_put_get() {
1173        let path = TempDir::new().expect(ERROR_TEMPDIR).into_path();
1174
1175        let value = Account {
1176            nonce: 18446744073709551615,
1177            bytecode_hash: Some(B256::random()),
1178            balance: U256::MAX,
1179        };
1180        let key = Address::from_str("0xa2c122be93b0074270ebee7f6b7292c7deb45047")
1181            .expect(ERROR_ETH_ADDRESS);
1182
1183        {
1184            let env = create_test_db_with_path(DatabaseEnvKind::RW, &path);
1185
1186            // PUT
1187            let result = env.update(|tx| {
1188                tx.put::<PlainAccountState>(key, value).expect(ERROR_PUT);
1189                200
1190            });
1191            assert_eq!(result.expect(ERROR_RETURN_VALUE), 200);
1192        }
1193
1194        let env = DatabaseEnv::open(
1195            &path,
1196            DatabaseEnvKind::RO,
1197            DatabaseArguments::new(ClientVersion::default()),
1198        )
1199        .expect(ERROR_DB_CREATION);
1200
1201        // GET
1202        let result =
1203            env.view(|tx| tx.get::<PlainAccountState>(key).expect(ERROR_GET)).expect(ERROR_GET);
1204
1205        assert_eq!(result, Some(value))
1206    }
1207
1208    #[test]
1209    fn db_dup_sort() {
1210        let env = create_test_db(DatabaseEnvKind::RW);
1211        let key = Address::from_str("0xa2c122be93b0074270ebee7f6b7292c7deb45047")
1212            .expect(ERROR_ETH_ADDRESS);
1213
1214        // PUT (0,0)
1215        let value00 = StorageEntry::default();
1216        env.update(|tx| tx.put::<PlainStorageState>(key, value00).expect(ERROR_PUT)).unwrap();
1217
1218        // PUT (2,2)
1219        let value22 = StorageEntry { key: B256::with_last_byte(2), value: U256::from(2) };
1220        env.update(|tx| tx.put::<PlainStorageState>(key, value22).expect(ERROR_PUT)).unwrap();
1221
1222        // PUT (1,1)
1223        let value11 = StorageEntry { key: B256::with_last_byte(1), value: U256::from(1) };
1224        env.update(|tx| tx.put::<PlainStorageState>(key, value11).expect(ERROR_PUT)).unwrap();
1225
1226        // Iterate with cursor
1227        {
1228            let tx = env.tx().expect(ERROR_INIT_TX);
1229            let mut cursor = tx.cursor_dup_read::<PlainStorageState>().unwrap();
1230
1231            // Notice that value11 and value22 have been ordered in the DB.
1232            assert_eq!(Some(value00), cursor.next_dup_val().unwrap());
1233            assert_eq!(Some(value11), cursor.next_dup_val().unwrap());
1234            assert_eq!(Some(value22), cursor.next_dup_val().unwrap());
1235        }
1236
1237        // Seek value with exact subkey
1238        {
1239            let tx = env.tx().expect(ERROR_INIT_TX);
1240            let mut cursor = tx.cursor_dup_read::<PlainStorageState>().unwrap();
1241            let mut walker = cursor.walk_dup(Some(key), Some(B256::with_last_byte(1))).unwrap();
1242            assert_eq!(
1243                (key, value11),
1244                walker
1245                    .next()
1246                    .expect("element should exist.")
1247                    .expect("should be able to retrieve it.")
1248            );
1249        }
1250    }
1251
1252    #[test]
1253    fn db_iterate_over_all_dup_values() {
1254        let env = create_test_db(DatabaseEnvKind::RW);
1255        let key1 = Address::from_str("0x1111111111111111111111111111111111111111")
1256            .expect(ERROR_ETH_ADDRESS);
1257        let key2 = Address::from_str("0x2222222222222222222222222222222222222222")
1258            .expect(ERROR_ETH_ADDRESS);
1259
1260        // PUT key1 (0,0)
1261        let value00 = StorageEntry::default();
1262        env.update(|tx| tx.put::<PlainStorageState>(key1, value00).expect(ERROR_PUT)).unwrap();
1263
1264        // PUT key1 (1,1)
1265        let value11 = StorageEntry { key: B256::with_last_byte(1), value: U256::from(1) };
1266        env.update(|tx| tx.put::<PlainStorageState>(key1, value11).expect(ERROR_PUT)).unwrap();
1267
1268        // PUT key2 (2,2)
1269        let value22 = StorageEntry { key: B256::with_last_byte(2), value: U256::from(2) };
1270        env.update(|tx| tx.put::<PlainStorageState>(key2, value22).expect(ERROR_PUT)).unwrap();
1271
1272        // Iterate with walk_dup
1273        {
1274            let tx = env.tx().expect(ERROR_INIT_TX);
1275            let mut cursor = tx.cursor_dup_read::<PlainStorageState>().unwrap();
1276            let mut walker = cursor.walk_dup(None, None).unwrap();
1277
1278            // Notice that value11 and value22 have been ordered in the DB.
1279            assert_eq!(Some(Ok((key1, value00))), walker.next());
1280            assert_eq!(Some(Ok((key1, value11))), walker.next());
1281            // NOTE: Dup cursor does NOT iterates on all values but only on duplicated values of the
1282            // same key. assert_eq!(Ok(Some(value22.clone())), walker.next());
1283            assert_eq!(None, walker.next());
1284        }
1285
1286        // Iterate by using `walk`
1287        {
1288            let tx = env.tx().expect(ERROR_INIT_TX);
1289            let mut cursor = tx.cursor_dup_read::<PlainStorageState>().unwrap();
1290            let first = cursor.first().unwrap().unwrap();
1291            let mut walker = cursor.walk(Some(first.0)).unwrap();
1292            assert_eq!(Some(Ok((key1, value00))), walker.next());
1293            assert_eq!(Some(Ok((key1, value11))), walker.next());
1294            assert_eq!(Some(Ok((key2, value22))), walker.next());
1295        }
1296    }
1297
1298    #[test]
1299    fn dup_value_with_same_subkey() {
1300        let env = create_test_db(DatabaseEnvKind::RW);
1301        let key1 = Address::new([0x11; 20]);
1302        let key2 = Address::new([0x22; 20]);
1303
1304        // PUT key1 (0,1)
1305        let value01 = StorageEntry { key: B256::with_last_byte(0), value: U256::from(1) };
1306        env.update(|tx| tx.put::<PlainStorageState>(key1, value01).expect(ERROR_PUT)).unwrap();
1307
1308        // PUT key1 (0,0)
1309        let value00 = StorageEntry::default();
1310        env.update(|tx| tx.put::<PlainStorageState>(key1, value00).expect(ERROR_PUT)).unwrap();
1311
1312        // PUT key2 (2,2)
1313        let value22 = StorageEntry { key: B256::with_last_byte(2), value: U256::from(2) };
1314        env.update(|tx| tx.put::<PlainStorageState>(key2, value22).expect(ERROR_PUT)).unwrap();
1315
1316        // Iterate with walk
1317        {
1318            let tx = env.tx().expect(ERROR_INIT_TX);
1319            let mut cursor = tx.cursor_dup_read::<PlainStorageState>().unwrap();
1320            let first = cursor.first().unwrap().unwrap();
1321            let mut walker = cursor.walk(Some(first.0)).unwrap();
1322
1323            // NOTE: Both values are present
1324            assert_eq!(Some(Ok((key1, value00))), walker.next());
1325            assert_eq!(Some(Ok((key1, value01))), walker.next());
1326            assert_eq!(Some(Ok((key2, value22))), walker.next());
1327        }
1328
1329        // seek_by_key_subkey
1330        {
1331            let tx = env.tx().expect(ERROR_INIT_TX);
1332            let mut cursor = tx.cursor_dup_read::<PlainStorageState>().unwrap();
1333
1334            // NOTE: There are two values with same SubKey but only first one is shown
1335            assert_eq!(Ok(Some(value00)), cursor.seek_by_key_subkey(key1, value00.key));
1336            // key1 but value is greater than the one in the DB
1337            assert_eq!(Ok(None), cursor.seek_by_key_subkey(key1, value22.key));
1338        }
1339    }
1340
1341    #[test]
1342    fn db_sharded_key() {
1343        let db: Arc<DatabaseEnv> = create_test_db(DatabaseEnvKind::RW);
1344        let real_key = address!("0xa2c122be93b0074270ebee7f6b7292c7deb45047");
1345
1346        for i in 1..5 {
1347            let key = ShardedKey::new(real_key, i * 100);
1348            let list = IntegerList::new_pre_sorted([i * 100u64]);
1349
1350            db.update(|tx| tx.put::<AccountsHistory>(key.clone(), list.clone()).expect(""))
1351                .unwrap();
1352        }
1353
1354        // Seek value with non existing key.
1355        {
1356            let tx = db.tx().expect(ERROR_INIT_TX);
1357            let mut cursor = tx.cursor_read::<AccountsHistory>().unwrap();
1358
1359            // It will seek the one greater or equal to the query. Since we have `Address | 100`,
1360            // `Address | 200` in the database and we're querying `Address | 150` it will return us
1361            // `Address | 200`.
1362            let mut walker = cursor.walk(Some(ShardedKey::new(real_key, 150))).unwrap();
1363            let (key, list) = walker
1364                .next()
1365                .expect("element should exist.")
1366                .expect("should be able to retrieve it.");
1367
1368            assert_eq!(ShardedKey::new(real_key, 200), key);
1369            let list200 = IntegerList::new_pre_sorted([200u64]);
1370            assert_eq!(list200, list);
1371        }
1372        // Seek greatest index
1373        {
1374            let tx = db.tx().expect(ERROR_INIT_TX);
1375            let mut cursor = tx.cursor_read::<AccountsHistory>().unwrap();
1376
1377            // It will seek the MAX value of transition index and try to use prev to get first
1378            // biggers.
1379            let _unknown = cursor.seek_exact(ShardedKey::new(real_key, u64::MAX)).unwrap();
1380            let (key, list) = cursor
1381                .prev()
1382                .expect("element should exist.")
1383                .expect("should be able to retrieve it.");
1384
1385            assert_eq!(ShardedKey::new(real_key, 400), key);
1386            let list400 = IntegerList::new_pre_sorted([400u64]);
1387            assert_eq!(list400, list);
1388        }
1389    }
1390}