1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use move_binary_format::{CompiledModule, file_format::SignatureToken};
use move_bytecode_utils::resolve_struct;
use move_core_types::{
    account_address::AccountAddress, ident_str, identifier::IdentStr, language_storage::StructTag,
};
use serde::{Deserialize, Serialize};

use crate::{IOTA_FRAMEWORK_ADDRESS, id::UID};

pub const CLOCK_MODULE_NAME: &IdentStr = ident_str!("clock");
pub const CLOCK_STRUCT_NAME: &IdentStr = ident_str!("Clock");
pub const RESOLVED_IOTA_CLOCK: (&AccountAddress, &IdentStr, &IdentStr) = (
    &IOTA_FRAMEWORK_ADDRESS,
    CLOCK_MODULE_NAME,
    CLOCK_STRUCT_NAME,
);
pub const CONSENSUS_COMMIT_PROLOGUE_FUNCTION_NAME: &IdentStr =
    ident_str!("consensus_commit_prologue");

#[derive(Debug, Serialize, Deserialize)]
pub struct Clock {
    pub id: UID,
    pub timestamp_ms: u64,
}

impl Clock {
    pub fn timestamp_ms(&self) -> u64 {
        self.timestamp_ms
    }

    pub fn type_() -> StructTag {
        StructTag {
            address: IOTA_FRAMEWORK_ADDRESS,
            module: CLOCK_MODULE_NAME.to_owned(),
            name: CLOCK_STRUCT_NAME.to_owned(),
            type_params: vec![],
        }
    }

    /// Detects a `&mut iota::clock::Clock` or `iota::clock::Clock` in the
    /// signature.
    pub fn is_mutable(view: &CompiledModule, s: &SignatureToken) -> bool {
        use SignatureToken as S;
        match s {
            S::MutableReference(inner) => Self::is_mutable(view, inner),
            S::Datatype(idx) => resolve_struct(view, *idx) == RESOLVED_IOTA_CLOCK,
            _ => false,
        }
    }
}