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