test_migrate.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //! Checks for migrating the previous config schema into the current one
  2. pub mod fixtures;
  3. use solana_program::system_program;
  4. use solana_program_test::*;
  5. use solana_sdk::{
  6. account::Account,
  7. instruction::{
  8. AccountMeta,
  9. Instruction,
  10. },
  11. pubkey::Pubkey,
  12. rent::Rent,
  13. signature::Signer,
  14. signer::keypair::Keypair,
  15. transaction::Transaction,
  16. };
  17. use bridge::accounts::{
  18. Bridge,
  19. BridgeConfig,
  20. BridgeData,
  21. };
  22. use log::info;
  23. use pyth2wormhole::config::{
  24. OldP2WConfigAccount,
  25. OldPyth2WormholeConfig,
  26. P2WConfigAccount,
  27. Pyth2WormholeConfig,
  28. };
  29. use pyth2wormhole_client as p2wc;
  30. use solitaire::{
  31. processors::seeded::Seeded,
  32. AccountState,
  33. BorshSerialize,
  34. };
  35. use fixtures::{
  36. passthrough,
  37. pyth,
  38. };
  39. #[tokio::test]
  40. async fn test_migrate_works() -> Result<(), solitaire::ErrBox> {
  41. info!("Starting");
  42. // Programs
  43. let p2w_program_id = Pubkey::new_unique();
  44. let wh_fixture_program_id = Pubkey::new_unique();
  45. // Authorities
  46. let p2w_owner = Keypair::new();
  47. let pyth_owner = Pubkey::new_unique();
  48. // On-chain state
  49. let old_p2w_config = OldPyth2WormholeConfig {
  50. owner: p2w_owner.pubkey(),
  51. wh_prog: wh_fixture_program_id,
  52. max_batch_size: pyth2wormhole::attest::P2W_MAX_BATCH_SIZE,
  53. pyth_owner,
  54. is_active: true,
  55. };
  56. info!("Before ProgramTest::new()");
  57. // Populate test environment
  58. let mut p2w_test = ProgramTest::new(
  59. "pyth2wormhole",
  60. p2w_program_id,
  61. processor!(pyth2wormhole::instruction::solitaire),
  62. );
  63. // Plant filled config accounts
  64. let old_p2w_config_bytes = old_p2w_config.try_to_vec()?;
  65. let old_p2w_config_account = Account {
  66. lamports: Rent::default().minimum_balance(old_p2w_config_bytes.len()),
  67. data: old_p2w_config_bytes,
  68. owner: p2w_program_id,
  69. executable: false,
  70. rent_epoch: 0,
  71. };
  72. let old_p2w_config_addr = OldP2WConfigAccount::key(None, &p2w_program_id);
  73. info!("Before add_account() calls");
  74. p2w_test.add_account(old_p2w_config_addr, old_p2w_config_account);
  75. // Add system program because the contract creates an account for new configuration account
  76. passthrough::add_passthrough(&mut p2w_test, "system", system_program::id());
  77. info!("System program under {}", system_program::id());
  78. info!("Before start_with_context");
  79. let mut ctx = p2w_test.start_with_context().await;
  80. let migrate_tx =
  81. p2wc::gen_migrate_tx(ctx.payer, p2w_program_id, p2w_owner, ctx.last_blockhash)?;
  82. info!("Before process_transaction");
  83. // Migration should fail because the new config account is already initialized
  84. ctx.banks_client.process_transaction(migrate_tx).await?;
  85. Ok(())
  86. }
  87. #[tokio::test]
  88. async fn test_migrate_already_migrated() -> Result<(), solitaire::ErrBox> {
  89. info!("Starting");
  90. // Programs
  91. let p2w_program_id = Pubkey::new_unique();
  92. let wh_fixture_program_id = Pubkey::new_unique();
  93. // Authorities
  94. let p2w_owner = Keypair::new();
  95. let pyth_owner = Pubkey::new_unique();
  96. let ops_owner = Keypair::new();
  97. // On-chain state
  98. let old_p2w_config = OldPyth2WormholeConfig {
  99. owner: p2w_owner.pubkey(),
  100. wh_prog: wh_fixture_program_id,
  101. max_batch_size: pyth2wormhole::attest::P2W_MAX_BATCH_SIZE,
  102. pyth_owner,
  103. is_active: true,
  104. };
  105. let new_p2w_config = Pyth2WormholeConfig {
  106. owner: p2w_owner.pubkey(),
  107. wh_prog: wh_fixture_program_id,
  108. max_batch_size: pyth2wormhole::attest::P2W_MAX_BATCH_SIZE,
  109. pyth_owner,
  110. is_active: true,
  111. ops_owner: Some(ops_owner.pubkey()),
  112. };
  113. info!("Before ProgramTest::new()");
  114. // Populate test environment
  115. let mut p2w_test = ProgramTest::new(
  116. "pyth2wormhole",
  117. p2w_program_id,
  118. processor!(pyth2wormhole::instruction::solitaire),
  119. );
  120. // Plant filled config accounts
  121. let old_p2w_config_bytes = old_p2w_config.try_to_vec()?;
  122. let old_p2w_config_account = Account {
  123. lamports: Rent::default().minimum_balance(old_p2w_config_bytes.len()),
  124. data: old_p2w_config_bytes,
  125. owner: p2w_program_id,
  126. executable: false,
  127. rent_epoch: 0,
  128. };
  129. let old_p2w_config_addr = OldP2WConfigAccount::key(None, &p2w_program_id);
  130. let new_p2w_config_bytes = new_p2w_config.try_to_vec()?;
  131. let new_p2w_config_account = Account {
  132. lamports: Rent::default().minimum_balance(new_p2w_config_bytes.len()),
  133. data: new_p2w_config_bytes,
  134. owner: p2w_program_id,
  135. executable: false,
  136. rent_epoch: 0,
  137. };
  138. let new_p2w_config_addr =
  139. P2WConfigAccount::<{ AccountState::Initialized }>::key(None, &p2w_program_id);
  140. info!("Before add_account() calls");
  141. p2w_test.add_account(old_p2w_config_addr, old_p2w_config_account);
  142. p2w_test.add_account(new_p2w_config_addr, new_p2w_config_account);
  143. info!("Before start_with_context");
  144. let mut ctx = p2w_test.start_with_context().await;
  145. let migrate_tx =
  146. p2wc::gen_migrate_tx(ctx.payer, p2w_program_id, p2w_owner, ctx.last_blockhash)?;
  147. info!("Before process_transaction");
  148. // Migration should fail because the new config account is already initialized
  149. assert!(ctx
  150. .banks_client
  151. .process_transaction(migrate_tx)
  152. .await
  153. .is_err());
  154. Ok(())
  155. }