Skip to content

Commit

Permalink
add support for return data
Browse files Browse the repository at this point in the history
  • Loading branch information
2501babe committed Dec 6, 2024
1 parent 0c7b0ed commit 93edfae
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions harness/src/fuzz/firedancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ fn parse_fixture_effects(
.compute_budget
.compute_unit_limit
.saturating_sub(effects.compute_units_available),
return_data: None, // TODO: Omitted for now.
resulting_accounts,
}
}
Expand Down
1 change: 1 addition & 0 deletions harness/src/fuzz/mollusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl From<&FuzzEffects> for InstructionResult {
execution_time,
program_result,
raw_result,
return_data: None, // TODO: Omitted for now.
resulting_accounts,
}
}
Expand Down
20 changes: 17 additions & 3 deletions harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ use {
sysvar_cache::SysvarCache,
},
solana_sdk::{
account::AccountSharedData, bpf_loader_upgradeable, feature_set::FeatureSet,
fee::FeeStructure, hash::Hash, instruction::Instruction, pubkey::Pubkey,
transaction_context::TransactionContext,
account::AccountSharedData,
bpf_loader_upgradeable,
feature_set::FeatureSet,
fee::FeeStructure,
hash::Hash,
instruction::Instruction,
pubkey::Pubkey,
transaction_context::{TransactionContext, TransactionReturnData},
},
solana_timings::ExecuteTimings,
std::sync::Arc,
Expand Down Expand Up @@ -199,6 +204,14 @@ impl Mollusk {
)
};

let return_data = match transaction_context.get_return_data() {
(program_id, data) if !data.is_empty() => Some(TransactionReturnData {
program_id: *program_id,
data: data.to_vec(),
}),
_ => None,
};

let resulting_accounts: Vec<(Pubkey, AccountSharedData)> = accounts
.iter()
.map(|(pubkey, account)| {
Expand All @@ -218,6 +231,7 @@ impl Mollusk {
execution_time: timings.details.execute_us,
program_result: invoke_result.clone().into(),
raw_result: invoke_result,
return_data,
resulting_accounts,
}
}
Expand Down
25 changes: 25 additions & 0 deletions harness/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use solana_sdk::{
instruction::InstructionError,
program_error::ProgramError,
pubkey::Pubkey,
transaction_context::TransactionReturnData,
};

/// The result code of the program's execution.
Expand Down Expand Up @@ -51,6 +52,8 @@ pub struct InstructionResult {
pub program_result: ProgramResult,
/// The raw result of the program's execution.
pub raw_result: Result<(), InstructionError>,
/// The return data produced by the instruction, if any.
pub return_data: Option<TransactionReturnData>,
/// The resulting accounts after executing the instruction.
///
/// This includes all accounts provided to the processor, in the order
Expand All @@ -66,6 +69,7 @@ impl Default for InstructionResult {
execution_time: 0,
program_result: ProgramResult::Success,
raw_result: Ok(()),
return_data: None,
resulting_accounts: vec![],
}
}
Expand Down Expand Up @@ -111,6 +115,20 @@ impl InstructionResult {
actual_result, check_result,
);
}
CheckType::ReturnData(return_data) => {
let check_return_data = return_data;
let Some(actual_return_data) = &self.return_data else {
panic!(
"CHECK: return data: got None, expected {:?}",
check_return_data,
);
};
assert_eq!(
actual_return_data, check_return_data,
"CHECK: return_data: got {:?}, expected {:?}",
actual_return_data, check_return_data,
);
}
CheckType::ResultingAccount(account) => {
let pubkey = account.pubkey;
let resulting_account = self
Expand Down Expand Up @@ -217,6 +235,8 @@ enum CheckType<'a> {
ExecutionTime(u64),
/// Check the result code of the program's execution.
ProgramResult(ProgramResult),
/// Check the return data produced by executing the instruction.
ReturnData(TransactionReturnData),
/// Check a resulting account after executing the instruction.
ResultingAccount(AccountCheck<'a>),
}
Expand Down Expand Up @@ -255,6 +275,11 @@ impl<'a> Check<'a> {
Check::new(CheckType::ProgramResult(ProgramResult::UnknownError(error)))
}

/// Check the return data produced by executing the instruction.
pub fn return_data(return_data: TransactionReturnData) -> Self {
Check::new(CheckType::ReturnData(return_data))
}

/// Check a resulting account after executing the instruction.
pub fn account(pubkey: &Pubkey) -> AccountCheckBuilder {
AccountCheckBuilder::new(pubkey)
Expand Down

0 comments on commit 93edfae

Please sign in to comment.