From e8d88f3237fc44c98a52cc6707d1d46975bd6b64 Mon Sep 17 00:00:00 2001 From: Jack May Date: Fri, 6 Sep 2019 14:30:56 -0700 Subject: [PATCH] Split SDK's timing.rs (#5823) --- cli/src/wallet.rs | 6 +- client/src/rpc_client.rs | 2 +- client/src/rpc_client_request.rs | 2 +- client/src/thin_client.rs | 3 +- core/src/banking_stage.rs | 17 ++-- core/src/blocktree.rs | 2 +- core/src/blocktree/db.rs | 2 +- core/src/blocktree/rocks.rs | 2 +- core/src/blocktree_processor.rs | 3 +- core/src/chacha_cuda.rs | 2 +- core/src/cluster_info.rs | 2 +- core/src/fetch_stage.rs | 2 +- core/src/leader_schedule_utils.rs | 2 +- core/src/ledger_cleanup_service.rs | 2 +- core/src/poh_recorder.rs | 6 +- core/src/replicator.rs | 3 +- core/src/storage_stage.rs | 4 +- core/src/validator.rs | 4 +- core/tests/rpc.rs | 2 +- genesis/src/main.rs | 5 +- ledger-tool/src/main.rs | 2 +- local_cluster/src/cluster_tests.rs | 6 +- local_cluster/src/local_cluster.rs | 4 +- local_cluster/tests/local_cluster.rs | 6 +- programs/stake_api/src/stake_instruction.rs | 2 +- programs/stake_api/src/stake_state.rs | 2 +- .../tests/storage_processor.rs | 4 +- programs/vote_api/src/vote_state.rs | 2 +- runtime/src/append_vec.rs | 2 +- runtime/src/bank.rs | 5 +- runtime/src/rent_collector.rs | 2 +- runtime/src/stakes.rs | 2 +- runtime/src/status_cache.rs | 4 +- sdk/src/account.rs | 2 +- sdk/src/clock.rs | 99 +++++++++++++++++++ sdk/src/fee_calculator.rs | 2 +- sdk/src/genesis_block.rs | 2 +- sdk/src/lib.rs | 1 + sdk/src/poh_config.rs | 2 +- sdk/src/sysvar/clock.rs | 4 +- sdk/src/sysvar/slot_hashes.rs | 2 +- sdk/src/sysvar/stake_history.rs | 2 +- sdk/src/timing.rs | 98 ------------------ validator/src/main.rs | 2 +- 44 files changed, 169 insertions(+), 163 deletions(-) create mode 100644 sdk/src/clock.rs diff --git a/cli/src/wallet.rs b/cli/src/wallet.rs index 8c9c9a1120..d203da9fb7 100644 --- a/cli/src/wallet.rs +++ b/cli/src/wallet.rs @@ -781,7 +781,7 @@ fn process_delegate_stake( )), Some(root_slot) => { let slot = rpc_client.get_slot()?; - if root_slot + solana_sdk::timing::DEFAULT_SLOTS_PER_TURN < slot { + if root_slot + solana_sdk::clock::DEFAULT_SLOTS_PER_TURN < slot { Err(WalletError::BadParameter( format!( "Unable to delegate. Vote account root slot ({}) is too old, the current slot is {}", root_slot, slot @@ -1307,8 +1307,8 @@ fn process_ping( // Sleep for half a slot if signal_receiver .recv_timeout(Duration::from_millis( - 500 * solana_sdk::timing::DEFAULT_TICKS_PER_SLOT - / solana_sdk::timing::DEFAULT_TICKS_PER_SECOND, + 500 * solana_sdk::clock::DEFAULT_TICKS_PER_SLOT + / solana_sdk::clock::DEFAULT_TICKS_PER_SECOND, )) .is_ok() { diff --git a/client/src/rpc_client.rs b/client/src/rpc_client.rs index 13b6507f8e..a7f8aefa8b 100644 --- a/client/src/rpc_client.rs +++ b/client/src/rpc_client.rs @@ -7,12 +7,12 @@ use bincode::serialize; use log::*; use serde_json::{json, Value}; use solana_sdk::account::Account; +use solana_sdk::clock::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT}; use solana_sdk::fee_calculator::FeeCalculator; use solana_sdk::hash::Hash; use solana_sdk::inflation::Inflation; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{KeypairUtil, Signature}; -use solana_sdk::timing::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT}; use solana_sdk::transaction::{self, Transaction, TransactionError}; use std::error; use std::io; diff --git a/client/src/rpc_client_request.rs b/client/src/rpc_client_request.rs index 5bce80c1fb..c401e9cd4a 100644 --- a/client/src/rpc_client_request.rs +++ b/client/src/rpc_client_request.rs @@ -2,7 +2,7 @@ use crate::client_error::ClientError; use crate::generic_rpc_client_request::GenericRpcClientRequest; use crate::rpc_request::{RpcError, RpcRequest}; use log::*; -use solana_sdk::timing::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT}; +use solana_sdk::clock::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT}; use std::io::{Error as IoError, ErrorKind}; use std::thread::sleep; use std::time::Duration; diff --git a/client/src/thin_client.rs b/client/src/thin_client.rs index 7c51ae4568..5c18ef82e2 100644 --- a/client/src/thin_client.rs +++ b/client/src/thin_client.rs @@ -8,6 +8,7 @@ use bincode::{serialize_into, serialized_size}; use log::*; use solana_sdk::account::Account; use solana_sdk::client::{AsyncClient, Client, SyncClient}; +use solana_sdk::clock::MAX_PROCESSING_AGE; use solana_sdk::fee_calculator::FeeCalculator; use solana_sdk::hash::Hash; use solana_sdk::instruction::Instruction; @@ -16,7 +17,7 @@ use solana_sdk::packet::PACKET_DATA_SIZE; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; use solana_sdk::system_instruction; -use solana_sdk::timing::{duration_as_ms, MAX_PROCESSING_AGE}; +use solana_sdk::timing::duration_as_ms; use solana_sdk::transaction::{self, Transaction}; use solana_sdk::transport::Result as TransportResult; use std::io; diff --git a/core/src/banking_stage.rs b/core/src/banking_stage.rs index 63741192c0..6c9ff6dcd5 100644 --- a/core/src/banking_stage.rs +++ b/core/src/banking_stage.rs @@ -22,12 +22,13 @@ use solana_metrics::{inc_new_counter_debug, inc_new_counter_info, inc_new_counte use solana_runtime::accounts_db::ErrorCounters; use solana_runtime::bank::Bank; use solana_runtime::locked_accounts_results::LockedAccountsResults; -use solana_sdk::poh_config::PohConfig; -use solana_sdk::pubkey::Pubkey; -use solana_sdk::timing::{ - self, DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT, MAX_PROCESSING_AGE, +use solana_sdk::clock::{ + DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT, MAX_PROCESSING_AGE, MAX_TRANSACTION_FORWARDING_DELAY, }; +use solana_sdk::poh_config::PohConfig; +use solana_sdk::pubkey::Pubkey; +use solana_sdk::timing::{duration_as_ms, timestamp}; use solana_sdk::transaction::{self, Transaction, TransactionError}; use std::cmp; use std::env; @@ -222,7 +223,7 @@ impl BankingStage { debug!( "@{:?} done processing buffered batches: {} time: {:?}ms tx count: {} tx/s: {}", - timing::timestamp(), + timestamp(), buffered_len, proc_start.as_ms(), new_tx_count, @@ -808,8 +809,8 @@ impl BankingStage { let count: usize = mms.iter().map(|x| x.1.len()).sum(); debug!( "@{:?} process start stalled for: {:?}ms txs: {} id: {}", - timing::timestamp(), - timing::duration_as_ms(&recv_start.elapsed()), + timestamp(), + duration_as_ms(&recv_start.elapsed()), count, id, ); @@ -878,7 +879,7 @@ impl BankingStage { inc_new_counter_debug!("banking_stage-time_ms", proc_start.as_ms() as usize); debug!( "@{:?} done processing transaction batches: {} time: {:?}ms tx count: {} tx/s: {} total count: {} id: {}", - timing::timestamp(), + timestamp(), mms_len, proc_start.as_ms(), new_tx_count, diff --git a/core/src/blocktree.rs b/core/src/blocktree.rs index 7861ef99fc..ae2093d6eb 100644 --- a/core/src/blocktree.rs +++ b/core/src/blocktree.rs @@ -34,7 +34,7 @@ use std::sync::{Arc, RwLock}; pub use self::meta::*; pub use self::rooted_slot_iterator::*; use crate::leader_schedule_cache::LeaderScheduleCache; -use solana_sdk::timing::Slot; +use solana_sdk::clock::Slot; mod db; mod meta; diff --git a/core/src/blocktree/db.rs b/core/src/blocktree/db.rs index 5c3df658d4..5b0f3fdf61 100644 --- a/core/src/blocktree/db.rs +++ b/core/src/blocktree/db.rs @@ -5,7 +5,7 @@ use bincode::{deserialize, serialize}; use serde::de::DeserializeOwned; use serde::Serialize; -use solana_sdk::timing::Slot; +use solana_sdk::clock::Slot; use std::borrow::Borrow; use std::collections::HashMap; use std::marker::PhantomData; diff --git a/core/src/blocktree/rocks.rs b/core/src/blocktree/rocks.rs index 51152223dc..b8aeb420ea 100644 --- a/core/src/blocktree/rocks.rs +++ b/core/src/blocktree/rocks.rs @@ -4,7 +4,7 @@ use crate::blocktree::db::{ }; use crate::blocktree::BlocktreeError; use crate::result::{Error, Result}; -use solana_sdk::timing::Slot; +use solana_sdk::clock::Slot; use byteorder::{BigEndian, ByteOrder}; diff --git a/core/src/blocktree_processor.rs b/core/src/blocktree_processor.rs index 7bd3b6747b..272fcc2c5a 100644 --- a/core/src/blocktree_processor.rs +++ b/core/src/blocktree_processor.rs @@ -7,9 +7,10 @@ use rayon::ThreadPool; use solana_metrics::{datapoint, datapoint_error, inc_new_counter_debug}; use solana_runtime::bank::Bank; use solana_runtime::locked_accounts_results::LockedAccountsResults; +use solana_sdk::clock::{Slot, MAX_RECENT_BLOCKHASHES}; use solana_sdk::genesis_block::GenesisBlock; use solana_sdk::hash::Hash; -use solana_sdk::timing::{duration_as_ms, Slot, MAX_RECENT_BLOCKHASHES}; +use solana_sdk::timing::duration_as_ms; use solana_sdk::transaction::Result; use std::result; use std::sync::Arc; diff --git a/core/src/chacha_cuda.rs b/core/src/chacha_cuda.rs index 5ea23fabb5..85e0304081 100644 --- a/core/src/chacha_cuda.rs +++ b/core/src/chacha_cuda.rs @@ -120,9 +120,9 @@ mod tests { use crate::chacha_cuda::chacha_cbc_encrypt_file_many_keys; use crate::entry::make_tiny_test_entries; use crate::replicator::sample_file; + use solana_sdk::clock::DEFAULT_SLOTS_PER_SEGMENT; use solana_sdk::hash::Hash; use solana_sdk::signature::{Keypair, KeypairUtil}; - use solana_sdk::timing::DEFAULT_SLOTS_PER_SEGMENT; use std::fs::{remove_dir_all, remove_file}; use std::path::Path; use std::sync::Arc; diff --git a/core/src/cluster_info.rs b/core/src/cluster_info.rs index 95051cfa46..66466a7509 100644 --- a/core/src/cluster_info.rs +++ b/core/src/cluster_info.rs @@ -1769,9 +1769,9 @@ mod tests { use crate::result::Error; use crate::shred::{DataShred, Shred}; use crate::test_tx::test_tx; + use solana_sdk::clock::DEFAULT_TICKS_PER_SLOT; use solana_sdk::hash::Hash; use solana_sdk::signature::{Keypair, KeypairUtil}; - use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT; use std::collections::HashSet; use std::net::{IpAddr, Ipv4Addr}; use std::sync::{Arc, RwLock}; diff --git a/core/src/fetch_stage.rs b/core/src/fetch_stage.rs index 5c89d9340f..7c043248cb 100644 --- a/core/src/fetch_stage.rs +++ b/core/src/fetch_stage.rs @@ -7,7 +7,7 @@ use crate::result::{Error, Result}; use crate::service::Service; use crate::streamer::{self, PacketReceiver, PacketSender}; use solana_metrics::{inc_new_counter_debug, inc_new_counter_info}; -use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT; +use solana_sdk::clock::DEFAULT_TICKS_PER_SLOT; use std::net::UdpSocket; use std::sync::atomic::AtomicBool; use std::sync::mpsc::{channel, RecvTimeoutError}; diff --git a/core/src/leader_schedule_utils.rs b/core/src/leader_schedule_utils.rs index 4954e9de66..0bfb9f109f 100644 --- a/core/src/leader_schedule_utils.rs +++ b/core/src/leader_schedule_utils.rs @@ -1,8 +1,8 @@ use crate::leader_schedule::LeaderSchedule; use crate::staking_utils; use solana_runtime::bank::Bank; +use solana_sdk::clock::NUM_CONSECUTIVE_LEADER_SLOTS; use solana_sdk::pubkey::Pubkey; -use solana_sdk::timing::NUM_CONSECUTIVE_LEADER_SLOTS; /// Return the leader schedule for the given epoch. pub fn leader_schedule(epoch: u64, bank: &Bank) -> Option { diff --git a/core/src/ledger_cleanup_service.rs b/core/src/ledger_cleanup_service.rs index ed1c8dec8d..ec8a5afd2f 100644 --- a/core/src/ledger_cleanup_service.rs +++ b/core/src/ledger_cleanup_service.rs @@ -3,8 +3,8 @@ use crate::blocktree::Blocktree; use crate::result::{Error, Result}; use crate::service::Service; +use solana_sdk::clock::DEFAULT_SLOTS_PER_EPOCH; use solana_sdk::pubkey::Pubkey; -use solana_sdk::timing::DEFAULT_SLOTS_PER_EPOCH; use std::string::ToString; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{Receiver, RecvTimeoutError}; diff --git a/core/src/poh_recorder.rs b/core/src/poh_recorder.rs index 1c17df12a8..49c64f562a 100644 --- a/core/src/poh_recorder.rs +++ b/core/src/poh_recorder.rs @@ -16,12 +16,12 @@ use crate::leader_schedule_cache::LeaderScheduleCache; use crate::poh::Poh; use crate::result::{Error, Result}; use solana_runtime::bank::Bank; +pub use solana_sdk::clock::Slot; +use solana_sdk::clock::NUM_CONSECUTIVE_LEADER_SLOTS; use solana_sdk::hash::Hash; use solana_sdk::poh_config::PohConfig; use solana_sdk::pubkey::Pubkey; use solana_sdk::timing; -pub use solana_sdk::timing::Slot; -use solana_sdk::timing::NUM_CONSECUTIVE_LEADER_SLOTS; use solana_sdk::transaction::Transaction; use std::cmp; use std::sync::mpsc::{channel, Receiver, Sender, SyncSender}; @@ -447,8 +447,8 @@ mod tests { use crate::blocktree::{get_tmp_ledger_path, Blocktree}; use crate::genesis_utils::{create_genesis_block, GenesisBlockInfo}; use crate::test_tx::test_tx; + use solana_sdk::clock::DEFAULT_TICKS_PER_SLOT; use solana_sdk::hash::hash; - use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT; use std::sync::mpsc::sync_channel; #[test] diff --git a/core/src/replicator.rs b/core/src/replicator.rs index 669517e40f..137b14f669 100644 --- a/core/src/replicator.rs +++ b/core/src/replicator.rs @@ -27,10 +27,11 @@ use solana_ed25519_dalek as ed25519_dalek; use solana_netutil::bind_in_range; use solana_sdk::account_utils::State; use solana_sdk::client::{AsyncClient, SyncClient}; +use solana_sdk::clock::{get_complete_segment_from_slot, get_segment_from_slot}; use solana_sdk::hash::{Hash, Hasher}; use solana_sdk::message::Message; use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; -use solana_sdk::timing::{get_complete_segment_from_slot, get_segment_from_slot, timestamp}; +use solana_sdk::timing::timestamp; use solana_sdk::transaction::Transaction; use solana_sdk::transport::TransportError; use solana_storage_api::storage_contract::StorageContract; diff --git a/core/src/storage_stage.rs b/core/src/storage_stage.rs index 26e72f821e..220f3cc3d9 100644 --- a/core/src/storage_stage.rs +++ b/core/src/storage_stage.rs @@ -15,12 +15,12 @@ use solana_runtime::bank::Bank; use solana_runtime::storage_utils::replicator_accounts; use solana_sdk::account::Account; use solana_sdk::account_utils::State; +use solana_sdk::clock::get_segment_from_slot; use solana_sdk::hash::Hash; use solana_sdk::instruction::Instruction; use solana_sdk::message::Message; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; -use solana_sdk::timing::get_segment_from_slot; use solana_sdk::transaction::Transaction; use solana_storage_api::storage_contract::{Proof, ProofStatus, StorageContract}; use solana_storage_api::storage_instruction; @@ -638,10 +638,10 @@ mod tests { use crate::{blocktree_processor, entry}; use rayon::prelude::*; use solana_runtime::bank::Bank; + use solana_sdk::clock::DEFAULT_TICKS_PER_SLOT; use solana_sdk::hash::{Hash, Hasher}; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil}; - use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT; use std::cmp::{max, min}; use std::fs::remove_dir_all; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; diff --git a/core/src/validator.rs b/core/src/validator.rs index 7dde38b02d..6e7a7f72bf 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -21,12 +21,14 @@ use crate::storage_stage::StorageState; use crate::tpu::Tpu; use crate::tvu::{Sockets, Tvu}; use solana_metrics::datapoint_info; +use solana_sdk::clock::{Slot, DEFAULT_SLOTS_PER_TURN}; use solana_sdk::genesis_block::GenesisBlock; use solana_sdk::hash::Hash; use solana_sdk::poh_config::PohConfig; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil}; -use solana_sdk::timing::{timestamp, Slot, DEFAULT_SLOTS_PER_TURN}; +use solana_sdk::timing::timestamp; + use std::fs; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::path::{Path, PathBuf}; diff --git a/core/tests/rpc.rs b/core/tests/rpc.rs index c00819e513..39c1db8d3e 100644 --- a/core/tests/rpc.rs +++ b/core/tests/rpc.rs @@ -63,7 +63,7 @@ fn test_rpc_send_tx() { "params": [signature], }); - for _ in 0..solana_sdk::timing::DEFAULT_TICKS_PER_SLOT { + for _ in 0..solana_sdk::clock::DEFAULT_TICKS_PER_SLOT { let response = client .post(&rpc_string) .set("Content-Type", "application/json") diff --git a/genesis/src/main.rs b/genesis/src/main.rs index 460eec0fea..4e0bc91f54 100644 --- a/genesis/src/main.rs +++ b/genesis/src/main.rs @@ -5,6 +5,7 @@ use clap::{crate_description, crate_name, crate_version, value_t_or_exit, App, A use serde::{Deserialize, Serialize}; use solana_core::blocktree::create_new_ledger; use solana_sdk::account::Account; +use solana_sdk::clock; use solana_sdk::fee_calculator::FeeCalculator; use solana_sdk::genesis_block::Builder; use solana_sdk::hash::{hash, Hash}; @@ -83,8 +84,8 @@ fn main() -> Result<(), Box> { .to_string(); let default_target_tick_duration = &timing::duration_as_ms(&PohConfig::default().target_tick_duration).to_string(); - let default_ticks_per_slot = &timing::DEFAULT_TICKS_PER_SLOT.to_string(); - let default_slots_per_epoch = &timing::DEFAULT_SLOTS_PER_EPOCH.to_string(); + let default_ticks_per_slot = &clock::DEFAULT_TICKS_PER_SLOT.to_string(); + let default_slots_per_epoch = &clock::DEFAULT_SLOTS_PER_EPOCH.to_string(); let matches = App::new(crate_name!()) .about(crate_description!()) diff --git a/ledger-tool/src/main.rs b/ledger-tool/src/main.rs index a858d6add8..f8b840d796 100644 --- a/ledger-tool/src/main.rs +++ b/ledger-tool/src/main.rs @@ -1,8 +1,8 @@ use clap::{crate_description, crate_name, crate_version, value_t_or_exit, App, Arg, SubCommand}; use solana_core::blocktree::Blocktree; use solana_core::blocktree_processor::process_blocktree; +use solana_sdk::clock::Slot; use solana_sdk::genesis_block::GenesisBlock; -use solana_sdk::timing::Slot; use std::collections::BTreeMap; use std::fs::File; use std::io::{stdout, Write}; diff --git a/local_cluster/src/cluster_tests.rs b/local_cluster/src/cluster_tests.rs index ba9bdbd0d1..d04c971326 100644 --- a/local_cluster/src/cluster_tests.rs +++ b/local_cluster/src/cluster_tests.rs @@ -15,15 +15,13 @@ use solana_core::{ use solana_runtime::epoch_schedule::MINIMUM_SLOTS_PER_EPOCH; use solana_sdk::{ client::SyncClient, + clock::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT, NUM_CONSECUTIVE_LEADER_SLOTS}, hash::Hash, poh_config::PohConfig, pubkey::Pubkey, signature::{Keypair, KeypairUtil, Signature}, system_transaction, - timing::{ - duration_as_ms, DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT, - NUM_CONSECUTIVE_LEADER_SLOTS, - }, + timing::duration_as_ms, transport::TransportError, }; use std::{ diff --git a/local_cluster/src/local_cluster.rs b/local_cluster/src/local_cluster.rs index 7d370579e1..8b034e684b 100644 --- a/local_cluster/src/local_cluster.rs +++ b/local_cluster/src/local_cluster.rs @@ -12,14 +12,14 @@ use solana_core::{ }; use solana_sdk::{ client::SyncClient, + clock::DEFAULT_TICKS_PER_SLOT, + clock::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_SLOTS_PER_SEGMENT}, genesis_block::GenesisBlock, message::Message, poh_config::PohConfig, pubkey::Pubkey, signature::{Keypair, KeypairUtil}, system_transaction, - timing::DEFAULT_TICKS_PER_SLOT, - timing::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_SLOTS_PER_SEGMENT}, transaction::Transaction, }; use solana_stake_api::{config as stake_config, stake_instruction, stake_state::StakeState}; diff --git a/local_cluster/tests/local_cluster.rs b/local_cluster/tests/local_cluster.rs index 8e34a42ef1..9e11160ae7 100644 --- a/local_cluster/tests/local_cluster.rs +++ b/local_cluster/tests/local_cluster.rs @@ -14,7 +14,7 @@ use solana_runtime::{ accounts_db::AccountsDB, epoch_schedule::{EpochSchedule, MINIMUM_SLOTS_PER_EPOCH}, }; -use solana_sdk::{client::SyncClient, poh_config::PohConfig, timing}; +use solana_sdk::{client::SyncClient, clock, poh_config::PohConfig}; use std::path::PathBuf; use std::{ collections::{HashMap, HashSet}, @@ -267,14 +267,14 @@ fn test_restart_node() { cluster_tests::sleep_n_epochs( 1.0, &cluster.genesis_block.poh_config, - timing::DEFAULT_TICKS_PER_SLOT, + clock::DEFAULT_TICKS_PER_SLOT, slots_per_epoch, ); cluster.restart_node(nodes[0], &validator_config); cluster_tests::sleep_n_epochs( 0.5, &cluster.genesis_block.poh_config, - timing::DEFAULT_TICKS_PER_SLOT, + clock::DEFAULT_TICKS_PER_SLOT, slots_per_epoch, ); cluster_tests::send_many_transactions( diff --git a/programs/stake_api/src/stake_instruction.rs b/programs/stake_api/src/stake_instruction.rs index 2101419c19..7993ea5497 100644 --- a/programs/stake_api/src/stake_instruction.rs +++ b/programs/stake_api/src/stake_instruction.rs @@ -7,10 +7,10 @@ use log::*; use serde_derive::{Deserialize, Serialize}; use solana_sdk::{ account::KeyedAccount, + clock::Slot, instruction::{AccountMeta, Instruction, InstructionError}, pubkey::Pubkey, system_instruction, sysvar, - timing::Slot, }; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] diff --git a/programs/stake_api/src/stake_state.rs b/programs/stake_api/src/stake_state.rs index a6ea435839..b5441cb25d 100644 --- a/programs/stake_api/src/stake_state.rs +++ b/programs/stake_api/src/stake_state.rs @@ -9,6 +9,7 @@ use serde_derive::{Deserialize, Serialize}; use solana_sdk::{ account::{Account, KeyedAccount}, account_utils::State, + clock::{Epoch, Slot}, instruction::InstructionError, instruction_processor_utils::DecodeError, pubkey::Pubkey, @@ -16,7 +17,6 @@ use solana_sdk::{ self, stake_history::{StakeHistory, StakeHistoryEntry}, }, - timing::{Epoch, Slot}, }; use solana_vote_api::vote_state::VoteState; diff --git a/programs/storage_program/tests/storage_processor.rs b/programs/storage_program/tests/storage_processor.rs index 4d160eeeaa..a37a391678 100644 --- a/programs/storage_program/tests/storage_processor.rs +++ b/programs/storage_program/tests/storage_processor.rs @@ -7,6 +7,7 @@ use solana_runtime::genesis_utils::{create_genesis_block, GenesisBlockInfo}; use solana_sdk::account::{create_keyed_accounts, Account, KeyedAccount}; use solana_sdk::account_utils::State; use solana_sdk::client::SyncClient; +use solana_sdk::clock::{get_segment_from_slot, DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT}; use solana_sdk::hash::{hash, Hash}; use solana_sdk::instruction::{Instruction, InstructionError}; use solana_sdk::message::Message; @@ -16,9 +17,6 @@ use solana_sdk::system_instruction; use solana_sdk::sysvar::clock::Clock; use solana_sdk::sysvar::rewards::Rewards; use solana_sdk::sysvar::{clock, rewards}; -use solana_sdk::timing::{ - get_segment_from_slot, DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT, -}; use solana_storage_api::id; use solana_storage_api::storage_contract::StorageAccount; use solana_storage_api::storage_contract::{ProofStatus, StorageContract, STORAGE_ACCOUNT_SPACE}; diff --git a/programs/vote_api/src/vote_state.rs b/programs/vote_api/src/vote_state.rs index e74b31bf85..a7db7a6472 100644 --- a/programs/vote_api/src/vote_state.rs +++ b/programs/vote_api/src/vote_state.rs @@ -8,12 +8,12 @@ use serde_derive::{Deserialize, Serialize}; use solana_sdk::{ account::{Account, KeyedAccount}, account_utils::State, + clock::{Epoch, Slot}, hash::Hash, instruction::InstructionError, instruction_processor_utils::DecodeError, pubkey::Pubkey, sysvar::clock::Clock, - timing::{Epoch, Slot}, }; use std::collections::VecDeque; diff --git a/runtime/src/append_vec.rs b/runtime/src/append_vec.rs index 042dbf30f9..2cf51d1b81 100644 --- a/runtime/src/append_vec.rs +++ b/runtime/src/append_vec.rs @@ -1,7 +1,7 @@ use bincode::{deserialize_from, serialize_into, serialized_size}; use memmap::MmapMut; use serde::{Deserialize, Serialize}; -use solana_sdk::{account::Account, pubkey::Pubkey, timing::Epoch}; +use solana_sdk::{account::Account, clock::Epoch, pubkey::Pubkey}; use std::fmt; use std::fs::{create_dir_all, remove_file, OpenOptions}; use std::io; diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index d47934a20d..94a1ff5b88 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -33,6 +33,7 @@ use solana_metrics::{ }; use solana_sdk::{ account::Account, + clock::{get_segment_from_slot, Epoch, Slot, MAX_RECENT_BLOCKHASHES}, fee_calculator::FeeCalculator, genesis_block::GenesisBlock, hash::{hashv, Hash}, @@ -46,7 +47,7 @@ use solana_sdk::{ slot_hashes::{self, SlotHashes}, stake_history, }, - timing::{duration_as_ns, get_segment_from_slot, Epoch, Slot, MAX_RECENT_BLOCKHASHES}, + timing::duration_as_ns, transaction::{Result, Transaction, TransactionError}, }; use std::collections::{HashMap, HashSet}; @@ -1533,6 +1534,7 @@ mod tests { create_genesis_block_with_leader, GenesisBlockInfo, BOOTSTRAP_LEADER_LAMPORTS, }; use bincode::{deserialize_from, serialize_into, serialized_size}; + use solana_sdk::clock::DEFAULT_TICKS_PER_SLOT; use solana_sdk::genesis_block::create_genesis_block; use solana_sdk::hash; use solana_sdk::instruction::InstructionError; @@ -1541,7 +1543,6 @@ mod tests { use solana_sdk::system_instruction; use solana_sdk::system_transaction; use solana_sdk::sysvar::{fees::Fees, rewards::Rewards}; - use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT; use solana_stake_api::stake_state::Stake; use solana_vote_api::vote_instruction; use solana_vote_api::vote_state::{VoteState, MAX_LOCKOUT_HISTORY}; diff --git a/runtime/src/rent_collector.rs b/runtime/src/rent_collector.rs index 554ab2ff90..a57b0a37f6 100644 --- a/runtime/src/rent_collector.rs +++ b/runtime/src/rent_collector.rs @@ -1,6 +1,6 @@ //! calculate and collect rent from Accounts use crate::epoch_schedule::EpochSchedule; -use solana_sdk::{account::Account, rent::Rent, timing::Epoch}; +use solana_sdk::{account::Account, clock::Epoch, rent::Rent}; #[derive(Default, Serialize, Deserialize, Clone)] pub struct RentCollector { diff --git a/runtime/src/stakes.rs b/runtime/src/stakes.rs index a617015a33..b4550145bc 100644 --- a/runtime/src/stakes.rs +++ b/runtime/src/stakes.rs @@ -1,9 +1,9 @@ //! Stakes serve as a cache of stake and vote accounts to derive //! node stakes use solana_sdk::account::Account; +use solana_sdk::clock::Epoch; use solana_sdk::pubkey::Pubkey; use solana_sdk::sysvar::stake_history::StakeHistory; -use solana_sdk::timing::Epoch; use solana_stake_api::stake_state::{new_stake_history_entry, StakeState}; use solana_vote_api::vote_state::VoteState; use std::collections::HashMap; diff --git a/runtime/src/status_cache.rs b/runtime/src/status_cache.rs index 1d45e02483..a28fe32c1b 100644 --- a/runtime/src/status_cache.rs +++ b/runtime/src/status_cache.rs @@ -1,13 +1,13 @@ use log::*; use rand::{thread_rng, Rng}; use serde::Serialize; +use solana_sdk::clock::{Slot, MAX_HASH_AGE_IN_SECONDS}; use solana_sdk::hash::Hash; use solana_sdk::signature::Signature; -use solana_sdk::timing::Slot; use std::collections::{HashMap, HashSet}; use std::sync::{Arc, Mutex}; -pub const MAX_CACHE_ENTRIES: usize = solana_sdk::timing::MAX_HASH_AGE_IN_SECONDS; +pub const MAX_CACHE_ENTRIES: usize = MAX_HASH_AGE_IN_SECONDS; const CACHED_SIGNATURE_SIZE: usize = 20; // Store forks in a single chunk of memory to avoid another lookup. diff --git a/sdk/src/account.rs b/sdk/src/account.rs index dbb331f06e..035bb8f709 100644 --- a/sdk/src/account.rs +++ b/sdk/src/account.rs @@ -1,4 +1,4 @@ -use crate::{pubkey::Pubkey, timing::Epoch}; +use crate::{pubkey::Pubkey, clock::Epoch}; use std::{cmp, fmt}; /// An Account with data that is stored on chain diff --git a/sdk/src/clock.rs b/sdk/src/clock.rs new file mode 100644 index 0000000000..d05fae837f --- /dev/null +++ b/sdk/src/clock.rs @@ -0,0 +1,99 @@ +//! Provides information about the network's clock which is made up of ticks, slots, segments, etc... + +// The default tick rate that the cluster attempts to achieve. Note that the actual tick +// rate at any given time should be expected to drift +pub const DEFAULT_TICKS_PER_SECOND: u64 = 10; + +// At 10 ticks/s, 4 ticks per slot implies that leader rotation and voting will happen +// every 400 ms. A fast voting cadence ensures faster finality and convergence +pub const DEFAULT_TICKS_PER_SLOT: u64 = 4; + +// 1 Epoch = 400 * 8192 ms ~= 55 minutes +pub const DEFAULT_SLOTS_PER_EPOCH: u64 = 8192; + +// Storage segment configuration +pub const DEFAULT_SLOTS_PER_SEGMENT: u64 = 1024; + +// 4 times longer than the max_lockout to allow enough time for PoRep (128 slots) +pub const DEFAULT_SLOTS_PER_TURN: u64 = 32 * 4; + +pub const NUM_CONSECUTIVE_LEADER_SLOTS: u64 = 4; + +/// The time window of recent block hash values that the bank will track the signatures +/// of over. Once the bank discards a block hash, it will reject any transactions that use +/// that `recent_blockhash` in a transaction. Lowering this value reduces memory consumption, +/// but requires clients to update its `recent_blockhash` more frequently. Raising the value +/// lengthens the time a client must wait to be certain a missing transaction will +/// not be processed by the network. +pub const MAX_HASH_AGE_IN_SECONDS: usize = 120; + +// This must be <= MAX_HASH_AGE_IN_SECONDS, otherwise there's risk for DuplicateSignature errors +pub const MAX_RECENT_BLOCKHASHES: usize = MAX_HASH_AGE_IN_SECONDS; + +// The maximum age of a blockhash that will be accepted by the leader +pub const MAX_PROCESSING_AGE: usize = MAX_RECENT_BLOCKHASHES / 2; + +/// This is maximum time consumed in forwarding a transaction from one node to next, before +/// it can be processed in the target node +#[cfg(feature = "cuda")] +pub const MAX_TRANSACTION_FORWARDING_DELAY: usize = 2; + +/// More delay is expected if CUDA is not enabled (as signature verification takes longer) +#[cfg(not(feature = "cuda"))] +pub const MAX_TRANSACTION_FORWARDING_DELAY: usize = 6; + +/// Converts a slot to a storage segment. Does not indicate that a segment is complete. +pub fn get_segment_from_slot(rooted_slot: Slot, slots_per_segment: u64) -> Segment { + ((rooted_slot + (slots_per_segment - 1)) / slots_per_segment) +} + +/// Given a slot returns the latest complete segment, if no segment could possibly be complete +/// for a given slot it returns `None` (i.e if `slot < slots_per_segment`) +pub fn get_complete_segment_from_slot( + rooted_slot: Slot, + slots_per_segment: u64, +) -> Option { + let completed_segment = rooted_slot / slots_per_segment; + if rooted_slot < slots_per_segment { + None + } else { + Some(completed_segment) + } +} + +/// Slot is a unit of time given to a leader for encoding, +/// is some some number of Ticks long. +pub type Slot = u64; + +/// A segment is some number of slots stored by replicators +pub type Segment = u64; + +/// Epoch is a unit of time a given leader schedule is honored, +/// some number of Slots. +pub type Epoch = u64; + +#[cfg(test)] +mod tests { + use super::*; + + fn get_segments(slot: Slot, slots_per_segment: u64) -> (Segment, Segment) { + ( + get_segment_from_slot(slot, slots_per_segment), + get_complete_segment_from_slot(slot, slots_per_segment).unwrap(), + ) + } + + #[test] + fn test_complete_segment_impossible() { + // slot < slots_per_segment so there can be no complete segments + assert_eq!(get_complete_segment_from_slot(5, 10), None); + } + + #[test] + fn test_segment_conversion() { + let (current, complete) = get_segments(2048, 1024); + assert_eq!(current, complete); + let (current, complete) = get_segments(2049, 1024); + assert!(complete < current); + } +} diff --git a/sdk/src/fee_calculator.rs b/sdk/src/fee_calculator.rs index 2628863777..8b5385afc9 100644 --- a/sdk/src/fee_calculator.rs +++ b/sdk/src/fee_calculator.rs @@ -1,5 +1,5 @@ use crate::message::Message; -use crate::timing::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT}; +use crate::clock::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT}; use log::*; #[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] diff --git a/sdk/src/genesis_block.rs b/sdk/src/genesis_block.rs index 82696b1d41..b5e208775e 100644 --- a/sdk/src/genesis_block.rs +++ b/sdk/src/genesis_block.rs @@ -9,7 +9,7 @@ use crate::pubkey::Pubkey; use crate::rent::Rent; use crate::signature::{Keypair, KeypairUtil}; use crate::system_program::{self, solana_system_program}; -use crate::timing::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT}; +use crate::clock::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT}; use bincode::{deserialize, serialize}; use memmap::Mmap; use std::fs::{File, OpenOptions}; diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index c880387d91..fa88f7a973 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -1,6 +1,7 @@ #[macro_use] extern crate cfg_if; +pub mod clock; pub mod pubkey; // On-chain program modules diff --git a/sdk/src/poh_config.rs b/sdk/src/poh_config.rs index 5c41acfb9c..5c39035365 100644 --- a/sdk/src/poh_config.rs +++ b/sdk/src/poh_config.rs @@ -1,4 +1,4 @@ -use crate::timing::DEFAULT_TICKS_PER_SECOND; +use crate::clock::DEFAULT_TICKS_PER_SECOND; use std::time::Duration; #[derive(Serialize, Deserialize, Clone, Debug)] diff --git a/sdk/src/sysvar/clock.rs b/sdk/src/sysvar/clock.rs index e5582fcaed..57d30cb02c 100644 --- a/sdk/src/sysvar/clock.rs +++ b/sdk/src/sysvar/clock.rs @@ -4,7 +4,7 @@ use crate::account::Account; use crate::sysvar; use bincode::serialized_size; -pub use crate::timing::{Epoch, Slot}; +pub use crate::clock::{Epoch, Slot}; const ID: [u8; 32] = [ 6, 167, 213, 23, 24, 199, 116, 201, 40, 86, 99, 152, 105, 29, 94, 182, 139, 94, 184, 163, 155, @@ -57,7 +57,7 @@ pub fn create_account( use crate::account::KeyedAccount; use crate::instruction::InstructionError; -use crate::timing::Segment; +use crate::clock::Segment; pub fn from_keyed_account(account: &KeyedAccount) -> Result { if !check_id(account.unsigned_key()) { diff --git a/sdk/src/sysvar/slot_hashes.rs b/sdk/src/sysvar/slot_hashes.rs index 6657508800..e6d50ee79b 100644 --- a/sdk/src/sysvar/slot_hashes.rs +++ b/sdk/src/sysvar/slot_hashes.rs @@ -8,7 +8,7 @@ use crate::sysvar; use bincode::serialized_size; use std::ops::Deref; -pub use crate::timing::Slot; +pub use crate::clock::Slot; const ID: [u8; 32] = [ 6, 167, 213, 23, 25, 47, 10, 175, 198, 242, 101, 227, 251, 119, 204, 122, 218, 130, 197, 41, diff --git a/sdk/src/sysvar/stake_history.rs b/sdk/src/sysvar/stake_history.rs index 3b58c94172..505992c78a 100644 --- a/sdk/src/sysvar/stake_history.rs +++ b/sdk/src/sysvar/stake_history.rs @@ -8,7 +8,7 @@ use bincode::serialized_size; use std::collections::HashMap; use std::ops::Deref; -pub use crate::timing::Epoch; +pub use crate::clock::Epoch; const ID: [u8; 32] = [ 6, 167, 213, 23, 25, 53, 132, 208, 254, 237, 155, 179, 67, 29, 19, 32, 107, 229, 68, 40, 27, diff --git a/sdk/src/timing.rs b/sdk/src/timing.rs index a4429f9596..be0c87dcc0 100644 --- a/sdk/src/timing.rs +++ b/sdk/src/timing.rs @@ -2,48 +2,6 @@ use std::time::Duration; use std::time::{SystemTime, UNIX_EPOCH}; -// The default tick rate that the cluster attempts to achieve. Note that the actual tick -// rate at any given time should be expected to drift -pub const DEFAULT_TICKS_PER_SECOND: u64 = 10; - -// At 10 ticks/s, 4 ticks per slot implies that leader rotation and voting will happen -// every 400 ms. A fast voting cadence ensures faster finality and convergence -pub const DEFAULT_TICKS_PER_SLOT: u64 = 4; - -// 1 Epoch = 400 * 8192 ms ~= 55 minutes -pub const DEFAULT_SLOTS_PER_EPOCH: u64 = 8192; - -// Storage segment configuration -pub const DEFAULT_SLOTS_PER_SEGMENT: u64 = 1024; - -// 4 times longer than the max_lockout to allow enough time for PoRep (128 slots) -pub const DEFAULT_SLOTS_PER_TURN: u64 = 32 * 4; - -pub const NUM_CONSECUTIVE_LEADER_SLOTS: u64 = 4; - -/// The time window of recent block hash values that the bank will track the signatures -/// of over. Once the bank discards a block hash, it will reject any transactions that use -/// that `recent_blockhash` in a transaction. Lowering this value reduces memory consumption, -/// but requires clients to update its `recent_blockhash` more frequently. Raising the value -/// lengthens the time a client must wait to be certain a missing transaction will -/// not be processed by the network. -pub const MAX_HASH_AGE_IN_SECONDS: usize = 120; - -// This must be <= MAX_HASH_AGE_IN_SECONDS, otherwise there's risk for DuplicateSignature errors -pub const MAX_RECENT_BLOCKHASHES: usize = MAX_HASH_AGE_IN_SECONDS; - -// The maximum age of a blockhash that will be accepted by the leader -pub const MAX_PROCESSING_AGE: usize = MAX_RECENT_BLOCKHASHES / 2; - -/// This is maximum time consumed in forwarding a transaction from one node to next, before -/// it can be processed in the target node -#[cfg(feature = "cuda")] -pub const MAX_TRANSACTION_FORWARDING_DELAY: usize = 2; - -/// More delay is expected if CUDA is not enabled (as signature verification takes longer) -#[cfg(not(feature = "cuda"))] -pub const MAX_TRANSACTION_FORWARDING_DELAY: usize = 6; - pub fn duration_as_ns(d: &Duration) -> u64 { d.as_secs() * 1_000_000_000 + u64::from(d.subsec_nanos()) } @@ -66,59 +24,3 @@ pub fn timestamp() -> u64 { .expect("create timestamp in timing"); duration_as_ms(&now) } - -/// Converts a slot to a storage segment. Does not indicate that a segment is complete. -pub fn get_segment_from_slot(rooted_slot: Slot, slots_per_segment: u64) -> Segment { - ((rooted_slot + (slots_per_segment - 1)) / slots_per_segment) -} - -/// Given a slot returns the latest complete segment, if no segment could possibly be complete -/// for a given slot it returns `None` (i.e if `slot < slots_per_segment`) -pub fn get_complete_segment_from_slot( - rooted_slot: Slot, - slots_per_segment: u64, -) -> Option { - let completed_segment = rooted_slot / slots_per_segment; - if rooted_slot < slots_per_segment { - None - } else { - Some(completed_segment) - } -} - -/// Slot is a unit of time given to a leader for encoding, -/// is some some number of Ticks long. Use a u64 to count them. -pub type Slot = u64; - -/// A segment is some number of slots stored by replicators -pub type Segment = u64; - -/// Epoch is a unit of time a given leader schedule is honored, -/// some number of Slots. Use a u64 to count them. -pub type Epoch = u64; - -#[cfg(test)] -mod tests { - use super::*; - - fn get_segments(slot: Slot, slots_per_segment: u64) -> (Segment, Segment) { - ( - get_segment_from_slot(slot, slots_per_segment), - get_complete_segment_from_slot(slot, slots_per_segment).unwrap(), - ) - } - - #[test] - fn test_complete_segment_impossible() { - // slot < slots_per_segment so there can be no complete segments - assert_eq!(get_complete_segment_from_slot(5, 10), None); - } - - #[test] - fn test_segment_conversion() { - let (current, complete) = get_segments(2048, 1024); - assert_eq!(current, complete); - let (current, complete) = get_segments(2049, 1024); - assert!(complete < current); - } -} diff --git a/validator/src/main.rs b/validator/src/main.rs index 717f1e3958..f57d61c518 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -12,9 +12,9 @@ use solana_core::ledger_cleanup_service::DEFAULT_MAX_LEDGER_SLOTS; use solana_core::service::Service; use solana_core::socketaddr; use solana_core::validator::{Validator, ValidatorConfig}; +use solana_sdk::clock::Slot; use solana_sdk::hash::Hash; use solana_sdk::signature::{read_keypair, Keypair, KeypairUtil}; -use solana_sdk::timing::Slot; use std::fs::{self, File}; use std::io::{self, Read}; use std::net::SocketAddr;