Solana Anchorで「Error: 3003: Failed to deserialize the account」エラー

現象

anchor testを実行すると、deserialize the accountエラーになる。

  post_to_earn
Transaction simulation failed: Error processing Instruction 0: custom program error: 0xbbb
    Program Fepp9QeEjxdqfYkokG8T6wtEZWadvWfwaXSeLThsrmjC invoke [1]
    Program log: Instruction: Create
    Program 11111111111111111111111111111111 invoke [2]
    Program 11111111111111111111111111111111 success
    Program log: AnchorError occurred. Error Code: AccountDidNotDeserialize. Error Number: 3003. Error Message: Failed to deserialize the account.
    Program Fepp9QeEjxdqfYkokG8T6wtEZWadvWfwaXSeLThsrmjC consumed 13067 of 1400000 compute units
    Program Fepp9QeEjxdqfYkokG8T6wtEZWadvWfwaXSeLThsrmjC failed: custom program error: 0xbbb
    1) Initializes accounts.
Transaction simulation failed: Error processing Instruction 0: custom program error: 0xbc4
    Program Fepp9QeEjxdqfYkokG8T6wtEZWadvWfwaXSeLThsrmjC invoke [1]
    Program log: Instruction: Increment
    Program log: AnchorError caused by account: counter. Error Code: AccountNotInitialized. Error Number: 3012. Error Message: The program expected this account to be already initialized.
    Program Fepp9QeEjxdqfYkokG8T6wtEZWadvWfwaXSeLThsrmjC consumed 5692 of 1400000 compute units
    Program Fepp9QeEjxdqfYkokG8T6wtEZWadvWfwaXSeLThsrmjC failed: custom program error: 0xbc4
    2) Posts and get rewards.


  0 passing (103ms)
  2 failing

  1) post_to_earn
       Initializes accounts.:
     Error: 3003: Failed to deserialize the account
      at Function.parse (node_modules/@project-serum/anchor/dist/cjs/error.js:54:20)
      at Object.rpc [as create] (node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:38:61)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async Context.<anonymous> (tests/post_to_earn.js:20:5)

  2) post_to_earn
       Posts and get rewards.:
     Error: 3012: The program expected this account to be already initialized
      at Function.parse (node_modules/@project-serum/anchor/dist/cjs/error.js:54:20)
      at Object.rpc [as increment] (node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:38:61)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async Context.<anonymous> (tests/post_to_earn.js:66:5)



error Command failed with exit code 2.

programsファイル

#[derive(Accounts)]
pub struct Create<'info> {
    #[account(init, payer = user, space = 8 + 8)]
    pub counter: Account<'info, Counter>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

原因

アカウントのdeserializeができていない。
アカウント関連の処理でエラーになっているため、いろいろなケースがあると思うが、今回はデータのスペースが足りずにデータ登録ができていないことが原因だった。

対応

以下のように「space = 8 + 40」と容量を増やして解決。

programsファイル

#[derive(Accounts)]
pub struct Create<'info> {
    #[account(init, payer = user, space = 8 + 40)]
    pub counter: Account<'info, Counter>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}