Add CPI support for upgradeable loader (#14193)
This commit is contained in:
7
programs/bpf/Cargo.lock
generated
7
programs/bpf/Cargo.lock
generated
@ -1985,6 +1985,13 @@ dependencies = [
|
||||
"solana-program",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-bpf-rust-invoke-and-return"
|
||||
version = "1.6.0"
|
||||
dependencies = [
|
||||
"solana-program",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-bpf-rust-invoked"
|
||||
version = "1.6.0"
|
||||
|
@ -51,6 +51,7 @@ members = [
|
||||
"rust/invoke",
|
||||
"rust/invoke_and_error",
|
||||
"rust/invoke_and_ok",
|
||||
"rust/invoke_and_return",
|
||||
"rust/invoked",
|
||||
"rust/iter",
|
||||
"rust/many_args",
|
||||
|
@ -72,6 +72,7 @@ fn main() {
|
||||
"invoke",
|
||||
"invoke_and_error",
|
||||
"invoke_and_ok",
|
||||
"invoke_and_return",
|
||||
"invoked",
|
||||
"iter",
|
||||
"many_args",
|
||||
|
18
programs/bpf/rust/invoke_and_return/Cargo.toml
Normal file
18
programs/bpf/rust/invoke_and_return/Cargo.toml
Normal file
@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "solana-bpf-rust-invoke-and-return"
|
||||
version = "1.6.0"
|
||||
description = "Solana BPF test program written in Rust"
|
||||
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
solana-program = { path = "../../../../sdk/program", version = "1.6.0" }
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
2
programs/bpf/rust/invoke_and_return/Xargo.toml
Normal file
2
programs/bpf/rust/invoke_and_return/Xargo.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[target.bpfel-unknown-unknown.dependencies.std]
|
||||
features = []
|
36
programs/bpf/rust/invoke_and_return/src/lib.rs
Normal file
36
programs/bpf/rust/invoke_and_return/src/lib.rs
Normal file
@ -0,0 +1,36 @@
|
||||
//! @brief Invokes an instruction and returns an error, the instruction invoked
|
||||
//! uses the instruction data provided and all the accounts
|
||||
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, bpf_loader_upgradeable, entrypoint, entrypoint::ProgramResult,
|
||||
instruction::AccountMeta, instruction::Instruction, program::invoke, pubkey::Pubkey,
|
||||
};
|
||||
|
||||
entrypoint!(process_instruction);
|
||||
#[allow(clippy::unnecessary_wraps)]
|
||||
fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
accounts: &[AccountInfo],
|
||||
instruction_data: &[u8],
|
||||
) -> ProgramResult {
|
||||
let to_call = accounts[0].key;
|
||||
let infos = accounts;
|
||||
let last = if bpf_loader_upgradeable::check_id(accounts[0].owner) {
|
||||
accounts.len() - 1
|
||||
} else {
|
||||
accounts.len()
|
||||
};
|
||||
let instruction = Instruction {
|
||||
accounts: accounts[1..last]
|
||||
.iter()
|
||||
.map(|acc| AccountMeta {
|
||||
pubkey: *acc.key,
|
||||
is_signer: acc.is_signer,
|
||||
is_writable: acc.is_writable,
|
||||
})
|
||||
.collect(),
|
||||
data: instruction_data.to_owned(),
|
||||
program_id: *to_call,
|
||||
};
|
||||
invoke(&instruction, &infos)
|
||||
}
|
@ -21,7 +21,9 @@ use solana_runtime::{
|
||||
};
|
||||
use solana_sdk::{
|
||||
account::Account,
|
||||
account_utils::StateMut,
|
||||
bpf_loader, bpf_loader_deprecated,
|
||||
bpf_loader_upgradeable::UpgradeableLoaderState,
|
||||
client::SyncClient,
|
||||
clock::{DEFAULT_SLOTS_PER_EPOCH, MAX_PROCESSING_AGE},
|
||||
entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS},
|
||||
@ -1557,3 +1559,58 @@ fn test_program_bpf_upgrade() {
|
||||
TransactionError::InstructionError(0, InstructionError::Custom(42))
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "bpf_rust")]
|
||||
#[test]
|
||||
fn test_program_bpf_invoke_upgradeable() {
|
||||
solana_logger::setup();
|
||||
|
||||
let GenesisConfigInfo {
|
||||
genesis_config,
|
||||
mint_keypair,
|
||||
..
|
||||
} = create_genesis_config(50);
|
||||
let mut bank = Bank::new(&genesis_config);
|
||||
let (name, id, entrypoint) = solana_bpf_loader_program!();
|
||||
bank.add_builtin(&name, id, entrypoint);
|
||||
let (name, id, entrypoint) = solana_bpf_loader_upgradeable_program!();
|
||||
bank.add_builtin(&name, id, entrypoint);
|
||||
let bank_client = BankClient::new(bank);
|
||||
let invoke_and_return = load_bpf_program(
|
||||
&bank_client,
|
||||
&bpf_loader::id(),
|
||||
&mint_keypair,
|
||||
"solana_bpf_rust_invoke_and_return",
|
||||
);
|
||||
|
||||
// deploy upgrade program
|
||||
let (program_id, _) =
|
||||
load_upgradeable_bpf_program(&bank_client, &mint_keypair, "solana_bpf_rust_upgradeable");
|
||||
|
||||
let data = bank_client.get_account(&program_id).unwrap().unwrap();
|
||||
let programdata_address = if let UpgradeableLoaderState::Program {
|
||||
programdata_address,
|
||||
} = data.state().unwrap()
|
||||
{
|
||||
programdata_address
|
||||
} else {
|
||||
panic!("Not a program");
|
||||
};
|
||||
|
||||
// call invoker program to invoke the upgradeable program
|
||||
let instruction = Instruction::new(
|
||||
invoke_and_return,
|
||||
&[0],
|
||||
vec![
|
||||
AccountMeta::new(program_id, false),
|
||||
AccountMeta::new(clock::id(), false),
|
||||
AccountMeta::new(fees::id(), false),
|
||||
AccountMeta::new(programdata_address, false),
|
||||
],
|
||||
);
|
||||
let result = bank_client.send_and_confirm_instruction(&mint_keypair, instruction);
|
||||
assert_eq!(
|
||||
result.unwrap_err().unwrap(),
|
||||
TransactionError::InstructionError(0, InstructionError::Custom(42))
|
||||
);
|
||||
}
|
||||
|
@ -12,7 +12,9 @@ use solana_runtime::message_processor::MessageProcessor;
|
||||
use solana_sdk::{
|
||||
account::Account,
|
||||
account_info::AccountInfo,
|
||||
account_utils::StateMut,
|
||||
bpf_loader_deprecated,
|
||||
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
|
||||
entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS},
|
||||
feature_set::{
|
||||
pubkey_log_syscall_enabled, ristretto_mul_syscall_enabled, sha256_syscall_enabled,
|
||||
@ -1367,7 +1369,7 @@ fn call<'a>(
|
||||
.get_callers_keyed_accounts()
|
||||
.iter()
|
||||
.collect::<Vec<&KeyedAccount>>();
|
||||
let (message, callee_program_id, callee_program_id_index) =
|
||||
let (message, callee_program_id) =
|
||||
MessageProcessor::create_message(&instruction, &keyed_account_refs, &signers)
|
||||
.map_err(SyscallError::InstructionError)?;
|
||||
let (accounts, account_refs) = syscall.translate_accounts(
|
||||
@ -1380,17 +1382,40 @@ fn call<'a>(
|
||||
// Process instruction
|
||||
|
||||
invoke_context.record_instruction(&instruction);
|
||||
|
||||
let program_account =
|
||||
(**accounts
|
||||
.get(callee_program_id_index)
|
||||
invoke_context
|
||||
.get_account(&callee_program_id)
|
||||
.ok_or(SyscallError::InstructionError(
|
||||
InstructionError::MissingAccount,
|
||||
))?)
|
||||
.clone();
|
||||
if !program_account.borrow().executable {
|
||||
))?;
|
||||
if !program_account.executable {
|
||||
return Err(SyscallError::InstructionError(InstructionError::AccountNotExecutable).into());
|
||||
}
|
||||
let executable_accounts = vec![(callee_program_id, program_account)];
|
||||
let programdata_executable = if program_account.owner == bpf_loader_upgradeable::id() {
|
||||
if let UpgradeableLoaderState::Program {
|
||||
programdata_address,
|
||||
} = program_account
|
||||
.state()
|
||||
.map_err(SyscallError::InstructionError)?
|
||||
{
|
||||
if let Some(account) = invoke_context.get_account(&programdata_address) {
|
||||
Some((programdata_address, RefCell::new(account)))
|
||||
} else {
|
||||
return Err(
|
||||
SyscallError::InstructionError(InstructionError::MissingAccount).into(),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return Err(SyscallError::InstructionError(InstructionError::MissingAccount).into());
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut executable_accounts = vec![(callee_program_id, RefCell::new(program_account))];
|
||||
if let Some(programdata) = programdata_executable {
|
||||
executable_accounts.push(programdata);
|
||||
}
|
||||
|
||||
#[allow(clippy::deref_addrof)]
|
||||
match MessageProcessor::process_cross_program_instruction(
|
||||
|
Reference in New Issue
Block a user