test_migrate.rs 5.2 KB

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