| 
									
										
										
										
											2019-11-06 14:15:00 -07:00
										 |  |  | use serde_json::Value;
 | 
					
						
							| 
									
										
										
										
											2019-10-04 16:13:21 -06:00
										 |  |  | use solana_cli::cli::{process_command, CliCommand, CliConfig};
 | 
					
						
							| 
									
										
										
										
											2019-03-16 22:37:20 -07:00
										 |  |  | use solana_client::rpc_client::RpcClient;
 | 
					
						
							| 
									
										
										
										
											2020-02-26 12:23:54 +08:00
										 |  |  | use solana_core::validator::TestValidator;
 | 
					
						
							| 
									
										
										
										
											2019-12-16 14:05:17 -07:00
										 |  |  | use solana_faucet::faucet::run_local_faucet;
 | 
					
						
							| 
									
										
										
										
											2020-06-04 20:14:12 -06:00
										 |  |  | use solana_sdk::{
 | 
					
						
							|  |  |  |     bpf_loader,
 | 
					
						
							|  |  |  |     pubkey::Pubkey,
 | 
					
						
							|  |  |  |     signature::{Keypair, Signer},
 | 
					
						
							|  |  |  | };
 | 
					
						
							| 
									
										
										
										
											2019-11-06 14:15:00 -07:00
										 |  |  | use std::{
 | 
					
						
							|  |  |  |     fs::{remove_dir_all, File},
 | 
					
						
							|  |  |  |     io::Read,
 | 
					
						
							|  |  |  |     path::PathBuf,
 | 
					
						
							|  |  |  |     str::FromStr,
 | 
					
						
							|  |  |  |     sync::mpsc::channel,
 | 
					
						
							|  |  |  | };
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #[test]
 | 
					
						
							| 
									
										
										
										
											2019-10-04 16:13:21 -06:00
										 |  |  | fn test_cli_deploy_program() {
 | 
					
						
							| 
									
										
										
										
											2019-02-05 08:03:52 -08:00
										 |  |  |     solana_logger::setup();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  |     let mut pathbuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
 | 
					
						
							|  |  |  |     pathbuf.push("tests");
 | 
					
						
							|  |  |  |     pathbuf.push("fixtures");
 | 
					
						
							|  |  |  |     pathbuf.push("noop");
 | 
					
						
							|  |  |  |     pathbuf.set_extension("so");
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-26 12:23:54 +08:00
										 |  |  |     let TestValidator {
 | 
					
						
							|  |  |  |         server,
 | 
					
						
							|  |  |  |         leader_data,
 | 
					
						
							|  |  |  |         alice,
 | 
					
						
							|  |  |  |         ledger_path,
 | 
					
						
							|  |  |  |         ..
 | 
					
						
							|  |  |  |     } = TestValidator::run();
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let (sender, receiver) = channel();
 | 
					
						
							| 
									
										
										
										
											2019-12-16 14:05:17 -07:00
										 |  |  |     run_local_faucet(alice, sender, None);
 | 
					
						
							|  |  |  |     let faucet_addr = receiver.recv().unwrap();
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-15 22:42:36 -07:00
										 |  |  |     let rpc_client = RpcClient::new_socket(leader_data.rpc);
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 01:14:49 +05:30
										 |  |  |     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 minimum_balance_for_rent_exemption = rpc_client
 | 
					
						
							|  |  |  |         .get_minimum_balance_for_rent_exemption(program_data.len())
 | 
					
						
							|  |  |  |         .unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 16:13:21 -06:00
										 |  |  |     let mut config = CliConfig::default();
 | 
					
						
							| 
									
										
										
										
											2020-02-24 17:03:30 -07:00
										 |  |  |     let keypair = Keypair::new();
 | 
					
						
							| 
									
										
										
										
											2019-05-06 07:38:26 -07:00
										 |  |  |     config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port());
 | 
					
						
							| 
									
										
										
										
											2019-10-04 16:13:21 -06:00
										 |  |  |     config.command = CliCommand::Airdrop {
 | 
					
						
							| 
									
										
										
										
											2019-12-16 14:05:17 -07:00
										 |  |  |         faucet_host: None,
 | 
					
						
							|  |  |  |         faucet_port: faucet_addr.port(),
 | 
					
						
							| 
									
										
										
										
											2020-02-16 11:41:00 -07:00
										 |  |  |         pubkey: None,
 | 
					
						
							| 
									
										
										
										
											2020-06-04 20:14:12 -06:00
										 |  |  |         lamports: 3 * minimum_balance_for_rent_exemption, // min balance for rent exemption for two programs + leftover for tx processing
 | 
					
						
							| 
									
										
										
										
											2019-08-29 20:45:53 -07:00
										 |  |  |     };
 | 
					
						
							| 
									
										
										
										
											2020-02-24 17:03:30 -07:00
										 |  |  |     config.signers = vec![&keypair];
 | 
					
						
							| 
									
										
										
										
											2019-01-28 14:52:35 -08:00
										 |  |  |     process_command(&config).unwrap();
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-04 20:14:12 -06:00
										 |  |  |     config.command = CliCommand::Deploy {
 | 
					
						
							|  |  |  |         program_location: pathbuf.to_str().unwrap().to_string(),
 | 
					
						
							|  |  |  |         address: None,
 | 
					
						
							|  |  |  |     };
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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();
 | 
					
						
							| 
									
										
										
										
											2019-11-06 14:15:00 -07:00
										 |  |  |     let program_id = Pubkey::from_str(&program_id_str).unwrap();
 | 
					
						
							| 
									
										
										
										
											2020-06-04 20:14:12 -06:00
										 |  |  |     let account0 = rpc_client.get_account(&program_id).unwrap();
 | 
					
						
							|  |  |  |     assert_eq!(account0.lamports, minimum_balance_for_rent_exemption);
 | 
					
						
							|  |  |  |     assert_eq!(account0.owner, bpf_loader::id());
 | 
					
						
							|  |  |  |     assert_eq!(account0.executable, true);
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let mut file = File::open(pathbuf.to_str().unwrap().to_string()).unwrap();
 | 
					
						
							|  |  |  |     let mut elf = Vec::new();
 | 
					
						
							|  |  |  |     file.read_to_end(&mut elf).unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-04 20:14:12 -06:00
										 |  |  |     assert_eq!(account0.data, elf);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Test custom address
 | 
					
						
							|  |  |  |     let custom_address_keypair = Keypair::new();
 | 
					
						
							|  |  |  |     config.signers = vec![&keypair, &custom_address_keypair];
 | 
					
						
							|  |  |  |     config.command = CliCommand::Deploy {
 | 
					
						
							|  |  |  |         program_location: pathbuf.to_str().unwrap().to_string(),
 | 
					
						
							|  |  |  |         address: Some(1),
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  |     process_command(&config).unwrap();
 | 
					
						
							|  |  |  |     let account1 = rpc_client
 | 
					
						
							|  |  |  |         .get_account(&custom_address_keypair.pubkey())
 | 
					
						
							|  |  |  |         .unwrap();
 | 
					
						
							|  |  |  |     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);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Attempt to redeploy to the same address
 | 
					
						
							|  |  |  |     process_command(&config).unwrap_err();
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-03 16:44:06 -08:00
										 |  |  |     server.close().unwrap();
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:49:32 -07:00
										 |  |  |     remove_dir_all(ledger_path).unwrap();
 | 
					
						
							|  |  |  | }
 |