1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use crate::abi::TypeAbi;
use alloc::string::String;
use core::ops::{BitOr, BitOrAssign};
use elrond_codec::*;

/// Flags concerning smart contract creation and upgrade.
/// Currently always represented as a 2-byte bitfield.
#[derive(Clone, Copy, PartialEq)]
pub struct CodeMetadata([u8; 2]);

const METADATA_UPGRADEABLE_BYTE: usize = 0;
const METADATA_UPGRADEABLE_MASK: u8 = 1;
const METADATA_PAYABLE_BYTE: usize = 1;
const METADATA_PAYABLE_MASK: u8 = 2;
const METADATA_READABLE_BYTE: usize = 0;
const METADATA_READABLE_MASK: u8 = 4;

impl CodeMetadata {
    pub const DEFAULT: CodeMetadata = CodeMetadata([0, 0]);
    pub const UPGRADEABLE: CodeMetadata = CodeMetadata([METADATA_UPGRADEABLE_MASK, 0]);
    pub const PAYABLE: CodeMetadata = CodeMetadata([0, METADATA_PAYABLE_MASK]);
    pub const READABLE: CodeMetadata = CodeMetadata([METADATA_READABLE_MASK, 0]);

    pub fn is_upgradeable(&self) -> bool {
        self.0[METADATA_UPGRADEABLE_BYTE] & METADATA_UPGRADEABLE_MASK > 0
    }

    pub fn is_payable(&self) -> bool {
        self.0[METADATA_PAYABLE_BYTE] & METADATA_PAYABLE_MASK > 0
    }

    pub fn is_readable(&self) -> bool {
        self.0[METADATA_READABLE_BYTE] & METADATA_READABLE_MASK > 0
    }

    pub fn from_flags(upgradeable: bool, payable: bool, readable: bool) -> CodeMetadata {
        let mut code_metadata = CodeMetadata::DEFAULT;
        if upgradeable {
            code_metadata |= CodeMetadata::UPGRADEABLE;
        }
        if payable {
            code_metadata |= CodeMetadata::PAYABLE;
        }
        if readable {
            code_metadata |= CodeMetadata::READABLE;
        }
        code_metadata
    }

    #[inline]
    pub fn as_ptr(&self) -> *const u8 {
        self.0[..].as_ptr()
    }

    #[inline]
    pub fn into_bytes(self) -> [u8; 2] {
        self.0
    }

    pub fn to_u16(&self) -> u16 {
        u16::from_be_bytes(self.0)
    }
}

impl From<[u8; 2]> for CodeMetadata {
    #[inline]
    fn from(arr: [u8; 2]) -> Self {
        CodeMetadata(arr)
    }
}

impl From<u16> for CodeMetadata {
    #[inline]
    fn from(value: u16) -> Self {
        CodeMetadata(value.to_be_bytes())
    }
}

impl BitOr for CodeMetadata {
    type Output = CodeMetadata;

    fn bitor(self, other: CodeMetadata) -> CodeMetadata {
        CodeMetadata([self.0[0] | other.0[0], self.0[1] | other.0[1]])
    }
}

impl<'a, 'b> BitOr<&'b CodeMetadata> for &'a CodeMetadata {
    type Output = CodeMetadata;

    fn bitor(self, other: &CodeMetadata) -> CodeMetadata {
        CodeMetadata([self.0[0] | other.0[0], self.0[1] | other.0[1]])
    }
}

impl BitOrAssign<CodeMetadata> for CodeMetadata {
    #[inline]
    fn bitor_assign(&mut self, other: Self) {
        self.0[0] |= other.0[0];
        self.0[1] |= other.0[1];
    }
}

impl BitOrAssign<&CodeMetadata> for CodeMetadata {
    #[inline]
    fn bitor_assign(&mut self, other: &CodeMetadata) {
        self.0[0] |= other.0[0];
        self.0[1] |= other.0[1];
    }
}

impl NestedEncode for CodeMetadata {
    fn dep_encode<O: NestedEncodeOutput>(&self, dest: &mut O) -> Result<(), EncodeError> {
        self.to_u16().dep_encode(dest)?;
        Ok(())
    }

