Skip to main content

gear_core/code/
metadata.rs

1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4//! Module code metadata
5
6use 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/// Status of the instrumentation.
19#[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    /// Code is not instrumented yet.
35    NotInstrumented,
36    /// Code is instrumented on weights version.
37    Instrumented {
38        /// Version of the instruction weights used for instrumentation.
39        version: u32,
40        /// Instrumented code length.
41        code_len: u32,
42    },
43    /// Failed to instrument code on weights version.
44    InstrumentationFailed {
45        /// Version of the instruction weights used for instrumentation.
46        version: u32,
47    },
48}
49
50/// Metadata for the code.
51#[derive(
52    Clone, Debug, Decode, DecodeAsType, Encode, EncodeAsType, TypeInfo, PartialEq, Eq, Hash,
53)]
54pub struct CodeMetadata {
55    /// Original code length.
56    original_code_len: u32,
57    /// Exports of the wasm module.
58    exports: BTreeSet<DispatchKind>,
59    // Static pages count from memory import.
60    static_pages: WasmPagesAmount,
61    /// Stack end page.
62    stack_end: Option<WasmPage>,
63    /// Instrumentation status, contains version of the instructions in case of instrumentation.
64    instrumentation_status: InstrumentationStatus,
65}
66
67impl CodeMetadata {
68    /// Creates a new instance of the code metadata.
69    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    /// Converts the metadata into the failed instrumentation state.
86    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    /// Returns the original code length.
96    pub fn original_code_len(&self) -> u32 {
97        self.original_code_len
98    }
99
100    /// Returns the instrumented code length.
101    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    /// Returns the code exports.
110    pub fn exports(&self) -> &BTreeSet<DispatchKind> {
111        &self.exports
112    }
113
114    /// Returns the static pages count from memory import.
115    pub fn static_pages(&self) -> WasmPagesAmount {
116        self.static_pages
117    }
118
119    /// Returns the stack end page.
120    pub fn stack_end(&self) -> Option<WasmPage> {
121        self.stack_end
122    }
123
124    /// Returns the instrumentation status.
125    pub fn instrumentation_status(&self) -> InstrumentationStatus {
126        self.instrumentation_status
127    }
128
129    /// Returns the version of the instructions.
130    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}