diff --git a/Cargo.toml b/Cargo.toml index e7ec578..43f0a06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,9 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -solana-program = "~1.9" -borsh = "0.9.3" -thiserror = "1.0.31" +solana-program = "2.0.10" +borsh = "1.5.1" +thiserror = "1.0.63" [lib] crate-type = ["cdylib", "lib"] \ No newline at end of file diff --git a/src/instruction.rs b/src/instruction.rs index dd806a0..4a007d1 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -14,21 +14,23 @@ struct StudentIntroPayload { impl IntroInstruction { pub fn unpack(input: &[u8]) -> Result { - let (variant, rest) = input + let (&discriminator, rest) = input .split_first() .ok_or(ProgramError::InvalidInstructionData)?; - let payload = StudentIntroPayload::try_from_slice(rest).unwrap(); - Ok(match variant { - 0 => Self::InitUserInput { + let payload = StudentIntroPayload::try_from_slice(rest) + .map_err(|_| ProgramError::InvalidInstructionData)?; + + match discriminator { + 0 => Ok(Self::InitUserInput { name: payload.name, message: payload.message, - }, - 1 => Self::UpdateStudentIntro { + }), + 1 => Ok(Self::UpdateStudentIntro { name: payload.name, message: payload.message, - }, + }), _ => return Err(ProgramError::InvalidInstructionData), - }) + } } } diff --git a/src/processor.rs b/src/processor.rs index 2bdfa22..7c24ede 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -1,10 +1,9 @@ use crate::error::StudentIntroError; use crate::instruction::IntroInstruction; use crate::state::StudentInfo; -use borsh::BorshSerialize; +use borsh::{BorshDeserialize, BorshSerialize}; use solana_program::{ account_info::{next_account_info, AccountInfo}, - borsh::try_from_slice_unchecked, entrypoint::ProgramResult, msg, program::invoke_signed, @@ -38,11 +37,13 @@ pub fn add_student_intro( name: String, message: String, ) -> ProgramResult { - msg!("Adding student intro..."); - msg!("Name: {}", name); - msg!("Message: {}", message); - let account_info_iter = &mut accounts.iter(); + msg!( + "Adding student intro... Name: {}, Message: {}", + name, + message + ); + let account_info_iter = &mut accounts.iter(); let initializer = next_account_info(account_info_iter)?; let user_account = next_account_info(account_info_iter)?; let system_program = next_account_info(account_info_iter)?; @@ -58,8 +59,8 @@ pub fn add_student_intro( msg!("Data length is larger than 1000 bytes"); return Err(StudentIntroError::InvalidDataLength.into()); } - let account_len: usize = 1000; + let account_len: usize = 1000; let rent = Rent::get()?; let rent_lamports = rent.minimum_balance(account_len); @@ -81,12 +82,9 @@ pub fn add_student_intro( msg!("PDA created: {}", pda); - msg!("unpacking state account"); - let mut account_data = - try_from_slice_unchecked::(&user_account.data.borrow()).unwrap(); - msg!("borrowed account data"); + msg!("Unpacking state account"); + let mut account_data = StudentInfo::try_from_slice(&user_account.data.borrow())?; - msg!("checking if account is already initialized"); if account_data.is_initialized() { msg!("Account already initialized"); return Err(ProgramError::AccountAlreadyInitialized); @@ -95,9 +93,10 @@ pub fn add_student_intro( account_data.name = name; account_data.msg = message; account_data.is_initialized = true; - msg!("serializing account"); + + msg!("Serializing account"); account_data.serialize(&mut &mut user_account.data.borrow_mut()[..])?; - msg!("state account serialized"); + msg!("State account serialized"); Ok(()) } @@ -108,20 +107,19 @@ pub fn update_student_intro( name: String, message: String, ) -> ProgramResult { - msg!("Updating student intro..."); - msg!("Name: {}", name); - msg!("Message: {}", message); - let account_info_iter = &mut accounts.iter(); + msg!( + "Updating student intro... Name: {}, Message: {}", + name, + message + ); + let account_info_iter = &mut accounts.iter(); let initializer = next_account_info(account_info_iter)?; let user_account = next_account_info(account_info_iter)?; - msg!("unpacking state account"); - let mut account_data = - try_from_slice_unchecked::(&user_account.data.borrow()).unwrap(); - msg!("borrowed account data"); + msg!("Unpacking state account"); + let mut account_data = StudentInfo::try_from_slice(&user_account.data.borrow())?; - msg!("checking if account is initialized"); if !account_data.is_initialized() { msg!("Account is not initialized"); return Err(StudentIntroError::UninitializedAccount.into()); @@ -136,6 +134,7 @@ pub fn update_student_intro( msg!("Invalid seeds for PDA"); return Err(StudentIntroError::InvalidPDA.into()); } + let update_len: usize = 1 + (4 + account_data.name.len()) + (4 + message.len()); if update_len > 1000 { msg!("Data length is larger than 1000 bytes"); @@ -144,9 +143,9 @@ pub fn update_student_intro( account_data.name = account_data.name; account_data.msg = message; - msg!("serializing account"); + msg!("Serializing account"); account_data.serialize(&mut &mut user_account.data.borrow_mut()[..])?; - msg!("state account serialized"); + msg!("State account serialized"); Ok(()) }