    fn dep_encode_or_exit<O: NestedEncodeOutput, ExitCtx: Clone>(
        &self,
        dest: &mut O,
        c: ExitCtx,
        exit: fn(ExitCtx, EncodeError) -> !,
    ) {
        self.to_u16().dep_encode_or_exit(dest, c, exit);
    }
}

impl TopEncode for CodeMetadata {
    #[inline]
    fn top_encode<O: TopEncodeOutput>(&self, output: O) -> Result<(), EncodeError> {
        top_encode_from_nested(self, output)
    }

    #[inline]
    fn top_encode_or_exit<O: TopEncodeOutput, ExitCtx: Clone>(
        &self,
        output: O,
        c: ExitCtx,
        exit: fn(ExitCtx, EncodeError) -> !,
    ) {
        top_encode_from_nested_or_exit(self, output, c, exit);
    }
}

impl NestedDecode for CodeMetadata {
    fn dep_decode<I: NestedDecodeInput>(input: &mut I) -> Result<Self, DecodeError> {
        Ok(CodeMetadata::from(u16::dep_decode(input)?))
    }

    fn dep_decode_or_exit<I: NestedDecodeInput, ExitCtx: Clone>(
        input: &mut I,
        c: ExitCtx,
        exit: fn(ExitCtx, DecodeError) -> !,
    ) -> Self {
        CodeMetadata::from(u16::dep_decode_or_exit(input, c, exit))
    }
}

impl TopDecode for CodeMetadata {
    fn top_decode<I: TopDecodeInput>(input: I) -> Result<Self, DecodeError> {
        top_decode_from_nested(input)
    }

    fn top_decode_or_exit<I: TopDecodeInput, ExitCtx: Clone>(
        input: I,
        c: ExitCtx,
        exit: fn(ExitCtx, DecodeError) -> !,
    ) -> Self {
        top_decode_from_nested_or_exit(input, c, exit)
    }
}

impl TypeAbi for CodeMetadata {
    fn type_name() -> String {
        "CodeMetadata".into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default() {
        assert!(!CodeMetadata::DEFAULT.is_upgradeable());
        assert!(!CodeMetadata::DEFAULT.is_payable());
        assert!(!CodeMetadata::DEFAULT.is_readable());
    }

    #[test]
    fn test_const() {
        assert!(CodeMetadata::UPGRADEABLE.is_upgradeable());
        assert!(CodeMetadata::PAYABLE.is_payable());
        assert!(CodeMetadata::READABLE.is_readable());
    }

    #[test]
    fn test_all() {
        let all = CodeMetadata::UPGRADEABLE | CodeMetadata::PAYABLE | CodeMetadata::READABLE;
        assert!(all.is_upgradeable());
        assert!(all.is_payable());
        assert!(all.is_readable());
    }

    /// Translated from vm-wasm.
    #[test]
    fn test_from_array() {
        assert!(CodeMetadata::from([1, 0]).is_upgradeable());
        assert!(!CodeMetadata::from([1, 0]).is_readable());
        assert!(CodeMetadata::from([0, 2]).is_payable());
        assert!(CodeMetadata::from([4, 0]).is_readable());
        assert!(!CodeMetadata::from([4, 0]).is_upgradeable());
        assert!(!CodeMetadata::from([0, 0]).is_upgradeable());
        assert!(!CodeMetadata::from([0, 0]).is_payable());
        assert!(!CodeMetadata::from([0, 0]).is_readable());
    }

    #[test]
    fn test_from_flags() {
        assert!(CodeMetadata::from_flags(true, false, false).is_upgradeable());
        assert!(CodeMetadata::from_flags(false, true, false).is_payable());
        assert!(CodeMetadata::from_flags(false, false, true).is_readable());
        assert!(!CodeMetadata::from_flags(false, false, false).is_upgradeable());
        assert!(!CodeMetadata::from_flags(false, false, false).is_payable());
        assert!(!CodeMetadata::from_flags(false, false, false).is_readable());

        assert_eq!(CodeMetadata::from_flags(true, true, true).to_u16(), 0x0502);
    }
}