nonce.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #![allow(clippy::arithmetic_side_effects)]
  2. use {
  3. solana_cli::{
  4. check_balance,
  5. cli::{process_command, request_and_confirm_airdrop, CliCommand, CliConfig},
  6. spend_utils::SpendAmount,
  7. test_utils::check_ready,
  8. },
  9. solana_cli_output::{parse_sign_only_reply_string, OutputFormat},
  10. solana_commitment_config::CommitmentConfig,
  11. solana_faucet::faucet::run_local_faucet_with_unique_port_for_tests,
  12. solana_hash::Hash,
  13. solana_keypair::{keypair_from_seed, Keypair},
  14. solana_native_token::LAMPORTS_PER_SOL,
  15. solana_pubkey::Pubkey,
  16. solana_rpc_client::rpc_client::RpcClient,
  17. solana_rpc_client_nonce_utils::blockhash_query::{self, BlockhashQuery},
  18. solana_signer::Signer,
  19. solana_streamer::socket::SocketAddrSpace,
  20. solana_system_interface::program as system_program,
  21. solana_test_validator::TestValidator,
  22. test_case::test_case,
  23. };
  24. #[test_case(None, false, None; "base")]
  25. #[test_case(Some(String::from("seed")), false, None; "with_seed")]
  26. #[test_case(None, true, None; "with_authority")]
  27. #[test_case(None, false, Some(1_000_000); "with_compute_unit_price")]
  28. fn test_nonce(seed: Option<String>, use_nonce_authority: bool, compute_unit_price: Option<u64>) {
  29. let mint_keypair = Keypair::new();
  30. let mint_pubkey = mint_keypair.pubkey();
  31. let faucet_addr = run_local_faucet_with_unique_port_for_tests(mint_keypair);
  32. let test_validator =
  33. TestValidator::with_no_fees(mint_pubkey, Some(faucet_addr), SocketAddrSpace::Unspecified);
  34. let rpc_client =
  35. RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed());
  36. let json_rpc_url = test_validator.rpc_url();
  37. let mut config_payer = CliConfig::recent_for_tests();
  38. config_payer.json_rpc_url.clone_from(&json_rpc_url);
  39. let payer = Keypair::new();
  40. config_payer.signers = vec![&payer];
  41. request_and_confirm_airdrop(
  42. &rpc_client,
  43. &config_payer,
  44. &config_payer.signers[0].pubkey(),
  45. 2000 * LAMPORTS_PER_SOL,
  46. )
  47. .unwrap();
  48. check_balance!(
  49. 2000 * LAMPORTS_PER_SOL,
  50. &rpc_client,
  51. &config_payer.signers[0].pubkey(),
  52. );
  53. let mut config_nonce = CliConfig::recent_for_tests();
  54. config_nonce.json_rpc_url = json_rpc_url;
  55. let nonce_keypair = keypair_from_seed(&[0u8; 32]).unwrap();
  56. config_nonce.signers = vec![&nonce_keypair];
  57. let nonce_account = if let Some(seed) = seed.as_ref() {
  58. Pubkey::create_with_seed(
  59. &config_nonce.signers[0].pubkey(),
  60. seed,
  61. &system_program::id(),
  62. )
  63. .unwrap()
  64. } else {
  65. nonce_keypair.pubkey()
  66. };
  67. let nonce_authority = Keypair::new();
  68. let optional_authority = if use_nonce_authority {
  69. Some(nonce_authority.pubkey())
  70. } else {
  71. None
  72. };
  73. // Create nonce account
  74. config_payer.signers.push(&nonce_keypair);
  75. config_payer.command = CliCommand::CreateNonceAccount {
  76. nonce_account: 1,
  77. seed,
  78. nonce_authority: optional_authority,
  79. memo: None,
  80. amount: SpendAmount::Some(1000 * LAMPORTS_PER_SOL),
  81. compute_unit_price,
  82. };
  83. process_command(&config_payer).unwrap();
  84. check_balance!(
  85. 1000 * LAMPORTS_PER_SOL,
  86. &rpc_client,
  87. &config_payer.signers[0].pubkey(),
  88. );
  89. check_balance!(1000 * LAMPORTS_PER_SOL, &rpc_client, &nonce_account);
  90. // Get nonce
  91. config_payer.signers.pop();
  92. config_payer.command = CliCommand::GetNonce(nonce_account);
  93. let first_nonce_string = process_command(&config_payer).unwrap();
  94. let first_nonce = first_nonce_string.parse::<Hash>().unwrap();
  95. // Get nonce
  96. config_payer.command = CliCommand::GetNonce(nonce_account);
  97. let second_nonce_string = process_command(&config_payer).unwrap();
  98. let second_nonce = second_nonce_string.parse::<Hash>().unwrap();
  99. assert_eq!(first_nonce, second_nonce);
  100. let mut authorized_signers: Vec<&dyn Signer> = vec![&payer];
  101. let index = if use_nonce_authority {
  102. authorized_signers.push(&nonce_authority);
  103. 1
  104. } else {
  105. 0
  106. };
  107. // New nonce
  108. config_payer.signers.clone_from(&authorized_signers);
  109. config_payer.command = CliCommand::NewNonce {
  110. nonce_account,
  111. nonce_authority: index,
  112. memo: None,
  113. compute_unit_price,
  114. };
  115. process_command(&config_payer).unwrap();
  116. // Get nonce
  117. config_payer.signers = vec![&payer];
  118. config_payer.command = CliCommand::GetNonce(nonce_account);
  119. let third_nonce_string = process_command(&config_payer).unwrap();
  120. let third_nonce = third_nonce_string.parse::<Hash>().unwrap();
  121. assert_ne!(first_nonce, third_nonce);
  122. // Withdraw from nonce account
  123. let payee_pubkey = solana_pubkey::new_rand();
  124. config_payer.signers = authorized_signers;
  125. config_payer.command = CliCommand::WithdrawFromNonceAccount {
  126. nonce_account,
  127. nonce_authority: index,
  128. memo: None,
  129. destination_account_pubkey: payee_pubkey,
  130. lamports: 100 * LAMPORTS_PER_SOL,
  131. compute_unit_price,
  132. };
  133. process_command(&config_payer).unwrap();
  134. check_balance!(
  135. 1000 * LAMPORTS_PER_SOL,
  136. &rpc_client,
  137. &config_payer.signers[0].pubkey(),
  138. );
  139. check_balance!(900 * LAMPORTS_PER_SOL, &rpc_client, &nonce_account);
  140. check_balance!(100 * LAMPORTS_PER_SOL, &rpc_client, &payee_pubkey);
  141. // Show nonce account
  142. config_payer.command = CliCommand::ShowNonceAccount {
  143. nonce_account_pubkey: nonce_account,
  144. use_lamports_unit: true,
  145. };
  146. process_command(&config_payer).unwrap();
  147. // Set new authority
  148. let new_authority = Keypair::new();
  149. config_payer.command = CliCommand::AuthorizeNonceAccount {
  150. nonce_account,
  151. nonce_authority: index,
  152. memo: None,
  153. new_authority: new_authority.pubkey(),
  154. compute_unit_price,
  155. };
  156. process_command(&config_payer).unwrap();
  157. // Old authority fails now
  158. config_payer.command = CliCommand::NewNonce {
  159. nonce_account,
  160. nonce_authority: index,
  161. memo: None,
  162. compute_unit_price,
  163. };
  164. process_command(&config_payer).unwrap_err();
  165. // New authority can advance nonce
  166. config_payer.signers = vec![&payer, &new_authority];
  167. config_payer.command = CliCommand::NewNonce {
  168. nonce_account,
  169. nonce_authority: 1,
  170. memo: None,
  171. compute_unit_price,
  172. };
  173. process_command(&config_payer).unwrap();
  174. // New authority can withdraw from nonce account
  175. config_payer.command = CliCommand::WithdrawFromNonceAccount {
  176. nonce_account,
  177. nonce_authority: 1,
  178. memo: None,
  179. destination_account_pubkey: payee_pubkey,
  180. lamports: 100 * LAMPORTS_PER_SOL,
  181. compute_unit_price,
  182. };
  183. process_command(&config_payer).unwrap();
  184. check_balance!(
  185. 1000 * LAMPORTS_PER_SOL,
  186. &rpc_client,
  187. &config_payer.signers[0].pubkey(),
  188. );
  189. check_balance!(800 * LAMPORTS_PER_SOL, &rpc_client, &nonce_account);
  190. check_balance!(200 * LAMPORTS_PER_SOL, &rpc_client, &payee_pubkey);
  191. }
  192. #[test]
  193. fn test_create_account_with_seed() {
  194. const ONE_SIG_FEE: u64 = 5000;
  195. agave_logger::setup();
  196. let mint_keypair = Keypair::new();
  197. let mint_pubkey = mint_keypair.pubkey();
  198. let faucet_addr = run_local_faucet_with_unique_port_for_tests(mint_keypair);
  199. let test_validator = TestValidator::with_custom_fees(
  200. mint_pubkey,
  201. ONE_SIG_FEE,
  202. Some(faucet_addr),
  203. SocketAddrSpace::Unspecified,
  204. );
  205. let offline_nonce_authority_signer = keypair_from_seed(&[1u8; 32]).unwrap();
  206. let online_nonce_creator_signer = keypair_from_seed(&[2u8; 32]).unwrap();
  207. let to_address = Pubkey::from([3u8; 32]);
  208. // Setup accounts
  209. let rpc_client =
  210. RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed());
  211. request_and_confirm_airdrop(
  212. &rpc_client,
  213. &CliConfig::recent_for_tests(),
  214. &offline_nonce_authority_signer.pubkey(),
  215. 42 * LAMPORTS_PER_SOL,
  216. )
  217. .unwrap();
  218. request_and_confirm_airdrop(
  219. &rpc_client,
  220. &CliConfig::recent_for_tests(),
  221. &online_nonce_creator_signer.pubkey(),
  222. 4242 * LAMPORTS_PER_SOL,
  223. )
  224. .unwrap();
  225. check_balance!(
  226. 42 * LAMPORTS_PER_SOL,
  227. &rpc_client,
  228. &offline_nonce_authority_signer.pubkey(),
  229. );
  230. check_balance!(
  231. 4242 * LAMPORTS_PER_SOL,
  232. &rpc_client,
  233. &online_nonce_creator_signer.pubkey(),
  234. );
  235. check_balance!(0, &rpc_client, &to_address);
  236. check_ready(&rpc_client);
  237. // Create nonce account
  238. let creator_pubkey = online_nonce_creator_signer.pubkey();
  239. let authority_pubkey = offline_nonce_authority_signer.pubkey();
  240. let seed = authority_pubkey.to_string()[0..32].to_string();
  241. let nonce_address =
  242. Pubkey::create_with_seed(&creator_pubkey, &seed, &system_program::id()).unwrap();
  243. check_balance!(0, &rpc_client, &nonce_address);
  244. let mut creator_config = CliConfig::recent_for_tests();
  245. creator_config.json_rpc_url = test_validator.rpc_url();
  246. creator_config.signers = vec![&online_nonce_creator_signer];
  247. creator_config.command = CliCommand::CreateNonceAccount {
  248. nonce_account: 0,
  249. seed: Some(seed),
  250. nonce_authority: Some(authority_pubkey),
  251. memo: None,
  252. amount: SpendAmount::Some(241 * LAMPORTS_PER_SOL),
  253. compute_unit_price: None,
  254. };
  255. process_command(&creator_config).unwrap();
  256. check_balance!(241 * LAMPORTS_PER_SOL, &rpc_client, &nonce_address);
  257. check_balance!(
  258. 42 * LAMPORTS_PER_SOL,
  259. &rpc_client,
  260. &offline_nonce_authority_signer.pubkey(),
  261. );
  262. check_balance!(
  263. 4001 * LAMPORTS_PER_SOL - ONE_SIG_FEE,
  264. &rpc_client,
  265. &online_nonce_creator_signer.pubkey(),
  266. );
  267. check_balance!(0, &rpc_client, &to_address);
  268. // Fetch nonce hash
  269. let nonce_hash = solana_rpc_client_nonce_utils::get_account_with_commitment(
  270. &rpc_client,
  271. &nonce_address,
  272. CommitmentConfig::processed(),
  273. )
  274. .and_then(|ref a| solana_rpc_client_nonce_utils::data_from_account(a))
  275. .unwrap()
  276. .blockhash();
  277. // Test by creating transfer TX with nonce, fully offline
  278. let mut authority_config = CliConfig::recent_for_tests();
  279. authority_config.json_rpc_url = String::default();
  280. authority_config.signers = vec![&offline_nonce_authority_signer];
  281. // Verify we cannot contact the cluster
  282. authority_config.command = CliCommand::ClusterVersion;
  283. process_command(&authority_config).unwrap_err();
  284. authority_config.command = CliCommand::Transfer {
  285. amount: SpendAmount::Some(10 * LAMPORTS_PER_SOL),
  286. to: to_address,
  287. from: 0,
  288. sign_only: true,
  289. dump_transaction_message: true,
  290. allow_unfunded_recipient: true,
  291. no_wait: false,
  292. blockhash_query: BlockhashQuery::None(nonce_hash),
  293. nonce_account: Some(nonce_address),
  294. nonce_authority: 0,
  295. memo: None,
  296. fee_payer: 0,
  297. derived_address_seed: None,
  298. derived_address_program_id: None,
  299. compute_unit_price: None,
  300. };
  301. authority_config.output_format = OutputFormat::JsonCompact;
  302. let sign_only_reply = process_command(&authority_config).unwrap();
  303. let sign_only = parse_sign_only_reply_string(&sign_only_reply);
  304. let authority_presigner = sign_only.presigner_of(&authority_pubkey).unwrap();
  305. assert_eq!(sign_only.blockhash, nonce_hash);
  306. // And submit it
  307. let mut submit_config = CliConfig::recent_for_tests();
  308. submit_config.json_rpc_url = test_validator.rpc_url();
  309. submit_config.signers = vec![&authority_presigner];
  310. submit_config.command = CliCommand::Transfer {
  311. amount: SpendAmount::Some(10 * LAMPORTS_PER_SOL),
  312. to: to_address,
  313. from: 0,
  314. sign_only: false,
  315. dump_transaction_message: true,
  316. allow_unfunded_recipient: true,
  317. no_wait: false,
  318. blockhash_query: BlockhashQuery::FeeCalculator(
  319. blockhash_query::Source::NonceAccount(nonce_address),
  320. sign_only.blockhash,
  321. ),
  322. nonce_account: Some(nonce_address),
  323. nonce_authority: 0,
  324. memo: None,
  325. fee_payer: 0,
  326. derived_address_seed: None,
  327. derived_address_program_id: None,
  328. compute_unit_price: None,
  329. };
  330. process_command(&submit_config).unwrap();
  331. check_balance!(241 * LAMPORTS_PER_SOL, &rpc_client, &nonce_address);
  332. check_balance!(
  333. 32 * LAMPORTS_PER_SOL - ONE_SIG_FEE,
  334. &rpc_client,
  335. &offline_nonce_authority_signer.pubkey(),
  336. );
  337. check_balance!(
  338. 4001 * LAMPORTS_PER_SOL - ONE_SIG_FEE,
  339. &rpc_client,
  340. &online_nonce_creator_signer.pubkey(),
  341. );
  342. check_balance!(10 * LAMPORTS_PER_SOL, &rpc_client, &to_address);
  343. }