iota_core/
verify_indexes.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{collections::BTreeMap, sync::Arc};
6
7use anyhow::{Result, anyhow, bail};
8use iota_types::{base_types::ObjectInfo, object::Owner};
9use tracing::info;
10use typed_store::traits::Map;
11
12use crate::{
13    authority::authority_store_tables::LiveObject,
14    jsonrpc_index::{CoinInfo, IndexStore},
15    state_accumulator::AccumulatorStore,
16};
17
18/// This is a very expensive function that verifies some of the secondary
19/// indexes. This is done by iterating through the live object set and
20/// recalculating these secodary indexes.
21pub fn verify_indexes(store: &dyn AccumulatorStore, indexes: Arc<IndexStore>) -> Result<()> {
22    info!("Begin running index verification checks");
23
24    let mut owner_index = BTreeMap::new();
25    let mut coin_index = BTreeMap::new();
26
27    tracing::info!("Reading live objects set");
28    for object in store.iter_live_object_set() {
29        let LiveObject::Normal(object) = object else {
30            continue;
31        };
32        let Owner::AddressOwner(owner) = object.owner else {
33            continue;
34        };
35
36        // Owner Index Calculation
37        let owner_index_key = (owner, object.id());
38        let object_info = ObjectInfo::new(&object.compute_object_reference(), &object);
39
40        owner_index.insert(owner_index_key, object_info);
41
42        // Coin Index Calculation
43        if let Some(type_tag) = object.coin_type_maybe() {
44            let info =
45                CoinInfo::from_object(&object).expect("already checked that this is a coin type");
46            let key = (owner, type_tag.to_string(), object.id());
47
48            coin_index.insert(key, info);
49        }
50    }
51
52    tracing::info!("Live objects set is prepared, about to verify indexes");
53
54    // Verify Owner Index
55    for (key, info) in indexes.tables().owner_index().unbounded_iter() {
56        let calculated_info = owner_index.remove(&key).ok_or_else(|| {
57            anyhow!(
58                "owner_index: found extra, unexpected entry {:?}",
59                (&key, &info)
60            )
61        })?;
62
63        if calculated_info != info {
64            bail!(
65                "owner_index: entry {key:?} is different: expected {calculated_info:?} found {info:?}"
66            );
67        }
68    }
69
70    if !owner_index.is_empty() {
71        bail!("owner_index: is missing entries: {owner_index:?}");
72    }
73    tracing::info!("Owner index is good");
74
75    // Verify Coin Index
76    for (key, info) in indexes.tables().coin_index().unbounded_iter() {
77        let calculated_info = coin_index.remove(&key).ok_or_else(|| {
78            anyhow!(
79                "coin_index: found extra, unexpected entry {:?}",
80                (&key, &info)
81            )
82        })?;
83
84        if calculated_info != info {
85            bail!(
86                "coin_index: entry {key:?} is different: expected {calculated_info:?} found {info:?}"
87            );
88        }
89    }
90    tracing::info!("Coin index is good");
91
92    if !coin_index.is_empty() {
93        bail!("coin_index: is missing entries: {coin_index:?}");
94    }
95
96    info!("Finished running index verification checks");
97
98    Ok(())
99}