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
use crate::serialization::{VecU8Deserializer, VecU8Serializer};
use massa_serialization::{
    Deserializer, SerializeError, Serializer, U64VarIntDeserializer, U64VarIntSerializer,
};
use nom::error::{ContextError, ParseError};
use nom::IResult;
use serde::{Deserialize, Serialize};
use std::ops::Bound::Included;

/// Current version of the bytecode
pub const BYTECODE_VERSION: u64 = 0;

/// Structure representing executable bytecode
#[derive(Default, Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct Bytecode(pub Vec<u8>);

/// Serializer for `Bytecode`
#[derive(Default)]
pub struct BytecodeSerializer {
    version_byte_serializer: U64VarIntSerializer,
    vec_u8_serializer: VecU8Serializer,
}

impl BytecodeSerializer {
    /// Creates a new `BytecodeSerializer`
    pub fn new() -> Self {
        Self {
            version_byte_serializer: U64VarIntSerializer::new(),
            vec_u8_serializer: VecU8Serializer::new(),
        }
    }
}

impl Serializer<Bytecode> for BytecodeSerializer {
    fn serialize(&self, value: &Bytecode, buffer: &mut Vec<u8>) -> Result<(), SerializeError> {
        self.version_byte_serializer
            .serialize(&BYTECODE_VERSION, buffer)?;
        self.vec_u8_serializer.serialize(&value.0, buffer)?;
        Ok(())
    }
}

/// Deserializer for `Bytecode`
pub struct BytecodeDeserializer {
    version_byte_deserializer: U64VarIntDeserializer,
    vec_u8_deserializer: VecU8Deserializer,
}

impl BytecodeDeserializer {
    /// Creates a new `LedgerEntryDeserializer`
    pub fn new(max_datastore_value_length: u64) -> Self {
        Self {
            version_byte_deserializer: U64VarIntDeserializer::new(Included(0), Included(u64::MAX)),
            vec_u8_deserializer: VecU8Deserializer::new(
                Included(u64::MIN),
                Included(max_datastore_value_length),
            ),
        }
    }
}

impl Deserializer<Bytecode> for BytecodeDeserializer {
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], Bytecode, E> {
        let (rest, _version) = self.version_byte_deserializer.deserialize(buffer)?;
        let (rest, bytecode_vec) = self.vec_u8_deserializer.deserialize(rest)?;

        Ok((rest, Bytecode(bytecode_vec)))
    }
}