iota_types/
clock.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use move_binary_format::{CompiledModule, file_format::SignatureToken};
6use move_bytecode_utils::resolve_struct;
7use move_core_types::{
8    account_address::AccountAddress, ident_str, identifier::IdentStr, language_storage::StructTag,
9};
10use serde::{Deserialize, Serialize};
11
12use crate::{IOTA_FRAMEWORK_ADDRESS, id::UID};
13
14pub const CLOCK_MODULE_NAME: &IdentStr = ident_str!("clock");
15pub const CLOCK_STRUCT_NAME: &IdentStr = ident_str!("Clock");
16pub const RESOLVED_IOTA_CLOCK: (&AccountAddress, &IdentStr, &IdentStr) = (
17    &IOTA_FRAMEWORK_ADDRESS,
18    CLOCK_MODULE_NAME,
19    CLOCK_STRUCT_NAME,
20);
21pub const CONSENSUS_COMMIT_PROLOGUE_FUNCTION_NAME: &IdentStr =
22    ident_str!("consensus_commit_prologue");
23
24#[derive(Debug, Serialize, Deserialize)]
25pub struct Clock {
26    pub id: UID,
27    pub timestamp_ms: u64,
28}
29
30impl Clock {
31    pub fn timestamp_ms(&self) -> u64 {
32        self.timestamp_ms
33    }
34
35    pub fn type_() -> StructTag {
36        StructTag {
37            address: IOTA_FRAMEWORK_ADDRESS,
38            module: CLOCK_MODULE_NAME.to_owned(),
39            name: CLOCK_STRUCT_NAME.to_owned(),
40            type_params: vec![],
41        }
42    }
43
44    /// Detects a `&mut iota::clock::Clock` or `iota::clock::Clock` in the
45    /// signature.
46    pub fn is_mutable(view: &CompiledModule, s: &SignatureToken) -> bool {
47        use SignatureToken as S;
48        match s {
49            S::MutableReference(inner) => Self::is_mutable(view, inner),
50            S::Datatype(idx) => resolve_struct(view, *idx) == RESOLVED_IOTA_CLOCK,
51            _ => false,
52        }
53    }
54}