37 lines
1.3 KiB
Rust
Raw Normal View History

2025-07-20 22:30:31 -03:00
use core::{account::Account, check_well_behaved_account_transition, types::Address};
2025-07-18 10:29:41 -03:00
2025-07-19 23:40:02 -03:00
use crate::mocked_components::sequencer::error::Error;
2025-07-18 10:29:41 -03:00
use super::MockedSequencer;
impl MockedSequencer {
2025-07-18 16:42:55 -03:00
/// Processes a public execution request of the program `P`.
2025-07-18 10:29:41 -03:00
pub fn process_public_execution<P: nssa::Program>(
&mut self,
input_account_addresses: &[Address],
instruction_data: P::InstructionData,
2025-07-19 23:40:02 -03:00
) -> Result<(), Error> {
2025-07-18 16:42:55 -03:00
// Fetch the current state of the input accounts.
2025-07-18 10:29:41 -03:00
let input_accounts: Vec<Account> = input_account_addresses
.iter()
2025-07-19 23:40:02 -03:00
.map(|address| self.get_account(address).ok_or(Error::NotFound))
2025-07-18 10:29:41 -03:00
.collect::<Result<_, _>>()?;
2025-07-18 16:42:55 -03:00
// Execute the program
2025-07-19 23:40:02 -03:00
let program_output =
nssa::execute_onchain::<P>(&input_accounts, instruction_data).map_err(|_| Error::BadInput)?;
2025-07-18 10:29:41 -03:00
// Assert accounts pre- and post-states preserve chains invariants
2025-07-20 22:30:31 -03:00
if !check_well_behaved_account_transition(&input_accounts, &program_output.accounts_post, P::PROGRAM_ID) {
return Err(Error::BadInput);
}
2025-07-18 16:42:55 -03:00
// Update the accounts states
2025-07-19 18:37:21 -03:00
program_output.accounts_post.into_iter().for_each(|account_post_state| {
self.accounts.insert(account_post_state.address, account_post_state);
});
2025-07-20 20:02:03 -03:00
2025-07-18 10:29:41 -03:00
Ok(())
}
}