test_migrate.rs 5.2 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. P2WConfigAccount,
  26. OldPyth2WormholeConfig,
  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 {owner: p2w_owner.pubkey(),
  50. wh_prog: wh_fixture_program_id,
  51. max_batch_size: pyth2wormhole::attest::P2W_MAX_BATCH_SIZE,
  52. pyth_owner,
  53. };
  54. info!("Before ProgramTest::new()");
  55. // Populate test environment
  56. let mut p2w_test = ProgramTest::new(
  57. "pyth2wormhole",
  58. p2w_program_id,
  59. processor!(pyth2wormhole::instruction::solitaire),
  60. );
  61. // Plant filled config accounts
  62. let old_p2w_config_bytes = old_p2w_config.try_to_vec()?;
  63. let old_p2w_config_account = Account {
  64. lamports: Rent::default().minimum_balance(old_p2w_config_bytes.len()),
  65. data: old_p2w_config_bytes,
  66. owner: p2w_program_id,
  67. executable: false,
  68. rent_epoch: 0,
  69. };
  70. let old_p2w_config_addr =
  71. OldP2WConfigAccount::key(None, &p2w_program_id);
  72. info!("Before add_account() calls");
  73. p2w_test.add_account(old_p2w_config_addr, old_p2w_config_account);
  74. // Add system program because the contract creates an account for new configuration account
  75. passthrough::add_passthrough(&mut p2w_test, "system", system_program::id());
  76. info!("System program under {}", system_program::id());
  77. info!("Before start_with_context");
  78. let mut ctx = p2w_test.start_with_context().await;
  79. let migrate_tx = p2wc::gen_migrate_tx(
  80. ctx.payer,
  81. p2w_program_id,
  82. p2w_owner,
  83. ctx.last_blockhash,
  84. )?;
  85. info!("Before process_transaction");
  86. // Migration should fail because the new config account is already initialized
  87. ctx.banks_client.process_transaction(migrate_tx).await?;
  88. Ok(())
  89. }
  90. #[tokio::test]
  91. async fn test_migrate_already_migrated() -> Result<(), solitaire::ErrBox> {
  92. info!("Starting");
  93. // Programs
  94. let p2w_program_id = Pubkey::new_unique();
  95. let wh_fixture_program_id = Pubkey::new_unique();
  96. // Authorities
  97. let p2w_owner = Keypair::new();
  98. let pyth_owner = Pubkey::new_unique();
  99. // On-chain state
  100. let old_p2w_config = OldPyth2WormholeConfig {owner: p2w_owner.pubkey(),
  101. wh_prog: wh_fixture_program_id,
  102. max_batch_size: pyth2wormhole::attest::P2W_MAX_BATCH_SIZE,
  103. pyth_owner,
  104. };
  105. let new_p2w_config = Pyth2WormholeConfig {owner: p2w_owner.pubkey(),
  106. wh_prog: wh_fixture_program_id,
  107. max_batch_size: pyth2wormhole::attest::P2W_MAX_BATCH_SIZE,
  108. pyth_owner,
  109. is_active: true,
  110. };
  111. info!("Before ProgramTest::new()");
  112. // Populate test environment
  113. let mut p2w_test = ProgramTest::new(
  114. "pyth2wormhole",
  115. p2w_program_id,
  116. processor!(pyth2wormhole::instruction::solitaire),
  117. );
  118. // Plant filled config accounts
  119. let old_p2w_config_bytes = old_p2w_config.try_to_vec()?;
  120. let old_p2w_config_account = Account {
  121. lamports: Rent::default().minimum_balance(old_p2w_config_bytes.len()),
  122. data: old_p2w_config_bytes,
  123. owner: p2w_program_id,
  124. executable: false,
  125. rent_epoch: 0,
  126. };
  127. let old_p2w_config_addr =
  128. OldP2WConfigAccount::key(None, &p2w_program_id);
  129. let new_p2w_config_bytes = new_p2w_config.try_to_vec()?;
  130. let new_p2w_config_account = Account {
  131. lamports: Rent::default().minimum_balance(new_p2w_config_bytes.len()),
  132. data: new_p2w_config_bytes,
  133. owner: p2w_program_id,
  134. executable: false,
  135. rent_epoch: 0,
  136. };
  137. let new_p2w_config_addr =
  138. P2WConfigAccount::<{AccountState::Initialized}>::key(None, &p2w_program_id);
  139. info!("Before add_account() calls");
  140. p2w_test.add_account(old_p2w_config_addr, old_p2w_config_account);
  141. p2w_test.add_account(new_p2w_config_addr, new_p2w_config_account);
  142. info!("Before start_with_context");
  143. let mut ctx = p2w_test.start_with_context().await;
  144. let migrate_tx = p2wc::gen_migrate_tx(
  145. ctx.payer,
  146. p2w_program_id,
  147. p2w_owner,
  148. ctx.last_blockhash,
  149. )?;
  150. info!("Before process_transaction");
  151. // Migration should fail because the new config account is already initialized
  152. assert!(ctx.banks_client.process_transaction(migrate_tx).await.is_err());
  153. Ok(())
  154. }