Skip to content

Conversation

@swimricky
Copy link
Contributor

Summary

Add integration tests using solana-program-test crate, add rust-toolchain.toml to pin rust version, add integration tests, update cpi caller auth seeds

@vercel
Copy link

vercel bot commented May 2, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
example-oracle-amm ⬜️ Ignored (Inspect) May 8, 2023 6:08pm
xc-admin-frontend ⬜️ Ignored (Inspect) May 8, 2023 6:08pm

@swimricky swimricky force-pushed the message-buffer/integration-tests branch from c623910 to cc2e14b Compare May 2, 2023 19:32
@swimricky swimricky requested a review from jayantk May 3, 2023 13:22
@swimricky swimricky marked this pull request as ready for review May 3, 2023 13:22
@swimricky
Copy link
Contributor Author

@jayantk there's a few tests that are just stubbed out for now but wanted to get this first set checked in since there were a few minor fixes and also to see if there's anything more urgent to work on first

Copy link
Contributor

@jayantk jayantk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is going in the right direction, though i'm having a hard time understanding what's actually being tested in each set of tests because there's a lot of repeated code. Can you please do the inline suggestion to reduce repetition, and then maybe leave a comment on any tests where the logic isn't immediately apparent to the reader? I'll then go back through and think about the test cases in detail.

ctx.accounts.message_buffer_program.key().as_ref(),
CPI.as_bytes(),
// ctx.accounts.message_buffer_program.key().as_ref(),
// UPD_PRICE_WRITE.as_bytes(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you delete this?


pub const ACCUMULATOR_UPDATER_IX_NAME: &str = "put_all";
pub const CPI: &str = "cpi";
pub const UPD_PRICE_WRITE: &str = "upd_price_write";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason you changed the PDA derivation path? I think there's no reason that the test needs to use exactly the same derivation path as the production program, right?

send_set_allowed_programs(banks_client, payer, &whitelist_pda, &allowed_programs)
.await
.unwrap();
let pyth_price_acct_id = 0u64;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you've copy-pasted a bunch of the lines that set up the tests. This is bad for two reasons: (1) if you ever need to change the code, you have to change it in a lot of places, and (2) it's hard for the reader to understand what's actually being tested in each test.

I suggest making a test harness or something to encapsulate this logic so it isn't repeated. You can take a look at https://github.com/pyth-network/pyth-client/blob/main/program/rust/src/tests/pyth_simulator.rs which is our test harness for the oracle program. I think that's worked reasonably well.

You already have some helper methods to reduce repetition, but I think you can go even further by doing two things:

  1. Make a function that deploys the message buffer program in a default setup. That function would combine this sequence of instructions here to get you a reasonably-initialized message buffer program.
  2. Make a struct to hold the common addresses and such, and then make the helper functions methods on that struct.


ctx.accounts
.whitelist
.is_allowed_program_auth(&allowed_program_auth)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR: but now that the message buffer account is only 1, should we move it to the Anchor context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there were some issues with using the anchor context since our message buffer is dynaimcally sized but also needs to support zero_copy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guibescos i was wrong about this. I was confusing the issues from working with an older implementation. I made a new PR to address this

.realloc(target_size, false)
.map_err(|_| MessageBufferError::ReallocFailed)?;
} else {
// Check that account doesn't get resized to smaller than the amount of
Copy link
Contributor

@guibescos guibescos May 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated : why would we want to ever make a MessageBuffer account smaller?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't have a specific use-case other than if we upgraded to something in the future where we didn't need all the space that was currently allocated, then we wouldn't have to pull empty bytes for each MessageBuffer

pub bump: u8,
pub admin: Pubkey,
// This is used by the `#[derive(InitSpace)]`
// to determine initial account size
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. I didn't know this feature.

Comment on lines 595 to 626
pub fn verify_full_price_msg(
id: u64,
price: u64,
price_expo: u64,
ema: u64,
ema_expo: u64,
msg: &[u8],
) -> Result<()> {
let mut cursor = Cursor::new(&msg[7..]);
let msg_id = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(id, msg_id);
let msg_price = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(price, msg_price);
let msg_price_expo = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(price_expo, msg_price_expo);
let msg_ema = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(ema, msg_ema);
let msg_ema_expo = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(ema_expo, msg_ema_expo);
Ok(())
}

pub fn verify_compact_price_msg(id: u64, price: u64, price_expo: u64, msg: &[u8]) -> Result<()> {
let mut cursor = Cursor::new(&msg[7..]);
let msg_id = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(id, msg_id);
let msg_price = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(price, msg_price);
let msg_price_expo = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(price_expo, msg_price_expo);
Ok(())
}
Copy link
Collaborator

@ali-behjati ali-behjati May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good if we can remove them (otherwise we need to always make sure that it is correct itself). I don't have the full context on the code. Is there any reason that we are manually ser/de ing on the mock contract? I think we can use borsh (and it will make the code simpler)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Reisen was working on a serializer/deserializer for the pythnet sdk. This was just to have some code to work off of that didn't rely on using the anchor client libraries

@swimricky swimricky force-pushed the message-buffer/integration-tests branch from 7adc4db to bacdc1b Compare May 8, 2023 18:06
#[tokio::test]
#[should_panic]
async fn fail_delete_buffer_invalid_admin() {
panic!()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are this placeholders for future tests you'll write?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@swimricky swimricky merged commit 2e32a22 into main May 9, 2023
@swimricky swimricky deleted the message-buffer/integration-tests branch May 9, 2023 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants