Upgradeable loader (#13689)
This commit is contained in:
14
programs/bpf/Cargo.lock
generated
14
programs/bpf/Cargo.lock
generated
@@ -2110,6 +2110,20 @@ dependencies = [
|
||||
"solana-program",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-bpf-rust-upgradeable"
|
||||
version = "1.5.0"
|
||||
dependencies = [
|
||||
"solana-program",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-bpf-rust-upgraded"
|
||||
version = "1.5.0"
|
||||
dependencies = [
|
||||
"solana-program",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-config-program"
|
||||
version = "1.5.0"
|
||||
|
@@ -68,6 +68,8 @@ members = [
|
||||
"rust/spoof1",
|
||||
"rust/spoof1_system",
|
||||
"rust/sysval",
|
||||
"rust/upgradeable",
|
||||
"rust/upgraded",
|
||||
]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
|
@@ -87,6 +87,8 @@ fn main() {
|
||||
"spoof1",
|
||||
"spoof1_system",
|
||||
"sysval",
|
||||
"upgradeable",
|
||||
"upgraded",
|
||||
];
|
||||
for program in rust_programs.iter() {
|
||||
println!(
|
||||
|
19
programs/bpf/rust/upgradeable/Cargo.toml
Normal file
19
programs/bpf/rust/upgradeable/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "solana-bpf-rust-upgradeable"
|
||||
version = "1.5.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.5.0" }
|
||||
|
||||
[lib]
|
||||
name = "solana_bpf_rust_upgradeable"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
2
programs/bpf/rust/upgradeable/Xargo.toml
Normal file
2
programs/bpf/rust/upgradeable/Xargo.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[target.bpfel-unknown-unknown.dependencies.std]
|
||||
features = []
|
16
programs/bpf/rust/upgradeable/src/lib.rs
Normal file
16
programs/bpf/rust/upgradeable/src/lib.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
//! @brief Example Rust-based BPF upgradeable program
|
||||
|
||||
extern crate solana_program;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey,
|
||||
};
|
||||
|
||||
entrypoint!(process_instruction);
|
||||
fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
_accounts: &[AccountInfo],
|
||||
_instruction_data: &[u8],
|
||||
) -> ProgramResult {
|
||||
msg!("Upgradeable program");
|
||||
Err(42.into())
|
||||
}
|
19
programs/bpf/rust/upgraded/Cargo.toml
Normal file
19
programs/bpf/rust/upgraded/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "solana-bpf-rust-upgraded"
|
||||
version = "1.5.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.5.0" }
|
||||
|
||||
[lib]
|
||||
name = "solana_bpf_rust_upgraded"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
2
programs/bpf/rust/upgraded/Xargo.toml
Normal file
2
programs/bpf/rust/upgraded/Xargo.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[target.bpfel-unknown-unknown.dependencies.std]
|
||||
features = []
|
16
programs/bpf/rust/upgraded/src/lib.rs
Normal file
16
programs/bpf/rust/upgraded/src/lib.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
//! @brief Example Rust-based BPF upgraded program
|
||||
|
||||
extern crate solana_program;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey,
|
||||
};
|
||||
|
||||
entrypoint!(process_instruction);
|
||||
fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
_accounts: &[AccountInfo],
|
||||
_instruction_data: &[u8],
|
||||
) -> ProgramResult {
|
||||
msg!("Upgraded program");
|
||||
Err(43.into())
|
||||
}
|
@@ -14,7 +14,10 @@ use solana_runtime::{
|
||||
bank::Bank,
|
||||
bank_client::BankClient,
|
||||
genesis_utils::{create_genesis_config, GenesisConfigInfo},
|
||||
loader_utils::load_program,
|
||||
loader_utils::{
|
||||
load_buffer_account, load_program, load_upgradeable_program, set_upgrade_authority,
|
||||
upgrade_program,
|
||||
},
|
||||
};
|
||||
use solana_sdk::{
|
||||
account::Account,
|
||||
@@ -94,6 +97,44 @@ fn write_bpf_program(
|
||||
}
|
||||
}
|
||||
|
||||
fn load_upgradeable_bpf_program(
|
||||
bank_client: &BankClient,
|
||||
payer_keypair: &Keypair,
|
||||
name: &str,
|
||||
) -> (Pubkey, Keypair) {
|
||||
let path = create_bpf_path(name);
|
||||
let mut file = File::open(&path).unwrap_or_else(|err| {
|
||||
panic!("Failed to open {}: {}", path.display(), err);
|
||||
});
|
||||
let mut elf = Vec::new();
|
||||
file.read_to_end(&mut elf).unwrap();
|
||||
load_upgradeable_program(bank_client, payer_keypair, elf)
|
||||
}
|
||||
|
||||
fn upgrade_bpf_program(
|
||||
bank_client: &BankClient,
|
||||
payer_keypair: &Keypair,
|
||||
executable_pubkey: &Pubkey,
|
||||
authority_keypair: &Keypair,
|
||||
name: &str,
|
||||
) {
|
||||
let path = create_bpf_path(name);
|
||||
let mut file = File::open(&path).unwrap_or_else(|err| {
|
||||
panic!("Failed to open {}: {}", path.display(), err);
|
||||
});
|
||||
let mut elf = Vec::new();
|
||||
file.read_to_end(&mut elf).unwrap();
|
||||
let buffer_pubkey = load_buffer_account(bank_client, payer_keypair, &elf);
|
||||
upgrade_program(
|
||||
bank_client,
|
||||
payer_keypair,
|
||||
executable_pubkey,
|
||||
&buffer_pubkey,
|
||||
&authority_keypair,
|
||||
&payer_keypair.pubkey(),
|
||||
)
|
||||
}
|
||||
|
||||
fn run_program(
|
||||
name: &str,
|
||||
program_id: &Pubkey,
|
||||
@@ -1382,3 +1423,79 @@ fn test_program_bpf_test_use_latest_executor2() {
|
||||
.send_and_confirm_message(&[&mint_keypair], message)
|
||||
.is_ok());
|
||||
}
|
||||
|
||||
#[cfg(feature = "bpf_rust")]
|
||||
#[test]
|
||||
fn test_program_bpf_upgrade() {
|
||||
solana_logger::setup();
|
||||
|
||||
let mut nonce = 0;
|
||||
let GenesisConfigInfo {
|
||||
genesis_config,
|
||||
mint_keypair,
|
||||
..
|
||||
} = create_genesis_config(50);
|
||||
let mut bank = Bank::new(&genesis_config);
|
||||
let (name, id, entrypoint) = solana_bpf_loader_upgradeable_program!();
|
||||
bank.add_builtin(&name, id, entrypoint);
|
||||
let bank_client = BankClient::new(bank);
|
||||
|
||||
// deploy upgrade program
|
||||
let (program_id, authority_keypair) =
|
||||
load_upgradeable_bpf_program(&bank_client, &mint_keypair, "solana_bpf_rust_upgradeable");
|
||||
|
||||
// call upgrade program
|
||||
nonce += 1;
|
||||
let instruction = Instruction::new(program_id, &[nonce], vec![]);
|
||||
let result = bank_client.send_and_confirm_instruction(&mint_keypair, instruction);
|
||||
assert_eq!(
|
||||
result.unwrap_err().unwrap(),
|
||||
TransactionError::InstructionError(0, InstructionError::Custom(42))
|
||||
);
|
||||
|
||||
// upgrade program
|
||||
upgrade_bpf_program(
|
||||
&bank_client,
|
||||
&mint_keypair,
|
||||
&program_id,
|
||||
&authority_keypair,
|
||||
"solana_bpf_rust_upgraded",
|
||||
);
|
||||
|
||||
// call upgraded program
|
||||
nonce += 1;
|
||||
let instruction = Instruction::new(program_id, &[nonce], vec![]);
|
||||
let result = bank_client.send_and_confirm_instruction(&mint_keypair, instruction);
|
||||
assert_eq!(
|
||||
result.unwrap_err().unwrap(),
|
||||
TransactionError::InstructionError(0, InstructionError::Custom(43))
|
||||
);
|
||||
|
||||
// upgrade back to the original program
|
||||
let new_authority_keypair = Keypair::new();
|
||||
set_upgrade_authority(
|
||||
&bank_client,
|
||||
&mint_keypair,
|
||||
&program_id,
|
||||
&authority_keypair,
|
||||
Some(&new_authority_keypair.pubkey()),
|
||||
);
|
||||
|
||||
// upgrade back to the original program
|
||||
upgrade_bpf_program(
|
||||
&bank_client,
|
||||
&mint_keypair,
|
||||
&program_id,
|
||||
&new_authority_keypair,
|
||||
"solana_bpf_rust_upgradeable",
|
||||
);
|
||||
|
||||
// call original program
|
||||
nonce += 1;
|
||||
let instruction = Instruction::new(program_id, &[nonce], vec![]);
|
||||
let result = bank_client.send_and_confirm_instruction(&mint_keypair, instruction);
|
||||
assert_eq!(
|
||||
result.unwrap_err().unwrap(),
|
||||
TransactionError::InstructionError(0, InstructionError::Custom(42))
|
||||
);
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
6
programs/bpf_loader/src/upgradeable.rs
Normal file
6
programs/bpf_loader/src/upgradeable.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
solana_sdk::declare_builtin!(
|
||||
solana_sdk::bpf_loader_upgradeable::ID,
|
||||
solana_bpf_loader_upgradeable_program,
|
||||
solana_bpf_loader_program::process_instruction,
|
||||
upgradeable::id
|
||||
);
|
6
programs/bpf_loader/src/upgradeable_with_jit.rs
Normal file
6
programs/bpf_loader/src/upgradeable_with_jit.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
solana_sdk::declare_builtin!(
|
||||
solana_sdk::bpf_loader_upgradeable::ID,
|
||||
solana_bpf_loader_upgradeable_program_with_jit,
|
||||
solana_bpf_loader_program::process_instruction_jit,
|
||||
upgradeable_with_jit::id
|
||||
);
|
Reference in New Issue
Block a user