Skip to main content

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 RECORD_ACCUMULATOR_OBJECT_CHANGES_FUNC: &IdentStr =
17    ident_str!("record_accumulator_object_changes");
18pub const ACCUMULATOR_OBJECT_COUNT_KEY_STRUCT_NAME: &IdentStr =
19    ident_str!("AccumulatorObjectCountKey");
20
21/// Rust version of the Move sui::accumulator_metadata::AccumulatorObjectCountKey type.
22/// This is used as a dynamic field key to store the net count of accumulator objects
23/// as a dynamic field on the accumulator root object.
24///
25/// There is no u8 in the Move definition, however empty structs in Move
26/// are represented as a single byte 0 in the serialized data.
27#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
28pub struct AccumulatorObjectCountKey(u8);
29
30impl MoveTypeTagTrait for AccumulatorObjectCountKey {
31    fn get_type_tag() -> TypeTag {
32        TypeTag::Struct(Box::new(StructTag {
33            address: SUI_FRAMEWORK_ADDRESS,
34            module: ACCUMULATOR_METADATA_MODULE.to_owned(),
35            name: ACCUMULATOR_OBJECT_COUNT_KEY_STRUCT_NAME.to_owned(),
36            type_params: vec![],
37        }))
38    }
39}
40
41/// Reads the accumulator object count from the accumulator root's dynamic fields.
42pub fn get_accumulator_object_count(object_store: &dyn ObjectStore) -> SuiResult<Option<u64>> {
43    DynamicFieldKey(
44        SUI_ACCUMULATOR_ROOT_OBJECT_ID,
45        AccumulatorObjectCountKey(0),
46        AccumulatorObjectCountKey::get_type_tag(),
47    )
48    .into_unbounded_id()?
49    .load_object(object_store)
50    .map(|o| o.load_value::<u64>())
51    .transpose()
52}