diff --git a/proto/src/convert.rs b/proto/src/convert.rs index a288b04..5aa6efe 100644 --- a/proto/src/convert.rs +++ b/proto/src/convert.rs @@ -1163,706 +1163,3 @@ impl TryFrom for Vec { .collect::, Self::Error>>() } } - -#[cfg(test)] -mod test { - use crate::solana::storage::confirmed_block; - use solana_sdk::bs58; - use {super::*, enum_iterator::all}; - - #[test] - fn test_reward_type_encode() { - let mut reward = Reward { - pubkey: "invalid".to_string(), - lamports: 123, - post_balance: 321, - reward_type: None, - commission: None, - }; - let gen_reward: confirmed_block::Reward = reward.clone().into(); - assert_eq!(reward, gen_reward.into()); - - reward.reward_type = Some(RewardType::Fee); - let gen_reward: confirmed_block::Reward = reward.clone().into(); - assert_eq!(reward, gen_reward.into()); - - reward.reward_type = Some(RewardType::Rent); - let gen_reward: confirmed_block::Reward = reward.clone().into(); - assert_eq!(reward, gen_reward.into()); - - reward.reward_type = Some(RewardType::Voting); - let gen_reward: confirmed_block::Reward = reward.clone().into(); - assert_eq!(reward, gen_reward.into()); - - reward.reward_type = Some(RewardType::Staking); - let gen_reward: confirmed_block::Reward = reward.clone().into(); - assert_eq!(reward, gen_reward.into()); - } - - #[test] - fn test_transaction_by_addr_encode() { - let info = TransactionByAddrInfo { - signature: bs58::decode("Nfo6rgemG1KLbk1xuNwfrQTsdxaGfLuWURHNRy9LYnDrubG7LFQZaA5obPNas9LQ6DdorJqxh2LxA3PsnWdkSrL") - .into_vec() - .map(Signature::try_from) - .unwrap() - .unwrap(), - err: None, - index: 5, - memo: Some("string".to_string()), - block_time: Some(1610674861) - }; - - let tx_by_addr_transaction_info: tx_by_addr::TransactionByAddrInfo = info.clone().into(); - assert_eq!(info, tx_by_addr_transaction_info.try_into().unwrap()); - } - - #[test] - fn test_transaction_error_encode() { - let transaction_error = TransactionError::AccountBorrowOutstanding; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::AccountInUse; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::AccountLoadedTwice; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::AccountNotFound; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::AlreadyProcessed; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::BlockhashNotFound; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::CallChainTooDeep; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::ClusterMaintenance; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::InsufficientFundsForFee; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::InvalidAccountForFee; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::InvalidAccountIndex; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::InvalidProgramForExecution; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::MissingSignatureForFee; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::ProgramAccountNotFound; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::SanitizeFailure; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::SignatureFailure; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::WouldExceedMaxBlockCostLimit; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::WouldExceedMaxVoteCostLimit; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::WouldExceedMaxAccountCostLimit; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::UnsupportedVersion; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::AccountAlreadyInitialized); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::AccountBorrowFailed); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::AccountBorrowOutstanding); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::AccountDataSizeChanged); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::AccountDataTooSmall); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::AccountNotExecutable); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::InstructionError(10, InstructionError::CallDepth); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ComputationalBudgetExceeded); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::DuplicateAccountIndex); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::DuplicateAccountOutOfSync); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::InstructionError( - 10, - InstructionError::ExecutableAccountNotRentExempt, - ); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ExecutableDataModified); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ExecutableLamportChange); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ExecutableModified); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ExternalAccountDataModified); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ExternalAccountLamportSpend); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::GenericError); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::InstructionError(10, InstructionError::Immutable); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::IncorrectAuthority); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::IncorrectProgramId); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::InsufficientFunds); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::InvalidAccountData); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::InvalidArgument); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::InvalidError); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::InvalidInstructionData); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::InvalidRealloc); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::InvalidSeeds); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::MaxSeedLengthExceeded); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::MissingAccount); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::MissingRequiredSignature); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ModifiedProgramId); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::NotEnoughAccountKeys); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::PrivilegeEscalation); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::InstructionError( - 10, - InstructionError::ProgramEnvironmentSetupFailure, - ); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ProgramFailedToCompile); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ProgramFailedToComplete); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ReadonlyDataModified); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ReadonlyLamportChange); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::ReentrancyNotAllowed); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::RentEpochModified); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::UnbalancedInstruction); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::UninitializedAccount); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::UnsupportedProgramId); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = - TransactionError::InstructionError(10, InstructionError::Custom(10)); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::DuplicateInstruction(10); - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - - let transaction_error = TransactionError::InsufficientFundsForRent { account_index: 10 }; - let tx_by_addr_transaction_error: tx_by_addr::TransactionError = - transaction_error.clone().into(); - assert_eq!( - transaction_error, - tx_by_addr_transaction_error.try_into().unwrap() - ); - } - - #[test] - fn test_error_enums() { - let ix_index = 1; - let custom_error = 42; - for error in all::() { - match error { - tx_by_addr::TransactionErrorType::DuplicateInstruction - | tx_by_addr::TransactionErrorType::InsufficientFundsForRent => { - let tx_by_addr_error = tx_by_addr::TransactionError { - transaction_error: error as i32, - instruction_error: None, - transaction_details: Some(tx_by_addr::TransactionDetails { - index: ix_index, - }), - }; - let transaction_error: TransactionError = tx_by_addr_error - .clone() - .try_into() - .unwrap_or_else(|_| panic!("{error:?} conversion implemented?")); - assert_eq!(tx_by_addr_error, transaction_error.into()); - } - tx_by_addr::TransactionErrorType::InstructionError => { - for ix_error in all::() { - if ix_error != tx_by_addr::InstructionErrorType::Custom { - let tx_by_addr_error = tx_by_addr::TransactionError { - transaction_error: error as i32, - instruction_error: Some(tx_by_addr::InstructionError { - index: ix_index, - error: ix_error as i32, - custom: None, - }), - transaction_details: None, - }; - let transaction_error: TransactionError = tx_by_addr_error - .clone() - .try_into() - .unwrap_or_else(|_| panic!("{ix_error:?} conversion implemented?")); - assert_eq!(tx_by_addr_error, transaction_error.into()); - } else { - let tx_by_addr_error = tx_by_addr::TransactionError { - transaction_error: error as i32, - instruction_error: Some(tx_by_addr::InstructionError { - index: ix_index, - error: ix_error as i32, - custom: Some(tx_by_addr::CustomError { - custom: custom_error, - }), - }), - transaction_details: None, - }; - let transaction_error: TransactionError = - tx_by_addr_error.clone().try_into().unwrap(); - assert_eq!(tx_by_addr_error, transaction_error.into()); - } - } - } - _ => { - let tx_by_addr_error = tx_by_addr::TransactionError { - transaction_error: error as i32, - instruction_error: None, - transaction_details: None, - }; - let transaction_error: TransactionError = tx_by_addr_error - .clone() - .try_into() - .unwrap_or_else(|_| panic!("{error:?} conversion implemented?")); - assert_eq!(tx_by_addr_error, transaction_error.into()); - } - } - } - } -}