cpi.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. use {
  2. solana_account_info::{next_account_info, AccountInfo},
  3. solana_instruction::{AccountMeta, Instruction},
  4. solana_keypair::Keypair,
  5. solana_msg::msg,
  6. solana_program::{instruction::get_stack_height, program::invoke},
  7. solana_program_entrypoint::{ProgramResult, MAX_PERMITTED_DATA_INCREASE},
  8. solana_program_test::{processor, ProgramTest},
  9. solana_pubkey::Pubkey,
  10. solana_rent::Rent,
  11. solana_signer::Signer,
  12. solana_system_interface::{instruction as system_instruction, program as system_program},
  13. solana_sysvar::Sysvar,
  14. solana_transaction::Transaction,
  15. std::slice,
  16. };
  17. // Process instruction to invoke into another program
  18. // We pass this specific number of accounts in order to test for a reported error.
  19. fn invoker_process_instruction(
  20. _program_id: &Pubkey,
  21. accounts: &[AccountInfo],
  22. _input: &[u8],
  23. ) -> ProgramResult {
  24. // if we can call `msg!` successfully, then InvokeContext exists as required
  25. msg!("Processing invoker instruction before CPI");
  26. let account_info_iter = &mut accounts.iter();
  27. let invoked_program_info = next_account_info(account_info_iter)?;
  28. invoke(
  29. &Instruction::new_with_bincode(
  30. *invoked_program_info.key,
  31. &[0],
  32. vec![AccountMeta::new_readonly(*invoked_program_info.key, false)],
  33. ),
  34. slice::from_ref(invoked_program_info),
  35. )?;
  36. msg!("Processing invoker instruction after CPI");
  37. Ok(())
  38. }
  39. // Process instruction to invoke into another program with duplicates
  40. fn invoker_dupes_process_instruction(
  41. _program_id: &Pubkey,
  42. accounts: &[AccountInfo],
  43. _input: &[u8],
  44. ) -> ProgramResult {
  45. // if we can call `msg!` successfully, then InvokeContext exists as required
  46. msg!("Processing invoker instruction before CPI");
  47. let account_info_iter = &mut accounts.iter();
  48. let invoked_program_info = next_account_info(account_info_iter)?;
  49. invoke(
  50. &Instruction::new_with_bincode(
  51. *invoked_program_info.key,
  52. &[0],
  53. vec![
  54. AccountMeta::new_readonly(*invoked_program_info.key, false),
  55. AccountMeta::new_readonly(*invoked_program_info.key, false),
  56. AccountMeta::new_readonly(*invoked_program_info.key, false),
  57. AccountMeta::new_readonly(*invoked_program_info.key, false),
  58. ],
  59. ),
  60. &[
  61. invoked_program_info.clone(),
  62. invoked_program_info.clone(),
  63. invoked_program_info.clone(),
  64. invoked_program_info.clone(),
  65. invoked_program_info.clone(),
  66. ],
  67. )?;
  68. msg!("Processing invoker instruction after CPI");
  69. Ok(())
  70. }
  71. // Process instruction to be invoked by another program
  72. fn invoked_process_instruction(
  73. _program_id: &Pubkey,
  74. _accounts: &[AccountInfo],
  75. _input: &[u8],
  76. ) -> ProgramResult {
  77. // if we can call `msg!` successfully, then InvokeContext exists as required
  78. msg!("Processing invoked instruction");
  79. Ok(())
  80. }
  81. // Process instruction to invoke into system program to create an account
  82. fn invoke_create_account(
  83. program_id: &Pubkey,
  84. accounts: &[AccountInfo],
  85. _input: &[u8],
  86. ) -> ProgramResult {
  87. msg!("Processing instruction before system program CPI instruction");
  88. let account_info_iter = &mut accounts.iter();
  89. let payer_info = next_account_info(account_info_iter)?;
  90. let create_account_info = next_account_info(account_info_iter)?;
  91. let system_program_info = next_account_info(account_info_iter)?;
  92. let rent = Rent::get()?;
  93. let minimum_balance = rent.minimum_balance(MAX_PERMITTED_DATA_INCREASE);
  94. invoke(
  95. &system_instruction::create_account(
  96. payer_info.key,
  97. create_account_info.key,
  98. minimum_balance,
  99. MAX_PERMITTED_DATA_INCREASE as u64,
  100. program_id,
  101. ),
  102. &[
  103. payer_info.clone(),
  104. create_account_info.clone(),
  105. system_program_info.clone(),
  106. ],
  107. )?;
  108. msg!("Processing instruction after system program CPI");
  109. Ok(())
  110. }
  111. #[tokio::test]
  112. async fn cpi() {
  113. let invoker_program_id = Pubkey::new_unique();
  114. let mut program_test = ProgramTest::new(
  115. "invoker",
  116. invoker_program_id,
  117. processor!(invoker_process_instruction),
  118. );
  119. let invoked_program_id = Pubkey::new_unique();
  120. program_test.add_program(
  121. "invoked",
  122. invoked_program_id,
  123. processor!(invoked_process_instruction),
  124. );
  125. let context = program_test.start_with_context().await;
  126. let instructions = vec![Instruction::new_with_bincode(
  127. invoker_program_id,
  128. &[0],
  129. vec![AccountMeta::new_readonly(invoked_program_id, false)],
  130. )];
  131. let transaction = Transaction::new_signed_with_payer(
  132. &instructions,
  133. Some(&context.payer.pubkey()),
  134. &[&context.payer],
  135. context.last_blockhash,
  136. );
  137. context
  138. .banks_client
  139. .process_transaction(transaction)
  140. .await
  141. .unwrap();
  142. }
  143. #[tokio::test]
  144. async fn cpi_dupes() {
  145. let invoker_program_id = Pubkey::new_unique();
  146. let mut program_test = ProgramTest::new(
  147. "invoker",
  148. invoker_program_id,
  149. processor!(invoker_dupes_process_instruction),
  150. );
  151. let invoked_program_id = Pubkey::new_unique();
  152. program_test.add_program(
  153. "invoked",
  154. invoked_program_id,
  155. processor!(invoked_process_instruction),
  156. );
  157. let context = program_test.start_with_context().await;
  158. let instructions = vec![Instruction::new_with_bincode(
  159. invoker_program_id,
  160. &[0],
  161. vec![
  162. AccountMeta::new_readonly(invoked_program_id, false),
  163. AccountMeta::new_readonly(invoked_program_id, false),
  164. AccountMeta::new_readonly(invoked_program_id, false),
  165. AccountMeta::new_readonly(invoked_program_id, false),
  166. ],
  167. )];
  168. let transaction = Transaction::new_signed_with_payer(
  169. &instructions,
  170. Some(&context.payer.pubkey()),
  171. &[&context.payer],
  172. context.last_blockhash,
  173. );
  174. context
  175. .banks_client
  176. .process_transaction(transaction)
  177. .await
  178. .unwrap();
  179. }
  180. #[tokio::test]
  181. async fn cpi_create_account() {
  182. let create_account_program_id = Pubkey::new_unique();
  183. let program_test = ProgramTest::new(
  184. "create_account",
  185. create_account_program_id,
  186. processor!(invoke_create_account),
  187. );
  188. let create_account_keypair = Keypair::new();
  189. let context = program_test.start_with_context().await;
  190. let instructions = vec![Instruction::new_with_bincode(
  191. create_account_program_id,
  192. &[0],
  193. vec![
  194. AccountMeta::new(context.payer.pubkey(), true),
  195. AccountMeta::new(create_account_keypair.pubkey(), true),
  196. AccountMeta::new_readonly(system_program::id(), false),
  197. ],
  198. )];
  199. let transaction = Transaction::new_signed_with_payer(
  200. &instructions,
  201. Some(&context.payer.pubkey()),
  202. &[&context.payer, &create_account_keypair],
  203. context.last_blockhash,
  204. );
  205. context
  206. .banks_client
  207. .process_transaction(transaction)
  208. .await
  209. .unwrap();
  210. }
  211. // Process instruction to invoke into another program
  212. fn invoker_stack_height(
  213. _program_id: &Pubkey,
  214. accounts: &[AccountInfo],
  215. _input: &[u8],
  216. ) -> ProgramResult {
  217. // if we can call `msg!` successfully, then InvokeContext exists as required
  218. msg!("Processing invoker instruction before CPI");
  219. let stack_height = get_stack_height();
  220. assert_eq!(stack_height, 1);
  221. let account_info_iter = &mut accounts.iter();
  222. let invoked_program_info = next_account_info(account_info_iter)?;
  223. invoke(
  224. &Instruction::new_with_bytes(*invoked_program_info.key, &[], vec![]),
  225. slice::from_ref(invoked_program_info),
  226. )?;
  227. msg!("Processing invoker instruction after CPI");
  228. Ok(())
  229. }
  230. // Process instruction to be invoked by another program
  231. fn invoked_stack_height(
  232. _program_id: &Pubkey,
  233. _accounts: &[AccountInfo],
  234. _input: &[u8],
  235. ) -> ProgramResult {
  236. let stack_height = get_stack_height();
  237. assert_eq!(stack_height, 2);
  238. Ok(())
  239. }
  240. #[tokio::test]
  241. async fn stack_height() {
  242. let invoker_stack_height_program_id = Pubkey::new_unique();
  243. let invoked_stack_height_program_id = Pubkey::new_unique();
  244. let mut program_test = ProgramTest::new(
  245. "invoker_stack_height",
  246. invoker_stack_height_program_id,
  247. processor!(invoker_stack_height),
  248. );
  249. program_test.add_program(
  250. "invoked_stack_height",
  251. invoked_stack_height_program_id,
  252. processor!(invoked_stack_height),
  253. );
  254. let context = program_test.start_with_context().await;
  255. let instructions = vec![Instruction::new_with_bytes(
  256. invoker_stack_height_program_id,
  257. &[],
  258. vec![AccountMeta::new_readonly(
  259. invoked_stack_height_program_id,
  260. false,
  261. )],
  262. )];
  263. let transaction = Transaction::new_signed_with_payer(
  264. &instructions,
  265. Some(&context.payer.pubkey()),
  266. &[&context.payer],
  267. context.last_blockhash,
  268. );
  269. context
  270. .banks_client
  271. .process_transaction(transaction)
  272. .await
  273. .unwrap();
  274. }