use super::*;
impl ExtManager {
pub(crate) fn read_state_bytes(
&mut self,
payload: Vec<u8>,
program_id: &ProgramId,
) -> Result<Vec<u8>> {
let executable_actor_data = Actors::modify(*program_id, |actor| {
if let Some(actor) = actor {
Ok(actor.get_executable_actor_data())
} else {
Err(TestError::ActorNotFound(*program_id))
}
})?;
if let Some((data, code)) = executable_actor_data {
core_processor::informational::execute_for_reply::<Ext<LazyPagesNative>, _>(
String::from("state"),
code,
Some(data.allocations),
Some((*program_id, Default::default())),
payload,
GAS_ALLOWANCE,
self.blocks_manager.get(),
)
.map_err(TestError::ReadStateError)
} else if let Some(mut program_mock) = Actors::modify(*program_id, |actor| {
actor.expect("Checked before").take_mock()
}) {
program_mock
.state()
.map_err(|err| TestError::ReadStateError(err.into()))
} else {
Err(TestError::ActorIsNotExecutable(*program_id))
}
}
pub(crate) fn read_state_bytes_using_wasm(
&mut self,
payload: Vec<u8>,
program_id: &ProgramId,
fn_name: &str,
wasm: Vec<u8>,
args: Option<Vec<u8>>,
) -> Result<Vec<u8>> {
let mapping_code = Code::try_new_mock_const_or_no_rules(
wasm,
true,
TryNewCodeConfig::new_no_exports_check(),
)
.map_err(|_| TestError::Instrumentation)?;
let mapping_code = InstrumentedCodeAndId::from(CodeAndId::new(mapping_code))
.into_parts()
.0;
let mut mapping_code_payload = args.unwrap_or_default();
mapping_code_payload.append(&mut self.read_state_bytes(payload, program_id)?);
core_processor::informational::execute_for_reply::<Ext<LazyPagesNative>, _>(
String::from(fn_name),
mapping_code,
None,
None,
mapping_code_payload,
GAS_ALLOWANCE,
self.blocks_manager.get(),
)
.map_err(TestError::ReadStateError)
}
pub(crate) fn read_memory_pages(&self, program_id: &ProgramId) -> BTreeMap<GearPage, PageBuf> {
Actors::access(*program_id, |actor| {
let program = match actor.unwrap_or_else(|| panic!("Actor id {program_id:?} not found"))
{
TestActor::Initialized(program) => program,
TestActor::Uninitialized(_, program) => program.as_ref().unwrap(),
TestActor::Dormant => panic!("Actor {program_id} isn't dormant"),
};
match program {
Program::Genuine(program) => program.pages_data.clone(),
Program::Mock(_) => panic!("Can't read memory of mock program"),
}
})
}
}