use crate::{
message::DispatchKind,
pages::{WasmPage, WasmPagesAmount},
};
use alloc::collections::BTreeSet;
use scale_decode::DecodeAsType;
use scale_encode::EncodeAsType;
use scale_info::{
TypeInfo,
scale::{Decode, Encode},
};
#[derive(
Clone, Copy, Debug, Decode, DecodeAsType, Encode, EncodeAsType, TypeInfo, PartialEq, Eq, Hash,
)]
pub enum InstrumentationStatus {
NotInstrumented,
Instrumented {
version: u32,
code_len: u32,
},
InstrumentationFailed {
version: u32,
},
}
#[derive(
Clone, Debug, Decode, DecodeAsType, Encode, EncodeAsType, TypeInfo, PartialEq, Eq, Hash,
)]
pub struct CodeMetadata {
original_code_len: u32,
exports: BTreeSet<DispatchKind>,
static_pages: WasmPagesAmount,
stack_end: Option<WasmPage>,
instrumentation_status: InstrumentationStatus,
}
impl CodeMetadata {
pub fn new(
original_code_len: u32,
exports: BTreeSet<DispatchKind>,
static_pages: WasmPagesAmount,
stack_end: Option<WasmPage>,
instrumentation_status: InstrumentationStatus,
) -> Self {
Self {
original_code_len,
exports,
static_pages,
stack_end,
instrumentation_status,
}
}
pub fn into_failed_instrumentation(self, instruction_weights_version: u32) -> Self {
Self {
instrumentation_status: InstrumentationStatus::InstrumentationFailed {
version: instruction_weights_version,
},
..self
}
}
pub fn original_code_len(&self) -> u32 {
self.original_code_len
}
pub fn instrumented_code_len(&self) -> Option<u32> {
match self.instrumentation_status {
InstrumentationStatus::NotInstrumented
| InstrumentationStatus::InstrumentationFailed { .. } => None,
InstrumentationStatus::Instrumented { code_len, .. } => Some(code_len),
}
}
pub fn exports(&self) -> &BTreeSet<DispatchKind> {
&self.exports
}
pub fn static_pages(&self) -> WasmPagesAmount {
self.static_pages
}
pub fn stack_end(&self) -> Option<WasmPage> {
self.stack_end
}
pub fn instrumentation_status(&self) -> InstrumentationStatus {
self.instrumentation_status
}
pub fn instruction_weights_version(&self) -> Option<u32> {
match self.instrumentation_status {
InstrumentationStatus::NotInstrumented => None,
InstrumentationStatus::Instrumented { version, .. } => Some(version),
InstrumentationStatus::InstrumentationFailed { version } => Some(version),
}
}
}