93 lines
3.1 KiB
Rust
Raw Normal View History

2026-04-03 18:11:34 -03:00
//! Clock Program.
//!
//! A system program that records the current block ID and timestamp into dedicated clock accounts.
//! Three accounts are maintained, updated at different block intervals (every 1, 10, and 50
//! blocks), allowing programs to read recent timestamps at various granularities.
//!
//! This program can only be invoked exclusively by the sequencer as the last transaction in every
//! block. Clock accounts are assigned to the clock program at genesis, so no claiming is required
//! here.
2026-04-01 00:01:11 -03:00
use clock_core::{
CLOCK_01_PROGRAM_ACCOUNT_ID, CLOCK_10_PROGRAM_ACCOUNT_ID, CLOCK_50_PROGRAM_ACCOUNT_ID,
ClockAccountData, Instruction,
};
2026-03-31 01:39:02 -03:00
use nssa_core::{
account::AccountWithMetadata,
program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs},
2026-03-31 01:39:02 -03:00
};
2026-03-20 18:05:48 -03:00
2026-03-31 01:39:02 -03:00
fn update_if_multiple(
pre: AccountWithMetadata,
divisor: u64,
current_block_id: u64,
2026-04-03 18:11:34 -03:00
updated_data: &[u8],
2026-03-31 01:39:02 -03:00
) -> (AccountWithMetadata, AccountPostState) {
2026-03-31 01:39:17 -03:00
if current_block_id.is_multiple_of(divisor) {
2026-03-31 01:39:02 -03:00
let mut post_account = pre.account.clone();
post_account.data = updated_data
.to_vec()
.try_into()
2026-04-03 18:11:34 -03:00
.expect("Clock account data should fit in account data");
2026-03-31 01:39:02 -03:00
(pre, AccountPostState::new(post_account))
} else {
let post = AccountPostState::new(pre.account.clone());
(pre, post)
}
}
2026-03-20 18:05:48 -03:00
fn main() {
let (
ProgramInput {
self_program_id,
2026-03-20 18:05:48 -03:00
pre_states,
2026-03-30 23:50:54 -03:00
instruction: timestamp,
2026-03-20 18:05:48 -03:00
},
instruction_words,
) = read_nssa_inputs::<Instruction>();
2026-03-31 01:39:02 -03:00
let Ok([pre_01, pre_10, pre_50]) = <[_; 3]>::try_from(pre_states) else {
2026-04-01 00:01:11 -03:00
panic!("Invalid number of input accounts");
2026-03-20 18:05:48 -03:00
};
2026-04-01 00:01:11 -03:00
// Verify pre-states correspond to the expected clock account IDs.
if pre_01.account_id != CLOCK_01_PROGRAM_ACCOUNT_ID
|| pre_10.account_id != CLOCK_10_PROGRAM_ACCOUNT_ID
|| pre_50.account_id != CLOCK_50_PROGRAM_ACCOUNT_ID
{
panic!("Invalid input accounts");
}
2026-04-03 18:11:34 -03:00
// Verify all clock accounts are owned by this program (assigned at genesis).
if pre_01.account.program_owner != self_program_id
|| pre_10.account.program_owner != self_program_id
|| pre_50.account.program_owner != self_program_id
{
panic!("Clock accounts must be owned by the clock program");
}
let prev_data = ClockAccountData::from_bytes(&pre_01.account.data.clone().into_inner());
2026-04-01 00:01:11 -03:00
let current_block_id = prev_data
.block_id
2026-03-20 18:05:48 -03:00
.checked_add(1)
2026-03-30 23:50:54 -03:00
.expect("Next block id should be within u64 boundaries");
2026-03-20 18:05:48 -03:00
2026-04-02 17:42:33 -03:00
let updated_data = ClockAccountData {
block_id: current_block_id,
timestamp,
}
.to_bytes();
2026-03-31 01:39:02 -03:00
2026-04-03 18:11:34 -03:00
let (pre_01, post_01) = update_if_multiple(pre_01, 1, current_block_id, &updated_data);
let (pre_10, post_10) = update_if_multiple(pre_10, 10, current_block_id, &updated_data);
let (pre_50, post_50) = update_if_multiple(pre_50, 50, current_block_id, &updated_data);
2026-03-20 18:05:48 -03:00
ProgramOutput::new(
self_program_id,
2026-03-31 01:39:02 -03:00
instruction_words,
vec![pre_01, pre_10, pre_50],
vec![post_01, post_10, post_50],
2026-04-01 00:01:11 -03:00
)
.write();
2026-03-20 18:05:48 -03:00
}