coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.68k stars 1.35k forks source link

AccountSysvarMismatch while using SlotHashes sysvar #2098

Open mentalrob opened 2 years ago

mentalrob commented 2 years ago

Program:

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
mod sysvars_test {
    use super::*;
    pub fn sysvars(_ctx: Context<Sysvars>) -> Result<()> {
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Sysvars<'info> {
    pub clock: Sysvar<'info, Clock>,
    pub rent: Sysvar<'info, Rent>,
    pub stake_history: Sysvar<'info, StakeHistory>,
    pub slot_hashes: Sysvar<'info, SlotHashes>,
}

Test:

import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { SysvarsTest } from "../target/types/sysvars_test";

describe("sysvars-test", () => {
  // Configure the client to use the local cluster.
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.SysvarsTest as Program<SysvarsTest>;

  it("Is initialized!", async () => {
    const tx = await program.methods
      .sysvars()
      .accounts({
        clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
        rent: anchor.web3.SYSVAR_RENT_PUBKEY,
        stakeHistory: anchor.web3.SYSVAR_STAKE_HISTORY_PUBKEY,
        slotHashes: anchor.web3.SYSVAR_SLOT_HASHES_PUBKEY
      })
      .rpc();
    console.log("Your transaction signature", tx);
  });
});

throws an error Error: AnchorError caused by account: slot_hashes. Error Code: AccountSysvarMismatch. Error Number: 3015. Error Message: The given public key does not match the required sysvar. while using anchor-test. If i remove SYSVAR_SLOT_HASHES_PUBKEY and slot_hashes it works.

callensm commented 2 years ago

closing for now...might be a local issue, cannot reproduce. consider rimraffing and getting a fresh clone of the repository.

feel free to reopen if the issue persists.

nash90 commented 2 years ago

@mentalrob Hello, did you figure the issue? I am getting the same issue as above.

  ).accounts({
    player: provider.wallet.publicKey,
    playerStats: playerStatsPDA,
    game: playerGamePDA,
    depositWallet: depositWallet,
    slotHashes: anchor.web3.SYSVAR_SLOT_HASHES_PUBKEY,
  }).rpc().catch((err) => {
    pub system_program: Program<'info, System>,

    pub slot_hashes: Sysvar<'info, SlotHashes>
}
       Is initialized!:
     Error: AnchorError caused by account: slot_hashes. Error Code: AccountSysvarMismatch. Error Number: 3015. Error Message: The given public key does not match the required sysvar.
mentalrob commented 2 years ago

I couldn't fixed it, used a nasty workaround

nash90 commented 2 years ago

@mentalrob thanks for quick reply. Is it possible to share the workaround?

@callensm Can we reopen this ticket?

mentalrob commented 2 years ago

I get the slothash offchain and giving it to the program as a parameter

nash90 commented 2 years ago

I get the slothash offchain and giving it to the program as a parameter

oh okay, is there a way to validate/verify the hash sent from client offchain is not fake?

0xlarry commented 1 year ago

I am still getting this error when trying to use Sysvar SlotHashes


> Program log: AnchorError caused by account: recent_slothashes. Error Code: AccountSysvarMismatch. Error Number: 3015. Error Message: The given public key does not match the required sysvar.```
HardhatChad commented 10 months ago

Getting this error as well. Only for this sysvar.

HardhatChad commented 10 months ago

Ah, here is why. SlotHashes doesn't support from_account_info.

https://github.com/solana-labs/solana/blob/e9a6bb35e2636275137fc47837f5afce4dbcf630/sdk/program/src/sysvar/slot_hashes.rs#L61

Aursen commented 10 months ago

The goal of this sysvar is to be used off chain