Skip to main content

sui_core/execution_cache/
writeback_cache.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! MemoryCache is a cache for the transaction execution which delays writes to the database until
5//! transaction results are certified (i.e. they appear in a certified checkpoint, or an effects cert
6//! is observed by a fullnode). The cache also stores committed data in memory in order to serve
7//! future reads without hitting the database.
8//!
9//! For storing uncommitted transaction outputs, we cannot evict the data at all until it is written
10//! to disk. Committed data not only can be evicted, but it is also unbounded (imagine a stream of
11//! transactions that keep splitting a coin into smaller coins).
12//!
13//! We also want to be able to support negative cache hits (i.e. the case where we can determine an
14//! object does not exist without hitting the database).
15//!
16//! To achieve both of these goals, we split the cache data into two pieces, a dirty set and a cached
17//! set. The dirty set has no automatic evictions, data is only removed after being committed. The
18//! cached set is in a bounded-sized cache with automatic evictions. In order to support negative
19//! cache hits, we treat the two halves of the cache as FIFO queue. Newly written (dirty) versions are
20//! inserted to one end of the dirty queue. As versions are committed to disk, they are
21//! removed from the other end of the dirty queue and inserted into the cache queue. The cache queue
22//! is truncated if it exceeds its maximum size, by removing all but the N newest versions.
23//!
24//! This gives us the property that the sequence of versions in the dirty and cached queues are the
25//! most recent versions of the object, i.e. there can be no "gaps". This allows for the following:
26//!
27//!   - Negative cache hits: If the queried version is not in memory, but is higher than the smallest
28//!     version in the cached queue, it does not exist in the db either.
29//!   - Bounded reads: When reading the most recent version that is <= some version bound, we can
30//!     correctly satisfy this query from the cache, or determine that we must go to the db.
31//!
32//! Note that at any time, either or both the dirty or the cached queue may be non-existent. There may be no
33//! dirty versions of the objects, in which case there will be no dirty queue. And, the cached queue
34//! may be evicted from the cache, in which case there will be no cached queue. Because only the cached
35//! queue can be evicted (the dirty queue can only become empty by moving versions from it to the cached
36//! queue), the "highest versions" property still holds in all cases.
37//!
38//! The above design is used for both objects and markers.
39
40use crate::accumulators::funds_read::AccountFundsRead;
41use crate::authority::AuthorityStore;
42use crate::authority::authority_per_epoch_store::AuthorityPerEpochStore;
43use crate::authority::authority_store::ExecutionLockWriteGuard;
44#[cfg(test)]
45use crate::authority::authority_store::{LockDetailsDeprecated, ObjectLockStatus, SuiLockResult};
46use crate::authority::authority_store_tables::LiveObject;
47use crate::authority::backpressure::BackpressureManager;
48use crate::authority::epoch_start_configuration::{EpochFlag, EpochStartConfiguration};
49use crate::fallback_fetch::do_fallback_lookup;
50use crate::global_state_hasher::GlobalStateHashStore;
51use crate::transaction_outputs::TransactionOutputs;
52
53use dashmap::DashMap;
54use dashmap::mapref::entry::Entry as DashMapEntry;
55use futures::{FutureExt, future::BoxFuture};
56use moka::sync::SegmentedCache as MokaCache;
57use mysten_common::ZipDebugEqIteratorExt;
58use mysten_common::debug_fatal;
59use mysten_common::random_util::randomize_cache_capacity_in_tests;
60use mysten_common::sync::notify_read::NotifyRead;
61use parking_lot::Mutex;
62use rayon::prelude::*;
63use std::collections::{BTreeMap, HashSet};
64use std::hash::Hash;
65use std::sync::Arc;
66use std::sync::atomic::AtomicU64;
67use std::time::Instant;
68use sui_config::ExecutionCacheConfig;
69use sui_macros::fail_point;
70use sui_protocol_config::ProtocolVersion;
71use sui_types::SUI_ACCUMULATOR_ROOT_OBJECT_ID;
72use sui_types::accumulator_event::AccumulatorEvent;
73use sui_types::accumulator_root::{AccumulatorObjId, AccumulatorValue};
74use sui_types::base_types::{
75    ConsensusObjectVersion, EpochId, FullObjectID, ObjectID, ObjectRef, SequenceNumber,
76    VerifiedExecutionData,
77};
78use sui_types::bridge::{Bridge, get_bridge};
79use sui_types::digests::{ObjectDigest, TransactionDigest, TransactionEffectsDigest};
80use sui_types::effects::{TransactionEffects, TransactionEvents};
81#[cfg(test)]
82use sui_types::error::SuiError;
83use sui_types::error::{SuiErrorKind, SuiResult, UserInputError};
84use sui_types::executable_transaction::VerifiedExecutableTransaction;
85use sui_types::global_state_hash::GlobalStateHash;
86use sui_types::message_envelope::Message;
87use sui_types::messages_checkpoint::CheckpointSequenceNumber;
88use sui_types::object::Object;
89use sui_types::storage::{
90    FullObjectKey, InputKey, MarkerValue, ObjectKey, ObjectOrTombstone, ObjectStore, PackageObject,
91};
92use sui_types::sui_system_state::{SuiSystemState, get_sui_system_state};
93use sui_types::transaction::{TransactionDataAPI, VerifiedTransaction};
94use tap::TapOptional;
95use tracing::{debug, info, instrument, trace, warn};
96
97use super::ExecutionCacheAPI;
98use super::FoundObjectOrTombstone;
99use super::cache_types::Ticket;
100use super::{
101    Batch, CheckpointCache, ExecutionCacheCommit, ExecutionCacheMetrics, ExecutionCacheReconfigAPI,
102    ExecutionCacheWrite, ObjectCacheRead, StateSyncAPI, TestingAPI, TransactionCacheRead,
103    cache_types::{CacheResult, CachedVersionMap, IsNewer, MonotonicCache},
104    implement_passthrough_traits,
105    object_locks::ObjectLocks,
106};
107
108#[cfg(test)]
109#[path = "unit_tests/writeback_cache_tests.rs"]
110pub mod writeback_cache_tests;
111
112#[cfg(test)]
113#[path = "unit_tests/notify_read_input_objects_tests.rs"]
114mod notify_read_input_objects_tests;
115
116#[derive(Clone, PartialEq, Eq)]
117enum ObjectEntry {
118    Object(Object),
119    Deleted,
120    Wrapped,
121}
122
123impl ObjectEntry {
124    #[cfg(test)]
125    fn unwrap_object(&self) -> &Object {
126        match self {
127            ObjectEntry::Object(o) => o,
128            _ => panic!("unwrap_object called on non-Object"),
129        }
130    }
131
132    fn is_tombstone(&self) -> bool {
133        match self {
134            ObjectEntry::Deleted | ObjectEntry::Wrapped => true,
135            ObjectEntry::Object(_) => false,
136        }
137    }
138
139    /// This entry as the entry of `object_id` at `version`: the object, or the tombstone of the
140    /// deletion or wrap at `version`.
141    fn into_object_or_tombstone(
142        self,
143        object_id: ObjectID,
144        version: SequenceNumber,
145    ) -> ObjectOrTombstone {
146        match self {
147            ObjectEntry::Object(object) => ObjectOrTombstone::Object(object),
148            ObjectEntry::Deleted => ObjectOrTombstone::Tombstone((
149                object_id,
150                version,
151                ObjectDigest::OBJECT_DIGEST_DELETED,
152            )),
153            ObjectEntry::Wrapped => ObjectOrTombstone::Tombstone((
154                object_id,
155                version,
156                ObjectDigest::OBJECT_DIGEST_WRAPPED,
157            )),
158        }
159    }
160}
161
162impl std::fmt::Debug for ObjectEntry {
163    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
164        match self {
165            ObjectEntry::Object(o) => {
166                write!(f, "ObjectEntry::Object({:?})", o.compute_object_reference())
167            }
168            ObjectEntry::Deleted => write!(f, "ObjectEntry::Deleted"),
169            ObjectEntry::Wrapped => write!(f, "ObjectEntry::Wrapped"),
170        }
171    }
172}
173
174impl From<Object> for ObjectEntry {
175    fn from(object: Object) -> Self {
176        ObjectEntry::Object(object)
177    }
178}
179
180impl From<ObjectOrTombstone> for ObjectEntry {
181    fn from(object: ObjectOrTombstone) -> Self {
182        match object {
183            ObjectOrTombstone::Object(o) => o.into(),
184            ObjectOrTombstone::Tombstone(obj_ref) => {
185                if obj_ref.2.is_deleted() {
186                    ObjectEntry::Deleted
187                } else if obj_ref.2.is_wrapped() {
188                    ObjectEntry::Wrapped
189                } else {
190                    panic!("tombstone digest must either be deleted or wrapped");
191                }
192            }
193        }
194    }
195}
196
197#[derive(Debug, Clone, PartialEq, Eq)]
198enum LatestObjectCacheEntry {
199    Object(SequenceNumber, ObjectEntry),
200    NonExistent,
201}
202
203impl LatestObjectCacheEntry {
204    #[cfg(test)]
205    fn version(&self) -> Option<SequenceNumber> {
206        match self {
207            LatestObjectCacheEntry::Object(version, _) => Some(*version),
208            LatestObjectCacheEntry::NonExistent => None,
209        }
210    }
211
212    fn is_alive(&self) -> bool {
213        match self {
214            LatestObjectCacheEntry::Object(_, entry) => !entry.is_tombstone(),
215            LatestObjectCacheEntry::NonExistent => false,
216        }
217    }
218}
219
220impl IsNewer for LatestObjectCacheEntry {
221    fn is_newer_than(&self, other: &LatestObjectCacheEntry) -> bool {
222        match (self, other) {
223            (LatestObjectCacheEntry::Object(v1, _), LatestObjectCacheEntry::Object(v2, _)) => {
224                v1 > v2
225            }
226            (LatestObjectCacheEntry::Object(_, _), LatestObjectCacheEntry::NonExistent) => true,
227            _ => false,
228        }
229    }
230}
231
232type MarkerKey = (EpochId, FullObjectID);
233
234/// UncommittedData stores execution outputs that are not yet written to the db. Entries in this
235/// struct can only be purged after they are committed.
236struct UncommittedData {
237    /// The object dirty set. All writes go into this table first. After we flush the data to the
238    /// db, the data is removed from this table and inserted into the object_cache.
239    ///
240    /// This table may contain both live and dead objects, since we flush both live and dead
241    /// objects to the db in order to support past object queries on fullnodes.
242    ///
243    /// Further, we only remove objects in FIFO order, which ensures that the cached
244    /// sequence of objects has no gaps. In other words, if we have versions 4, 8, 13 of
245    /// an object, we can deduce that version 9 does not exist. This also makes child object
246    /// reads efficient. `object_cache` cannot contain a more recent version of an object than
247    /// `objects`, and neither can have any gaps. Therefore if there is any object <= the version
248    /// bound for a child read in objects, it is the correct object to return.
249    objects: DashMap<ObjectID, CachedVersionMap<ObjectEntry>>,
250
251    // Markers for received objects and deleted shared objects. This contains all of the dirty
252    // marker state, which is committed to the db at the same time as other transaction data.
253    // After markers are committed to the db we remove them from this table and insert them into
254    // marker_cache.
255    markers: DashMap<MarkerKey, CachedVersionMap<MarkerValue>>,
256
257    transaction_effects: DashMap<TransactionEffectsDigest, TransactionEffects>,
258
259    transaction_events: DashMap<TransactionDigest, TransactionEvents>,
260
261    unchanged_loaded_runtime_objects: DashMap<TransactionDigest, Vec<ObjectKey>>,
262
263    executed_effects_digests: DashMap<TransactionDigest, TransactionEffectsDigest>,
264
265    // Transaction outputs that have not yet been written to the DB. Items are removed from this
266    // table as they are flushed to the db.
267    pending_transaction_writes: DashMap<TransactionDigest, Arc<TransactionOutputs>>,
268
269    total_transaction_inserts: AtomicU64,
270    total_transaction_commits: AtomicU64,
271}
272
273impl UncommittedData {
274    fn new() -> Self {
275        Self {
276            objects: DashMap::with_shard_amount(2048),
277            markers: DashMap::with_shard_amount(2048),
278            transaction_effects: DashMap::with_shard_amount(2048),
279            executed_effects_digests: DashMap::with_shard_amount(2048),
280            pending_transaction_writes: DashMap::with_shard_amount(2048),
281            transaction_events: DashMap::with_shard_amount(2048),
282            unchanged_loaded_runtime_objects: DashMap::with_shard_amount(2048),
283            total_transaction_inserts: AtomicU64::new(0),
284            total_transaction_commits: AtomicU64::new(0),
285        }
286    }
287
288    fn clear(&self) {
289        self.objects.clear();
290        self.markers.clear();
291        self.transaction_effects.clear();
292        self.executed_effects_digests.clear();
293        self.pending_transaction_writes.clear();
294        self.transaction_events.clear();
295        self.unchanged_loaded_runtime_objects.clear();
296        self.total_transaction_inserts
297            .store(0, std::sync::atomic::Ordering::Relaxed);
298        self.total_transaction_commits
299            .store(0, std::sync::atomic::Ordering::Relaxed);
300    }
301
302    fn is_empty(&self) -> bool {
303        let empty = self.pending_transaction_writes.is_empty();
304        if empty && cfg!(debug_assertions) {
305            assert!(
306                self.objects.is_empty()
307                    && self.markers.is_empty()
308                    && self.transaction_effects.is_empty()
309                    && self.executed_effects_digests.is_empty()
310                    && self.transaction_events.is_empty()
311                    && self.unchanged_loaded_runtime_objects.is_empty()
312                    && self
313                        .total_transaction_inserts
314                        .load(std::sync::atomic::Ordering::Relaxed)
315                        == self
316                            .total_transaction_commits
317                            .load(std::sync::atomic::Ordering::Relaxed),
318            );
319        }
320        empty
321    }
322}
323
324// Point items (anything without a version number) can be negatively cached as None
325type PointCacheItem<T> = Option<T>;
326
327// PointCacheItem can only be used for insert-only collections, so a Some entry
328// is always newer than a None entry.
329impl<T: Eq + std::fmt::Debug> IsNewer for PointCacheItem<T> {
330    fn is_newer_than(&self, other: &PointCacheItem<T>) -> bool {
331        match (self, other) {
332            (Some(_), None) => true,
333
334            (Some(a), Some(b)) => {
335                // conflicting inserts should never happen
336                debug_assert_eq!(a, b);
337                false
338            }
339
340            _ => false,
341        }
342    }
343}
344
345/// CachedData stores data that has been committed to the db, but is likely to be read soon.
346struct CachedCommittedData {
347    // See module level comment for an explanation of caching strategy.
348    object_cache: MokaCache<ObjectID, Arc<Mutex<CachedVersionMap<ObjectEntry>>>>,
349
350    // See module level comment for an explanation of caching strategy.
351    marker_cache: MokaCache<MarkerKey, Arc<Mutex<CachedVersionMap<MarkerValue>>>>,
352
353    transactions: MonotonicCache<TransactionDigest, PointCacheItem<Arc<VerifiedTransaction>>>,
354
355    transaction_effects:
356        MonotonicCache<TransactionEffectsDigest, PointCacheItem<Arc<TransactionEffects>>>,
357
358    transaction_events: MonotonicCache<TransactionDigest, PointCacheItem<Arc<TransactionEvents>>>,
359
360    executed_effects_digests:
361        MonotonicCache<TransactionDigest, PointCacheItem<TransactionEffectsDigest>>,
362
363    transaction_executed_in_last_epoch:
364        MonotonicCache<(EpochId, TransactionDigest), PointCacheItem<()>>,
365
366    // Objects that were read at transaction signing time - allows us to access them again at
367    // execution time with a single lock / hash lookup
368    _transaction_objects: MokaCache<TransactionDigest, Vec<Object>>,
369}
370
371impl CachedCommittedData {
372    fn new(config: &ExecutionCacheConfig) -> Self {
373        let object_cache = MokaCache::builder(8)
374            .max_capacity(randomize_cache_capacity_in_tests(
375                config.object_cache_size(),
376            ))
377            .build();
378        let marker_cache = MokaCache::builder(8)
379            .max_capacity(randomize_cache_capacity_in_tests(
380                config.marker_cache_size(),
381            ))
382            .build();
383
384        let transactions = MonotonicCache::new(randomize_cache_capacity_in_tests(
385            config.transaction_cache_size(),
386        ));
387        let transaction_effects = MonotonicCache::new(randomize_cache_capacity_in_tests(
388            config.effect_cache_size(),
389        ));
390        let transaction_events = MonotonicCache::new(randomize_cache_capacity_in_tests(
391            config.events_cache_size(),
392        ));
393        let executed_effects_digests = MonotonicCache::new(randomize_cache_capacity_in_tests(
394            config.executed_effect_cache_size(),
395        ));
396
397        let transaction_objects = MokaCache::builder(8)
398            .max_capacity(randomize_cache_capacity_in_tests(
399                config.transaction_objects_cache_size(),
400            ))
401            .build();
402
403        let transaction_executed_in_last_epoch = MonotonicCache::new(
404            randomize_cache_capacity_in_tests(config.executed_effect_cache_size()),
405        );
406
407        Self {
408            object_cache,
409            marker_cache,
410            transactions,
411            transaction_effects,
412            transaction_events,
413            executed_effects_digests,
414            transaction_executed_in_last_epoch,
415            _transaction_objects: transaction_objects,
416        }
417    }
418
419    fn clear_and_assert_empty(&self) {
420        self.object_cache.invalidate_all();
421        self.marker_cache.invalidate_all();
422        self.transactions.invalidate_all();
423        self.transaction_effects.invalidate_all();
424        self.transaction_events.invalidate_all();
425        self.executed_effects_digests.invalidate_all();
426        self.transaction_executed_in_last_epoch.invalidate_all();
427        self._transaction_objects.invalidate_all();
428
429        assert_empty(&self.object_cache);
430        assert_empty(&self.marker_cache);
431        assert!(self.transactions.is_empty());
432        assert!(self.transaction_effects.is_empty());
433        assert!(self.transaction_events.is_empty());
434        assert!(self.executed_effects_digests.is_empty());
435        assert!(self.transaction_executed_in_last_epoch.is_empty());
436        assert_empty(&self._transaction_objects);
437    }
438}
439
440fn assert_empty<K, V>(cache: &MokaCache<K, V>)
441where
442    K: std::hash::Hash + std::cmp::Eq + std::cmp::PartialEq + Send + Sync + 'static,
443    V: std::clone::Clone + std::marker::Send + std::marker::Sync + 'static,
444{
445    if cache.iter().next().is_some() {
446        panic!("cache should be empty");
447    }
448}
449
450pub struct WritebackCache {
451    dirty: UncommittedData,
452    cached: CachedCommittedData,
453
454    // We separately cache the latest version of each object. Although this seems
455    // redundant, it is the only way to support populating the cache after a read.
456    // We cannot simply insert objects that we read off the disk into `object_cache`,
457    // since that may violate the no-missing-versions property.
458    // `object_by_id_cache` is also written to on writes so that it is always coherent.
459    // Hence it contains both committed and dirty object data.
460    object_by_id_cache: MonotonicCache<ObjectID, LatestObjectCacheEntry>,
461
462    // The packages cache is treated separately from objects, because they are immutable and can be
463    // used by any number of transactions. Additionally, many operations require loading large
464    // numbers of packages (due to dependencies), so we want to try to keep all packages in memory.
465    //
466    // Also, this cache can contain packages that are dirty or committed, so it does not live in
467    // UncachedData or CachedCommittedData. The cache is populated in two ways:
468    // - when packages are written (in which case they will also be present in the dirty set)
469    // - after a cache miss. Because package IDs are unique (only one version exists for each ID)
470    //   we do not need to worry about the contiguous version property.
471    // - note that we removed any unfinalized packages from the cache during revert_state_update().
472    packages: MokaCache<ObjectID, PackageObject>,
473
474    object_locks: ObjectLocks,
475
476    executed_effects_digests_notify_read: NotifyRead<TransactionDigest, TransactionEffectsDigest>,
477    object_notify_read: NotifyRead<InputKey, ()>,
478
479    store: Arc<AuthorityStore>,
480    backpressure_threshold: u64,
481    backpressure_manager: Arc<BackpressureManager>,
482    metrics: Arc<ExecutionCacheMetrics>,
483}
484
485macro_rules! check_cache_entry_by_version {
486    ($self: ident, $table: expr, $level: expr, $cache: expr, $version: expr) => {
487        $self.metrics.record_cache_request($table, $level);
488        if let Some(cache) = $cache {
489            if let Some(entry) = cache.get(&$version) {
490                $self.metrics.record_cache_hit($table, $level);
491                return CacheResult::Hit(entry.clone());
492            }
493
494            if let Some(least_version) = cache.get_least() {
495                if least_version.0 < $version {
496                    // If the version is greater than the least version in the cache, then we know
497                    // that the object does not exist anywhere
498                    $self.metrics.record_cache_negative_hit($table, $level);
499                    return CacheResult::NegativeHit;
500                }
501            }
502        }
503        $self.metrics.record_cache_miss($table, $level);
504    };
505}
506
507macro_rules! check_cache_entry_by_latest {
508    ($self: ident, $table: expr, $level: expr, $cache: expr) => {
509        $self.metrics.record_cache_request($table, $level);
510        if let Some(cache) = $cache {
511            if let Some((version, entry)) = cache.get_highest() {
512                $self.metrics.record_cache_hit($table, $level);
513                return CacheResult::Hit((*version, entry.clone()));
514            } else {
515                panic!("empty CachedVersionMap should have been removed");
516            }
517        }
518        $self.metrics.record_cache_miss($table, $level);
519    };
520}
521
522impl WritebackCache {
523    /// Load an implicitly read system object at the requested version.
524    /// In normal execution, this function can block wait until the object is available at the requested version,
525    /// and it is guaranteed to return an object with the requested version.
526    /// In dry-runs, this function will never block wait, but may return None if the requested version was pruned by this point.
527    pub(crate) fn load_implicitly_read_system_object(
528        &self,
529        object_id: &ObjectID,
530        version: ConsensusObjectVersion,
531    ) -> Option<Object> {
532        assert!(
533            sui_types::IMPLICITLY_READ_SYSTEM_OBJECTS.contains(object_id),
534            "{object_id} is not an implicitly read system object"
535        );
536        let ConsensusObjectVersion {
537            initial_shared_version,
538            version,
539        } = version;
540        if let Some(object) = ObjectCacheRead::get_object_by_key(self, object_id, version) {
541            return Some(object);
542        }
543        self.metrics
544            .implicit_system_object_read_waits
545            .with_label_values(&[object_id.to_string().as_str()])
546            .inc();
547        let wait_start = Instant::now();
548        let key = InputKey::VersionedObject {
549            id: FullObjectID::Consensus((*object_id, initial_shared_version)),
550            version,
551        };
552        // Block wait until the object is available at the requested version.
553        // Note that before blocking, we check if the latest version already passed the requested version,
554        // if so it must imply that we have already produced the requested version.
555        // We are doing this check instead of exact version comparison to handle the rare case during
556        // dry-runs where the requested version was pruned by this point.
557        // Also note that in the case of dry-run, this will never block wait.
558        self.object_notify_read.read_one_blocking(
559            "load_implicitly_read_system_object",
560            &key,
561            |_key| {
562                ObjectCacheRead::get_object(self, object_id)
563                    .is_some_and(|latest| latest.version() >= version)
564                    .then_some(())
565            },
566        );
567        self.metrics
568            .implicit_system_object_read_wait_latency
569            .with_label_values(&[object_id.to_string().as_str()])
570            .observe(wait_start.elapsed().as_secs_f64());
571        ObjectCacheRead::get_object_by_key(self, object_id, version)
572    }
573
574    pub fn new(
575        config: &ExecutionCacheConfig,
576        store: Arc<AuthorityStore>,
577        metrics: Arc<ExecutionCacheMetrics>,
578        backpressure_manager: Arc<BackpressureManager>,
579    ) -> Self {
580        let packages = MokaCache::builder(8)
581            .max_capacity(randomize_cache_capacity_in_tests(
582                config.package_cache_size(),
583            ))
584            .build();
585        Self {
586            dirty: UncommittedData::new(),
587            cached: CachedCommittedData::new(config),
588            object_by_id_cache: MonotonicCache::new(randomize_cache_capacity_in_tests(
589                config.object_by_id_cache_size(),
590            )),
591            packages,
592            object_locks: ObjectLocks::new(),
593            executed_effects_digests_notify_read: NotifyRead::new(),
594            object_notify_read: NotifyRead::new(),
595            store,
596            backpressure_manager,
597            backpressure_threshold: config.backpressure_threshold(),
598            metrics,
599        }
600    }
601
602    pub fn new_for_tests(store: Arc<AuthorityStore>) -> Self {
603        Self::new(
604            &Default::default(),
605            store,
606            ExecutionCacheMetrics::new(&prometheus::Registry::new()).into(),
607            BackpressureManager::new_for_tests(),
608        )
609    }
610
611    #[cfg(test)]
612    pub fn reset_for_test(&mut self) {
613        let mut new = Self::new(
614            &Default::default(),
615            self.store.clone(),
616            self.metrics.clone(),
617            self.backpressure_manager.clone(),
618        );
619        std::mem::swap(self, &mut new);
620    }
621
622    pub fn evict_executed_effects_from_cache_for_testing(&self, tx_digest: &TransactionDigest) {
623        self.cached.executed_effects_digests.invalidate(tx_digest);
624        self.cached.transaction_events.invalidate(tx_digest);
625        self.cached.transactions.invalidate(tx_digest);
626    }
627
628    fn write_object_entry(
629        &self,
630        object_id: &ObjectID,
631        version: SequenceNumber,
632        object: ObjectEntry,
633    ) {
634        trace!(?object_id, ?version, ?object, "inserting object entry");
635        self.metrics.record_cache_write("object");
636
637        // We must hold the lock for the object entry while inserting to the
638        // object_by_id_cache. Otherwise, a surprising bug can occur:
639        //
640        // 1. A thread executing TX1 can write object (O,1) to the dirty set and then pause.
641        // 2. TX2, which reads (O,1) can begin executing, because ExecutionScheduler immediately
642        //    schedules transactions if their inputs are available. It does not matter that TX1
643        //    hasn't finished executing yet.
644        // 3. TX2 can write (O,2) to both the dirty set and the object_by_id_cache.
645        // 4. The thread executing TX1 can resume and write (O,1) to the object_by_id_cache.
646        //
647        // Now, any subsequent attempt to get the latest version of O will return (O,1) instead of
648        // (O,2).
649        //
650        // This seems very unlikely, but it may be possible under the following circumstances:
651        // - While a thread is unlikely to pause for so long, moka cache uses optimistic
652        //   lock-free algorithms that have retry loops. Possibly, under high contention, this
653        //   code might spin for a surprisingly long time.
654        // - Additionally, many concurrent re-executions of the same tx could happen due to
655        //   the tx finalizer, plus checkpoint executor, consensus, and RPCs from fullnodes.
656        let mut entry = self.dirty.objects.entry(*object_id).or_default();
657
658        self.object_by_id_cache
659            .insert(
660                object_id,
661                LatestObjectCacheEntry::Object(version, object.clone()),
662                Ticket::Write,
663            )
664            // While Ticket::Write cannot expire, this insert may still fail.
665            // See the comment in `MonotonicCache::insert`.
666            .ok();
667
668        entry.insert(version, object.clone());
669
670        if let ObjectEntry::Object(object) = &object {
671            if object.is_package() {
672                self.object_notify_read
673                    .notify(&InputKey::Package { id: *object_id }, &());
674            } else if !object.is_child_object() {
675                self.object_notify_read.notify(
676                    &InputKey::VersionedObject {
677                        id: object.full_id(),
678                        version: object.version(),
679                    },
680                    &(),
681                );
682            }
683        }
684    }
685
686    fn write_marker_value(
687        &self,
688        epoch_id: EpochId,
689        object_key: FullObjectKey,
690        marker_value: MarkerValue,
691    ) {
692        tracing::trace!("inserting marker value {object_key:?}: {marker_value:?}",);
693        self.metrics.record_cache_write("marker");
694        self.dirty
695            .markers
696            .entry((epoch_id, object_key.id()))
697            .or_default()
698            .value_mut()
699            .insert(object_key.version(), marker_value);
700        // It is possible for a transaction to use a consensus stream ended
701        // object in the input, hence we must notify that it is now available
702        // at the assigned version, so that any transaction waiting for this
703        // object version can start execution.
704        if matches!(marker_value, MarkerValue::ConsensusStreamEnded(_)) {
705            self.object_notify_read.notify(
706                &InputKey::VersionedObject {
707                    id: object_key.id(),
708                    version: object_key.version(),
709                },
710                &(),
711            );
712        }
713    }
714
715    // lock both the dirty and committed sides of the cache, and then pass the entries to
716    // the callback. Written with the `with` pattern because any other way of doing this
717    // creates lifetime hell.
718    fn with_locked_cache_entries<K, V, R>(
719        dirty_map: &DashMap<K, CachedVersionMap<V>>,
720        cached_map: &MokaCache<K, Arc<Mutex<CachedVersionMap<V>>>>,
721        key: &K,
722        cb: impl FnOnce(Option<&CachedVersionMap<V>>, Option<&CachedVersionMap<V>>) -> R,
723    ) -> R
724    where
725        K: Copy + Eq + Hash + Send + Sync + 'static,
726        V: Send + Sync + 'static,
727    {
728        let dirty_entry = dirty_map.entry(*key);
729        let dirty_entry = match &dirty_entry {
730            DashMapEntry::Occupied(occupied) => Some(occupied.get()),
731            DashMapEntry::Vacant(_) => None,
732        };
733
734        let cached_entry = cached_map.get(key);
735        let cached_lock = cached_entry.as_ref().map(|entry| entry.lock());
736        let cached_entry = cached_lock.as_deref();
737
738        cb(dirty_entry, cached_entry)
739    }
740
741    // Attempt to get an object from the cache. The DB is not consulted.
742    // Can return Hit, Miss, or NegativeHit (if the object is known to not exist).
743    fn get_object_entry_by_key_cache_only(
744        &self,
745        object_id: &ObjectID,
746        version: SequenceNumber,
747    ) -> CacheResult<ObjectEntry> {
748        Self::with_locked_cache_entries(
749            &self.dirty.objects,
750            &self.cached.object_cache,
751            object_id,
752            |dirty_entry, cached_entry| {
753                check_cache_entry_by_version!(
754                    self,
755                    "object_by_version",
756                    "uncommitted",
757                    dirty_entry,
758                    version
759                );
760                check_cache_entry_by_version!(
761                    self,
762                    "object_by_version",
763                    "committed",
764                    cached_entry,
765                    version
766                );
767                CacheResult::Miss
768            },
769        )
770    }
771
772    fn get_object_by_key_cache_only(
773        &self,
774        object_id: &ObjectID,
775        version: SequenceNumber,
776    ) -> CacheResult<Object> {
777        match self.get_object_entry_by_key_cache_only(object_id, version) {
778            CacheResult::Hit(entry) => match entry {
779                ObjectEntry::Object(object) => CacheResult::Hit(object),
780                ObjectEntry::Deleted | ObjectEntry::Wrapped => CacheResult::NegativeHit,
781            },
782            CacheResult::Miss => CacheResult::Miss,
783            CacheResult::NegativeHit => CacheResult::NegativeHit,
784        }
785    }
786
787    fn get_object_entry_by_id_cache_only(
788        &self,
789        request_type: &'static str,
790        object_id: &ObjectID,
791    ) -> CacheResult<(SequenceNumber, ObjectEntry)> {
792        self.metrics
793            .record_cache_request(request_type, "object_by_id");
794        let entry = self.object_by_id_cache.get(object_id);
795
796        if cfg!(debug_assertions)
797            && let Some(entry) = &entry
798        {
799            // check that cache is coherent
800            let highest: Option<ObjectEntry> = self
801                .dirty
802                .objects
803                .get(object_id)
804                .and_then(|entry| entry.get_highest().map(|(_, o)| o.clone()))
805                .or_else(|| {
806                    let obj: Option<ObjectEntry> = self
807                        .store
808                        .get_latest_object_or_tombstone(*object_id)
809                        .unwrap()
810                        .map(|(_, o)| o.into());
811                    obj
812                });
813
814            let cache_entry = match &*entry.lock() {
815                LatestObjectCacheEntry::Object(_, entry) => Some(entry.clone()),
816                LatestObjectCacheEntry::NonExistent => None,
817            };
818
819            // If the cache entry is a tombstone, the db entry may be missing if it was pruned.
820            let tombstone_possibly_pruned = highest.is_none()
821                && cache_entry
822                    .as_ref()
823                    .map(|e| e.is_tombstone())
824                    .unwrap_or(false);
825
826            if highest != cache_entry && !tombstone_possibly_pruned {
827                tracing::error!(
828                    ?highest,
829                    ?cache_entry,
830                    ?tombstone_possibly_pruned,
831                    "object_by_id cache is incoherent for {:?}",
832                    object_id
833                );
834                panic!("object_by_id cache is incoherent for {:?}", object_id);
835            }
836        }
837
838        if let Some(entry) = entry {
839            let entry = entry.lock();
840            match &*entry {
841                LatestObjectCacheEntry::Object(latest_version, latest_object) => {
842                    self.metrics.record_cache_hit(request_type, "object_by_id");
843                    return CacheResult::Hit((*latest_version, latest_object.clone()));
844                }
845                LatestObjectCacheEntry::NonExistent => {
846                    self.metrics
847                        .record_cache_negative_hit(request_type, "object_by_id");
848                    return CacheResult::NegativeHit;
849                }
850            }
851        } else {
852            self.metrics.record_cache_miss(request_type, "object_by_id");
853        }
854
855        Self::with_locked_cache_entries(
856            &self.dirty.objects,
857            &self.cached.object_cache,
858            object_id,
859            |dirty_entry, cached_entry| {
860                check_cache_entry_by_latest!(self, request_type, "uncommitted", dirty_entry);
861                check_cache_entry_by_latest!(self, request_type, "committed", cached_entry);
862                CacheResult::Miss
863            },
864        )
865    }
866
867    fn get_marker_value_cache_only(
868        &self,
869        object_key: FullObjectKey,
870        epoch_id: EpochId,
871    ) -> CacheResult<MarkerValue> {
872        Self::with_locked_cache_entries(
873            &self.dirty.markers,
874            &self.cached.marker_cache,
875            &(epoch_id, object_key.id()),
876            |dirty_entry, cached_entry| {
877                check_cache_entry_by_version!(
878                    self,
879                    "marker_by_version",
880                    "uncommitted",
881                    dirty_entry,
882                    object_key.version()
883                );
884                check_cache_entry_by_version!(
885                    self,
886                    "marker_by_version",
887                    "committed",
888                    cached_entry,
889                    object_key.version()
890                );
891                CacheResult::Miss
892            },
893        )
894    }
895
896    fn get_latest_marker_value_cache_only(
897        &self,
898        object_id: FullObjectID,
899        epoch_id: EpochId,
900    ) -> CacheResult<(SequenceNumber, MarkerValue)> {
901        Self::with_locked_cache_entries(
902            &self.dirty.markers,
903            &self.cached.marker_cache,
904            &(epoch_id, object_id),
905            |dirty_entry, cached_entry| {
906                check_cache_entry_by_latest!(self, "marker_latest", "uncommitted", dirty_entry);
907                check_cache_entry_by_latest!(self, "marker_latest", "committed", cached_entry);
908                CacheResult::Miss
909            },
910        )
911    }
912
913    fn get_object_impl(&self, request_type: &'static str, id: &ObjectID) -> Option<Object> {
914        let ticket = self.object_by_id_cache.get_ticket_for_read(id);
915        match self.get_object_entry_by_id_cache_only(request_type, id) {
916            CacheResult::Hit((_, entry)) => match entry {
917                ObjectEntry::Object(object) => Some(object),
918                ObjectEntry::Deleted | ObjectEntry::Wrapped => None,
919            },
920            CacheResult::NegativeHit => None,
921            CacheResult::Miss => {
922                let obj = self
923                    .store
924                    .get_latest_object_or_tombstone(*id)
925                    .expect("db error");
926                match obj {
927                    Some((key, obj)) => {
928                        self.cache_latest_object_by_id(
929                            id,
930                            LatestObjectCacheEntry::Object(key.1, obj.clone().into()),
931                            ticket,
932                        );
933                        match obj {
934                            ObjectOrTombstone::Object(object) => Some(object),
935                            ObjectOrTombstone::Tombstone(_) => None,
936                        }
937                    }
938                    None => {
939                        self.cache_object_not_found(id, ticket);
940                        None
941                    }
942                }
943            }
944        }
945    }
946
947    fn record_db_get(&self, request_type: &'static str) -> &AuthorityStore {
948        self.metrics.record_cache_request(request_type, "db");
949        &self.store
950    }
951
952    fn record_db_multi_get(&self, request_type: &'static str, count: usize) -> &AuthorityStore {
953        self.metrics
954            .record_cache_multi_request(request_type, "db", count);
955        &self.store
956    }
957
958    #[instrument(level = "debug", skip_all)]
959    fn write_transaction_outputs(&self, epoch_id: EpochId, tx_outputs: Arc<TransactionOutputs>) {
960        let tx_digest = *tx_outputs.transaction.digest();
961        trace!(?tx_digest, "writing transaction outputs to cache");
962
963        assert!(
964            !self.transaction_executed_in_last_epoch(&tx_digest, epoch_id),
965            "Transaction {:?} was already executed in epoch {}",
966            tx_digest,
967            epoch_id.saturating_sub(1)
968        );
969
970        let TransactionOutputs {
971            transaction,
972            effects,
973            markers,
974            written,
975            deleted,
976            wrapped,
977            events,
978            unchanged_loaded_runtime_objects,
979            ..
980        } = &*tx_outputs;
981
982        // Deletions and wraps must be written first. The reason is that one of the deletes
983        // may be a child object, and if we write the parent object first, a reader may or may
984        // not see the previous version of the child object, instead of the deleted/wrapped
985        // tombstone, which would cause an execution fork
986        for ObjectKey(id, version) in deleted.iter() {
987            self.write_object_entry(id, *version, ObjectEntry::Deleted);
988        }
989
990        for ObjectKey(id, version) in wrapped.iter() {
991            self.write_object_entry(id, *version, ObjectEntry::Wrapped);
992        }
993
994        // Update all markers
995        for (object_key, marker_value) in markers.iter() {
996            self.write_marker_value(epoch_id, *object_key, *marker_value);
997        }
998
999        // Write children before parents to ensure that readers do not observe a parent object
1000        // before its most recent children are visible.
1001        for (object_id, object) in written.iter() {
1002            if object.is_child_object() {
1003                self.write_object_entry(object_id, object.version(), object.clone().into());
1004            }
1005        }
1006        for (object_id, object) in written.iter() {
1007            if !object.is_child_object() {
1008                self.write_object_entry(object_id, object.version(), object.clone().into());
1009                if object.is_package() {
1010                    debug!("caching package: {:?}", object.compute_object_reference());
1011                    self.packages
1012                        .insert(*object_id, PackageObject::new(object.clone()));
1013                }
1014            }
1015        }
1016
1017        let tx_digest = *transaction.digest();
1018        debug!(
1019            ?tx_digest,
1020            "Writing transaction output objects to cache: {:?}",
1021            written
1022                .values()
1023                .map(|o| (o.id(), o.version()))
1024                .collect::<Vec<_>>(),
1025        );
1026        let effects_digest = effects.digest();
1027
1028        self.metrics.record_cache_write("transaction_block");
1029        self.dirty
1030            .pending_transaction_writes
1031            .insert(tx_digest, tx_outputs.clone());
1032
1033        // insert transaction effects before executed_effects_digests so that there
1034        // are never dangling entries in executed_effects_digests
1035        self.metrics.record_cache_write("transaction_effects");
1036        self.dirty
1037            .transaction_effects
1038            .insert(effects_digest, effects.clone());
1039
1040        // note: if events.data.is_empty(), then there are no events for this transaction. We
1041        // store it anyway to avoid special cases in commint_transaction_outputs, and translate
1042        // an empty events structure to None when reading.
1043        self.metrics.record_cache_write("transaction_events");
1044        self.dirty
1045            .transaction_events
1046            .insert(tx_digest, events.clone());
1047
1048        self.metrics
1049            .record_cache_write("unchanged_loaded_runtime_objects");
1050        self.dirty
1051            .unchanged_loaded_runtime_objects
1052            .insert(tx_digest, unchanged_loaded_runtime_objects.clone());
1053
1054        self.metrics.record_cache_write("executed_effects_digests");
1055        self.dirty
1056            .executed_effects_digests
1057            .insert(tx_digest, effects_digest);
1058
1059        self.executed_effects_digests_notify_read
1060            .notify(&tx_digest, &effects_digest);
1061
1062        self.metrics
1063            .pending_notify_read
1064            .set(self.executed_effects_digests_notify_read.num_pending() as i64);
1065
1066        let prev = self
1067            .dirty
1068            .total_transaction_inserts
1069            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1070
1071        let pending_count = (prev + 1).saturating_sub(
1072            self.dirty
1073                .total_transaction_commits
1074                .load(std::sync::atomic::Ordering::Relaxed),
1075        );
1076
1077        self.set_backpressure(pending_count);
1078    }
1079
1080    fn build_db_batch(&self, epoch: EpochId, digests: &[TransactionDigest]) -> Batch {
1081        let _metrics_guard = mysten_metrics::monitored_scope("WritebackCache::build_db_batch");
1082        let mut all_outputs = Vec::with_capacity(digests.len());
1083        for tx in digests {
1084            let Some(outputs) = self
1085                .dirty
1086                .pending_transaction_writes
1087                .get(tx)
1088                .map(|o| o.clone())
1089            else {
1090                // This can happen in the following rare case:
1091                // All transactions in the checkpoint are committed to the db (by commit_transaction_outputs,
1092                // called in CheckpointExecutor::process_executed_transactions), but the process crashes before
1093                // the checkpoint water mark is bumped. We will then re-commit the checkpoint at startup,
1094                // despite that all transactions are already executed.
1095                warn!("Attempt to commit unknown transaction {:?}", tx);
1096                continue;
1097            };
1098            all_outputs.push(outputs);
1099        }
1100
1101        let batch = self
1102            .store
1103            .build_db_batch(epoch, &all_outputs)
1104            .expect("db error");
1105        (all_outputs, batch)
1106    }
1107
1108    // Commits dirty data for the given TransactionDigest to the db.
1109    #[instrument(level = "debug", skip_all)]
1110    fn commit_transaction_outputs(
1111        &self,
1112        epoch: EpochId,
1113        (all_outputs, db_batch): Batch,
1114        digests: &[TransactionDigest],
1115    ) {
1116        let _metrics_guard =
1117            mysten_metrics::monitored_scope("WritebackCache::commit_transaction_outputs");
1118        fail_point!("writeback-cache-commit");
1119        trace!(?digests);
1120
1121        // Flush writes to disk before removing anything from dirty set. otherwise,
1122        // a cache eviction could cause a value to disappear briefly, even if we insert to the
1123        // cache before removing from the dirty set.
1124        db_batch.write().expect("db error");
1125
1126        let _metrics_guard =
1127            mysten_metrics::monitored_scope("WritebackCache::commit_transaction_outputs::flush");
1128        // Parallel phase: tx-level metadata is keyed by unique tx_digest/effects_digest,
1129        // so there are no cross-transaction ordering constraints.
1130        all_outputs.par_iter().with_min_len(16).for_each(|outputs| {
1131            let tx_digest = outputs.transaction.digest();
1132            assert!(
1133                self.dirty
1134                    .pending_transaction_writes
1135                    .remove(tx_digest)
1136                    .is_some()
1137            );
1138            self.flush_tx_metadata_from_dirty_to_cached(*tx_digest, outputs);
1139        });
1140
1141        // Sequential phase: object/marker versions must be popped in causal order
1142        // (oldest first) per object_id. Multiple transactions in the batch can touch
1143        // the same shared object at consecutive versions, so this loop must preserve
1144        // the order of all_outputs.
1145        for outputs in all_outputs.iter() {
1146            self.flush_objects_from_dirty_to_cached(epoch, outputs);
1147        }
1148
1149        let num_outputs = all_outputs.len() as u64;
1150        let num_commits = self
1151            .dirty
1152            .total_transaction_commits
1153            .fetch_add(num_outputs, std::sync::atomic::Ordering::Relaxed)
1154            + num_outputs;
1155
1156        let pending_count = self
1157            .dirty
1158            .total_transaction_inserts
1159            .load(std::sync::atomic::Ordering::Relaxed)
1160            .saturating_sub(num_commits);
1161
1162        self.set_backpressure(pending_count);
1163    }
1164
1165    fn approximate_pending_transaction_count(&self) -> u64 {
1166        let num_commits = self
1167            .dirty
1168            .total_transaction_commits
1169            .load(std::sync::atomic::Ordering::Relaxed);
1170
1171        self.dirty
1172            .total_transaction_inserts
1173            .load(std::sync::atomic::Ordering::Relaxed)
1174            .saturating_sub(num_commits)
1175    }
1176
1177    fn set_backpressure(&self, pending_count: u64) {
1178        let backpressure = pending_count > self.backpressure_threshold;
1179        let backpressure_changed = self.backpressure_manager.set_backpressure(backpressure);
1180        if backpressure_changed {
1181            self.metrics.backpressure_toggles.inc();
1182        }
1183        self.metrics
1184            .backpressure_status
1185            .set(if backpressure { 1 } else { 0 });
1186    }
1187
1188    // Flushes tx-level metadata for a single transaction from dirty to cache.
1189    // All keys are unique per transaction (tx_digest, effects_digest), so this
1190    // is safe to call in parallel across transactions.
1191    fn flush_tx_metadata_from_dirty_to_cached(
1192        &self,
1193        tx_digest: TransactionDigest,
1194        outputs: &TransactionOutputs,
1195    ) {
1196        // TODO: outputs should have a strong count of 1 so we should be able to move out of it
1197        let TransactionOutputs {
1198            transaction,
1199            effects,
1200            events,
1201            ..
1202        } = outputs;
1203
1204        let effects_digest = effects.digest();
1205
1206        // Update cache before removing from self.dirty to avoid
1207        // unnecessary cache misses
1208        self.cached
1209            .transactions
1210            .insert(
1211                &tx_digest,
1212                PointCacheItem::Some(transaction.clone()),
1213                Ticket::Write,
1214            )
1215            .ok();
1216        self.cached
1217            .transaction_effects
1218            .insert(
1219                &effects_digest,
1220                PointCacheItem::Some(effects.clone().into()),
1221                Ticket::Write,
1222            )
1223            .ok();
1224        self.cached
1225            .executed_effects_digests
1226            .insert(
1227                &tx_digest,
1228                PointCacheItem::Some(effects_digest),
1229                Ticket::Write,
1230            )
1231            .ok();
1232        self.cached
1233            .transaction_events
1234            .insert(
1235                &tx_digest,
1236                PointCacheItem::Some(events.clone().into()),
1237                Ticket::Write,
1238            )
1239            .ok();
1240
1241        self.dirty
1242            .transaction_effects
1243            .remove(&effects_digest)
1244            .expect("effects must exist");
1245
1246        self.dirty
1247            .transaction_events
1248            .remove(&tx_digest)
1249            .expect("events must exist");
1250
1251        self.dirty
1252            .unchanged_loaded_runtime_objects
1253            .remove(&tx_digest)
1254            .expect("unchanged_loaded_runtime_objects must exist");
1255
1256        self.dirty
1257            .executed_effects_digests
1258            .remove(&tx_digest)
1259            .expect("executed effects must exist");
1260    }
1261
1262    // Flushes object and marker versions for a single transaction from dirty to cache.
1263    // Multiple transactions in the same batch can modify the same shared object at
1264    // consecutive versions, so callers must invoke this in causal (checkpoint) order.
1265    fn flush_objects_from_dirty_to_cached(&self, epoch: EpochId, outputs: &TransactionOutputs) {
1266        let TransactionOutputs {
1267            markers,
1268            written,
1269            deleted,
1270            wrapped,
1271            ..
1272        } = outputs;
1273
1274        for (object_key, marker_value) in markers.iter() {
1275            Self::move_version_from_dirty_to_cache(
1276                &self.dirty.markers,
1277                &self.cached.marker_cache,
1278                (epoch, object_key.id()),
1279                object_key.version(),
1280                marker_value,
1281            );
1282        }
1283
1284        for (object_id, object) in written.iter() {
1285            Self::move_version_from_dirty_to_cache(
1286                &self.dirty.objects,
1287                &self.cached.object_cache,
1288                *object_id,
1289                object.version(),
1290                &ObjectEntry::Object(object.clone()),
1291            );
1292        }
1293
1294        for ObjectKey(object_id, version) in deleted.iter() {
1295            Self::move_version_from_dirty_to_cache(
1296                &self.dirty.objects,
1297                &self.cached.object_cache,
1298                *object_id,
1299                *version,
1300                &ObjectEntry::Deleted,
1301            );
1302        }
1303
1304        for ObjectKey(object_id, version) in wrapped.iter() {
1305            Self::move_version_from_dirty_to_cache(
1306                &self.dirty.objects,
1307                &self.cached.object_cache,
1308                *object_id,
1309                *version,
1310                &ObjectEntry::Wrapped,
1311            );
1312        }
1313    }
1314
1315    // Move the oldest/least entry from the dirty queue to the cache queue.
1316    // This is called after the entry is committed to the db.
1317    fn move_version_from_dirty_to_cache<K, V>(
1318        dirty: &DashMap<K, CachedVersionMap<V>>,
1319        cache: &MokaCache<K, Arc<Mutex<CachedVersionMap<V>>>>,
1320        key: K,
1321        version: SequenceNumber,
1322        value: &V,
1323    ) where
1324        K: Eq + std::hash::Hash + Clone + Send + Sync + Copy + 'static,
1325        V: Send + Sync + Clone + Eq + std::fmt::Debug + 'static,
1326    {
1327        static MAX_VERSIONS: usize = 3;
1328
1329        // IMPORTANT: lock both the dirty set entry and the cache entry before modifying either.
1330        // this ensures that readers cannot see a value temporarily disappear.
1331        let dirty_entry = dirty.entry(key);
1332        let cache_entry = cache.entry(key).or_default();
1333        let mut cache_map = cache_entry.value().lock();
1334
1335        // insert into cache and drop old versions.
1336        cache_map.insert(version, value.clone());
1337        // TODO: make this automatic by giving CachedVersionMap an optional max capacity
1338        cache_map.truncate_to(MAX_VERSIONS);
1339
1340        let DashMapEntry::Occupied(mut occupied_dirty_entry) = dirty_entry else {
1341            panic!("dirty map must exist");
1342        };
1343
1344        let removed = occupied_dirty_entry.get_mut().pop_oldest(&version);
1345
1346        assert_eq!(removed.as_ref(), Some(value), "dirty version must exist");
1347
1348        // if there are no versions remaining, remove the map entry
1349        if occupied_dirty_entry.get().is_empty() {
1350            occupied_dirty_entry.remove();
1351        }
1352    }
1353
1354    // Updates the latest object id cache with an entry that was read from the db.
1355    fn cache_latest_object_by_id(
1356        &self,
1357        object_id: &ObjectID,
1358        object: LatestObjectCacheEntry,
1359        ticket: Ticket,
1360    ) {
1361        trace!("caching object by id: {:?} {:?}", object_id, object);
1362        if self
1363            .object_by_id_cache
1364            .insert(object_id, object, ticket)
1365            .is_ok()
1366        {
1367            self.metrics.record_cache_write("object_by_id");
1368        } else {
1369            trace!("discarded cache write due to expired ticket");
1370            self.metrics.record_ticket_expiry();
1371        }
1372    }
1373
1374    fn cache_object_not_found(&self, object_id: &ObjectID, ticket: Ticket) {
1375        self.cache_latest_object_by_id(object_id, LatestObjectCacheEntry::NonExistent, ticket);
1376    }
1377
1378    fn clear_state_end_of_epoch_impl(&self, execution_guard: &ExecutionLockWriteGuard<'_>) {
1379        info!("clearing state at end of epoch");
1380
1381        // Note: there cannot be any concurrent writes to self.dirty while we are in this function,
1382        // as all transaction execution is paused.
1383        for r in self.dirty.pending_transaction_writes.iter() {
1384            let outputs = r.value();
1385            if !outputs
1386                .transaction
1387                .transaction_data()
1388                .shared_input_objects()
1389                .is_empty()
1390            {
1391                debug_fatal!("transaction must be single writer");
1392            }
1393            info!(
1394                "clearing state for transaction {:?}",
1395                outputs.transaction.digest()
1396            );
1397            for (object_id, object) in outputs.written.iter() {
1398                if object.is_package() {
1399                    info!("removing non-finalized package from cache: {:?}", object_id);
1400                    self.packages.invalidate(object_id);
1401                }
1402                self.object_by_id_cache.invalidate(object_id);
1403                self.cached.object_cache.invalidate(object_id);
1404            }
1405
1406            for ObjectKey(object_id, _) in outputs.deleted.iter().chain(outputs.wrapped.iter()) {
1407                self.object_by_id_cache.invalidate(object_id);
1408                self.cached.object_cache.invalidate(object_id);
1409            }
1410        }
1411
1412        self.dirty.clear();
1413        self.set_backpressure(0);
1414
1415        info!("clearing old transaction locks");
1416        self.object_locks.clear();
1417        info!("clearing object per epoch marker table");
1418        self.store
1419            .clear_object_per_epoch_marker_table(execution_guard)
1420            .expect("db error");
1421    }
1422
1423    fn bulk_insert_genesis_objects_impl(&self, objects: &[Object]) {
1424        self.store
1425            .bulk_insert_genesis_objects(objects)
1426            .expect("db error");
1427        for obj in objects {
1428            self.cached.object_cache.invalidate(&obj.id());
1429            self.object_by_id_cache.invalidate(&obj.id());
1430        }
1431    }
1432
1433    fn insert_genesis_object_impl(&self, object: Object) {
1434        self.object_by_id_cache.invalidate(&object.id());
1435        self.cached.object_cache.invalidate(&object.id());
1436        self.store.insert_genesis_object(object).expect("db error");
1437    }
1438
1439    pub fn clear_caches_and_assert_empty(&self) {
1440        info!("clearing caches");
1441        self.cached.clear_and_assert_empty();
1442        self.object_by_id_cache.invalidate_all();
1443        assert!(&self.object_by_id_cache.is_empty());
1444        self.packages.invalidate_all();
1445        assert_empty(&self.packages);
1446    }
1447}
1448
1449fn account_amount_from_object(account_obj: &Object) -> u128 {
1450    let (_, AccumulatorValue::U128(value)) =
1451        account_obj.data.try_as_move().unwrap().try_into().unwrap();
1452    value.value
1453}
1454
1455impl AccountFundsRead for WritebackCache {
1456    fn get_latest_account_amount(&self, account_id: &AccumulatorObjId) -> u128 {
1457        ObjectCacheRead::get_object(self, account_id.inner())
1458            .map(|account_obj| account_amount_from_object(&account_obj))
1459            .unwrap_or(0)
1460    }
1461
1462    fn get_consistent_latest_account_amount_and_version(
1463        &self,
1464        account_id: &AccumulatorObjId,
1465    ) -> (u128, SequenceNumber) {
1466        // Settlement is not atomic. A settlement transaction writes the accumulator
1467        // objects at version V+1 first, and then a later barrier transaction bumps the
1468        // root from V to V+1. A reader that observes the state in between sees
1469        // post-settlement account objects alongside the pre-settlement root version,
1470        // and reading the "latest" account can therefore disagree with the root we
1471        // just captured.
1472        //
1473        // We handle this with two pieces:
1474        //
1475        // 1. MVCC read capped at the captured root version (see the call site below).
1476        //    By construction of the settlement/barrier ordering, every account object's
1477        //    version is <= the root version after the corresponding barrier runs, so
1478        //    capping at the captured root strips away any newer-settlement writes that
1479        //    have raced ahead of the barrier. The returned amount is the balance at or
1480        //    before the captured root version, even if the account object has a newer
1481        //    latest version by the time this method returns.
1482        //
1483        // 2. Root-version stability check (pre == post). `get_account_amount_at_version`
1484        //    is only safe to call when the target version has not been pruned. Pruning
1485        //    is tied to root advancement, so by reading the root before and after the
1486        //    MVCC read and retrying on mismatch, we ensure that no root advance (and
1487        //    therefore no pruning of the version we read at) could have happened while
1488        //    we were reading — the data we read is still live in the system (memory or
1489        //    db) throughout the call.
1490        let mut pre_root_version =
1491            ObjectCacheRead::get_object(self, &SUI_ACCUMULATOR_ROOT_OBJECT_ID)
1492                .unwrap()
1493                .version();
1494        let starting_root_version = pre_root_version;
1495        let mut loop_iter = 0;
1496        loop {
1497            loop_iter += 1;
1498            // Safe because of (1) and (2) above: the stability check below bounds the
1499            // lifetime of `pre_root_version` to a window in which no pruning happens.
1500            let value = self.get_account_amount_at_version(account_id, pre_root_version);
1501            let post_root_version =
1502                ObjectCacheRead::get_object(self, &SUI_ACCUMULATOR_ROOT_OBJECT_ID)
1503                    .unwrap()
1504                    .version();
1505            if pre_root_version == post_root_version {
1506                if loop_iter > 10 {
1507                    debug!(
1508                        iterations = loop_iter,
1509                        starting_root_version = %starting_root_version,
1510                        ending_root_version = %post_root_version,
1511                        "Root version stabilized after multiple iterations during MVCC read"
1512                    );
1513                }
1514                return (value, pre_root_version);
1515            }
1516            debug!(
1517                "Root version changed from {} to {} during MVCC read, retrying",
1518                pre_root_version, post_root_version
1519            );
1520            pre_root_version = post_root_version;
1521        }
1522    }
1523
1524    fn get_account_amount_at_version(
1525        &self,
1526        account_id: &AccumulatorObjId,
1527        version: SequenceNumber,
1528    ) -> u128 {
1529        let account_obj = self.find_object_lt_or_eq_version(*account_id.inner(), version);
1530        account_obj
1531            .map(|account_obj| account_amount_from_object(&account_obj))
1532            .unwrap_or(0)
1533    }
1534}
1535
1536impl ExecutionCacheAPI for WritebackCache {}
1537
1538impl ExecutionCacheCommit for WritebackCache {
1539    fn build_db_batch(&self, epoch: EpochId, digests: &[TransactionDigest]) -> Batch {
1540        self.build_db_batch(epoch, digests)
1541    }
1542
1543    fn set_highest_committed_checkpoint_in_batch(
1544        &self,
1545        batch: &mut Batch,
1546        checkpoint: CheckpointSequenceNumber,
1547    ) {
1548        self.store
1549            .perpetual_tables
1550            .set_highest_committed_checkpoint(&mut batch.1, checkpoint)
1551            .expect("db error");
1552    }
1553
1554    fn commit_transaction_outputs(
1555        &self,
1556        epoch: EpochId,
1557        batch: Batch,
1558        digests: &[TransactionDigest],
1559    ) {
1560        WritebackCache::commit_transaction_outputs(self, epoch, batch, digests)
1561    }
1562
1563    fn persist_transaction(&self, tx: &VerifiedExecutableTransaction) {
1564        self.store.persist_transaction(tx).expect("db error");
1565    }
1566
1567    fn approximate_pending_transaction_count(&self) -> u64 {
1568        WritebackCache::approximate_pending_transaction_count(self)
1569    }
1570}
1571
1572impl ObjectCacheRead for WritebackCache {
1573    fn get_package_object(&self, package_id: &ObjectID) -> SuiResult<Option<PackageObject>> {
1574        self.metrics
1575            .record_cache_request("package", "package_cache");
1576        if let Some(p) = self.packages.get(package_id) {
1577            if cfg!(debug_assertions) {
1578                let canonical_package = self
1579                    .dirty
1580                    .objects
1581                    .get(package_id)
1582                    .and_then(|v| match v.get_highest().map(|v| v.1.clone()) {
1583                        Some(ObjectEntry::Object(object)) => Some(object),
1584                        _ => None,
1585                    })
1586                    .or_else(|| self.store.get_object(package_id));
1587
1588                if let Some(canonical_package) = canonical_package {
1589                    assert_eq!(
1590                        canonical_package.digest(),
1591                        p.object().digest(),
1592                        "Package object cache is inconsistent for package {:?}",
1593                        package_id
1594                    );
1595                }
1596            }
1597            self.metrics.record_cache_hit("package", "package_cache");
1598            return Ok(Some(p));
1599        } else {
1600            self.metrics.record_cache_miss("package", "package_cache");
1601        }
1602
1603        // We try the dirty objects cache as well before going to the database. This is necessary
1604        // because the package could be evicted from the package cache before it is committed
1605        // to the database.
1606        if let Some(p) = self.get_object_impl("package", package_id) {
1607            if p.is_package() {
1608                let p = PackageObject::new(p);
1609                tracing::trace!(
1610                    "caching package: {:?}",
1611                    p.object().compute_object_reference()
1612                );
1613                self.metrics.record_cache_write("package");
1614                self.packages.insert(*package_id, p.clone());
1615                Ok(Some(p))
1616            } else {
1617                Err(SuiErrorKind::UserInputError {
1618                    error: UserInputError::MoveObjectAsPackage {
1619                        object_id: *package_id,
1620                    },
1621                }
1622                .into())
1623            }
1624        } else {
1625            Ok(None)
1626        }
1627    }
1628
1629    fn force_reload_system_packages(&self, _system_package_ids: &[ObjectID]) {
1630        // This is a no-op because all writes go through the cache, therefore it can never
1631        // be incoherent
1632    }
1633
1634    // get_object and variants.
1635
1636    fn get_object(&self, id: &ObjectID) -> Option<Object> {
1637        self.get_object_impl("object_latest", id)
1638    }
1639
1640    fn get_object_by_key(&self, object_id: &ObjectID, version: SequenceNumber) -> Option<Object> {
1641        match self.get_object_by_key_cache_only(object_id, version) {
1642            CacheResult::Hit(object) => Some(object),
1643            CacheResult::NegativeHit => None,
1644            CacheResult::Miss => self
1645                .record_db_get("object_by_version")
1646                .get_object_by_key(object_id, version),
1647        }
1648    }
1649
1650    fn multi_get_objects_by_key(&self, object_keys: &[ObjectKey]) -> Vec<Option<Object>> {
1651        do_fallback_lookup(
1652            object_keys,
1653            |key| match self.get_object_by_key_cache_only(&key.0, key.1) {
1654                CacheResult::Hit(maybe_object) => CacheResult::Hit(Some(maybe_object)),
1655                CacheResult::NegativeHit => CacheResult::NegativeHit,
1656                CacheResult::Miss => CacheResult::Miss,
1657            },
1658            |remaining| {
1659                self.record_db_multi_get("object_by_version", remaining.len())
1660                    .multi_get_objects_by_key(remaining)
1661                    .expect("db error")
1662            },
1663        )
1664    }
1665
1666    fn object_exists_by_key(&self, object_id: &ObjectID, version: SequenceNumber) -> bool {
1667        match self.get_object_by_key_cache_only(object_id, version) {
1668            CacheResult::Hit(_) => true,
1669            CacheResult::NegativeHit => false,
1670            CacheResult::Miss => self
1671                .record_db_get("object_by_version")
1672                .object_exists_by_key(object_id, version)
1673                .expect("db error"),
1674        }
1675    }
1676
1677    fn multi_object_exists_by_key(&self, object_keys: &[ObjectKey]) -> Vec<bool> {
1678        do_fallback_lookup(
1679            object_keys,
1680            |key| match self.get_object_by_key_cache_only(&key.0, key.1) {
1681                CacheResult::Hit(_) => CacheResult::Hit(true),
1682                CacheResult::NegativeHit => CacheResult::Hit(false),
1683                CacheResult::Miss => CacheResult::Miss,
1684            },
1685            |remaining| {
1686                self.record_db_multi_get("object_by_version", remaining.len())
1687                    .multi_object_exists_by_key(remaining)
1688                    .expect("db error")
1689            },
1690        )
1691    }
1692
1693    fn get_latest_object_ref_or_tombstone(&self, object_id: ObjectID) -> Option<ObjectRef> {
1694        match self.get_object_entry_by_id_cache_only("latest_objref_or_tombstone", &object_id) {
1695            CacheResult::Hit((version, entry)) => Some(match entry {
1696                ObjectEntry::Object(object) => object.compute_object_reference(),
1697                ObjectEntry::Deleted => (object_id, version, ObjectDigest::OBJECT_DIGEST_DELETED),
1698                ObjectEntry::Wrapped => (object_id, version, ObjectDigest::OBJECT_DIGEST_WRAPPED),
1699            }),
1700            CacheResult::NegativeHit => None,
1701            CacheResult::Miss => self
1702                .record_db_get("latest_objref_or_tombstone")
1703                .get_latest_object_ref_or_tombstone(object_id)
1704                .expect("db error"),
1705        }
1706    }
1707
1708    fn get_latest_object_or_tombstone(
1709        &self,
1710        object_id: ObjectID,
1711    ) -> Option<(ObjectKey, ObjectOrTombstone)> {
1712        match self.get_object_entry_by_id_cache_only("latest_object_or_tombstone", &object_id) {
1713            CacheResult::Hit((version, entry)) => {
1714                let key = ObjectKey(object_id, version);
1715                Some(match entry {
1716                    ObjectEntry::Object(object) => (key, object.into()),
1717                    ObjectEntry::Deleted => (
1718                        key,
1719                        ObjectOrTombstone::Tombstone((
1720                            object_id,
1721                            version,
1722                            ObjectDigest::OBJECT_DIGEST_DELETED,
1723                        )),
1724                    ),
1725                    ObjectEntry::Wrapped => (
1726                        key,
1727                        ObjectOrTombstone::Tombstone((
1728                            object_id,
1729                            version,
1730                            ObjectDigest::OBJECT_DIGEST_WRAPPED,
1731                        )),
1732                    ),
1733                })
1734            }
1735            CacheResult::NegativeHit => None,
1736            CacheResult::Miss => self
1737                .record_db_get("latest_object_or_tombstone")
1738                .get_latest_object_or_tombstone(object_id)
1739                .expect("db error"),
1740        }
1741    }
1742
1743    fn multi_input_objects_available_cache_only(&self, keys: &[InputKey]) -> Vec<bool> {
1744        keys.iter()
1745            .map(|key| {
1746                if key.is_cancelled() {
1747                    true
1748                } else {
1749                    match key {
1750                        // See multi_input_objects_available: a consensus input is not satisfied
1751                        // by an object at the same version on a different stream.
1752                        InputKey::VersionedObject { id, version } => {
1753                            match self.get_object_by_key_cache_only(&id.id(), *version) {
1754                                CacheResult::Hit(object) => {
1755                                    !matches!(id, FullObjectID::Consensus(_))
1756                                        || object.full_id() == *id
1757                                }
1758                                _ => false,
1759                            }
1760                        }
1761                        InputKey::Package { id } => self.packages.contains_key(id),
1762                    }
1763                }
1764            })
1765            .collect()
1766    }
1767
1768    #[instrument(level = "trace", skip_all, fields(object_id, version_bound))]
1769    fn find_object_lt_or_eq_version(
1770        &self,
1771        object_id: ObjectID,
1772        version_bound: SequenceNumber,
1773    ) -> Option<Object> {
1774        macro_rules! check_cache_entry {
1775            ($level: expr, $objects: expr) => {
1776                self.metrics
1777                    .record_cache_request("object_lt_or_eq_version", $level);
1778                if let Some(objects) = $objects {
1779                    if let Some((_, object)) = objects
1780                        .all_versions_lt_or_eq_descending(&version_bound)
1781                        .next()
1782                    {
1783                        if let ObjectEntry::Object(object) = object {
1784                            self.metrics
1785                                .record_cache_hit("object_lt_or_eq_version", $level);
1786                            return Some(object.clone());
1787                        } else {
1788                            // if we find a tombstone, the object does not exist
1789                            self.metrics
1790                                .record_cache_negative_hit("object_lt_or_eq_version", $level);
1791                            return None;
1792                        }
1793                    } else {
1794                        self.metrics
1795                            .record_cache_miss("object_lt_or_eq_version", $level);
1796                    }
1797                }
1798            };
1799        }
1800
1801        // if we have the latest version cached, and it is within the bound, we are done
1802        self.metrics
1803            .record_cache_request("object_lt_or_eq_version", "object_by_id");
1804        let latest_cache_entry = self.object_by_id_cache.get(&object_id);
1805        if let Some(latest) = &latest_cache_entry {
1806            let latest = latest.lock();
1807            match &*latest {
1808                LatestObjectCacheEntry::Object(latest_version, object) => {
1809                    if *latest_version <= version_bound {
1810                        if let ObjectEntry::Object(object) = object {
1811                            self.metrics
1812                                .record_cache_hit("object_lt_or_eq_version", "object_by_id");
1813                            return Some(object.clone());
1814                        } else {
1815                            // object is a tombstone, but is still within the version bound
1816                            self.metrics.record_cache_negative_hit(
1817                                "object_lt_or_eq_version",
1818                                "object_by_id",
1819                            );
1820                            return None;
1821                        }
1822                    }
1823                    // latest object is not within the version bound. fall through.
1824                }
1825                // No object by this ID exists at all
1826                LatestObjectCacheEntry::NonExistent => {
1827                    self.metrics
1828                        .record_cache_negative_hit("object_lt_or_eq_version", "object_by_id");
1829                    return None;
1830                }
1831            }
1832        }
1833        self.metrics
1834            .record_cache_miss("object_lt_or_eq_version", "object_by_id");
1835
1836        Self::with_locked_cache_entries(
1837            &self.dirty.objects,
1838            &self.cached.object_cache,
1839            &object_id,
1840            |dirty_entry, cached_entry| {
1841                check_cache_entry!("committed", dirty_entry);
1842                check_cache_entry!("uncommitted", cached_entry);
1843
1844                // Much of the time, the query will be for the very latest object version, so
1845                // try that first. But we have to be careful:
1846                // 1. We must load the tombstone if it is present, because its version may exceed
1847                //    the version_bound, in which case we must do a scan.
1848                // 2. You might think we could just call `self.store.get_latest_object_or_tombstone` here.
1849                //    But we cannot, because there may be a more recent version in the dirty set, which
1850                //    we skipped over in check_cache_entry! because of the version bound. However, if we
1851                //    skipped it above, we will skip it here as well, again due to the version bound.
1852                // 3. Despite that, we really want to warm the cache here. Why? Because if the object is
1853                //    cold (not being written to), then we will very soon be able to start serving reads
1854                //    of it from the object_by_id cache, IF we can warm the cache. If we don't warm the
1855                //    the cache here, and no writes to the object occur, then we will always have to go
1856                //    to the db for the object.
1857                //
1858                // Lastly, it is important to understand the rationale for all this: If the object is
1859                // write-hot, we will serve almost all reads to it from the dirty set (or possibly the
1860                // cached set if it is only written to once every few checkpoints). If the object is
1861                // write-cold (or non-existent) and read-hot, then we will serve almost all reads to it
1862                // from the object_by_id cache check above.  Most of the apparently wasteful code here
1863                // exists only to ensure correctness in all the edge cases.
1864                let latest: Option<(SequenceNumber, ObjectEntry)> =
1865                    if let Some(dirty_set) = dirty_entry {
1866                        dirty_set
1867                            .get_highest()
1868                            .cloned()
1869                            .tap_none(|| panic!("dirty set cannot be empty"))
1870                    } else {
1871                        // TODO: we should try not to read from the db while holding the locks.
1872                        self.record_db_get("object_lt_or_eq_version_latest")
1873                            .get_latest_object_or_tombstone(object_id)
1874                            .expect("db error")
1875                            .map(|(ObjectKey(_, version), obj_or_tombstone)| {
1876                                (version, ObjectEntry::from(obj_or_tombstone))
1877                            })
1878                    };
1879
1880                if let Some((obj_version, obj_entry)) = latest {
1881                    // we can always cache the latest object (or tombstone), even if it is not within the
1882                    // version_bound. This is done in order to warm the cache in the case where a sequence
1883                    // of transactions all read the same child object without writing to it.
1884
1885                    // Note: no need to call with_object_by_id_cache_update here, because we are holding
1886                    // the lock on the dirty cache entry, and `latest` cannot become out-of-date
1887                    // while we hold that lock.
1888                    self.cache_latest_object_by_id(
1889                        &object_id,
1890                        LatestObjectCacheEntry::Object(obj_version, obj_entry.clone()),
1891                        // We can get a ticket at the last second, because we are holding the lock
1892                        // on dirty, so there cannot be any concurrent writes.
1893                        self.object_by_id_cache.get_ticket_for_read(&object_id),
1894                    );
1895
1896                    if obj_version <= version_bound {
1897                        match obj_entry {
1898                            ObjectEntry::Object(object) => Some(object),
1899                            ObjectEntry::Deleted | ObjectEntry::Wrapped => None,
1900                        }
1901                    } else {
1902                        // The latest object exceeded the bound, so now we have to do a scan
1903                        // But we already know there is no dirty entry within the bound,
1904                        // so we go to the db.
1905                        self.record_db_get("object_lt_or_eq_version_scan")
1906                            .find_object_lt_or_eq_version(object_id, version_bound)
1907                            .expect("db error")
1908                    }
1909
1910                // no object found in dirty set or db, object does not exist
1911                // When this is called from a read api (i.e. not the execution path) it is
1912                // possible that the object has been deleted and pruned. In this case,
1913                // there would be no entry at all on disk, but we may have a tombstone in the
1914                // cache
1915                } else if let Some(latest_cache_entry) = latest_cache_entry {
1916                    // If there is a latest cache entry, it had better not be a live object!
1917                    assert!(!latest_cache_entry.lock().is_alive());
1918                    None
1919                } else {
1920                    // If there is no latest cache entry, we can insert one.
1921                    let highest = cached_entry.and_then(|c| c.get_highest());
1922                    assert!(highest.is_none() || highest.unwrap().1.is_tombstone());
1923                    self.cache_object_not_found(
1924                        &object_id,
1925                        // okay to get ticket at last second - see above
1926                        self.object_by_id_cache.get_ticket_for_read(&object_id),
1927                    );
1928                    None
1929                }
1930            },
1931        )
1932    }
1933
1934    fn find_object_or_tombstone_lt_or_eq_version_unlocked(
1935        &self,
1936        object_id: ObjectID,
1937        version_bound: SequenceNumber,
1938    ) -> Option<FoundObjectOrTombstone> {
1939        // Copied out under the shard's read lock, which is released at the end of the statement,
1940        // before the store read.
1941        let uncommitted = self.dirty.objects.get(&object_id).and_then(|versions| {
1942            versions
1943                .all_versions_lt_or_eq_descending(&version_bound)
1944                .next()
1945                .map(|(version, entry)| (*version, entry.clone()))
1946        });
1947        // `commit_transaction_outputs` writes a version to the store before
1948        // `flush_objects_from_dirty_to_cached` removes it from the dirty set, so a version that
1949        // left the dirty set after the read above is in the store now.
1950        let committed = self
1951            .record_db_get("object_or_tombstone_lt_or_eq_version_unlocked")
1952            .perpetual_tables
1953            .find_object_or_tombstone_lt_or_eq_version(object_id, version_bound)
1954            .expect("db error");
1955        let committed_version = committed.as_ref().map(|committed| match committed {
1956            ObjectOrTombstone::Object(object) => object.version(),
1957            ObjectOrTombstone::Tombstone((_, version, _)) => *version,
1958        });
1959        match uncommitted {
1960            Some((version, entry))
1961                if committed_version.is_none_or(|committed| committed < version) =>
1962            {
1963                Some(FoundObjectOrTombstone {
1964                    entry: entry.into_object_or_tombstone(object_id, version),
1965                    committed: false,
1966                })
1967            }
1968            _ => committed.map(|entry| FoundObjectOrTombstone {
1969                entry,
1970                committed: true,
1971            }),
1972        }
1973    }
1974
1975    fn get_sui_system_state_object_unsafe(&self) -> SuiResult<SuiSystemState> {
1976        get_sui_system_state(self)
1977    }
1978
1979    fn get_bridge_object_unsafe(&self) -> SuiResult<Bridge> {
1980        get_bridge(self)
1981    }
1982
1983    fn get_marker_value(
1984        &self,
1985        object_key: FullObjectKey,
1986        epoch_id: EpochId,
1987    ) -> Option<MarkerValue> {
1988        match self.get_marker_value_cache_only(object_key, epoch_id) {
1989            CacheResult::Hit(marker) => Some(marker),
1990            CacheResult::NegativeHit => None,
1991            CacheResult::Miss => self
1992                .record_db_get("marker_by_version")
1993                .get_marker_value(object_key, epoch_id)
1994                .expect("db error"),
1995        }
1996    }
1997
1998    fn get_latest_marker(
1999        &self,
2000        object_id: FullObjectID,
2001        epoch_id: EpochId,
2002    ) -> Option<(SequenceNumber, MarkerValue)> {
2003        match self.get_latest_marker_value_cache_only(object_id, epoch_id) {
2004            CacheResult::Hit((v, marker)) => Some((v, marker)),
2005            CacheResult::NegativeHit => {
2006                panic!("cannot have negative hit when getting latest marker")
2007            }
2008            CacheResult::Miss => self
2009                .record_db_get("marker_latest")
2010                .get_latest_marker(object_id, epoch_id)
2011                .expect("db error"),
2012        }
2013    }
2014
2015    #[cfg(test)]
2016    fn get_lock(&self, obj_ref: ObjectRef, epoch_store: &AuthorityPerEpochStore) -> SuiLockResult {
2017        let cur_epoch = epoch_store.epoch();
2018        let Some(obj) = self.get_object_impl("lock", &obj_ref.0) else {
2019            return Err(SuiError::from(UserInputError::ObjectNotFound {
2020                object_id: obj_ref.0,
2021                // even though we know the requested version, we leave it as None to indicate
2022                // that the object does not exist at any version
2023                version: None,
2024            }));
2025        };
2026        let actual_objref = obj.compute_object_reference();
2027        if obj_ref != actual_objref {
2028            Ok(ObjectLockStatus::LockedAtDifferentVersion {
2029                locked_ref: actual_objref,
2030            })
2031        } else {
2032            // requested object ref is live, check if there is a lock
2033            Ok(
2034                match self
2035                    .object_locks
2036                    .get_transaction_lock(&obj_ref, epoch_store)?
2037                {
2038                    Some(tx_digest) => ObjectLockStatus::LockedToTx {
2039                        locked_by_tx: LockDetailsDeprecated {
2040                            epoch: cur_epoch,
2041                            tx_digest,
2042                        },
2043                    },
2044                    None => ObjectLockStatus::Initialized,
2045                },
2046            )
2047        }
2048    }
2049
2050    fn _get_live_objref(&self, object_id: ObjectID) -> SuiResult<ObjectRef> {
2051        let obj = self.get_object_impl("live_objref", &object_id).ok_or(
2052            UserInputError::ObjectNotFound {
2053                object_id,
2054                version: None,
2055            },
2056        )?;
2057        Ok(obj.compute_object_reference())
2058    }
2059
2060    fn object_versions_iter(
2061        &self,
2062        object_id: ObjectID,
2063        versions: std::ops::RangeInclusive<SequenceNumber>,
2064        descending: bool,
2065    ) -> SuiResult<Box<dyn Iterator<Item = SuiResult<ObjectOrTombstone>> + '_>> {
2066        let versions = self
2067            .store
2068            .perpetual_tables
2069            .object_versions_iter(object_id, versions, descending)?;
2070        Ok(Box::new(versions))
2071    }
2072
2073    fn get_highest_pruned_checkpoint(&self) -> Option<CheckpointSequenceNumber> {
2074        self.store
2075            .perpetual_tables
2076            .get_highest_pruned_checkpoint()
2077            .expect("db error")
2078    }
2079
2080    fn notify_read_input_objects<'a>(
2081        &'a self,
2082        input_and_receiving_keys: &'a [InputKey],
2083        receiving_keys: &'a HashSet<InputKey>,
2084        epoch: EpochId,
2085    ) -> BoxFuture<'a, ()> {
2086        self.object_notify_read
2087            .read(
2088                "notify_read_input_objects",
2089                input_and_receiving_keys,
2090                move |keys| {
2091                    self.multi_input_objects_available(keys, receiving_keys, epoch)
2092                        .into_iter()
2093                        .map(|available| if available { Some(()) } else { None })
2094                        .collect::<Vec<_>>()
2095                },
2096            )
2097            .map(|_| ())
2098            .boxed()
2099    }
2100}
2101
2102impl TransactionCacheRead for WritebackCache {
2103    fn multi_get_transaction_blocks(
2104        &self,
2105        digests: &[TransactionDigest],
2106    ) -> Vec<Option<Arc<VerifiedTransaction>>> {
2107        let digests_and_tickets: Vec<_> = digests
2108            .iter()
2109            .map(|d| (*d, self.cached.transactions.get_ticket_for_read(d)))
2110            .collect();
2111        do_fallback_lookup(
2112            &digests_and_tickets,
2113            |(digest, _)| {
2114                self.metrics
2115                    .record_cache_request("transaction_block", "uncommitted");
2116                if let Some(tx) = self.dirty.pending_transaction_writes.get(digest) {
2117                    self.metrics
2118                        .record_cache_hit("transaction_block", "uncommitted");
2119                    return CacheResult::Hit(Some(tx.transaction.clone()));
2120                }
2121                self.metrics
2122                    .record_cache_miss("transaction_block", "uncommitted");
2123
2124                self.metrics
2125                    .record_cache_request("transaction_block", "committed");
2126
2127                match self
2128                    .cached
2129                    .transactions
2130                    .get(digest)
2131                    .map(|l| l.lock().clone())
2132                {
2133                    Some(PointCacheItem::Some(tx)) => {
2134                        self.metrics
2135                            .record_cache_hit("transaction_block", "committed");
2136                        CacheResult::Hit(Some(tx))
2137                    }
2138                    Some(PointCacheItem::None) => CacheResult::NegativeHit,
2139                    None => {
2140                        self.metrics
2141                            .record_cache_miss("transaction_block", "committed");
2142
2143                        CacheResult::Miss
2144                    }
2145                }
2146            },
2147            |remaining| {
2148                let remaining_digests: Vec<_> = remaining.iter().map(|(d, _)| *d).collect();
2149                let results: Vec<_> = self
2150                    .record_db_multi_get("transaction_block", remaining.len())
2151                    .multi_get_transaction_blocks(&remaining_digests)
2152                    .expect("db error")
2153                    .into_iter()
2154                    .map(|o| o.map(Arc::new))
2155                    .collect();
2156                for ((digest, ticket), result) in remaining.iter().zip_debug_eq(results.iter()) {
2157                    if result.is_none() {
2158                        self.cached.transactions.insert(digest, None, *ticket).ok();
2159                    }
2160                }
2161                results
2162            },
2163        )
2164    }
2165
2166    fn multi_get_executed_effects_digests(
2167        &self,
2168        digests: &[TransactionDigest],
2169    ) -> Vec<Option<TransactionEffectsDigest>> {
2170        let digests_and_tickets: Vec<_> = digests
2171            .iter()
2172            .map(|d| {
2173                (
2174                    *d,
2175                    self.cached.executed_effects_digests.get_ticket_for_read(d),
2176                )
2177            })
2178            .collect();
2179        do_fallback_lookup(
2180            &digests_and_tickets,
2181            |(digest, _)| {
2182                self.metrics
2183                    .record_cache_request("executed_effects_digests", "uncommitted");
2184                if let Some(digest) = self.dirty.executed_effects_digests.get(digest) {
2185                    self.metrics
2186                        .record_cache_hit("executed_effects_digests", "uncommitted");
2187                    return CacheResult::Hit(Some(*digest));
2188                }
2189                self.metrics
2190                    .record_cache_miss("executed_effects_digests", "uncommitted");
2191
2192                self.metrics
2193                    .record_cache_request("executed_effects_digests", "committed");
2194                match self
2195                    .cached
2196                    .executed_effects_digests
2197                    .get(digest)
2198                    .map(|l| *l.lock())
2199                {
2200                    Some(PointCacheItem::Some(digest)) => {
2201                        self.metrics
2202                            .record_cache_hit("executed_effects_digests", "committed");
2203                        CacheResult::Hit(Some(digest))
2204                    }
2205                    Some(PointCacheItem::None) => CacheResult::NegativeHit,
2206                    None => {
2207                        self.metrics
2208                            .record_cache_miss("executed_effects_digests", "committed");
2209                        CacheResult::Miss
2210                    }
2211                }
2212            },
2213            |remaining| {
2214                let remaining_digests: Vec<_> = remaining.iter().map(|(d, _)| *d).collect();
2215                let results = self
2216                    .record_db_multi_get("executed_effects_digests", remaining.len())
2217                    .multi_get_executed_effects_digests(&remaining_digests)
2218                    .expect("db error");
2219                for ((digest, ticket), result) in remaining.iter().zip_debug_eq(results.iter()) {
2220                    if result.is_none() {
2221                        self.cached
2222                            .executed_effects_digests
2223                            .insert(digest, None, *ticket)
2224                            .ok();
2225                    }
2226                }
2227                results
2228            },
2229        )
2230    }
2231
2232    fn multi_get_effects(
2233        &self,
2234        digests: &[TransactionEffectsDigest],
2235    ) -> Vec<Option<TransactionEffects>> {
2236        let digests_and_tickets: Vec<_> = digests
2237            .iter()
2238            .map(|d| (*d, self.cached.transaction_effects.get_ticket_for_read(d)))
2239            .collect();
2240        do_fallback_lookup(
2241            &digests_and_tickets,
2242            |(digest, _)| {
2243                self.metrics
2244                    .record_cache_request("transaction_effects", "uncommitted");
2245                if let Some(effects) = self.dirty.transaction_effects.get(digest) {
2246                    self.metrics
2247                        .record_cache_hit("transaction_effects", "uncommitted");
2248                    return CacheResult::Hit(Some(effects.clone()));
2249                }
2250                self.metrics
2251                    .record_cache_miss("transaction_effects", "uncommitted");
2252
2253                self.metrics
2254                    .record_cache_request("transaction_effects", "committed");
2255                match self
2256                    .cached
2257                    .transaction_effects
2258                    .get(digest)
2259                    .map(|l| l.lock().clone())
2260                {
2261                    Some(PointCacheItem::Some(effects)) => {
2262                        self.metrics
2263                            .record_cache_hit("transaction_effects", "committed");
2264                        CacheResult::Hit(Some((*effects).clone()))
2265                    }
2266                    Some(PointCacheItem::None) => CacheResult::NegativeHit,
2267                    None => {
2268                        self.metrics
2269                            .record_cache_miss("transaction_effects", "committed");
2270                        CacheResult::Miss
2271                    }
2272                }
2273            },
2274            |remaining| {
2275                let remaining_digests: Vec<_> = remaining.iter().map(|(d, _)| *d).collect();
2276                let results = self
2277                    .record_db_multi_get("transaction_effects", remaining.len())
2278                    .multi_get_effects(remaining_digests.iter())
2279                    .expect("db error");
2280                for ((digest, ticket), result) in remaining.iter().zip_debug_eq(results.iter()) {
2281                    if result.is_none() {
2282                        self.cached
2283                            .transaction_effects
2284                            .insert(digest, None, *ticket)
2285                            .ok();
2286                    }
2287                }
2288                results
2289            },
2290        )
2291    }
2292
2293    fn transaction_executed_in_last_epoch(
2294        &self,
2295        digest: &TransactionDigest,
2296        current_epoch: EpochId,
2297    ) -> bool {
2298        if current_epoch == 0 {
2299            return false;
2300        }
2301        let last_epoch = current_epoch - 1;
2302        let cache_key = (last_epoch, *digest);
2303
2304        let ticket = self
2305            .cached
2306            .transaction_executed_in_last_epoch
2307            .get_ticket_for_read(&cache_key);
2308
2309        if let Some(cached) = self
2310            .cached
2311            .transaction_executed_in_last_epoch
2312            .get(&cache_key)
2313        {
2314            return cached.lock().is_some();
2315        }
2316
2317        let was_executed = self
2318            .store
2319            .perpetual_tables
2320            .was_transaction_executed_in_last_epoch(digest, current_epoch);
2321
2322        let value = if was_executed { Some(()) } else { None };
2323        self.cached
2324            .transaction_executed_in_last_epoch
2325            .insert(&cache_key, value, ticket)
2326            .ok();
2327
2328        was_executed
2329    }
2330
2331    fn notify_read_executed_effects_digests<'a>(
2332        &'a self,
2333        task_name: &'static str,
2334        digests: &'a [TransactionDigest],
2335    ) -> BoxFuture<'a, Vec<TransactionEffectsDigest>> {
2336        self.executed_effects_digests_notify_read
2337            .read(task_name, digests, |digests| {
2338                self.multi_get_executed_effects_digests(digests)
2339            })
2340            .boxed()
2341    }
2342
2343    fn multi_get_events(
2344        &self,
2345        event_digests: &[TransactionDigest],
2346    ) -> Vec<Option<TransactionEvents>> {
2347        fn map_events(events: TransactionEvents) -> Option<TransactionEvents> {
2348            if events.data.is_empty() {
2349                None
2350            } else {
2351                Some(events)
2352            }
2353        }
2354
2355        let digests_and_tickets: Vec<_> = event_digests
2356            .iter()
2357            .map(|d| (*d, self.cached.transaction_events.get_ticket_for_read(d)))
2358            .collect();
2359        do_fallback_lookup(
2360            &digests_and_tickets,
2361            |(digest, _)| {
2362                self.metrics
2363                    .record_cache_request("transaction_events", "uncommitted");
2364                if let Some(events) = self.dirty.transaction_events.get(digest).map(|e| e.clone()) {
2365                    self.metrics
2366                        .record_cache_hit("transaction_events", "uncommitted");
2367
2368                    return CacheResult::Hit(map_events(events));
2369                }
2370                self.metrics
2371                    .record_cache_miss("transaction_events", "uncommitted");
2372
2373                self.metrics
2374                    .record_cache_request("transaction_events", "committed");
2375                match self
2376                    .cached
2377                    .transaction_events
2378                    .get(digest)
2379                    .map(|l| l.lock().clone())
2380                {
2381                    Some(PointCacheItem::Some(events)) => {
2382                        self.metrics
2383                            .record_cache_hit("transaction_events", "committed");
2384                        CacheResult::Hit(map_events((*events).clone()))
2385                    }
2386                    Some(PointCacheItem::None) => CacheResult::NegativeHit,
2387                    None => {
2388                        self.metrics
2389                            .record_cache_miss("transaction_events", "committed");
2390
2391                        CacheResult::Miss
2392                    }
2393                }
2394            },
2395            |remaining| {
2396                let remaining_digests: Vec<_> = remaining.iter().map(|(d, _)| *d).collect();
2397                let results = self
2398                    .store
2399                    .multi_get_events(&remaining_digests)
2400                    .expect("db error");
2401                for ((digest, ticket), result) in remaining.iter().zip_debug_eq(results.iter()) {
2402                    if result.is_none() {
2403                        self.cached
2404                            .transaction_events
2405                            .insert(digest, None, *ticket)
2406                            .ok();
2407                    }
2408                }
2409                results
2410            },
2411        )
2412    }
2413
2414    fn get_unchanged_loaded_runtime_objects(
2415        &self,
2416        digest: &TransactionDigest,
2417    ) -> Option<Vec<ObjectKey>> {
2418        self.dirty
2419            .unchanged_loaded_runtime_objects
2420            .get(digest)
2421            .map(|b| b.clone())
2422            .or_else(|| {
2423                self.store
2424                    .get_unchanged_loaded_runtime_objects(digest)
2425                    .expect("db error")
2426            })
2427    }
2428
2429    fn multi_get_unchanged_loaded_runtime_objects(
2430        &self,
2431        digests: &[TransactionDigest],
2432    ) -> Vec<Option<Vec<ObjectKey>>> {
2433        do_fallback_lookup(
2434            digests,
2435            |digest| match self.dirty.unchanged_loaded_runtime_objects.get(digest) {
2436                Some(objects) => CacheResult::Hit(Some(objects.clone())),
2437                None => CacheResult::Miss,
2438            },
2439            |digests| {
2440                self.store
2441                    .multi_get_unchanged_loaded_runtime_objects(digests)
2442                    .expect("db error")
2443            },
2444        )
2445    }
2446
2447    fn get_highest_pruned_effects_checkpoint(&self) -> Option<CheckpointSequenceNumber> {
2448        self.store
2449            .perpetual_tables
2450            .get_highest_pruned_effects_checkpoint()
2451            .expect("db error")
2452    }
2453
2454    fn take_accumulator_events(&self, digest: &TransactionDigest) -> Option<Vec<AccumulatorEvent>> {
2455        self.dirty
2456            .pending_transaction_writes
2457            .get(digest)
2458            .map(|transaction_output| transaction_output.take_accumulator_events())
2459    }
2460}
2461
2462impl ExecutionCacheWrite for WritebackCache {
2463    fn validate_owned_object_versions(&self, owned_input_objects: &[ObjectRef]) -> SuiResult {
2464        ObjectLocks::validate_owned_object_versions(self, owned_input_objects)
2465    }
2466
2467    fn write_transaction_outputs(&self, epoch_id: EpochId, tx_outputs: Arc<TransactionOutputs>) {
2468        WritebackCache::write_transaction_outputs(self, epoch_id, tx_outputs);
2469    }
2470
2471    #[cfg(test)]
2472    fn write_object_entry_for_test(&self, object: Object) {
2473        self.write_object_entry(&object.id(), object.version(), object.into());
2474    }
2475}
2476
2477implement_passthrough_traits!(WritebackCache);
2478
2479impl GlobalStateHashStore for WritebackCache {
2480    fn get_object_ref_prior_to_key_deprecated(
2481        &self,
2482        object_id: &ObjectID,
2483        version: SequenceNumber,
2484    ) -> SuiResult<Option<ObjectRef>> {
2485        // There is probably a more efficient way to implement this, but since this is only used by
2486        // old protocol versions, it is better to do the simple thing that is obviously correct.
2487        // In this case we previous version from all sources and choose the highest
2488        let mut candidates = Vec::new();
2489
2490        let check_versions =
2491            |versions: &CachedVersionMap<ObjectEntry>| match versions.get_prior_to(&version) {
2492                Some((version, object_entry)) => match object_entry {
2493                    ObjectEntry::Object(object) => {
2494                        assert_eq!(object.version(), version);
2495                        Some(object.compute_object_reference())
2496                    }
2497                    ObjectEntry::Deleted => {
2498                        Some((*object_id, version, ObjectDigest::OBJECT_DIGEST_DELETED))
2499                    }
2500                    ObjectEntry::Wrapped => {
2501                        Some((*object_id, version, ObjectDigest::OBJECT_DIGEST_WRAPPED))
2502                    }
2503                },
2504                None => None,
2505            };
2506
2507        // first check dirty data
2508        if let Some(objects) = self.dirty.objects.get(object_id)
2509            && let Some(prior) = check_versions(&objects)
2510        {
2511            candidates.push(prior);
2512        }
2513
2514        if let Some(objects) = self.cached.object_cache.get(object_id)
2515            && let Some(prior) = check_versions(&objects.lock())
2516        {
2517            candidates.push(prior);
2518        }
2519
2520        if let Some(prior) = self
2521            .store
2522            .get_object_ref_prior_to_key_deprecated(object_id, version)?
2523        {
2524            candidates.push(prior);
2525        }
2526
2527        // sort candidates by version, and return the highest
2528        candidates.sort_by_key(|(_, version, _)| *version);
2529        Ok(candidates.pop())
2530    }
2531
2532    fn get_root_state_hash_for_epoch(
2533        &self,
2534        epoch: EpochId,
2535    ) -> SuiResult<Option<(CheckpointSequenceNumber, GlobalStateHash)>> {
2536        self.store.get_root_state_hash_for_epoch(epoch)
2537    }
2538
2539    fn insert_state_hash_for_epoch(
2540        &self,
2541        epoch: EpochId,
2542        checkpoint_seq_num: &CheckpointSequenceNumber,
2543        acc: &GlobalStateHash,
2544    ) -> SuiResult {
2545        self.store
2546            .insert_state_hash_for_epoch(epoch, checkpoint_seq_num, acc)
2547    }
2548
2549    fn iter_live_object_set(
2550        &self,
2551        include_wrapped_tombstone: bool,
2552    ) -> Box<dyn Iterator<Item = LiveObject> + '_> {
2553        // The only time it is safe to iterate the live object set is at an epoch boundary,
2554        // at which point the db is consistent and the dirty cache is empty. So this does
2555        // read the cache
2556        assert!(
2557            self.dirty.is_empty(),
2558            "cannot iterate live object set with dirty data"
2559        );
2560        self.store.iter_live_object_set(include_wrapped_tombstone)
2561    }
2562
2563    // A version of iter_live_object_set that reads the cache. Only use for testing. If used
2564    // on a live validator, can cause the server to block for as long as it takes to iterate
2565    // the entire live object set.
2566    fn iter_cached_live_object_set_for_testing(
2567        &self,
2568        include_wrapped_tombstone: bool,
2569    ) -> Box<dyn Iterator<Item = LiveObject> + '_> {
2570        // hold iter until we are finished to prevent any concurrent inserts/deletes
2571        let iter = self.dirty.objects.iter();
2572        let mut dirty_objects = BTreeMap::new();
2573
2574        // add everything from the store
2575        for obj in self.store.iter_live_object_set(include_wrapped_tombstone) {
2576            dirty_objects.insert(obj.object_id(), obj);
2577        }
2578
2579        // add everything from the cache, but also remove deletions
2580        for entry in iter {
2581            let id = *entry.key();
2582            let value = entry.value();
2583            match value.get_highest().unwrap() {
2584                (_, ObjectEntry::Object(object)) => {
2585                    dirty_objects.insert(id, LiveObject::Normal(object.clone()));
2586                }
2587                (version, ObjectEntry::Wrapped) => {
2588                    if include_wrapped_tombstone {
2589                        dirty_objects.insert(id, LiveObject::Wrapped(ObjectKey(id, *version)));
2590                    } else {
2591                        dirty_objects.remove(&id);
2592                    }
2593                }
2594                (_, ObjectEntry::Deleted) => {
2595                    dirty_objects.remove(&id);
2596                }
2597            }
2598        }
2599
2600        Box::new(dirty_objects.into_values())
2601    }
2602}
2603
2604// TODO: For correctness, we must at least invalidate the cache when items are written through this
2605// trait (since they could be negatively cached as absent). But it may or may not be optimal to
2606// actually insert them into the cache. For instance if state sync is running ahead of execution,
2607// they might evict other items that are about to be read. This could be an area for tuning in the
2608// future.
2609impl StateSyncAPI for WritebackCache {
2610    fn insert_transaction_and_effects(
2611        &self,
2612        transaction: &VerifiedTransaction,
2613        transaction_effects: &TransactionEffects,
2614    ) {
2615        self.store
2616            .insert_transaction_and_effects(transaction, transaction_effects)
2617            .expect("db error");
2618        self.cached
2619            .transactions
2620            .insert(
2621                transaction.digest(),
2622                PointCacheItem::Some(Arc::new(transaction.clone())),
2623                Ticket::Write,
2624            )
2625            .ok();
2626        self.cached
2627            .transaction_effects
2628            .insert(
2629                &transaction_effects.digest(),
2630                PointCacheItem::Some(Arc::new(transaction_effects.clone())),
2631                Ticket::Write,
2632            )
2633            .ok();
2634    }
2635
2636    fn multi_insert_transaction_and_effects(
2637        &self,
2638        transactions_and_effects: &[VerifiedExecutionData],
2639    ) {
2640        self.store
2641            .multi_insert_transaction_and_effects(transactions_and_effects.iter())
2642            .expect("db error");
2643        for VerifiedExecutionData {
2644            transaction,
2645            effects,
2646        } in transactions_and_effects
2647        {
2648            self.cached
2649                .transactions
2650                .insert(
2651                    transaction.digest(),
2652                    PointCacheItem::Some(Arc::new(transaction.clone())),
2653                    Ticket::Write,
2654                )
2655                .ok();
2656            self.cached
2657                .transaction_effects
2658                .insert(
2659                    &effects.digest(),
2660                    PointCacheItem::Some(Arc::new(effects.clone())),
2661                    Ticket::Write,
2662                )
2663                .ok();
2664        }
2665    }
2666}