-
Notifications
You must be signed in to change notification settings - Fork 116
Command to delete a product #262
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::mem::{ | ||
size_of, | ||
size_of_val, | ||
}; | ||
|
||
use solana_program::pubkey::Pubkey; | ||
use solana_sdk::signer::Signer; | ||
|
||
use crate::c_oracle_header::{ | ||
pc_map_table_t, | ||
pc_pub_key_t, | ||
}; | ||
use crate::tests::pyth_simulator::PythSimulator; | ||
use crate::utils::pubkey_equal; | ||
|
||
#[tokio::test] | ||
async fn test_del_product() { | ||
let mut sim = PythSimulator::new().await; | ||
let mapping_keypair = sim.init_mapping().await.unwrap(); | ||
let product1 = sim.add_product(&mapping_keypair).await.unwrap(); | ||
let product2 = sim.add_product(&mapping_keypair).await.unwrap(); | ||
let product3 = sim.add_product(&mapping_keypair).await.unwrap(); | ||
let product4 = sim.add_product(&mapping_keypair).await.unwrap(); | ||
let product5 = sim.add_product(&mapping_keypair).await.unwrap(); | ||
let _price3 = sim.add_price(&product3, -8).await.unwrap(); | ||
|
||
let mapping_keypair2 = sim.init_mapping().await.unwrap(); | ||
let product1_2 = sim.add_product(&mapping_keypair2).await.unwrap(); | ||
|
||
assert!(sim.get_account(product2.pubkey()).await.is_some()); | ||
assert!(sim.get_account(product4.pubkey()).await.is_some()); | ||
|
||
// Can't delete a product with a price account | ||
assert!(sim.del_product(&mapping_keypair, &product3).await.is_err()); | ||
// Can't delete mismatched product/mapping accounts | ||
assert!(sim | ||
.del_product(&mapping_keypair, &product1_2) | ||
.await | ||
.is_err()); | ||
|
||
assert!(sim.del_product(&mapping_keypair, &product2).await.is_ok()); | ||
|
||
assert!(sim.get_account(product2.pubkey()).await.is_none()); | ||
|
||
let mapping_data = sim | ||
.get_account_data_as::<pc_map_table_t>(mapping_keypair.pubkey()) | ||
.await | ||
.unwrap(); | ||
assert!(mapping_product_list_equals( | ||
&mapping_data, | ||
vec![ | ||
product1.pubkey(), | ||
product5.pubkey(), | ||
product3.pubkey(), | ||
product4.pubkey() | ||
] | ||
)); | ||
assert!(sim.get_account(product5.pubkey()).await.is_some()); | ||
|
||
|
||
assert!(sim.del_product(&mapping_keypair, &product4).await.is_ok()); | ||
let mapping_data = sim | ||
.get_account_data_as::<pc_map_table_t>(mapping_keypair.pubkey()) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(mapping_product_list_equals( | ||
&mapping_data, | ||
vec![product1.pubkey(), product5.pubkey(), product3.pubkey()] | ||
)); | ||
} | ||
|
||
/// Returns true if the list of products in `mapping_data` contains the keys in `expected` (in the | ||
/// same order). Also checks `mapping_data.num_` and `size_`. | ||
fn mapping_product_list_equals(mapping_data: &pc_map_table_t, expected: Vec<Pubkey>) -> bool { | ||
if mapping_data.num_ != expected.len() as u32 { | ||
return false; | ||
} | ||
|
||
let expected_size = (size_of::<pc_map_table_t>() - size_of_val(&mapping_data.prod_) | ||
+ expected.len() * size_of::<pc_pub_key_t>()) as u32; | ||
if mapping_data.size_ != expected_size { | ||
return false; | ||
} | ||
|
||
for i in 0..expected.len() { | ||
if !pubkey_equal(&mapping_data.prod_[i], &expected[i].to_bytes()) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Rather than assert, can we used
.checked_sub
on the subtractions below? We can (and should) enable clippy’s arithmetic lint which will be safer. We should really be using checked arithmetic for everything on-chain.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.
good call on the arithmetic checks. will enable in a separate pr.