0

在开发 Solana 的链上程序时,我因超过最大计算单元的错误而崩溃。有没有办法增加最大计算单元?

4

2 回答 2

0

1.9.2 及更高版本的 SDK 确实对增加(或减少)计算预算和堆大小进行了更改。在https://github.com/solana-labs/solana/blob/master/sdk/src/compute_budget.rs#L35的 solana 存储库中sdk/src/compute_budget.rs

这应该适用于本地测试,但尚未为 devnet、mainnet-beta 等启用。

它的工作方式是创建一个request_units指令并放入事务中的第一条指令:

/// Submits the program instruction as per the
/// instruction definition
fn submit_transaction(
    rpc_client: &RpcClient,
    wallet_signer: &dyn Signer,
    instructions: Vec<Instruction>,
) -> Result<Signature, Box<dyn std::error::Error>> {
    let mut transaction =
        Transaction::new_unsigned(Message::new(&instructions, Some(&wallet_signer.pubkey())));
    let recent_blockhash = rpc_client
        .get_latest_blockhash()
        .map_err(|err| format!("error: unable to get recent blockhash: {}", err))?;
    transaction
        .try_sign(&vec![wallet_signer], recent_blockhash)
        .map_err(|err| format!("error: failed to sign transaction: {}", err))?;

    let signature = rpc_client
        .send_and_confirm_transaction(&transaction)
        .map_err(|err| format!("error: send transaction: {}", err))?;
    Ok(signature)
}

/// 
fn foo() {
    // Other details omitted
    let accounts = &[];
    let instruction = Instruction::new_with_borsh(PROG_KEY, &0u8, accounts.to_vec());
    let bump_budget = ComputeBudgetInstruction::request_units(400_000u32);
    let txn = submit_transaction(
        &connection,
        &main_payer,
        [bump_budget, instruction.clone(), instruction.clone()].to_vec(),
    );

请注意,在日志输出中,第 3 行和第 7 行中的每条指令都有一个回撤:

[2022-02-05T09:08:49.715294000Z DEBUG solana_runtime::message_processor::stable_log] Program PWDnx8LkjJUn9bAVzG6Fp6BuvB41x7DkBZdo9YLMGcc invoke [1]
[2022-02-05T09:08:49.715522000Z DEBUG solana_runtime::message_processor::stable_log] Program log: process_instruction: PWDnx8LkjJUn9bAVzG6Fp6BuvB41x7DkBZdo9YLMGcc: 0 accounts, data=[0]
[2022-02-05T09:08:49.715551000Z DEBUG solana_runtime::message_processor::stable_log] Program PWDnx8LkjJUn9bAVzG6Fp6BuvB41x7DkBZdo9YLMGcc consumed 12843 of 400000 compute units
[2022-02-05T09:08:49.715675000Z DEBUG solana_runtime::message_processor::stable_log] Program PWDnx8LkjJUn9bAVzG6Fp6BuvB41x7DkBZdo9YLMGcc success
[2022-02-05T09:08:49.723680000Z DEBUG solana_runtime::message_processor::stable_log] Program PWDnx8LkjJUn9bAVzG6Fp6BuvB41x7DkBZdo9YLMGcc invoke [1]
[2022-02-05T09:08:49.723818000Z DEBUG solana_runtime::message_processor::stable_log] Program log: process_instruction: PWDnx8LkjJUn9bAVzG6Fp6BuvB41x7DkBZdo9YLMGcc: 0 accounts, data=[0]
[2022-02-05T09:08:49.723837000Z DEBUG solana_runtime::message_processor::stable_log] Program PWDnx8LkjJUn9bAVzG6Fp6BuvB41x7DkBZdo9YLMGcc consumed 12843 of 387157 compute units
[2022-02-05T09:08:49.724017000Z DEBUG solana_runtime::message_processor::stable_log] Program PWDnx8LkjJUn9bAVzG6Fp6BuvB41x7DkBZdo9YLMGcc success
于 2022-02-04T13:34:40.083 回答
0

如果您solana-program-test用于测试您的应用程序,您可以使用 设置更高或更低的计算单位set_compute_max_units(),即:

use solana_program_test::ProgramTest;
use crate::id;
use crate::processor::process_instruction;

let mut pt = ProgramTest::new("my_program", id(), processor!(process_instruction));
pt.set_compute_max_units(5_000_000);

完整示例位于https://github.com/solana-labs/solana-program-library/blob/78e29e9238e555967b9125799d7d420d7d12b959/token/program/tests/assert_instruction_count.rs#L24

对于solana-test-validator,目前不支持此功能,但欢迎 PR!

于 2022-02-03T22:51:33.507 回答