'Solana Rust - Failed to serialize or deserialize account data: Unknown
I'm new to Solana and Rust. I've done some modifications with solana js hello world example and the error with borsh serialize and deserialize occurs. Here is my rust program:
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint,
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
pubkey::Pubkey,
};
/// Define the type of state stored in accounts
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct GreetingAccount {
/// number of greetings
pub id: u32,
pub name: String
}
// Declare and export the program's entrypoint
entrypoint!(process_instruction);
// Program entrypoint's implementation
pub fn process_instruction(
program_id: &Pubkey, // Public key of the account the hello world program was loaded into
accounts: &[AccountInfo], // The account to say hello to
_instruction_data: &[u8], // Ignored, all helloworld instructions are hellos
) -> ProgramResult {
msg!("Hello World Rust program entrypoint");
// Iterating accounts is safer than indexing
let accounts_iter = &mut accounts.iter();
// Get the account to say hello to
let account = next_account_info(accounts_iter)?;
// The account must be owned by the program in order to modify its data
if account.owner != program_id {
msg!("Greeted account does not have the correct program id");
return Err(ProgramError::IncorrectProgramId);
}
// Increment and store the number of times the account has been greeted
let mut greeting_account = GreetingAccount::deserialize(&mut &account.data.borrow()[..])?;
let msg = GreetingAccount::deserialize(&mut &_instruction_data[..])?;
greeting_account.id = msg.id;
greeting_account.name = msg.name;
greeting_account.serialize(&mut &mut account.data.borrow_mut()[..])?;
msg!("Greeted {} time(s)!", greeting_account.id);
Ok(())
}
The typescript client code is here for sending transaction:
export async function sayHello(): Promise<void> {
console.log('Saying hello to', greetedPubkey.toBase58());
const instruction = new TransactionInstruction({
keys: [{pubkey: greetedPubkey, isSigner: false, isWritable: true}],
programId,
data: Buffer.from(borsh.serialize(
GreetingSchema,
new GreetingAccount({id: 126, name: 'anas'}),
)),
});
await sendAndConfirmTransaction(
connection,
new Transaction().add(instruction),
[payer],
);
}
The error logs from the console:
logs: [
'Program 7X4jotvCZgDyEPHtAGCZeYXeJb4A8mjZFUhx9two37Vp invoke [1]',
'Program log: Hello World Rust program entrypoint',
'Program 7X4jotvCZgDyEPHtAGCZeYXeJb4A8mjZFUhx9two37Vp consumed 1412 of 1400000 compute units',
'Program 7X4jotvCZgDyEPHtAGCZeYXeJb4A8mjZFUhx9two37Vp failed: Failed to serialize or deserialize account data: Unknown'
]
The error is due to this line of code:
greeting_account.serialize(&mut &mut account.data.borrow_mut()[..])?;
Can someone help me identifying the reason of this failure?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
