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
#![allow(clippy::bad_bit_mask)]

use bitflags::bitflags;

bitflags! {
    #[derive(Default)]
    pub struct VMCodeMetadata: u16 {
        const DEFAULT = 0;
        const UPGRADEABLE = 0b0000_0001_0000_0000; // LSB of first byte
        const READABLE = 0b0000_0100_0000_0000; // 3rd LSB of first byte
        const PAYABLE = 0b0000_0000_0000_0010; // 2nd LSB of second byte
        const PAYABLE_BY_SC = 0b0000_0000_0000_0100; // 3rd LSB of second byte
    }
}

impl VMCodeMetadata {
    pub fn is_upgradeable(&self) -> bool {
        *self & VMCodeMetadata::UPGRADEABLE != VMCodeMetadata::DEFAULT
    }

    pub fn is_payable(&self) -> bool {
        *self & VMCodeMetadata::PAYABLE != VMCodeMetadata::DEFAULT
    }

    pub fn is_payable_by_sc(&self) -> bool {
        *self & VMCodeMetadata::PAYABLE_BY_SC != VMCodeMetadata::DEFAULT
    }

    pub fn is_readable(&self) -> bool {
        *self & VMCodeMetadata::READABLE != VMCodeMetadata::DEFAULT
    }

    pub fn to_byte_array(&self) -> [u8; 2] {
        self.bits().to_be_bytes()
    }

    pub fn to_vec(&self) -> Vec<u8> {
        self.to_byte_array().to_vec()
    }
}

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

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

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

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

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

        assert_eq!(all.bits(), 0x0506);

        assert_eq!(VMCodeMetadata::from_bits_truncate(0xffff), all);
    }

    #[test]
    fn test_each() {
        assert!(VMCodeMetadata::UPGRADEABLE.is_upgradeable());
        assert!(!VMCodeMetadata::PAYABLE.is_upgradeable());
        assert!(!VMCodeMetadata::PAYABLE_BY_SC.is_upgradeable());
        assert!(!VMCodeMetadata::READABLE.is_upgradeable());

        assert!(!VMCodeMetadata::UPGRADEABLE.is_payable());
        assert!(VMCodeMetadata::PAYABLE.is_payable());
        assert!(!VMCodeMetadata::PAYABLE_BY_SC.is_payable());
        assert!(!VMCodeMetadata::READABLE.is_payable());

        assert!(!VMCodeMetadata::UPGRADEABLE.is_payable_by_sc());
        assert!(!VMCodeMetadata::PAYABLE.is_payable_by_sc());
        assert!(VMCodeMetadata::PAYABLE_BY_SC.is_payable_by_sc());
        assert!(!VMCodeMetadata::READABLE.is_payable_by_sc());

        assert!(!VMCodeMetadata::UPGRADEABLE.is_readable());
        assert!(!VMCodeMetadata::PAYABLE.is_readable());
        assert!(!VMCodeMetadata::PAYABLE_BY_SC.is_readable());
        assert!(VMCodeMetadata::READABLE.is_readable());
    }

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