test_migrate.rs 5.3 KB

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