gear_core/code/
metadata.rs1use crate::{
7 message::DispatchKind,
8 pages::{WasmPage, WasmPagesAmount},
9};
10use alloc::collections::BTreeSet;
11use scale_decode::DecodeAsType;
12use scale_encode::EncodeAsType;
13use scale_info::{
14 TypeInfo,
15 scale::{Decode, Encode},
16};
17
18#[derive(
20 Clone,
21 Copy,
22 Debug,
23 Decode,
24 DecodeAsType,
25 Encode,
26 EncodeAsType,
27 TypeInfo,
28 PartialEq,
29 Eq,
30 Hash,
31 derive_more::IsVariant,
32)]
33pub enum InstrumentationStatus {
34 NotInstrumented,
36 Instrumented {
38 version: u32,
40 code_len: u32,
42 },
43 InstrumentationFailed {
45 version: u32,
47 },
48}
49
50#[derive(
52 Clone, Debug, Decode, DecodeAsType, Encode, EncodeAsType, TypeInfo, PartialEq, Eq, Hash,
53)]
54pub struct CodeMetadata {
55 original_code_len: u32,
57 exports: BTreeSet<DispatchKind>,
59 static_pages: WasmPagesAmount,
61 stack_end: Option<WasmPage>,
63 instrumentation_status: InstrumentationStatus,
65}
66
67impl CodeMetadata {
68 pub fn new(
70 original_code_len: u32,
71 exports: BTreeSet<DispatchKind>,
72 static_pages: WasmPagesAmount,
73 stack_end: Option<WasmPage>,
74 instrumentation_status: InstrumentationStatus,
75 ) -> Self {
76 Self {
77 original_code_len,
78 exports,
79 static_pages,
80 stack_end,
81 instrumentation_status,
82 }
83 }
84
85 pub fn into_failed_instrumentation(self, instruction_weights_version: u32) -> Self {
87 Self {
88 instrumentation_status: InstrumentationStatus::InstrumentationFailed {
89 version: instruction_weights_version,
90 },
91 ..self
92 }
93 }
94
95 pub fn original_code_len(&self) -> u32 {
97 self.original_code_len
98 }
99
100 pub fn instrumented_code_len(&self) -> Option<u32> {
102 match self.instrumentation_status {
103 InstrumentationStatus::NotInstrumented
104 | InstrumentationStatus::InstrumentationFailed { .. } => None,
105 InstrumentationStatus::Instrumented { code_len, .. } => Some(code_len),
106 }
107 }
108
109 pub fn exports(&self) -> &BTreeSet<DispatchKind> {
111 &self.exports
112 }
113
114 pub fn static_pages(&self) -> WasmPagesAmount {
116 self.static_pages
117 }
118
119 pub fn stack_end(&self) -> Option<WasmPage> {
121 self.stack_end
122 }
123
124 pub fn instrumentation_status(&self) -> InstrumentationStatus {
126 self.instrumentation_status
127 }
128
129 pub fn instruction_weights_version(&self) -> Option<u32> {
131 match self.instrumentation_status {
132 InstrumentationStatus::NotInstrumented => None,
133 InstrumentationStatus::Instrumented { version, .. } => Some(version),
134 InstrumentationStatus::InstrumentationFailed { version } => Some(version),
135 }
136 }
137}