sui_types/
accumulator_metadata.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{
5    MoveTypeTagTrait, SUI_ACCUMULATOR_ROOT_OBJECT_ID, SUI_FRAMEWORK_ADDRESS,
6    dynamic_field::DynamicFieldKey, error::SuiResult, storage::ObjectStore,
7};
8use move_core_types::{
9    ident_str,
10    identifier::IdentStr,
11    language_storage::{StructTag, TypeTag},
12};
13use serde::{Deserialize, Serialize};
14
15pub const ACCUMULATOR_METADATA_MODULE: &IdentStr = ident_str!("accumulator_metadata");
16pub const ACCUMULATOR_OBJECT_COUNT_KEY_STRUCT_NAME: &IdentStr =
17    ident_str!("AccumulatorObjectCountKey");
18
19/// Rust version of the Move sui::accumulator_metadata::AccumulatorObjectCountKey type.
20/// This is used as a dynamic field key to store the net count of accumulator objects
21/// as a dynamic field on the accumulator root object.
22///
23/// There is no u8 in the Move definition, however empty structs in Move
24/// are represented as a single byte 0 in the serialized data.
25#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
26pub struct AccumulatorObjectCountKey(u8);
27
28impl MoveTypeTagTrait for AccumulatorObjectCountKey {
29    fn get_type_tag() -> TypeTag {
30        TypeTag::Struct(Box::new(StructTag {
31            address: SUI_FRAMEWORK_ADDRESS,
32            module: ACCUMULATOR_METADATA_MODULE.to_owned(),
33            name: ACCUMULATOR_OBJECT_COUNT_KEY_STRUCT_NAME.to_owned(),
34            type_params: vec![],
35        }))
36    }
37}
38
39/// Reads the accumulator object count from the accumulator root's dynamic fields.
40pub fn get_accumulator_object_count(object_store: &dyn ObjectStore) -> SuiResult<Option<u64>> {
41    DynamicFieldKey(
42        SUI_ACCUMULATOR_ROOT_OBJECT_ID,
43        AccumulatorObjectCountKey(0),
44        AccumulatorObjectCountKey::get_type_tag(),
45    )
46    .into_unbounded_id()?
47    .load_object(object_store)
48    .map(|o| o.load_value::<u64>())
49    .transpose()
50}