11use {
22 anchor_lang:: { prelude:: AccountMeta , InstructionData } ,
3- pyth_lazer_solana_contract:: ed25519_program_args,
3+ pyth_lazer_solana_contract:: { ed25519_program_args, ANCHOR_DISCRIMINATOR_BYTES } ,
44 solana_program_test:: { BanksClient , ProgramTest } ,
55 solana_sdk:: {
6- ed25519_program, hash:: Hash , instruction:: Instruction , pubkey:: Pubkey , signature:: Keypair ,
7- signer:: Signer , system_instruction, system_program, system_transaction, sysvar,
6+ account:: Account ,
7+ ed25519_program,
8+ hash:: Hash ,
9+ instruction:: Instruction ,
10+ pubkey:: { Pubkey , PUBKEY_BYTES } ,
11+ signature:: Keypair ,
12+ signer:: Signer ,
13+ system_instruction, system_program, system_transaction, sysvar,
814 transaction:: Transaction ,
915 } ,
1016 std:: env,
1117} ;
1218
19+ fn program_test ( ) -> ProgramTest {
20+ if env:: var ( "SBF_OUT_DIR" ) . is_err ( ) {
21+ env:: set_var (
22+ "SBF_OUT_DIR" ,
23+ format ! (
24+ "{}/../../../../target/sbf-solana-solana/release" ,
25+ env:: var( "CARGO_MANIFEST_DIR" ) . unwrap( )
26+ ) ,
27+ ) ;
28+ }
29+ println ! ( "if add_program fails, run `cargo build-sbf` first." ) ;
30+ ProgramTest :: new (
31+ "pyth_lazer_solana_contract" ,
32+ pyth_lazer_solana_contract:: ID ,
33+ None ,
34+ )
35+ }
36+
1337struct Setup {
1438 banks_client : BanksClient ,
1539 payer : Keypair ,
1640 recent_blockhash : Hash ,
1741}
1842
1943impl Setup {
20- async fn new ( ) -> Self {
21- if env:: var ( "SBF_OUT_DIR" ) . is_err ( ) {
22- env:: set_var (
23- "SBF_OUT_DIR" ,
24- format ! (
25- "{}/../../../../target/sbf-solana-solana/release" ,
26- env:: var( "CARGO_MANIFEST_DIR" ) . unwrap( )
27- ) ,
28- ) ;
29- }
30- println ! ( "if add_program fails, run `cargo build-sbf` first." ) ;
31- let program_test = ProgramTest :: new (
32- "pyth_lazer_solana_contract" ,
33- pyth_lazer_solana_contract:: ID ,
34- None ,
35- ) ;
44+ async fn with_program_test ( program_test : ProgramTest ) -> Self {
3645 let ( banks_client, payer, recent_blockhash) = program_test. start ( ) . await ;
3746 Self {
3847 banks_client,
@@ -41,6 +50,10 @@ impl Setup {
4150 }
4251 }
4352
53+ async fn new ( ) -> Self {
54+ Self :: with_program_test ( program_test ( ) ) . await
55+ }
56+
4457 async fn create_treasury ( & mut self ) -> Pubkey {
4558 let treasury =
4659 Pubkey :: create_with_seed ( & self . payer . pubkey ( ) , "treasury" , & system_program:: ID )
@@ -154,14 +167,14 @@ impl Setup {
154167}
155168
156169#[ tokio:: test]
157- async fn test_with_init_v2 ( ) {
170+ async fn test_basic ( ) {
158171 let mut setup = Setup :: new ( ) . await ;
159172 let treasury = setup. create_treasury ( ) . await ;
160173
161174 let mut transaction_init_contract = Transaction :: new_with_payer (
162175 & [ Instruction :: new_with_bytes (
163176 pyth_lazer_solana_contract:: ID ,
164- & pyth_lazer_solana_contract:: instruction:: InitializeV2 {
177+ & pyth_lazer_solana_contract:: instruction:: Initialize {
165178 top_authority : setup. payer . pubkey ( ) ,
166179 treasury,
167180 }
@@ -195,32 +208,33 @@ async fn test_with_init_v2() {
195208}
196209
197210#[ tokio:: test]
198- async fn test_with_init_v1_and_migrate ( ) {
199- let mut setup = Setup :: new ( ) . await ;
200- let treasury = setup. create_treasury ( ) . await ;
201-
202- let mut transaction_init_contract = Transaction :: new_with_payer (
203- & [ Instruction :: new_with_bytes (
204- pyth_lazer_solana_contract:: ID ,
205- & pyth_lazer_solana_contract:: instruction:: Initialize {
206- top_authority : setup. payer . pubkey ( ) ,
207- }
208- . data ( ) ,
209- vec ! [
210- AccountMeta :: new( setup. payer. pubkey( ) , true ) ,
211- AccountMeta :: new( pyth_lazer_solana_contract:: STORAGE_ID , false ) ,
212- AccountMeta :: new_readonly( system_program:: ID , false ) ,
213- ] ,
214- ) ] ,
215- Some ( & setup. payer . pubkey ( ) ) ,
211+ async fn test_migrate_from_0_1_0 ( ) {
212+ let mut program_test = program_test ( ) ;
213+ // Create a storage PDA account with the data that was produced by the program v0.1.0.
214+ let mut old_storage_data = hex:: decode (
215+ "d175ffb9c4af4409aa4dcb5d31150b162b664abd843cb231cee5c0ebf759ce371d9cb36ffc653796\
216+ 0174313a6525edf99936aa1477e94c72bc5cc617b21745f5f03296f3154461f214ffffffffffffff7\
217+ f00000000000000000000000000000000000000000000000000000000000000000000000000000000",
218+ )
219+ . unwrap ( ) ;
220+ let top_authority = Keypair :: new ( ) ;
221+ // Replace top authority pubkey in storage PDA data to allow successful migration.
222+ old_storage_data[ ANCHOR_DISCRIMINATOR_BYTES ..ANCHOR_DISCRIMINATOR_BYTES + PUBKEY_BYTES ]
223+ . copy_from_slice ( & top_authority. pubkey ( ) . to_bytes ( ) ) ;
224+ program_test. add_account (
225+ pyth_lazer_solana_contract:: STORAGE_ID ,
226+ Account {
227+ lamports : 1733040 ,
228+ data : old_storage_data,
229+ owner : pyth_lazer_solana_contract:: ID ,
230+ executable : false ,
231+ rent_epoch : 18446744073709551615 ,
232+ } ,
216233 ) ;
217- transaction_init_contract. sign ( & [ & setup. payer ] , setup. recent_blockhash ) ;
218- setup
219- . banks_client
220- . process_transaction ( transaction_init_contract)
221- . await
222- . unwrap ( ) ;
234+ let mut setup = Setup :: with_program_test ( program_test) . await ;
235+ let treasury = setup. create_treasury ( ) . await ;
223236
237+ // Make sure storage PDA will be rent-exempt after resize.
224238 let tx_transfer = system_transaction:: transfer (
225239 & setup. payer ,
226240 & pyth_lazer_solana_contract:: STORAGE_ID ,
@@ -236,31 +250,30 @@ async fn test_with_init_v1_and_migrate() {
236250 let mut transaction_migrate_contract = Transaction :: new_with_payer (
237251 & [ Instruction :: new_with_bytes (
238252 pyth_lazer_solana_contract:: ID ,
239- & pyth_lazer_solana_contract:: instruction:: MigrateToStorageV2 { treasury } . data ( ) ,
253+ & pyth_lazer_solana_contract:: instruction:: MigrateFrom010 { treasury } . data ( ) ,
240254 vec ! [
241- AccountMeta :: new( setup . payer . pubkey( ) , true ) ,
255+ AccountMeta :: new( top_authority . pubkey( ) , true ) ,
242256 AccountMeta :: new( pyth_lazer_solana_contract:: STORAGE_ID , false ) ,
243257 AccountMeta :: new_readonly( system_program:: ID , false ) ,
244258 ] ,
245259 ) ] ,
246260 Some ( & setup. payer . pubkey ( ) ) ,
247261 ) ;
248- transaction_migrate_contract. sign ( & [ & setup. payer ] , setup. recent_blockhash ) ;
262+ transaction_migrate_contract. sign ( & [ & setup. payer , & top_authority ] , setup. recent_blockhash ) ;
249263 setup
250264 . banks_client
251265 . process_transaction ( transaction_migrate_contract)
252266 . await
253267 . unwrap ( ) ;
254268
255- let verifying_key =
256- hex:: decode ( "74313a6525edf99936aa1477e94c72bc5cc617b21745f5f03296f3154461f214" ) . unwrap ( ) ;
257269 let message = hex:: decode (
258270 "b9011a82e5cddee2c1bd364c8c57e1c98a6a28d194afcad410ff412226c8b2ae931ff59a57147cb47c7307\
259271 afc2a0a1abec4dd7e835a5b7113cf5aeac13a745c6bed6c60074313a6525edf99936aa1477e94c72bc5cc61\
260272 7b21745f5f03296f3154461f2141c0075d3c7931c9773f30a240600010102000000010000e1f50500000000",
261273 )
262274 . unwrap ( ) ;
263275
264- setup. set_trusted ( verifying_key. try_into ( ) . unwrap ( ) ) . await ;
276+ // The contract will recognize the trusted signer without calling `set_trusted`
277+ // because it was present in the original storage PDA data.
265278 setup. verify_message ( & message, treasury) . await ;
266279}
0 commit comments