-
Notifications
You must be signed in to change notification settings - Fork 14
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
fuzz: refactor fixture processing harnesses to use &mut self
#77
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d349318
make fixture methods mut self
buffalojoec 46e5b40
refactor fixture APIs
buffalojoec e88fcee
make run_checks pub
buffalojoec a264bb7
remove unused checks arg
buffalojoec cb51af4
add fixture partial validation API
buffalojoec 4b59910
add helpers
buffalojoec 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 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,218 @@ | ||
//! Checks to run against a fixture when validating. | ||
|
||
use { | ||
crate::result::{Check, InstructionResult}, | ||
solana_sdk::{ | ||
account::{Account, ReadableAccount}, | ||
pubkey::Pubkey, | ||
}, | ||
}; | ||
|
||
/// Checks to run against a fixture when validating. | ||
/// | ||
/// Similar to Mollusk's `result::Check`, this allows a developer to dictate | ||
/// the type of checks to run on the fixture's effects. | ||
/// | ||
/// Keep in mind that validation of fixtures works slightly differently than | ||
/// typical Mollusk unit tests. In a Mollusk test, you can provide the value to | ||
/// compare a portion of the result against (ie. compute units). However, when | ||
/// comparing the result of a Mollusk invocation against a fixture, the value | ||
/// from the fixture itself is used. | ||
/// | ||
/// For that reason, these are unary variants, and do not offer the developer a | ||
/// way to provide values to check against. | ||
pub enum FixtureCheck { | ||
/// Validate compute units consumed. | ||
ComputeUnits, | ||
/// Validate the program result. | ||
ProgramResult, | ||
/// Validate the return data. | ||
ReturnData, | ||
/// Validate all resulting accounts. | ||
AllResultingAccounts { | ||
/// Whether or not to validate each account's data. | ||
data: bool, | ||
/// Whether or not to validate each account's lamports. | ||
lamports: bool, | ||
/// Whether or not to validate each account's owner. | ||
owner: bool, | ||
/// Whether or not to validate each account's space. | ||
space: bool, | ||
}, | ||
/// Validate the resulting accounts at certain addresses. | ||
OnlyResultingAccounts { | ||
/// The addresses on which to apply the validation. | ||
addresses: Vec<Pubkey>, | ||
/// Whether or not to validate each account's data. | ||
data: bool, | ||
/// Whether or not to validate each account's lamports. | ||
lamports: bool, | ||
/// Whether or not to validate each account's owner. | ||
owner: bool, | ||
/// Whether or not to validate each account's space. | ||
space: bool, | ||
}, | ||
/// Validate all of the resulting accounts _except_ the provided addresses. | ||
AllResultingAccountsExcept { | ||
/// The addresses on which to _not_ apply the validation. | ||
ignore_addresses: Vec<Pubkey>, | ||
/// On non-ignored accounts, whether or not to validate each account's | ||
/// data. | ||
data: bool, | ||
/// On non-ignored accounts, whether or not to validate each account's | ||
/// lamports. | ||
lamports: bool, | ||
/// On non-ignored accounts, whether or not to validate each account's | ||
/// owner. | ||
owner: bool, | ||
/// On non-ignored accounts, whether or not to validate each account's | ||
/// space. | ||
space: bool, | ||
}, | ||
} | ||
|
||
impl FixtureCheck { | ||
/// Validate all possible checks for all resulting accounts. | ||
/// | ||
/// Note: To omit certain checks, use the variant directly, ie. | ||
/// `FixtureCheck::AllResultingAccounts { data: false, .. }`. | ||
pub fn all_resulting_accounts() -> Self { | ||
Self::AllResultingAccounts { | ||
data: true, | ||
lamports: true, | ||
owner: true, | ||
space: true, | ||
} | ||
} | ||
|
||
/// Validate all possible checks for only the resulting accounts at certain | ||
/// addresses. | ||
/// | ||
/// Note: To omit certain checks, use the variant directly, ie. | ||
/// `FixtureCheck::OnlyResultingAccounts { data: false, .. }`. | ||
pub fn only_resulting_accounts(addresses: &[Pubkey]) -> Self { | ||
Self::OnlyResultingAccounts { | ||
addresses: addresses.to_vec(), | ||
data: true, | ||
lamports: true, | ||
owner: true, | ||
space: true, | ||
} | ||
} | ||
|
||
/// Validate all possible checks for all of the resulting accounts _except_ | ||
/// the provided addresses. | ||
/// | ||
/// Note: To omit certain checks, use the variant directly, ie. | ||
/// `FixtureCheck::AllResultingAccountsExcept { data: false, .. }`. | ||
pub fn all_resulting_accounts_except(ignore_addresses: &[Pubkey]) -> Self { | ||
Self::AllResultingAccountsExcept { | ||
ignore_addresses: ignore_addresses.to_vec(), | ||
data: true, | ||
lamports: true, | ||
owner: true, | ||
space: true, | ||
} | ||
} | ||
} | ||
|
||
fn add_account_checks<'a>( | ||
checks: &mut Vec<Check<'a>>, | ||
accounts: impl Iterator<Item = &'a (Pubkey, Account)>, | ||
data: bool, | ||
lamports: bool, | ||
owner: bool, | ||
space: bool, | ||
) { | ||
for (pubkey, account) in accounts { | ||
let mut builder = Check::account(pubkey); | ||
if data { | ||
builder = builder.data(account.data()); | ||
} | ||
if lamports { | ||
builder = builder.lamports(account.lamports()); | ||
} | ||
if owner { | ||
builder = builder.owner(account.owner()); | ||
} | ||
if space { | ||
builder = builder.space(account.data().len()); | ||
} | ||
checks.push(builder.build()); | ||
} | ||
} | ||
|
||
pub(crate) fn evaluate_results_with_fixture_checks( | ||
expected: &InstructionResult, | ||
result: &InstructionResult, | ||
fixture_checks: &[FixtureCheck], | ||
) { | ||
let mut checks = vec![]; | ||
|
||
for fixture_check in fixture_checks { | ||
match fixture_check { | ||
FixtureCheck::ComputeUnits => { | ||
checks.push(Check::compute_units(expected.compute_units_consumed)) | ||
} | ||
FixtureCheck::ProgramResult => { | ||
checks.push(Check::program_result(expected.program_result.clone())) | ||
} | ||
FixtureCheck::ReturnData => checks.push(Check::return_data(&expected.return_data)), | ||
FixtureCheck::AllResultingAccounts { | ||
data, | ||
lamports, | ||
owner, | ||
space, | ||
} => { | ||
add_account_checks( | ||
&mut checks, | ||
expected.resulting_accounts.iter(), | ||
*data, | ||
*lamports, | ||
*owner, | ||
*space, | ||
); | ||
} | ||
FixtureCheck::OnlyResultingAccounts { | ||
addresses, | ||
data, | ||
lamports, | ||
owner, | ||
space, | ||
} => { | ||
add_account_checks( | ||
&mut checks, | ||
expected | ||
.resulting_accounts | ||
.iter() | ||
.filter(|(pubkey, _)| addresses.contains(pubkey)), | ||
*data, | ||
*lamports, | ||
*owner, | ||
*space, | ||
); | ||
} | ||
FixtureCheck::AllResultingAccountsExcept { | ||
ignore_addresses, | ||
data, | ||
lamports, | ||
owner, | ||
space, | ||
} => { | ||
add_account_checks( | ||
&mut checks, | ||
expected | ||
.resulting_accounts | ||
.iter() | ||
.filter(|(pubkey, _)| !ignore_addresses.contains(pubkey)), | ||
*data, | ||
*lamports, | ||
*owner, | ||
*space, | ||
); | ||
} | ||
} | ||
} | ||
|
||
result.run_checks(&checks); | ||
} |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
Would it make sense to have a variant without any data representing the case where you want to check everything from the resulting accounts? It would be a simpler way to do that (no need to add the
bool
values) and it might be the common case.Not so sure about the naming that we could have, but maybe something along the lines:
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.
Added helpers for this in my most recent commit. Nice suggestion!