cnidarium/write_batch.rs
1use std::sync::Arc;
2
3// HashMap is okay here because we don't care about ordering of substore roots.
4use std::collections::HashMap;
5
6use crate::{
7 cache::Cache,
8 store::{multistore, substore::SubstoreConfig},
9 RootHash,
10};
11
12/// A staged write batch that can be committed to RocksDB.
13///
14/// This allows for write batches to be prepared and committed at a later time.
15pub struct StagedWriteBatch {
16 /// The write batch to commit to RocksDB.
17 pub(crate) write_batch: rocksdb::WriteBatch,
18 /// The new version of the chain state.
19 pub(crate) version: jmt::Version,
20 /// The new versions of each substore.
21 pub(crate) multistore_versions: multistore::MultistoreCache,
22 /// The root hash of the chain state corresponding to this set of changes.
23 pub(crate) root_hash: RootHash,
24 /// The configs, root hashes, and new versions of each substore
25 /// that was updated in this batch.
26 #[allow(clippy::disallowed_types)]
27 pub(crate) substore_roots: HashMap<Arc<SubstoreConfig>, (RootHash, u64)>,
28 /// Whether or not to perform a migration.
29 pub(crate) perform_migration: bool,
30 /// A lightweight copy of the changeset, this is useful to provide
31 /// a stream of changes to subscribers.
32 pub(crate) changes: Arc<Cache>,
33}
34
35impl StagedWriteBatch {
36 /// Returns the new version of the chain state corresponding to this set of changes.
37 pub fn version(&self) -> jmt::Version {
38 self.version
39 }
40
41 /// Returns the root hash of the jmt corresponding to this set of changes.
42 pub fn root_hash(&self) -> &RootHash {
43 &self.root_hash
44 }
45
46 /// Returns the version of a substore in this batch, if it exists
47 /// and `None` otherwise.
48 pub fn substore_version(&self, prefix: &str) -> Option<jmt::Version> {
49 let Some(substore_config) = self
50 .multistore_versions
51 .config
52 .find_substore(prefix.as_bytes())
53 else {
54 return None;
55 };
56
57 self.multistore_versions.get_version(&substore_config)
58 }
59}