|
3 | 3 | use std::str::FromStr;
|
4 | 4 |
|
5 | 5 | use account_compression::{
|
6 |
| - self, errors::AccountCompressionErrorCode, utils::constants::GROUP_AUTHORITY_SEED, |
7 |
| - GroupAuthority, RegisteredProgram, ID, |
| 6 | + self, |
| 7 | + errors::AccountCompressionErrorCode, |
| 8 | + utils::constants::{CPI_AUTHORITY_PDA_SEED, GROUP_AUTHORITY_SEED}, |
| 9 | + GroupAuthority, RegisteredProgram, RegisteredProgramV1, ID, |
8 | 10 | };
|
9 |
| -use anchor_lang::{system_program, InstructionData, ToAccountMetas}; |
| 11 | +use anchor_lang::{system_program, AnchorDeserialize, InstructionData, ToAccountMetas}; |
10 | 12 | use light_program_test::{
|
| 13 | + env_accounts_v1::get_registered_program_pda, |
11 | 14 | test_env::{get_group_pda, OLD_SYSTEM_PROGRAM_ID_TEST_KEYPAIR},
|
12 | 15 | test_rpc::ProgramTestRpcConnection,
|
13 | 16 | };
|
@@ -331,3 +334,164 @@ async fn test_create_and_update_group() {
|
331 | 334 | assert_eq!(recipient_balance, rent_exemption);
|
332 | 335 | }
|
333 | 336 | }
|
| 337 | + |
| 338 | +#[tokio::test] |
| 339 | +async fn test_resize_registered_program_pda() { |
| 340 | + let mut program_test = ProgramTest::default(); |
| 341 | + program_test.add_program("account_compression", ID, None); |
| 342 | + let system_program_id = |
| 343 | + Pubkey::from_str("SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7").unwrap(); |
| 344 | + let registered_program = Pubkey::find_program_address( |
| 345 | + &[system_program_id.to_bytes().as_slice()], |
| 346 | + &account_compression::ID, |
| 347 | + ) |
| 348 | + .0; |
| 349 | + program_test.add_account(registered_program, get_registered_program_pda()); |
| 350 | + program_test.set_compute_max_units(1_400_000u64); |
| 351 | + let context = program_test.start_with_context().await; |
| 352 | + let mut context = ProgramTestRpcConnection::new(context); |
| 353 | + let payer = context.get_payer().insecure_clone(); |
| 354 | + |
| 355 | + let instruction_data = account_compression::instruction::ResizeRegisteredProgramPda {}; |
| 356 | + let accounts = account_compression::accounts::ResizeRegisteredProgramPda { |
| 357 | + authority: context.get_payer().pubkey(), |
| 358 | + registered_program_pda: registered_program, |
| 359 | + system_program: Pubkey::default(), |
| 360 | + }; |
| 361 | + let instruction = Instruction { |
| 362 | + program_id: account_compression::ID, |
| 363 | + accounts: accounts.to_account_metas(Some(true)), |
| 364 | + data: instruction_data.data(), |
| 365 | + }; |
| 366 | + // Resize |
| 367 | + { |
| 368 | + let pre_account = context |
| 369 | + .get_account(registered_program) |
| 370 | + .await |
| 371 | + .unwrap() |
| 372 | + .unwrap(); |
| 373 | + assert_eq!(pre_account.data.len(), RegisteredProgramV1::LEN); |
| 374 | + let account_data = RegisteredProgramV1::deserialize(&mut &pre_account.data[8..]).unwrap(); |
| 375 | + println!("account_data: {:?}", account_data); |
| 376 | + let mut transaction = |
| 377 | + Transaction::new_with_payer(&[instruction.clone()], Some(&payer.pubkey())); |
| 378 | + let recent_blockhash = context.get_latest_blockhash().await.unwrap(); |
| 379 | + transaction.sign(&[&payer], recent_blockhash); |
| 380 | + context.process_transaction(transaction).await.unwrap(); |
| 381 | + |
| 382 | + let account = context |
| 383 | + .get_account(registered_program) |
| 384 | + .await |
| 385 | + .unwrap() |
| 386 | + .unwrap(); |
| 387 | + assert_eq!(account.data.len(), RegisteredProgram::LEN); |
| 388 | + let expected_registered_program = RegisteredProgram { |
| 389 | + registered_program_id: system_program_id, |
| 390 | + group_authority_pda: account_data.group_authority_pda, |
| 391 | + registered_program_signer_pda: Pubkey::find_program_address( |
| 392 | + &[CPI_AUTHORITY_PDA_SEED], |
| 393 | + &system_program_id, |
| 394 | + ) |
| 395 | + .0, |
| 396 | + }; |
| 397 | + let account_des = RegisteredProgram::deserialize(&mut &account.data[8..]).unwrap(); |
| 398 | + assert_eq!(expected_registered_program, account_des); |
| 399 | + } |
| 400 | + // Resize again should fail. |
| 401 | + { |
| 402 | + let mut transaction = |
| 403 | + Transaction::new_with_payer(&[instruction.clone()], Some(&payer.pubkey())); |
| 404 | + let recent_blockhash = context.get_latest_blockhash().await.unwrap(); |
| 405 | + transaction.sign(&[&payer], recent_blockhash); |
| 406 | + let result = context.process_transaction(transaction).await; |
| 407 | + assert_rpc_error( |
| 408 | + result, |
| 409 | + 0, |
| 410 | + anchor_lang::error::ErrorCode::ConstraintRaw.into(), |
| 411 | + ) |
| 412 | + .unwrap(); |
| 413 | + } |
| 414 | + |
| 415 | + // Invalid program owner. |
| 416 | + { |
| 417 | + let mut account = get_registered_program_pda(); |
| 418 | + account.owner = Pubkey::new_unique(); |
| 419 | + let mut program_test = ProgramTest::default(); |
| 420 | + program_test.add_program("account_compression", ID, None); |
| 421 | + let system_program_id = |
| 422 | + Pubkey::from_str("SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7").unwrap(); |
| 423 | + let registered_program = Pubkey::find_program_address( |
| 424 | + &[system_program_id.to_bytes().as_slice()], |
| 425 | + &account_compression::ID, |
| 426 | + ) |
| 427 | + .0; |
| 428 | + program_test.add_account(registered_program, account); |
| 429 | + program_test.set_compute_max_units(1_400_000u64); |
| 430 | + let context = program_test.start_with_context().await; |
| 431 | + let mut context = ProgramTestRpcConnection::new(context); |
| 432 | + let payer = context.get_payer().insecure_clone(); |
| 433 | + |
| 434 | + let instruction_data = account_compression::instruction::ResizeRegisteredProgramPda {}; |
| 435 | + let accounts = account_compression::accounts::ResizeRegisteredProgramPda { |
| 436 | + authority: context.get_payer().pubkey(), |
| 437 | + registered_program_pda: registered_program, |
| 438 | + system_program: Pubkey::default(), |
| 439 | + }; |
| 440 | + let instruction = Instruction { |
| 441 | + program_id: account_compression::ID, |
| 442 | + accounts: accounts.to_account_metas(Some(true)), |
| 443 | + data: instruction_data.data(), |
| 444 | + }; |
| 445 | + let mut transaction = |
| 446 | + Transaction::new_with_payer(&[instruction.clone()], Some(&payer.pubkey())); |
| 447 | + let recent_blockhash = context.get_latest_blockhash().await.unwrap(); |
| 448 | + transaction.sign(&[&payer], recent_blockhash); |
| 449 | + let result = context.process_transaction(transaction).await; |
| 450 | + assert_rpc_error( |
| 451 | + result, |
| 452 | + 0, |
| 453 | + light_account_checks::error::AccountError::AccountOwnedByWrongProgram.into(), |
| 454 | + ) |
| 455 | + .unwrap(); |
| 456 | + } |
| 457 | + // Invalid account discriminator. |
| 458 | + { |
| 459 | + let mut account = get_registered_program_pda(); |
| 460 | + account.data[0..8].copy_from_slice(&[1u8; 8]); |
| 461 | + let mut program_test = ProgramTest::default(); |
| 462 | + program_test.add_program("account_compression", ID, None); |
| 463 | + let system_program_id = |
| 464 | + Pubkey::from_str("SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7").unwrap(); |
| 465 | + let registered_program = Pubkey::find_program_address( |
| 466 | + &[system_program_id.to_bytes().as_slice()], |
| 467 | + &account_compression::ID, |
| 468 | + ) |
| 469 | + .0; |
| 470 | + program_test.add_account(registered_program, account); |
| 471 | + program_test.set_compute_max_units(1_400_000u64); |
| 472 | + let context = program_test.start_with_context().await; |
| 473 | + let mut context = ProgramTestRpcConnection::new(context); |
| 474 | + let payer = context.get_payer().insecure_clone(); |
| 475 | + let instruction_data = account_compression::instruction::ResizeRegisteredProgramPda {}; |
| 476 | + let accounts = account_compression::accounts::ResizeRegisteredProgramPda { |
| 477 | + authority: context.get_payer().pubkey(), |
| 478 | + registered_program_pda: registered_program, |
| 479 | + system_program: Pubkey::default(), |
| 480 | + }; |
| 481 | + let instruction = Instruction { |
| 482 | + program_id: account_compression::ID, |
| 483 | + accounts: accounts.to_account_metas(Some(true)), |
| 484 | + data: instruction_data.data(), |
| 485 | + }; |
| 486 | + let mut transaction = Transaction::new_with_payer(&[instruction], Some(&payer.pubkey())); |
| 487 | + let recent_blockhash = context.get_latest_blockhash().await.unwrap(); |
| 488 | + transaction.sign(&[&payer], recent_blockhash); |
| 489 | + let result = context.process_transaction(transaction).await; |
| 490 | + assert_rpc_error( |
| 491 | + result, |
| 492 | + 0, |
| 493 | + anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch.into(), |
| 494 | + ) |
| 495 | + .unwrap(); |
| 496 | + } |
| 497 | +} |
0 commit comments