【问题标题】:What does "Invoke context not set error" occur when I am testing?我在测试时会发生什么“调用上下文未设置错误”?
【发布时间】:2021-08-12 15:42:42
【问题描述】:

当我在此代码上运行 cargo test-bpf --manifest-path=./Cargo.toml 时

#[tokio::test]
async fn test_init_mapping() {
    let program_id = Pubkey::new_unique();
    let mut init_map_test = ProgramTest::new(
        "simple", // Run the BPF version with `cargo test-bpf`
        program_id,
        processor!(process_instruction), 
    );
    let main_pubkey = Pubkey::new_unique();
    let main_account = Account {
        lamports: 100,
        owner: program_id,
        ..Account::default()
    };
    init_map_test.add_account(main_pubkey, main_account);
    let (mut banks_client, payer, recent_blockhash) = init_map_test.start().await;
}

我收到此错误。

线程“test_init_mapping”在“未设置调用上下文!”时出现恐慌**

我不知道如何调试它,所以任何帮助都很棒。我已将问题缩小到最后一行。我一定是在我的项目中的某个地方设置了错误?

我认为这段代码不是问题,因为它是从 helloworld 示例粘贴的副本。

编辑:我留下了原始问题的重要细节。在最后一行之后我有 msg!("started"); ,我认为无关紧要,所以将其排除在外

【问题讨论】:

  • 通常,当程序实际运行时会触发该错误。不幸的是,我复制了这段代码,它适用于我使用最新的 1.7.10 版本。您使用的是什么版本的 SDK 和 CLI?
  • 嗨,乔恩。我使用 1.6.10 是因为我复制了 metaplex github 项目来尝试遵循该结构。你知道我可以克隆的 1.7.10 中的项目吗?当我尝试使用 1.7.10 再次运行此脚本时,我收到一条错误消息“无法安装 bpf-tools”
  • 不确定是什么问题。我已经把这个放在 github 上,这对我有用github.com/antonnewcombe/nothing

标签: rust rust-cargo solana


【解决方案1】:

您的编辑是这里的关键部分:

编辑:我留下了原始问题的重要细节。在最后一行之后我有 msg!("started"); ,我认为无关紧要,所以将其排除在外

如果您查看msg! 宏的代码,它只是调用sol_log,在ProgramTest 环境中,它变成了这个调用:https://github.com/solana-labs/solana/blob/7a8807b8bba2a0bd41f696d3309487d6423a0b4b/program-test/src/lib.rs#L227

msg! 只能从设置了invoke_context 的程序中调用,这就是您遇到此问题的原因。

要解决此问题,请删除对msg! 的调用并使用简单的println!。但是,在您的程序中,您可以随意使用msg!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多