Timestamp
Rooch's Framework provides a timestamp function, located in frameworks/rooch-framework/sources/timestamp.move
, which saves Unix time. UNIX time, or POSIX time, is the time representation used by UNIX or UNIX-like systems: the total number of seconds from 00:00:00 on January 1, 1970 UTC to the present. On most Unix systems, Unix time can be passed date +%s
command to check.
This module maintains a global wall clock that stores the current Unix time in milliseconds.
Through the timestamp, the genesis time of the Rooch network can be accurately located. As Layer2, Rooch needs to synchronize the block information on Layer1, so timestamp is the key factor to keep data synchronized.
Rooch needs to update or check its own timestamp through the timestamp of Layer1's block header.
In the future, Rooch will also implement TickTransaction
, which can update its own timestamp through the time offset.
Relay Ethereum block information
At present, Rooch has implemented the function of relaying Ethereum block information. You can continuously obtain block transaction information on Ethereum through the following command:
rooch server start --eth-rpc-url https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161
Note that the
timestamp
after the block heightnumber
.
How to view the current timestamp
After starting the relay service, we can use the rooch object --id 0x2::timestamp::Timestamp
command to obtain some information returned by the timestamp object. The returned information includes the current timestamp.
rooch object --id 0x2::timestamp::Timestamp
{
"value": "0x711ab0301fd517b135b88f57e84f254c94758998a602596be8ae7ba56a0d14b300000000000000000000000000000000000000000000000000000000000000000060f968ce8b010000",
"value_type": "0x2::object::ObjectEntity<0x2::timestamp::Timestamp>",
"decoded_value": {
"abilities": 0,
"type": "0x2::object::ObjectEntity<0x2::timestamp::Timestamp>",
"value": {
"flag": 0,
"id": "0x4e8d2c243339c6e02f8b7dd34436a1b1eb541b0fe4d938f845f4dbb9d9f218a2",
"owner": "0x0000000000000000000000000000000000000000000000000000000000000000",
"value": {
"abilities": 8,
"type": "0x2::timestamp::Timestamp",
"value": {
"milliseconds": "1699975068000" <== Note!
}
}
}
}
}
Note that the
milliseconds
line of data. This is the timestamp recorded by Rooch!
Because the minimum granularity of Rooch's timestamp is milliseconds, such as the above output result of 1699975068000
, so we remove the last three zeros to get the actual timestamp, which is 1699975068
.
Move the timestamp forward
Rooch's timestamp module currently provides an entry function to change the current timestamp:
public entry fun fast_forward_seconds_for_local(ctx: &mut Context, timestamp_seconds: u64)
Let's try calling this function from the command line:
rooch move run --function 0x3::timestamp::fast_forward_seconds_for_local --args u64:22222222222
Check the timestamp again:
rooch object --id 0x2::timestamp::Timestamp
{
"value": "0x711ab0301fd517b135b88f57e84f254c94758998a602596be8ae7ba56a0d14b3000000000000000000000000000000000000000000000000000000000000000000d09941d2c1150000",
"value_type": "0x2::object::ObjectEntity<0x2::timestamp::Timestamp>",
"decoded_value": {
"abilities": 0,
"type": "0x2::object::ObjectEntity<0x2::timestamp::Timestamp>",
"value": {
"flag": 0,
"id": "0x4e8d2c243339c6e02f8b7dd34436a1b1eb541b0fe4d938f845f4dbb9d9f218a2",
"owner": "0x0000000000000000000000000000000000000000000000000000000000000000",
"value": {
"abilities": 8,
"type": "0x2::timestamp::Timestamp",
"value": {
"milliseconds": "23922200386000"
}
}
}
}
}
At this point, the timestamp has indeed advanced quite a bit. But be aware that this feature can only be used on the local network, because the real time will not change because of one of our commands!
This function may be used more often in game development scenarios in the future.
Use timestamp function
After starting the relay service, we can call the timestamp function to get the current timestamp.
An entry function is simply written below to demonstrate obtaining the timestamp value:
module timestamp::timestamp {
use std::debug::print;
use moveos_std::timestamp::{Self, Timestamp};
use moveos_std::object::Object;
use moveos_std::object;
entry fun get_timestamp(timestamp_obj: &Object<Timestamp>) {
let timestamp = object::borrow(timestamp_obj);
let now_seconds = timestamp::seconds(timestamp);
print(&now_seconds);
}
}
The std::debug::print
function is used here, and you can determine whether the timestamp is accurately obtained by observing the output on the server.
Because the minimum granularity of Rooch's timestamp is milliseconds, we also need to use the seconds
function provided in the module to convert it, so that we can get our actual timestamp.
After deploying the tested contract, let's try calling this function and observe the output in the terminal.
After starting the relay service, execute the Shell command:
rooch move run --function 0x9fd886140ae373a85101d2c7a5f2eda077ca9a05c3b1c221594ff4473a702c83::timestamp::get_timestamp --args object:0x2::timestamp::Timestamp --sender-account default
You can see the debug
message from the server, and we have successfully obtained the timestamp value.