在 Solana 账户中存储数据的问题

Problems in storing data in Solana Account

我在将数据存储到帐户方面需要帮助。 这是我到目前为止所做的。

下面是我的'process_instruction'入口点,

pub fn process_instruction(...) -> ProgramResult {

    let account_info_iter = &mut accounts.iter();
    // get account where we have to store state
    let states_account_info = next_account_info(account_info_iter)?;

    let xyz: u64 = 1234567;
    let counter_struct = Counter {
        data: xyz
    };
    counter_struct.serialize(&mut &mut states_account_info.data.borrow_mut()[..])?;

    Ok(())
}

这是我的计数器结构

#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct Counter {
    /// mappings of the keys
    pub data: u64,
}

这就是我在测试中创建帐户的方式,

let state_account_pubkey = Pubkey::new_unique();
let mut program_test = ProgramTest::new(
    "solana_states_save_program",
    program_id,
    processor!(process_instruction),
);

program_test.add_account(
    state_account_pubkey, 
    Account {
        lamports: 5,
        owner: program_id,
        ..Account::default()
    },
);

但是在执行测试后,在 process_instruction() 方法中(在 counter_struct.serialize(...) 语句中),

thread 'main' panicked at 'called Result::unwrap() on an Err value: TransactionError(InstructionError(0, BorshIoError("Unknown")))'

请帮忙。

在测试中创建帐户时,我们还需要添加帐户数据 space,但我没有这样做。

program_test.add_account(
    state_account_pubkey, 
    Account {
        lamports: 5,
        data: vec![0_u8; mem::size_of::<u32>()],
        owner: program_id,
        ..Account::default()
    },
);

希望对某人有所帮助。