From d3dc3d060a2207c64a2c879621fc959de4c84660 Mon Sep 17 00:00:00 2001 From: Kevin Heavey Date: Tue, 14 Jan 2025 16:52:16 +0100 Subject: [PATCH] remove almost all solana-sdk usage from solana-stake-program (#4447) --- Cargo.lock | 12 ++++ programs/sbf/Cargo.lock | 9 +++ programs/stake/Cargo.toml | 12 ++++ programs/stake/benches/stake.rs | 37 ++++++------- programs/stake/src/config.rs | 9 ++- programs/stake/src/lib.rs | 5 +- programs/stake/src/points.rs | 12 ++-- programs/stake/src/rewards.rs | 19 +++---- programs/stake/src/stake_instruction.rs | 73 ++++++++++++------------- programs/stake/src/stake_state.rs | 37 ++++++------- svm/examples/Cargo.lock | 9 +++ 11 files changed, 131 insertions(+), 103 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1925e6aec3328..47a111259e4b9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9299,15 +9299,27 @@ dependencies = [ "log", "proptest", "solana-account", + "solana-bincode", + "solana-clock", "solana-compute-budget", "solana-config-program", + "solana-epoch-rewards", + "solana-epoch-schedule", "solana-feature-set", "solana-instruction", "solana-log-collector", "solana-logger", + "solana-native-token", + "solana-packet", + "solana-program", "solana-program-runtime", "solana-pubkey", + "solana-rent", "solana-sdk", + "solana-sdk-ids", + "solana-sysvar", + "solana-sysvar-id", + "solana-transaction-context", "solana-type-overrides", "solana-vote-program", "test-case", diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 62400889e332da..9f1999293a41a0 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -7850,13 +7850,22 @@ dependencies = [ "bincode", "log", "solana-account", + "solana-bincode", + "solana-clock", "solana-config-program", "solana-feature-set", "solana-instruction", "solana-log-collector", + "solana-native-token", + "solana-packet", + "solana-program", "solana-program-runtime", "solana-pubkey", + "solana-rent", "solana-sdk", + "solana-sdk-ids", + "solana-sysvar", + "solana-transaction-context", "solana-type-overrides", "solana-vote-program", ] diff --git a/programs/stake/Cargo.toml b/programs/stake/Cargo.toml index 09f279dfd1853b..2cd5d5b69afbd1 100644 --- a/programs/stake/Cargo.toml +++ b/programs/stake/Cargo.toml @@ -13,13 +13,22 @@ edition = { workspace = true } bincode = { workspace = true } log = { workspace = true } solana-account = { workspace = true } +solana-bincode = { workspace = true } +solana-clock = { workspace = true } solana-config-program = { workspace = true } solana-feature-set = { workspace = true } solana-instruction = { workspace = true } solana-log-collector = { workspace = true } +solana-native-token = { workspace = true } +solana-packet = { workspace = true } +solana-program = { workspace = true } solana-program-runtime = { workspace = true } solana-pubkey = { workspace = true } +solana-rent = { workspace = true } solana-sdk = { workspace = true } +solana-sdk-ids = { workspace = true } +solana-sysvar = { workspace = true } +solana-transaction-context = { workspace = true } solana-type-overrides = { workspace = true } solana-vote-program = { workspace = true, default-features = false } @@ -28,7 +37,10 @@ assert_matches = { workspace = true } criterion = { workspace = true } proptest = { workspace = true } solana-compute-budget = { workspace = true } +solana-epoch-rewards = { workspace = true } +solana-epoch-schedule = { workspace = true } solana-logger = { workspace = true } +solana-sysvar-id = { workspace = true } test-case = { workspace = true } [features] diff --git a/programs/stake/benches/stake.rs b/programs/stake/benches/stake.rs index 21e57d58ef9da7..873382647fdb09 100644 --- a/programs/stake/benches/stake.rs +++ b/programs/stake/benches/stake.rs @@ -2,31 +2,26 @@ use { bincode::serialize, criterion::{black_box, criterion_group, criterion_main, Criterion}, solana_account::{create_account_shared_data_for_test, AccountSharedData, WritableAccount}, + solana_clock::{Clock, Epoch}, solana_feature_set::FeatureSet, solana_instruction::AccountMeta, - solana_program_runtime::invoke_context::mock_process_instruction, - solana_pubkey::Pubkey, - solana_sdk::{ - clock::{Clock, Epoch}, - stake::{ - instruction::{ - self, AuthorizeCheckedWithSeedArgs, AuthorizeWithSeedArgs, LockupArgs, - LockupCheckedArgs, StakeInstruction, - }, - stake_flags::StakeFlags, - state::{Authorized, Lockup, StakeAuthorize, StakeStateV2}, - }, - stake_history::StakeHistory, - sysvar::{ - clock, - rent::{self, Rent}, - stake_history, + solana_program::stake::{ + instruction::{ + self, AuthorizeCheckedWithSeedArgs, AuthorizeWithSeedArgs, LockupArgs, + LockupCheckedArgs, StakeInstruction, }, + stake_flags::StakeFlags, + state::{Authorized, Lockup, StakeAuthorize, StakeStateV2}, }, + solana_program_runtime::invoke_context::mock_process_instruction, + solana_pubkey::Pubkey, + solana_rent::Rent, + solana_sdk_ids::sysvar::{clock, rent, stake_history}, solana_stake_program::{ stake_instruction, stake_state::{Delegation, Meta, Stake}, }, + solana_sysvar::stake_history::StakeHistory, solana_vote_program::vote_state::{self, VoteState, VoteStateVersions}, std::sync::Arc, }; @@ -207,7 +202,7 @@ impl TestSetup { fn bench_initialize(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.add_account( - solana_sdk::sysvar::rent::id(), + solana_sdk_ids::sysvar::rent::id(), create_account_shared_data_for_test(&Rent::default()), ); @@ -224,7 +219,7 @@ fn bench_initialize(c: &mut Criterion) { fn bench_initialize_checked(c: &mut Criterion) { let mut test_setup = TestSetup::new(); test_setup.add_account( - solana_sdk::sysvar::rent::id(), + solana_sdk_ids::sysvar::rent::id(), create_account_shared_data_for_test(&Rent::default()), ); // add staker account @@ -626,7 +621,7 @@ fn bench_deactivate_delinquent(c: &mut Criterion) { // reference vote account has been consistently voting let mut vote_state = VoteState::default(); - for epoch in 0..=solana_sdk::stake::MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION { + for epoch in 0..=solana_program::stake::MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION { vote_state.increment_credits(epoch as Epoch, 1); } let reference_vote_address = Pubkey::new_unique(); @@ -662,7 +657,7 @@ fn bench_deactivate_delinquent(c: &mut Criterion) { test_setup.add_account( clock::id(), create_account_shared_data_for_test(&Clock { - epoch: solana_sdk::stake::MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION as u64, + epoch: solana_program::stake::MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION as u64, ..Clock::default() }), ); diff --git a/programs/stake/src/config.rs b/programs/stake/src/config.rs index 1d27b823138ebe..a76d1e19eb7585 100644 --- a/programs/stake/src/config.rs +++ b/programs/stake/src/config.rs @@ -1,17 +1,16 @@ //! config for staking //! carries variables that the stake program cares about -#[allow(deprecated)] -use solana_sdk::stake::config; #[deprecated( since = "1.8.0", note = "Please use `solana_sdk::stake::config` or `solana_program::stake::config` instead" )] -pub use solana_sdk::stake::config::*; +pub use solana_program::stake::config::*; use { bincode::deserialize, solana_account::{AccountSharedData, ReadableAccount, WritableAccount}, solana_config_program::{create_config_account, get_config_data}, - solana_sdk::{genesis_config::GenesisConfig, transaction_context::BorrowedAccount}, + solana_sdk::genesis_config::GenesisConfig, + solana_transaction_context::BorrowedAccount, }; #[allow(deprecated)] @@ -33,7 +32,7 @@ pub fn add_genesis_account(genesis_config: &mut GenesisConfig) -> u64 { account.set_lamports(lamports.max(1)); - genesis_config.add_account(config::id(), account); + genesis_config.add_account(solana_sdk_ids::config::id(), account); lamports } diff --git a/programs/stake/src/lib.rs b/programs/stake/src/lib.rs index e56d9d4b996676..f06cef980dda59 100644 --- a/programs/stake/src/lib.rs +++ b/programs/stake/src/lib.rs @@ -4,10 +4,11 @@ since = "1.8.0", note = "Please use `solana_sdk::stake::program::id` or `solana_program::stake::program::id` instead" )] -pub use solana_sdk::stake::program::{check_id, id}; +pub use solana_sdk_ids::stake::{check_id, id}; use { solana_feature_set::{self as feature_set, FeatureSet}, - solana_sdk::{genesis_config::GenesisConfig, native_token::LAMPORTS_PER_SOL}, + solana_native_token::LAMPORTS_PER_SOL, + solana_sdk::genesis_config::GenesisConfig, }; pub mod config; diff --git a/programs/stake/src/points.rs b/programs/stake/src/points.rs index 32bee9cdac7a38..4c6e4c2398733e 100644 --- a/programs/stake/src/points.rs +++ b/programs/stake/src/points.rs @@ -2,13 +2,11 @@ //! Used by `solana-runtime`. use { + solana_clock::Epoch, solana_instruction::error::InstructionError, + solana_program::stake::state::{Delegation, Stake, StakeStateV2}, solana_pubkey::Pubkey, - solana_sdk::{ - clock::Epoch, - stake::state::{Delegation, Stake, StakeStateV2}, - stake_history::StakeHistory, - }, + solana_sysvar::stake_history::StakeHistory, solana_vote_program::vote_state::VoteState, std::cmp::Ordering, }; @@ -212,7 +210,7 @@ pub(crate) fn calculate_stake_points_and_credits( #[cfg(test)] mod tests { - use {super::*, crate::stake_state::new_stake, solana_sdk::native_token}; + use {super::*, crate::stake_state::new_stake, solana_native_token::sol_to_lamports}; #[test] fn test_stake_state_calculate_points_with_typical_values() { @@ -221,7 +219,7 @@ mod tests { // bootstrap means fully-vested stake at epoch 0 with // 10_000_000 SOL is a big but not unreasaonable stake let stake = new_stake( - native_token::sol_to_lamports(10_000_000f64), + sol_to_lamports(10_000_000f64), &Pubkey::default(), &vote_state, u64::MAX, diff --git a/programs/stake/src/rewards.rs b/programs/stake/src/rewards.rs index 6e07d6ff987656..3994e4f7ab13f1 100644 --- a/programs/stake/src/rewards.rs +++ b/programs/stake/src/rewards.rs @@ -6,17 +6,14 @@ use { calculate_stake_points_and_credits, CalculatedStakePoints, InflationPointCalculationEvent, PointValue, SkippedReason, }, - solana_account::{AccountSharedData, WritableAccount}, + solana_account::{state_traits::StateMut, AccountSharedData, WritableAccount}, + solana_clock::Epoch, solana_instruction::error::InstructionError, - solana_sdk::{ - account_utils::StateMut, - clock::Epoch, - stake::{ - instruction::StakeError, - state::{Stake, StakeStateV2}, - }, - stake_history::StakeHistory, + solana_program::stake::{ + instruction::StakeError, + state::{Stake, StakeStateV2}, }, + solana_sysvar::stake_history::StakeHistory, solana_vote_program::vote_state::VoteState, }; @@ -231,8 +228,8 @@ mod tests { use { super::*, crate::{points::null_tracer, stake_state::new_stake}, + solana_native_token::sol_to_lamports, solana_pubkey::Pubkey, - solana_sdk::native_token, test_case::test_case, }; @@ -640,7 +637,7 @@ mod tests { // bootstrap means fully-vested stake at epoch 0 with // 10_000_000 SOL is a big but not unreasaonable stake let stake = new_stake( - native_token::sol_to_lamports(10_000_000f64), + sol_to_lamports(10_000_000f64), &Pubkey::default(), &vote_state, u64::MAX, diff --git a/programs/stake/src/stake_instruction.rs b/programs/stake/src/stake_instruction.rs index f04de8baaa2598..01c2c63e6462d5 100644 --- a/programs/stake/src/stake_instruction.rs +++ b/programs/stake/src/stake_instruction.rs @@ -5,20 +5,18 @@ use { withdraw, }, log::*, + solana_bincode::limited_deserialize, solana_instruction::error::InstructionError, + solana_program::stake::{ + instruction::{LockupArgs, StakeError, StakeInstruction}, + program::id, + state::{Authorized, Lockup}, + }, solana_program_runtime::{ declare_process_instruction, sysvar_cache::get_sysvar_with_account_check, }, solana_pubkey::Pubkey, - solana_sdk::{ - program_utils::limited_deserialize, - stake::{ - instruction::{LockupArgs, StakeError, StakeInstruction}, - program::id, - state::{Authorized, Lockup}, - }, - transaction_context::{IndexOfAccount, InstructionContext, TransactionContext}, - }, + solana_transaction_context::{IndexOfAccount, InstructionContext, TransactionContext}, }; fn get_optional_pubkey<'a>( @@ -75,7 +73,8 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context| let signers = instruction_context.get_signers(transaction_context)?; - let stake_instruction: StakeInstruction = limited_deserialize(data)?; + let stake_instruction: StakeInstruction = + limited_deserialize(data, solana_packet::PACKET_DATA_SIZE as u64)?; if epoch_rewards_active && !matches!(stake_instruction, StakeInstruction::GetMinimumDelegation) { return Err(StakeError::EpochRewardsActive.into()); @@ -394,32 +393,32 @@ mod tests { create_account_shared_data_for_test, state_traits::StateMut, AccountSharedData, ReadableAccount, WritableAccount, }, + solana_clock::{Clock, Epoch, UnixTimestamp}, + solana_epoch_rewards::EpochRewards, + solana_epoch_schedule::EpochSchedule, solana_feature_set::FeatureSet, solana_instruction::{AccountMeta, Instruction}, + solana_program::stake::{ + config as stake_config, + instruction::{ + self, authorize_checked, authorize_checked_with_seed, initialize_checked, + set_lockup_checked, AuthorizeCheckedWithSeedArgs, AuthorizeWithSeedArgs, + LockupArgs, StakeError, + }, + stake_flags::StakeFlags, + state::{warmup_cooldown_rate, Authorized, Lockup, StakeAuthorize}, + MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION, + }, solana_program_runtime::invoke_context::mock_process_instruction, solana_pubkey::Pubkey, - solana_sdk::{ - clock::{Clock, Epoch, UnixTimestamp}, - epoch_schedule::EpochSchedule, - rent::Rent, - stake::{ - config as stake_config, - instruction::{ - self, authorize_checked, authorize_checked_with_seed, initialize_checked, - set_lockup_checked, AuthorizeCheckedWithSeedArgs, AuthorizeWithSeedArgs, - LockupArgs, StakeError, - }, - stake_flags::StakeFlags, - state::{warmup_cooldown_rate, Authorized, Lockup, StakeAuthorize}, - MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION, - }, - stake_history::{StakeHistory, StakeHistoryEntry}, + solana_rent::Rent, + solana_sdk_ids::{ system_program, - sysvar::{ - clock, - epoch_rewards::{self, EpochRewards}, - epoch_schedule, rent, rewards, stake_history, - }, + sysvar::{clock, epoch_rewards, epoch_schedule, rent, rewards, stake_history}, + }, + solana_sysvar::{ + rewards::Rewards, + stake_history::{StakeHistory, StakeHistoryEntry}, }, solana_vote_program::vote_state::{self, VoteState, VoteStateVersions}, std::{collections::HashSet, str::FromStr, sync::Arc}, @@ -503,9 +502,9 @@ mod tests { ( *pubkey, if clock::check_id(pubkey) { - create_account_shared_data_for_test(&clock::Clock::default()) + create_account_shared_data_for_test(&Clock::default()) } else if rewards::check_id(pubkey) { - create_account_shared_data_for_test(&rewards::Rewards::new(0.0)) + create_account_shared_data_for_test(&Rewards::new(0.0)) } else if stake_history::check_id(pubkey) { create_account_shared_data_for_test(&StakeHistory::default()) } else if stake_config::check_id(pubkey) { @@ -841,13 +840,13 @@ mod tests { let rent = Rent::default(); let rent_account = create_account_shared_data_for_test(&rent); let rewards_address = rewards::id(); - let rewards_account = create_account_shared_data_for_test(&rewards::Rewards::new(0.0)); + let rewards_account = create_account_shared_data_for_test(&Rewards::new(0.0)); let stake_history_address = stake_history::id(); let stake_history_account = create_account_shared_data_for_test(&StakeHistory::default()); let vote_address = Pubkey::new_unique(); let vote_account = AccountSharedData::new(0, 0, &solana_vote_program::id()); let clock_address = clock::id(); - let clock_account = create_account_shared_data_for_test(&clock::Clock::default()); + let clock_account = create_account_shared_data_for_test(&Clock::default()); #[allow(deprecated)] let config_address = stake_config::id(); #[allow(deprecated)] @@ -2415,7 +2414,7 @@ mod tests { &serialize(&StakeInstruction::DelegateStake).unwrap(), transaction_accounts.clone(), instruction_accounts.clone(), - Err(solana_sdk::instruction::InstructionError::IncorrectProgramId), + Err(solana_instruction::error::InstructionError::IncorrectProgramId), ); // verify that non-stakes fail delegate() @@ -2427,7 +2426,7 @@ mod tests { &serialize(&StakeInstruction::DelegateStake).unwrap(), transaction_accounts, instruction_accounts, - Err(solana_sdk::instruction::InstructionError::IncorrectProgramId), + Err(solana_instruction::error::InstructionError::IncorrectProgramId), ); } diff --git a/programs/stake/src/stake_state.rs b/programs/stake/src/stake_state.rs index f0b53ddd8e5006..e67bf832014000 100644 --- a/programs/stake/src/stake_state.rs +++ b/programs/stake/src/stake_state.rs @@ -7,27 +7,25 @@ since = "1.8.0", note = "Please use `solana_sdk::stake::state` or `solana_program::stake::state` instead" )] -pub use solana_sdk::stake::state::*; +pub use solana_program::stake::state::*; use { solana_account::{state_traits::StateMut, AccountSharedData, ReadableAccount}, + solana_clock::{Clock, Epoch}, solana_feature_set::FeatureSet, solana_instruction::error::InstructionError, solana_log_collector::ic_msg, + solana_program::stake::{ + instruction::{LockupArgs, StakeError}, + program::id, + stake_flags::StakeFlags, + tools::{acceptable_reference_epoch_credits, eligible_for_deactivate_delinquent}, + }, solana_program_runtime::invoke_context::InvokeContext, solana_pubkey::Pubkey, - solana_sdk::{ - clock::{Clock, Epoch}, - rent::Rent, - stake::{ - instruction::{LockupArgs, StakeError}, - program::id, - stake_flags::StakeFlags, - tools::{acceptable_reference_epoch_credits, eligible_for_deactivate_delinquent}, - }, - stake_history::{StakeHistory, StakeHistoryEntry}, - transaction_context::{ - BorrowedAccount, IndexOfAccount, InstructionContext, TransactionContext, - }, + solana_rent::Rent, + solana_sysvar::stake_history::{StakeHistory, StakeHistoryEntry}, + solana_transaction_context::{ + BorrowedAccount, IndexOfAccount, InstructionContext, TransactionContext, }, solana_vote_program::vote_state::{self, VoteState, VoteStateVersions}, std::{collections::HashSet, convert::TryFrom}, @@ -1451,13 +1449,12 @@ mod tests { super::*, proptest::prelude::*, solana_account::{create_account_shared_data_for_test, AccountSharedData}, + solana_epoch_schedule::EpochSchedule, + solana_program::stake::state::warmup_cooldown_rate, solana_program_runtime::with_mock_invoke_context, solana_pubkey::Pubkey, - solana_sdk::{ - epoch_schedule::EpochSchedule, - stake::state::warmup_cooldown_rate, - sysvar::{epoch_schedule, SysvarId}, - }, + solana_sdk_ids::sysvar::epoch_schedule, + solana_sysvar_id::SysvarId, test_case::test_case, }; @@ -2238,7 +2235,7 @@ mod tests { panic!( "stake minimum_balance: {} lamports, {} SOL", minimum_balance, - minimum_balance as f64 / solana_sdk::native_token::LAMPORTS_PER_SOL as f64 + minimum_balance as f64 / solana_native_token::LAMPORTS_PER_SOL as f64 ); } diff --git a/svm/examples/Cargo.lock b/svm/examples/Cargo.lock index 4add8f8ee12ba1..095f61244d9b23 100644 --- a/svm/examples/Cargo.lock +++ b/svm/examples/Cargo.lock @@ -7179,13 +7179,22 @@ dependencies = [ "bincode", "log", "solana-account", + "solana-bincode", + "solana-clock", "solana-config-program", "solana-feature-set", "solana-instruction", "solana-log-collector", + "solana-native-token", + "solana-packet", + "solana-program", "solana-program-runtime", "solana-pubkey", + "solana-rent", "solana-sdk", + "solana-sdk-ids", + "solana-sysvar", + "solana-transaction-context", "solana-type-overrides", "solana-vote-program", ]