gear_core/code/
metadata.rs1use crate::{
22 message::DispatchKind,
23 pages::{WasmPage, WasmPagesAmount},
24};
25use alloc::collections::BTreeSet;
26use scale_decode::DecodeAsType;
27use scale_encode::EncodeAsType;
28use scale_info::{
29 TypeInfo,
30 scale::{Decode, Encode},
31};
32
33#[derive(
35 Clone, Copy, Debug, Decode, DecodeAsType, Encode, EncodeAsType, TypeInfo, PartialEq, Eq, Hash,
36)]
37pub enum InstrumentationStatus {
38 NotInstrumented,
40 Instrumented {
42 version: u32,
44 code_len: u32,
46 },
47 InstrumentationFailed {
49 version: u32,
51 },
52}
53
54#[derive(
56 Clone, Debug, Decode, DecodeAsType, Encode, EncodeAsType, TypeInfo, PartialEq, Eq, Hash,
57)]
58pub struct CodeMetadata {
59 original_code_len: u32,
61 exports: BTreeSet<DispatchKind>,
63 static_pages: WasmPagesAmount,
65 stack_end: Option<WasmPage>,
67 instrumentation_status: InstrumentationStatus,
69}
70
71impl CodeMetadata {
72 pub fn new(
74 original_code_len: u32,
75 exports: BTreeSet<DispatchKind>,
76 static_pages: WasmPagesAmount,
77 stack_end: Option<WasmPage>,
78 instrumentation_status: InstrumentationStatus,
79 ) -> Self {
80 Self {
81 original_code_len,
82 exports,
83 static_pages,
84 stack_end,
85 instrumentation_status,
86 }
87 }
88
89 pub fn into_failed_instrumentation(self, instruction_weights_version: u32) -> Self {
91 Self {
92 instrumentation_status: InstrumentationStatus::InstrumentationFailed {
93 version: instruction_weights_version,
94 },
95 ..self
96 }
97 }
98
99 pub fn original_code_len(&self) -> u32 {
101 self.original_code_len
102 }
103
104 pub fn instrumented_code_len(&self) -> Option<u32> {
106 match self.instrumentation_status {
107 InstrumentationStatus::NotInstrumented
108 | InstrumentationStatus::InstrumentationFailed { .. } => None,
109 InstrumentationStatus::Instrumented { code_len, .. } => Some(code_len),
110 }
111 }
112
113 pub fn exports(&self) -> &BTreeSet<DispatchKind> {
115 &self.exports
116 }
117
118 pub fn static_pages(&self) -> WasmPagesAmount {
120 self.static_pages
121 }
122
123 pub fn stack_end(&self) -> Option<WasmPage> {
125 self.stack_end
126 }
127
128 pub fn instrumentation_status(&self) -> InstrumentationStatus {
130 self.instrumentation_status
131 }
132
133 pub fn instruction_weights_version(&self) -> Option<u32> {
135 match self.instrumentation_status {
136 InstrumentationStatus::NotInstrumented => None,
137 InstrumentationStatus::Instrumented { version, .. } => Some(version),
138 InstrumentationStatus::InstrumentationFailed { version } => Some(version),
139 }
140 }
141}