-
Notifications
You must be signed in to change notification settings - Fork 308
[message-buffer 13/x] Rust Integration Tests #794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎ |
c623910 to
cc2e14b
Compare
|
@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 |
jayantk
left a comment
There was a problem hiding this 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(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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:
- 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.
- 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)?; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
message_buffer/programs/mock-cpi-caller/tests/cases/test_create_msg_buffer.rs
Outdated
Show resolved
Hide resolved
| 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(()) | ||
| } |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
add rust-toolchain.toml to pin rust version, add integration tests, update cpi caller auth seeds
…stContext for less duplication
7adc4db to
bacdc1b
Compare
pythnet/message_buffer/programs/mock-cpi-caller/tests/program_test/mod.rs
Show resolved
Hide resolved
| #[tokio::test] | ||
| #[should_panic] | ||
| async fn fail_delete_buffer_invalid_admin() { | ||
| panic!() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
Summary
Add integration tests using
solana-program-testcrate, add rust-toolchain.toml to pin rust version, add integration tests, update cpi caller auth seeds