Skip to content

Commit

Permalink
sdk: Extract clock crate (solana-labs#1967)
Browse files Browse the repository at this point in the history
* extract clock crate

* update clock usage in solana-program

* fmt

* fmt after rebase

* update lock file after rebase

* fmt after rebase

* fmt after rebase

* fmt

* make serde optional in solana-clock

* fix description

Co-authored-by: Jon C <me@jonc.dev>

* fix docs link

Co-authored-by: Jon C <me@jonc.dev>

* fix accidental deletions from workspace members table

---------

Co-authored-by: Jon C <me@jonc.dev>
  • Loading branch information
2 people authored and wen-coding committed Sep 4, 2024
1 parent 2339101 commit 76f7380
Show file tree
Hide file tree
Showing 30 changed files with 99 additions and 47 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ members = [
"sdk/atomic-u64",
"sdk/cargo-build-sbf",
"sdk/cargo-test-sbf",
"sdk/clock",
"sdk/decode-error",
"sdk/gen-headers",
"sdk/macro",
Expand Down Expand Up @@ -366,6 +367,7 @@ solana-cli = { path = "cli", version = "=2.1.0" }
solana-cli-config = { path = "cli-config", version = "=2.1.0" }
solana-cli-output = { path = "cli-output", version = "=2.1.0" }
solana-client = { path = "client", version = "=2.1.0" }
solana-clock = { path = "sdk/clock", version = "=2.1.0" }
solana-compute-budget = { path = "compute-budget", version = "=2.1.0" }
solana-compute-budget-program = { path = "programs/compute-budget", version = "=2.1.0" }
solana-config-program = { path = "programs/config", version = "=2.1.0" }
Expand Down
10 changes: 10 additions & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions sdk/clock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "solana-clock"
description = "Solana Clock and Time Definitions"
documentation = "https://docs.rs/solana-clock"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-sdk-macro = { workspace = true }

[dev-dependencies]
static_assertions = { workspace = true }

[features]
serde = ["dep:serde", "dep:serde_derive"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
5 changes: 4 additions & 1 deletion sdk/program/src/clock.rs → sdk/clock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
//!
//! [oracle]: https://docs.solanalabs.com/implemented-proposals/validator-timestamp-oracle
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
use solana_sdk_macro::CloneZeroed;

/// The default tick rate that the cluster attempts to achieve (160 per second).
Expand Down Expand Up @@ -172,7 +174,8 @@ pub type UnixTimestamp = i64;
///
/// All members of `Clock` start from 0 upon network boot.
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, CloneZeroed, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, CloneZeroed, Default, PartialEq, Eq)]
pub struct Clock {
/// The current `Slot`.
pub slot: Slot,
Expand Down
1 change: 1 addition & 0 deletions sdk/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde_derive = { workspace = true }
sha2 = { workspace = true }
sha3 = { workspace = true }
solana-atomic-u64 = { workspace = true }
solana-clock = { workspace = true, features = ["serde"] }
solana-decode-error = { workspace = true }
solana-frozen-abi = { workspace = true, optional = true, features = ["frozen-abi"] }
solana-frozen-abi-macro = { workspace = true, optional = true, features = ["frozen-abi"] }
Expand Down
3 changes: 2 additions & 1 deletion sdk/program/src/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
use {
crate::{
clock::Epoch, debug_account_data::*, entrypoint::MAX_PERMITTED_DATA_INCREASE,
debug_account_data::*, entrypoint::MAX_PERMITTED_DATA_INCREASE,
program_error::ProgramError, pubkey::Pubkey,
},
solana_clock::Epoch,
solana_program_memory::sol_memset,
std::{
cell::{Ref, RefCell, RefMut},
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/address_lookup_table/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use {
crate::{
address_lookup_table::program::id,
clock::Slot,
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
system_program,
},
serde_derive::{Deserialize, Serialize},
solana_clock::Slot,
};

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/address_lookup_table/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
use solana_frozen_abi_macro::{AbiEnumVisitor, AbiExample};
use {
serde_derive::{Deserialize, Serialize},
solana_clock::Slot,
solana_program::{
address_lookup_table::error::AddressLookupError,
clock::Slot,
instruction::InstructionError,
pubkey::Pubkey,
slot_hashes::{SlotHashes, MAX_ENTRIES},
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/epoch_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! the chain there is a "warmup" period, where epochs are short, with subsequent
//! epochs increasing in slots until they last for [`DEFAULT_SLOTS_PER_EPOCH`].
pub use crate::clock::{Epoch, Slot, DEFAULT_SLOTS_PER_EPOCH};
pub use solana_clock::{Epoch, Slot, DEFAULT_SLOTS_PER_EPOCH};
use solana_sdk_macro::CloneZeroed;

/// The default number of slots before an epoch starts to calculate the leader schedule.
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/example_mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub mod solana_sdk {
};

pub mod account {
use crate::{clock::Epoch, pubkey::Pubkey};
use {crate::pubkey::Pubkey, solana_clock::Epoch};
#[derive(Clone)]
pub struct Account {
pub lamports: u64,
Expand Down
11 changes: 7 additions & 4 deletions sdk/program/src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
//! 2. When the next epoch is entered the runtime will check for new activation requests and
//! active them. When this occurs, the activation slot is recorded in the feature account
use crate::{
account_info::AccountInfo, clock::Slot, instruction::Instruction, program_error::ProgramError,
pubkey::Pubkey, rent::Rent, system_instruction,
use {
crate::{
account_info::AccountInfo, instruction::Instruction, program_error::ProgramError,
pubkey::Pubkey, rent::Rent, system_instruction,
},
solana_clock::Slot,
};

crate::declare_id!("Feature111111111111111111111111111111111111");
Expand Down Expand Up @@ -60,7 +63,7 @@ pub fn activate_with_lamports(

#[cfg(test)]
mod test {
use {super::*, solana_program::clock::Slot};
use super::*;

#[test]
fn test_feature_size_of() {
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/fee_calculator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Calculation of transaction fees.
#![allow(clippy::arithmetic_side_effects)]
use {crate::clock::DEFAULT_MS_PER_SLOT, log::*};
use {log::*, solana_clock::DEFAULT_MS_PER_SLOT};

#[repr(C)]
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/last_restart_slot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Information about the last restart slot (hard fork).
use {crate::clock::Slot, solana_sdk_macro::CloneZeroed};
use {solana_clock::Slot, solana_sdk_macro::CloneZeroed};

#[repr(C)]
#[derive(Serialize, Deserialize, Debug, CloneZeroed, PartialEq, Eq, Default)]
Expand Down
3 changes: 1 addition & 2 deletions sdk/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ pub mod borsh1;
pub mod bpf_loader;
pub mod bpf_loader_deprecated;
pub mod bpf_loader_upgradeable;
pub mod clock;
pub mod compute_units;
pub mod debug_account_data;
pub mod ed25519_program;
Expand Down Expand Up @@ -530,7 +529,6 @@ pub mod sysvar;
pub mod vote;
pub mod wasm;

pub use solana_msg::msg;
#[deprecated(since = "2.1.0", note = "Use `solana-program-memory` crate instead")]
pub use solana_program_memory as program_memory;
#[deprecated(since = "2.1.0", note = "Use `solana-sanitize` crate instead")]
Expand All @@ -541,6 +539,7 @@ pub use solana_secp256k1_recover as secp256k1_recover;
pub use solana_short_vec as short_vec;
#[cfg(target_arch = "wasm32")]
pub use wasm_bindgen::prelude::wasm_bindgen;
pub use {solana_clock as clock, solana_msg::msg};

/// The [config native program][np].
///
Expand Down
11 changes: 7 additions & 4 deletions sdk/program/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
//! [`invoke_signed`]: invoke_signed
//! [cpi]: https://solana.com/docs/core/cpi
use crate::{
account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction, pubkey::Pubkey,
stable_layout::stable_instruction::StableInstruction,
use {
crate::{
account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction,
pubkey::Pubkey, stable_layout::stable_instruction::StableInstruction,
},
solana_clock::Epoch,
};

/// Invoke a cross-program instruction.
Expand Down Expand Up @@ -396,7 +399,7 @@ pub fn get_return_data() -> Option<(Pubkey, Vec<u8>)> {
pub fn check_type_assumptions() {
extern crate memoffset;
use {
crate::{clock::Epoch, instruction::AccountMeta},
crate::instruction::AccountMeta,
memoffset::offset_of,
std::{
cell::RefCell,
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/rent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![allow(clippy::arithmetic_side_effects)]

use {crate::clock::DEFAULT_SLOTS_PER_EPOCH, solana_sdk_macro::CloneZeroed};
use {solana_clock::DEFAULT_SLOTS_PER_EPOCH, solana_sdk_macro::CloneZeroed};

/// Configuration of network rent.
#[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/slot_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! [`sysvar::slot_hashes`]: crate::sysvar::slot_hashes
pub use crate::clock::Slot;
pub use solana_clock::Slot;
use {
crate::hash::Hash,
std::{
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/stake/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use {
crate::{
clock::{Epoch, UnixTimestamp},
instruction::{AccountMeta, Instruction},
program_error::ProgramError,
pubkey::Pubkey,
Expand All @@ -19,6 +18,7 @@ use {
log::*,
num_derive::{FromPrimitive, ToPrimitive},
serde_derive::{Deserialize, Serialize},
solana_clock::{Epoch, UnixTimestamp},
solana_decode_error::DecodeError,
thiserror::Error,
};
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/stake/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use borsh::{io, BorshDeserialize, BorshSchema, BorshSerialize};
use {
crate::{
clock::{Clock, Epoch, UnixTimestamp},
instruction::InstructionError,
pubkey::Pubkey,
stake::{
Expand All @@ -17,6 +16,7 @@ use {
},
stake_history::{StakeHistoryEntry, StakeHistoryGetEntry},
},
solana_clock::{Clock, Epoch, UnixTimestamp},
std::collections::HashSet,
};

Expand Down
5 changes: 3 additions & 2 deletions sdk/program/src/stake/tools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Utility functions
use crate::{
clock::Epoch, program_error::ProgramError, stake::MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION,
use {
crate::{program_error::ProgramError, stake::MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION},
solana_clock::Epoch,
};

/// Helper function for programs to call [`GetMinimumDelegation`] and then fetch the return data
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/stake_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! [`sysvar::stake_history`]: crate::sysvar::stake_history
pub use crate::clock::Epoch;
pub use solana_clock::Epoch;
use std::ops::Deref;

pub const MAX_ENTRIES: usize = 512; // it should never take as many as 512 epochs to warm up or cool down
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/sysvar/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@
//! # Ok::<(), anyhow::Error>(())
//! ```
pub use crate::clock::Clock;
use crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar};
pub use solana_clock::Clock;

crate::declare_sysvar_id!("SysvarC1ock11111111111111111111111111111111", Clock);

Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/sysvar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ mod tests {
use {
super::*,
crate::{
clock::Epoch,
entrypoint::SUCCESS,
program_error::ProgramError,
program_stubs::{set_syscall_stubs, SyscallStubs},
pubkey::Pubkey,
},
solana_clock::Epoch,
std::{cell::RefCell, rc::Rc},
};

Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/sysvar/recent_blockhashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl Deref for RecentBlockhashes {

#[cfg(test)]
mod tests {
use {super::*, crate::clock::MAX_PROCESSING_AGE};
use {super::*, solana_clock::MAX_PROCESSING_AGE};

#[test]
#[allow(clippy::assertions_on_constants)]
Expand Down
3 changes: 1 addition & 2 deletions sdk/program/src/sysvar/slot_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ pub use crate::slot_hashes::SlotHashes;
use {
crate::{
account_info::AccountInfo,
clock::Slot,
hash::Hash,
program_error::ProgramError,
slot_hashes::MAX_ENTRIES,
sysvar::{get_sysvar, Sysvar, SysvarId},
},
bytemuck_derive::{Pod, Zeroable},
solana_clock::Slot,
};

const U64_SIZE: usize = std::mem::size_of::<u64>();
Expand Down Expand Up @@ -218,7 +218,6 @@ mod tests {
use {
super::*,
crate::{
clock::Slot,
hash::{hash, Hash},
slot_hashes::MAX_ENTRIES,
sysvar::tests::mock_get_sysvar_syscall,
Expand Down
Loading

0 comments on commit 76f7380

Please sign in to comment.