Replace deploy command with program deploy in tests (#28398)

replace deploy command with program deploy in tests
This commit is contained in:
kirill lykov 2022-10-18 10:57:53 +02:00 committed by GitHub
parent 547f07526b
commit c7b2356d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 98 additions and 44 deletions

View File

@ -11,7 +11,6 @@ use {
solana_rpc_client::rpc_client::RpcClient, solana_rpc_client::rpc_client::RpcClient,
solana_sdk::{ solana_sdk::{
account_utils::StateMut, account_utils::StateMut,
bpf_loader,
bpf_loader_upgradeable::{self, UpgradeableLoaderState}, bpf_loader_upgradeable::{self, UpgradeableLoaderState},
commitment_config::CommitmentConfig, commitment_config::CommitmentConfig,
pubkey::Pubkey, pubkey::Pubkey,
@ -44,8 +43,13 @@ fn test_cli_program_deploy_non_upgradeable() {
let mut file = File::open(noop_path.to_str().unwrap()).unwrap(); let mut file = File::open(noop_path.to_str().unwrap()).unwrap();
let mut program_data = Vec::new(); let mut program_data = Vec::new();
file.read_to_end(&mut program_data).unwrap(); file.read_to_end(&mut program_data).unwrap();
let minimum_balance_for_rent_exemption = rpc_client let minimum_balance_for_programdata = rpc_client
.get_minimum_balance_for_rent_exemption(program_data.len()) .get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
program_data.len(),
))
.unwrap();
let minimum_balance_for_program = rpc_client
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_program())
.unwrap(); .unwrap();
let mut config = CliConfig::recent_for_tests(); let mut config = CliConfig::recent_for_tests();
@ -54,17 +58,22 @@ fn test_cli_program_deploy_non_upgradeable() {
config.signers = vec![&keypair]; config.signers = vec![&keypair];
config.command = CliCommand::Airdrop { config.command = CliCommand::Airdrop {
pubkey: None, pubkey: None,
lamports: 4 * minimum_balance_for_rent_exemption, // min balance for rent exemption for three programs + leftover for tx processing lamports: 4 * minimum_balance_for_programdata, // min balance for rent exemption for three programs + leftover for tx processing
}; };
process_command(&config).unwrap(); process_command(&config).unwrap();
config.command = CliCommand::Deploy { config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: noop_path.to_str().unwrap().to_string(), program_location: Some(noop_path.to_str().unwrap().to_string()),
address: None, program_signer_index: None,
use_deprecated_loader: false, program_pubkey: None,
buffer_signer_index: None,
buffer_pubkey: None,
allow_excessive_balance: false, allow_excessive_balance: false,
upgrade_authority_signer_index: 0,
is_final: true,
max_len: None,
skip_fee_check: false, skip_fee_check: false,
}; });
config.output_format = OutputFormat::JsonCompact; config.output_format = OutputFormat::JsonCompact;
let response = process_command(&config); let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap(); let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
@ -77,70 +86,115 @@ fn test_cli_program_deploy_non_upgradeable() {
.unwrap(); .unwrap();
let program_id = Pubkey::from_str(program_id_str).unwrap(); let program_id = Pubkey::from_str(program_id_str).unwrap();
let account0 = rpc_client.get_account(&program_id).unwrap(); let account0 = rpc_client.get_account(&program_id).unwrap();
assert_eq!(account0.lamports, minimum_balance_for_rent_exemption); assert_eq!(account0.lamports, minimum_balance_for_program);
assert_eq!(account0.owner, bpf_loader::id()); assert_eq!(account0.owner, bpf_loader_upgradeable::id());
assert!(account0.executable); assert!(account0.executable);
let mut file = File::open(noop_path.to_str().unwrap()).unwrap(); let (programdata_pubkey, _) =
let mut elf = Vec::new(); Pubkey::find_program_address(&[program_id.as_ref()], &bpf_loader_upgradeable::id());
file.read_to_end(&mut elf).unwrap(); let programdata_account = rpc_client.get_account(&programdata_pubkey).unwrap();
assert_eq!(account0.data, elf); assert_eq!(
programdata_account.lamports,
minimum_balance_for_programdata
);
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!programdata_account.executable);
assert_eq!(
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
);
// Test custom address // Test custom address
let custom_address_keypair = Keypair::new(); let custom_address_keypair = Keypair::new();
config.signers = vec![&keypair, &custom_address_keypair]; config.signers = vec![&keypair, &custom_address_keypair];
config.command = CliCommand::Deploy { config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: noop_path.to_str().unwrap().to_string(), program_location: Some(noop_path.to_str().unwrap().to_string()),
address: Some(1), program_signer_index: Some(1),
use_deprecated_loader: false, program_pubkey: None,
buffer_signer_index: None,
buffer_pubkey: None,
allow_excessive_balance: false, allow_excessive_balance: false,
upgrade_authority_signer_index: 0,
is_final: true,
max_len: None,
skip_fee_check: false, skip_fee_check: false,
}; });
process_command(&config).unwrap(); process_command(&config).unwrap();
let account1 = rpc_client let account1 = rpc_client
.get_account(&custom_address_keypair.pubkey()) .get_account(&custom_address_keypair.pubkey())
.unwrap(); .unwrap();
assert_eq!(account1.lamports, minimum_balance_for_rent_exemption); assert_eq!(account1.lamports, minimum_balance_for_program);
assert_eq!(account1.owner, bpf_loader::id()); assert_eq!(account1.owner, bpf_loader_upgradeable::id());
assert!(account1.executable); assert!(account1.executable);
assert_eq!(account1.data, account0.data); let (programdata_pubkey, _) = Pubkey::find_program_address(
&[custom_address_keypair.pubkey().as_ref()],
&bpf_loader_upgradeable::id(),
);
let programdata_account = rpc_client.get_account(&programdata_pubkey).unwrap();
assert_eq!(
programdata_account.lamports,
minimum_balance_for_programdata
);
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!programdata_account.executable);
assert_eq!(
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
);
// Attempt to redeploy to the same address // Attempt to redeploy to the same address
process_command(&config).unwrap_err(); let err = process_command(&config).unwrap_err();
assert_eq!(
format!(
"Program {} is no longer upgradeable",
custom_address_keypair.pubkey()
),
format!("{}", err)
);
// Attempt to deploy to account with excess balance // Attempt to deploy to account with excess balance
let custom_address_keypair = Keypair::new(); let custom_address_keypair = Keypair::new();
config.signers = vec![&custom_address_keypair]; config.signers = vec![&custom_address_keypair];
config.command = CliCommand::Airdrop { config.command = CliCommand::Airdrop {
pubkey: None, pubkey: None,
lamports: 2 * minimum_balance_for_rent_exemption, // Anything over minimum_balance_for_rent_exemption should trigger err lamports: 2 * minimum_balance_for_programdata, // Anything over minimum_balance_for_programdata should trigger err
}; };
process_command(&config).unwrap(); process_command(&config).unwrap();
config.signers = vec![&keypair, &custom_address_keypair]; config.signers = vec![&keypair, &custom_address_keypair];
config.command = CliCommand::Deploy { config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: noop_path.to_str().unwrap().to_string(), program_location: Some(noop_path.to_str().unwrap().to_string()),
address: Some(1), program_signer_index: Some(1),
use_deprecated_loader: false, program_pubkey: None,
buffer_signer_index: None,
buffer_pubkey: None,
allow_excessive_balance: false, allow_excessive_balance: false,
upgrade_authority_signer_index: 0,
is_final: true,
max_len: None,
skip_fee_check: false, skip_fee_check: false,
}; });
process_command(&config).unwrap_err(); let err = process_command(&config).unwrap_err();
assert_eq!(
format!(
"Account {} is not an upgradeable program or already in use",
custom_address_keypair.pubkey()
),
format!("{}", err)
);
// Use forcing parameter to deploy to account with excess balance // Use forcing parameter to deploy to account with excess balance
config.command = CliCommand::Deploy { config.command = CliCommand::Program(ProgramCliCommand::Deploy {
program_location: noop_path.to_str().unwrap().to_string(), program_location: Some(noop_path.to_str().unwrap().to_string()),
address: Some(1), program_signer_index: Some(1),
use_deprecated_loader: false, program_pubkey: None,
buffer_signer_index: None,
buffer_pubkey: None,
allow_excessive_balance: true, allow_excessive_balance: true,
upgrade_authority_signer_index: 0,
is_final: true,
max_len: None,
skip_fee_check: false, skip_fee_check: false,
}; });
process_command(&config).unwrap(); process_command(&config).unwrap_err();
let account2 = rpc_client
.get_account(&custom_address_keypair.pubkey())
.unwrap();
assert_eq!(account2.lamports, 2 * minimum_balance_for_rent_exemption);
assert_eq!(account2.owner, bpf_loader::id());
assert!(account2.executable);
assert_eq!(account2.data, account0.data);
} }
#[test] #[test]