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 iota_sdk_types::Identifier;
6use move_binary_format::{CompiledModule, file_format::SignatureToken};
7use move_bytecode_utils::resolve_struct;
8use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr};
9use serde::{Deserialize, Serialize};
10
11use crate::{IOTA_FRAMEWORK_ADDRESS, id::UID};
12
13pub const RESOLVED_IOTA_CLOCK: (&AccountAddress, &IdentStr, &IdentStr) = (
14    &IOTA_FRAMEWORK_ADDRESS,
15    ident_str!("clock"),
16    ident_str!("Clock"),
17);
18pub const CONSENSUS_COMMIT_PROLOGUE_FUNCTION_NAME: Identifier =
19    Identifier::from_static("consensus_commit_prologue");
20
21#[derive(Debug, Serialize, Deserialize)]
22pub struct Clock {
23    pub id: UID,
24    pub timestamp_ms: u64,
25}
26
27impl Clock {
28    pub fn timestamp_ms(&self) -> u64 {
29        self.timestamp_ms
30    }
31
32    /// Detects a `&mut iota::clock::Clock` or `iota::clock::Clock` in the
33    /// signature.
34    pub fn is_mutable(view: &CompiledModule, s: &SignatureToken) -> bool {
35        use SignatureToken as S;
36        match s {
37            S::MutableReference(inner) => Self::is_mutable(view, inner),
38            S::Datatype(idx) => resolve_struct(view, *idx) == RESOLVED_IOTA_CLOCK,
39            _ => false,
40        }
41    }
42}