From 14adcc18938debf33a8a689a1109bf12df0aeb30 Mon Sep 17 00:00:00 2001 From: 0xCipherCoder Date: Mon, 16 Sep 2024 03:07:52 +0530 Subject: [PATCH 1/2] Fixed code --- Cargo.toml | 6 +++--- src/processor.rs | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) 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/processor.rs b/src/processor.rs index 2bdfa22..5feee1c 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, @@ -83,7 +82,7 @@ pub fn add_student_intro( msg!("unpacking state account"); let mut account_data = - try_from_slice_unchecked::(&user_account.data.borrow()).unwrap(); + StudentInfo::try_from_slice(&user_account.data.borrow())?; msg!("borrowed account data"); msg!("checking if account is already initialized"); @@ -118,7 +117,7 @@ pub fn update_student_intro( msg!("unpacking state account"); let mut account_data = - try_from_slice_unchecked::(&user_account.data.borrow()).unwrap(); + StudentInfo::try_from_slice(&user_account.data.borrow())?; msg!("borrowed account data"); msg!("checking if account is initialized"); From 5e414c87f449d9a56c2d974e565474cac5547dac Mon Sep 17 00:00:00 2001 From: 0xCipherCoder Date: Mon, 16 Sep 2024 03:30:58 +0530 Subject: [PATCH 2/2] Updated dependencies and code snippets to latest version --- src/instruction.rs | 18 ++++++++++-------- src/processor.rs | 46 +++++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 31 deletions(-) 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 5feee1c..7c24ede 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -37,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)?; @@ -57,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); @@ -80,12 +82,9 @@ pub fn add_student_intro( msg!("PDA created: {}", pda); - msg!("unpacking state account"); - let mut account_data = - StudentInfo::try_from_slice(&user_account.data.borrow())?; - 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); @@ -94,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(()) } @@ -107,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 = - StudentInfo::try_from_slice(&user_account.data.borrow())?; - 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()); @@ -135,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"); @@ -143,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(()) }