test_attest.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. pub mod fixtures;
  2. use solana_program_test::*;
  3. use solana_sdk::{
  4. account::Account,
  5. instruction::{
  6. AccountMeta,
  7. Instruction,
  8. },
  9. pubkey::Pubkey,
  10. rent::Rent,
  11. signature::Signer,
  12. signer::keypair::Keypair,
  13. transaction::Transaction,
  14. };
  15. use bridge::accounts::{
  16. Bridge,
  17. BridgeConfig,
  18. BridgeData,
  19. };
  20. use pyth2wormhole::config::{
  21. P2WConfigAccount,
  22. Pyth2WormholeConfig,
  23. };
  24. use pyth2wormhole_client as p2wc;
  25. use solitaire::{
  26. processors::seeded::Seeded,
  27. AccountState,
  28. BorshSerialize,
  29. };
  30. use std::time::Duration;
  31. use fixtures::{
  32. passthrough,
  33. pyth,
  34. };
  35. #[tokio::test]
  36. async fn test_happy_path() -> Result<(), p2wc::ErrBoxSend> {
  37. // Programs
  38. let p2w_program_id = Pubkey::new_unique();
  39. let wh_fixture_program_id = Pubkey::new_unique();
  40. // Authorities
  41. let p2w_owner = Pubkey::new_unique();
  42. let pyth_owner = Pubkey::new_unique();
  43. let ops_owner = Pubkey::new_unique();
  44. // On-chain state
  45. let p2w_config = Pyth2WormholeConfig {
  46. owner: p2w_owner,
  47. wh_prog: wh_fixture_program_id,
  48. max_batch_size: pyth2wormhole::attest::P2W_MAX_BATCH_SIZE,
  49. pyth_owner,
  50. is_active: true,
  51. ops_owner: Some(ops_owner),
  52. };
  53. let bridge_config = BridgeData {
  54. config: BridgeConfig {
  55. fee: 0xdeadbeef,
  56. ..Default::default()
  57. },
  58. ..Default::default()
  59. };
  60. // Populate test environment
  61. let mut p2w_test = ProgramTest::new(
  62. "pyth2wormhole",
  63. p2w_program_id,
  64. processor!(pyth2wormhole::instruction::solitaire),
  65. );
  66. // Plant a filled config account
  67. let p2w_config_bytes = p2w_config.try_to_vec()?;
  68. let p2w_config_account = Account {
  69. lamports: Rent::default().minimum_balance(p2w_config_bytes.len()),
  70. data: p2w_config_bytes,
  71. owner: p2w_program_id,
  72. executable: false,
  73. rent_epoch: 0,
  74. };
  75. let p2w_config_addr =
  76. P2WConfigAccount::<{ AccountState::Initialized }>::key(None, &p2w_program_id);
  77. p2w_test.add_account(p2w_config_addr, p2w_config_account);
  78. // Plant a bridge config
  79. let bridge_config_bytes = bridge_config.try_to_vec()?;
  80. let wh_bridge_config_account = Account {
  81. lamports: Rent::default().minimum_balance(bridge_config_bytes.len()),
  82. data: bridge_config_bytes,
  83. owner: wh_fixture_program_id,
  84. executable: false,
  85. rent_epoch: 0,
  86. };
  87. let wh_bridge_config_addr =
  88. Bridge::<{ AccountState::Initialized }>::key(None, &wh_fixture_program_id);
  89. p2w_test.add_account(wh_bridge_config_addr, wh_bridge_config_account);
  90. passthrough::add_passthrough(&mut p2w_test, "wormhole", wh_fixture_program_id);
  91. let (prod_id, price_id) = pyth::add_test_symbol(&mut p2w_test, &pyth_owner);
  92. let mut ctx = p2w_test.start_with_context().await;
  93. let symbols = vec![p2wc::P2WSymbol {
  94. name: Some("Mock symbol".to_owned()),
  95. product_addr: prod_id,
  96. price_addr: price_id,
  97. }];
  98. let attest_tx = p2wc::gen_attest_tx(
  99. p2w_program_id,
  100. &p2w_config,
  101. &ctx.payer,
  102. 0,
  103. symbols.as_slice(),
  104. ctx.last_blockhash,
  105. )?;
  106. // NOTE: 2022-09-05
  107. // Execution of this transaction is commented out as for some unknown reasons
  108. // Solana test suite has some unknown behavior in this transaction. It is probably a
  109. // memory leak that causes either segfault or an invalid error (after a reading an unkown
  110. // variable from memory). It is probably solved in the following PR:
  111. // https://github.com/solana-labs/solana/pull/26507
  112. //
  113. // TODO: add this check when the above PR is released in our Solana package.
  114. // ctx.banks_client.process_transaction(attest_tx).await?;
  115. Ok(())
  116. }