简体   繁体   English

Solana 锚点错误:发送交易失败:交易无效:交易未能正确清理账户偏移

[英]Solana Anchor Error: failed to send transaction: invalid transaction: Transaction failed to sanitize accounts offsets correctly

I am trying to run the following code in Anchor Solana, with program in rust as follows:我正在尝试在 Anchor Solana 中运行以下代码,rust 中的程序如下:

        use anchor_lang::prelude::*;

        declare_id!("RnbXAWg5mCvmSafjd1CnYaz32qLgZHdeHK6xzHDi1yU");

        #[program]
        pub mod sol_proj_1 {
            use super::*;
            pub fn initialize(ctx: Context<Initialize>, data: u64) -> ProgramResult {
                let my_account = &mut ctx.accounts.my_account;
                my_account.data = data;
                println!("hello there!");

                Ok(())
            }
            pub fn update(ctx: Context<Update>, data: u64) -> ProgramResult {
                let my_account = &mut ctx.accounts.my_account;
                my_account.data = data;
                Ok(())
            }
        }
        // #[derive(Accounts)]
        // pub struct Initialize {}
        #[derive(Accounts)]
        pub struct Initialize<'info> {
            #[account(init, payer = user, space = 8 + 8)]
            pub my_account: Account<'info, MyAccount>,
            #[account(mut)]
            pub user: Signer<'info>,
            pub system_program: Program<'info, System>,
        }

        #[derive(Accounts)]
        pub struct Update<'info> {
            #[account(mut)]
            pub my_account: Account<'info, MyAccount>,
        }

        #[account]
        pub struct MyAccount {
            pub data: u64,
        }

The test program is as follow:测试程序如下:

        import * as anchor from '@project-serum/anchor';
        import { Program } from '@project-serum/anchor';
        import { SolProj1 } from '../target/types/sol_proj_1';
        const assert = require("assert");

        describe('sol_proj_1', () => {

          // Configure the client to use the local cluster.
          const provider = anchor.Provider.local();

           anchor.setProvider(provider);

           
          // The Account to create.
          const myAccount = anchor.web3.Keypair.generate();

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

          it('Is initialized!', async () => {

            
            // Add your test here.
            const tx = await program.rpc.initialize(new anchor.BN(1234), {
              accounts: {
                myAccount: myAccount.publicKey,
                user: provider.wallet.publicKey,
                systemProgram: program.programId,
              },
              signers: [myAccount],
            });
           
            /
            console.log("Your transaction signature", tx);
          });

          
        });

With error when I run the following command运行以下命令时出现错误

Anchor test

  1) sol_proj_1
       Is initialized!:
     Error: failed to send transaction: invalid transaction: Transaction failed to sanitize accounts offsets correctly
      at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:3740:13)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:3700:20)
      at sendAndConfirmRawTransaction (node_modules/@solana/web3.js/src/util/send-and-confirm-raw-transaction.ts:27:21)
      at Provider.send (node_modules/@project-serum/anchor/src/provider.ts:118:18)
      at Object.rpc [as initialize] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:25:23)

I tried the following我尝试了以下

  1. change the program ownership as I found that this can cause the issue but that did not work.更改程序所有权,因为我发现这可能会导致问题,但没有奏效。
  2. I also added entries in Anchor.toml, the tests are running on localhost我还在 Anchor.toml 中添加了条目,测试在 localhost 上运行
  3. I found that empty wallet may also cause this issue but I have airdrop 100 sols in it我发现空钱包也可能导致这个问题,但我有空投 100 sols
  4. The Rust code deploys correctly but the test is failing the 'sanitizationfailure' is listed as follow "Transaction failed to sanitize accounts offsets correctly implies that account locks are not taken for this TX, and should not be unlocked." Rust 代码正确部署,但测试失败,“清理失败”如下所示“交易未能正确清理帐户偏移量意味着此 TX 未采取帐户锁定,不应解锁。” I couldn't find any info how to take out the locks source: https://docs.rs/solana-sdk/1.9.2/solana_sdk/transaction/enum.TransactionError.html我找不到任何信息如何取出锁源: https://docs.rs/solana-sdk/1.9.2/solana_sdk/transaction/enum.TransactionError.html

Any help is appreciated!任何帮助表示赞赏!

The only thing I can see obvious is maybe the way you're passing in the system program from the frontend.我唯一能看到的明显可能是您从前端传递系统程序的方式。 You're passing in your program's id, when you should be passing the in system program id.当您应该传递系统程序 id 时,您正在传递程序的 id。 So instead, try:因此,请尝试:

const tx = await program.rpc.initialize(new anchor.BN(1234), {
     accounts: {
         myAccount: myAccount.publicKey,
         user: provider.wallet.publicKey,
         systemProgram: SystemProgram.programId,
     },
     signers: [myAccount],
});

I had this error before and have solved.我之前遇到过这个错误并且已经解决了。 In my case, I find that the Program_Id in Anchor.toml is different from lib.rs, they should be the same.就我而言,我发现 Anchor.toml 中的 Program_Id 与 lib.rs 不同,它们应该是相同的。 Program_Id is generated by running "anchor deploy" in terminal. Program_Id 是通过在终端中运行“anchor deploy”生成的。

Anchor.toml
[programs.localnet]
solana_app = "<Program_ID>"

lib.rs
declare_id!("<Program_ID>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 锚点测试交易失败并显示“缺少指令所需的帐户” - Anchor test transaction failed with "An account required by the instruction is missing" Anchor测试交易模拟失败:指令要求的账户丢失 - Anchor test Transaction simulation failed: An account required by the instruction is missing 从多重签名账户支付 solana 交易费用 - paying solana transaction fee from multisig account 是否可以让 Solana 程序成为交易的签名者? - Is it possible to make a Solana program the signer for a transaction? 交易模拟失败:错误处理指令0:自定义程序错误:0x0 - Transaction simulation failed: Error processing Instruction 0: custom program error: 0x0 交易模拟失败:错误处理指令0:使用未经授权的签名者或可写帐户进行跨程序调用 - Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account 尝试使用 Anchor 在 Devnet 上初始化 VRF 时我的事务失败(0x0 错误) - My transaction fails when trying to Init VRF on Devnet using Anchor (0x0 Error) Solana Rust 智能合约构建错误:build failed - Solana Rust smart contract build error: build failed 将 postgresql 事务封装在一个结构中 - Encapsulate a postgresql Transaction in a structure Solana Anchor 中的嵌套帐户(可能是帐户中的自定义数据类型) - Nested Accounts in Solana Anchor (Maybe a custom data types in account)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM