Rework upgradeable loader cli (#14209)

This commit is contained in:
Jack May 2020-12-21 13:02:53 -08:00 committed by GitHub
parent 7b08cb1f0d
commit 3316e7166c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1781 additions and 1373 deletions

View File

@ -58,6 +58,15 @@ impl CliSignerInfo {
Some(0)
}
}
pub fn index_of_or_none(&self, pubkey: Option<Pubkey>) -> Option<usize> {
if let Some(pubkey) = pubkey {
self.signers
.iter()
.position(|signer| signer.pubkey() == pubkey)
} else {
None
}
}
}
pub struct DefaultSigner {

File diff suppressed because it is too large Load Diff

View File

@ -26,6 +26,7 @@ pub mod cluster_query;
pub mod feature;
pub mod inflation;
pub mod nonce;
pub mod program;
pub mod send_tpu;
pub mod spend_utils;
pub mod stake;

1537
cli/src/program.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,8 @@
use serde_json::Value;
use solana_cli::cli::{process_command, CliCommand, CliConfig};
use solana_cli::{
cli::{process_command, CliCommand, CliConfig},
program::ProgramCliCommand,
};
use solana_client::rpc_client::RpcClient;
use solana_core::test_validator::TestValidator;
use solana_faucet::faucet::run_local_faucet;
@ -13,7 +16,7 @@ use solana_sdk::{
use std::{fs::File, io::Read, path::PathBuf, str::FromStr, sync::mpsc::channel};
#[test]
fn test_cli_deploy_program() {
fn test_cli_program_deploy_non_upgradeable() {
solana_logger::setup();
let mut pathbuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
@ -41,25 +44,21 @@ fn test_cli_deploy_program() {
let mut config = CliConfig::recent_for_tests();
let keypair = Keypair::new();
config.json_rpc_url = test_validator.rpc_url();
config.signers = vec![&keypair];
config.command = CliCommand::Airdrop {
faucet_host: None,
faucet_port: faucet_addr.port(),
pubkey: None,
lamports: 4 * minimum_balance_for_rent_exemption, // min balance for rent exemption for three programs + leftover for tx processing
};
config.signers = vec![&keypair];
process_command(&config).unwrap();
config.command = CliCommand::ProgramDeploy {
config.command = CliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
buffer: None,
address: None,
use_deprecated_loader: false,
use_upgradeable_loader: false,
allow_excessive_balance: false,
upgrade_authority: None,
max_len: None,
};
let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let program_id_str = json
@ -78,24 +77,19 @@ fn test_cli_deploy_program() {
assert_eq!(account0.lamports, minimum_balance_for_rent_exemption);
assert_eq!(account0.owner, bpf_loader::id());
assert_eq!(account0.executable, true);
let mut file = File::open(pathbuf.to_str().unwrap().to_string()).unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
assert_eq!(account0.data, elf);
// Test custom address
let custom_address_keypair = Keypair::new();
config.signers = vec![&keypair, &custom_address_keypair];
config.command = CliCommand::ProgramDeploy {
config.command = CliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
buffer: Some(1),
address: Some(1),
use_deprecated_loader: false,
use_upgradeable_loader: false,
allow_excessive_balance: false,
upgrade_authority: None,
max_len: None,
};
process_command(&config).unwrap();
let account1 = rpc_client
@ -106,43 +100,36 @@ fn test_cli_deploy_program() {
assert_eq!(account1.lamports, minimum_balance_for_rent_exemption);
assert_eq!(account1.owner, bpf_loader::id());
assert_eq!(account1.executable, true);
assert_eq!(account0.data, account1.data);
assert_eq!(account1.data, account0.data);
// Attempt to redeploy to the same address
process_command(&config).unwrap_err();
// Attempt to deploy to account with excess balance
let custom_address_keypair = Keypair::new();
config.signers = vec![&custom_address_keypair];
config.command = CliCommand::Airdrop {
faucet_host: None,
faucet_port: faucet_addr.port(),
pubkey: None,
lamports: 2 * minimum_balance_for_rent_exemption, // Anything over minimum_balance_for_rent_exemption should trigger err
};
config.signers = vec![&custom_address_keypair];
process_command(&config).unwrap();
config.signers = vec![&keypair, &custom_address_keypair];
config.command = CliCommand::ProgramDeploy {
config.command = CliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
buffer: Some(1),
address: Some(1),
use_deprecated_loader: false,
use_upgradeable_loader: false,
allow_excessive_balance: false,
upgrade_authority: None,
max_len: None,
};
process_command(&config).unwrap_err();
// Use forcing parameter to deploy to account with excess balance
config.command = CliCommand::ProgramDeploy {
config.command = CliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
buffer: Some(1),
address: Some(1),
use_deprecated_loader: false,
use_upgradeable_loader: false,
allow_excessive_balance: true,
upgrade_authority: None,
max_len: None,
};
process_command(&config).unwrap();
let account2 = rpc_client
@ -153,11 +140,11 @@ fn test_cli_deploy_program() {
assert_eq!(account2.lamports, 2 * minimum_balance_for_rent_exemption);
assert_eq!(account2.owner, bpf_loader::id());
assert_eq!(account2.executable, true);
assert_eq!(account0.data, account2.data);
assert_eq!(account2.data, account0.data);
}
#[test]
fn test_cli_deploy_upgradeable_program() {
fn test_cli_program_deploy_no_authority() {
solana_logger::setup();
let mut pathbuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
@ -179,11 +166,6 @@ fn test_cli_deploy_upgradeable_program() {
let mut program_data = Vec::new();
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
println!(
"max_len {:?} {:?}",
max_len,
UpgradeableLoaderState::programdata_len(max_len)
);
let minimum_balance_for_programdata = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
@ -206,16 +188,18 @@ fn test_cli_deploy_upgradeable_program() {
config.signers = vec![&keypair];
process_command(&config).unwrap();
// Deploy and attempt to upgrade a non-upgradeable program
config.command = CliCommand::ProgramDeploy {
// Deploy a program with no authority
config.signers = vec![&keypair];
config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
buffer: None,
use_deprecated_loader: false,
use_upgradeable_loader: true,
program_signer_index: None,
program_pubkey: None,
buffer_signer_index: None,
allow_excessive_balance: false,
upgrade_authority: None,
max_len: Some(max_len),
};
upgrade_authority_signer_index: None,
upgrade_authority_pubkey: None,
max_len: None,
});
let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let program_id_str = json
@ -227,25 +211,132 @@ fn test_cli_deploy_upgradeable_program() {
.unwrap();
let program_id = Pubkey::from_str(&program_id_str).unwrap();
// Attempt to upgrade the program
config.signers = vec![&keypair, &upgrade_authority];
config.command = CliCommand::ProgramUpgrade {
config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
program: program_id,
buffer: None,
upgrade_authority: 1,
};
program_signer_index: None,
program_pubkey: Some(program_id),
buffer_signer_index: None,
allow_excessive_balance: false,
upgrade_authority_signer_index: Some(1),
upgrade_authority_pubkey: Some(upgrade_authority.pubkey()),
max_len: None,
});
process_command(&config).unwrap_err();
}
#[test]
fn test_cli_program_deploy_with_authority() {
solana_logger::setup();
let mut pathbuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
pathbuf.push("tests");
pathbuf.push("fixtures");
pathbuf.push("noop");
pathbuf.set_extension("so");
let mint_keypair = Keypair::new();
let test_validator = TestValidator::with_no_fees(mint_keypair.pubkey());
let (sender, receiver) = channel();
run_local_faucet(mint_keypair, sender, None);
let faucet_addr = receiver.recv().unwrap();
let rpc_client = RpcClient::new(test_validator.rpc_url());
let mut file = File::open(pathbuf.to_str().unwrap()).unwrap();
let mut program_data = Vec::new();
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
let minimum_balance_for_programdata = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.unwrap();
let minimum_balance_for_program = rpc_client
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::program_len().unwrap())
.unwrap();
let upgrade_authority = Keypair::new();
let mut config = CliConfig::recent_for_tests();
let keypair = Keypair::new();
config.json_rpc_url = test_validator.rpc_url();
config.signers = vec![&keypair];
config.command = CliCommand::Airdrop {
faucet_host: None,
faucet_port: faucet_addr.port(),
pubkey: None,
lamports: 100 * minimum_balance_for_programdata + minimum_balance_for_program,
};
process_command(&config).unwrap();
// Deploy the upgradeable program with specified program_id
let program_keypair = Keypair::new();
config.signers = vec![&keypair, &upgrade_authority, &program_keypair];
config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
program_signer_index: Some(2),
program_pubkey: Some(program_keypair.pubkey()),
buffer_signer_index: None,
allow_excessive_balance: false,
upgrade_authority_signer_index: Some(1),
upgrade_authority_pubkey: Some(upgrade_authority.pubkey()),
max_len: Some(max_len),
});
let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let program_id_str = json
.as_object()
.unwrap()
.get("programId")
.unwrap()
.as_str()
.unwrap();
assert_eq!(
program_keypair.pubkey(),
Pubkey::from_str(&program_id_str).unwrap()
);
let program_account = rpc_client
.get_account_with_commitment(&program_keypair.pubkey(), CommitmentConfig::recent())
.unwrap()
.value
.unwrap();
assert_eq!(program_account.lamports, minimum_balance_for_program);
assert_eq!(program_account.owner, bpf_loader_upgradeable::id());
assert_eq!(program_account.executable, true);
let (programdata_pubkey, _) = Pubkey::find_program_address(
&[program_keypair.pubkey().as_ref()],
&bpf_loader_upgradeable::id(),
);
let programdata_account = rpc_client
.get_account_with_commitment(&programdata_pubkey, CommitmentConfig::recent())
.unwrap()
.value
.unwrap();
assert_eq!(
programdata_account.lamports,
minimum_balance_for_programdata
);
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert_eq!(programdata_account.executable, false);
assert_eq!(
programdata_account.data[UpgradeableLoaderState::programdata_data_offset().unwrap()..],
program_data[..]
);
// Deploy the upgradeable program
config.command = CliCommand::ProgramDeploy {
config.signers = vec![&keypair, &upgrade_authority];
config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
buffer: None,
use_deprecated_loader: false,
use_upgradeable_loader: true,
program_signer_index: None,
program_pubkey: None,
buffer_signer_index: None,
allow_excessive_balance: false,
upgrade_authority: Some(upgrade_authority.pubkey()),
upgrade_authority_signer_index: Some(1),
upgrade_authority_pubkey: Some(upgrade_authority.pubkey()),
max_len: Some(max_len),
};
});
let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let program_id_str = json
@ -284,12 +375,16 @@ fn test_cli_deploy_upgradeable_program() {
// Upgrade the program
config.signers = vec![&keypair, &upgrade_authority];
config.command = CliCommand::ProgramUpgrade {
config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
program: program_id,
buffer: None,
upgrade_authority: 1,
};
program_signer_index: None,
program_pubkey: Some(program_id),
buffer_signer_index: None,
allow_excessive_balance: false,
upgrade_authority_signer_index: Some(1),
upgrade_authority_pubkey: Some(upgrade_authority.pubkey()),
max_len: Some(max_len),
});
let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let program_id_str = json
@ -329,11 +424,11 @@ fn test_cli_deploy_upgradeable_program() {
// Set a new authority
let new_upgrade_authority = Keypair::new();
config.signers = vec![&keypair, &upgrade_authority];
config.command = CliCommand::SetProgramUpgradeAuthority {
config.command = CliCommand::Program(ProgramCliCommand::SetUpgradeAuthority {
program: program_id,
upgrade_authority: 1,
upgrade_authority_index: Some(1),
new_upgrade_authority: Some(new_upgrade_authority.pubkey()),
};
});
let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let new_upgrade_authority_str = json
@ -350,12 +445,16 @@ fn test_cli_deploy_upgradeable_program() {
// Upgrade with new authority
config.signers = vec![&keypair, &new_upgrade_authority];
config.command = CliCommand::ProgramUpgrade {
config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
program: program_id,
buffer: None,
upgrade_authority: 1,
};
program_signer_index: None,
program_pubkey: Some(program_id),
buffer_signer_index: None,
allow_excessive_balance: false,
upgrade_authority_signer_index: Some(1),
upgrade_authority_pubkey: Some(new_upgrade_authority.pubkey()),
max_len: None,
});
let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let program_id_str = json
@ -392,13 +491,13 @@ fn test_cli_deploy_upgradeable_program() {
program_data[..]
);
// Set a no authority
// Set no authority
config.signers = vec![&keypair, &new_upgrade_authority];
config.command = CliCommand::SetProgramUpgradeAuthority {
config.command = CliCommand::Program(ProgramCliCommand::SetUpgradeAuthority {
program: program_id,
upgrade_authority: 1,
upgrade_authority_index: Some(1),
new_upgrade_authority: None,
};
});
let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let new_upgrade_authority_str = json
@ -412,11 +511,15 @@ fn test_cli_deploy_upgradeable_program() {
// Upgrade with no authority
config.signers = vec![&keypair, &new_upgrade_authority];
config.command = CliCommand::ProgramUpgrade {
config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: pathbuf.to_str().unwrap().to_string(),
program: program_id,
buffer: None,
upgrade_authority: 1,
};
program_signer_index: None,
program_pubkey: Some(program_id),
buffer_signer_index: None,
allow_excessive_balance: false,
upgrade_authority_signer_index: Some(1),
upgrade_authority_pubkey: Some(new_upgrade_authority.pubkey()),
max_len: None,
});
process_command(&config).unwrap_err();
}

View File

@ -48,6 +48,10 @@ impl RpcSender for MockSender {
return Ok(Value::Null);
}
let val = match request {
RpcRequest::GetAccountInfo => serde_json::to_value(Response {
context: RpcResponseContext { slot: 1 },
value: Value::Null,
})?,
RpcRequest::GetBalance => serde_json::to_value(Response {
context: RpcResponseContext { slot: 1 },
value: Value::Number(Number::from(50)),

View File

@ -217,7 +217,7 @@ fn build_bpf_package(
println!();
println!("To deploy this program:");
println!(" $ solana deploy {}", program_so.display());
println!(" $ solana program deploy {}", program_so.display());
} else if config.dump {
println!("Note: --dump is only available for crates with a cdylib target");
}