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
use super::event_builder::{
    TYPE_ADDRESS, TYPE_BOOL, TYPE_BYTEARRAY, TYPE_H256, TYPE_INT, TYPE_STRING,
};
use super::Error;
use super::Source;
use super::{VmValueBuilderCommon, VmValueDecoder, VmValueEncoder};
use crate::prelude::*;

pub struct VmValueBuilder {
    pub(crate) common: VmValueBuilderCommon,
}

impl Default for VmValueBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl VmValueBuilder {
    pub fn new() -> Self {
        let common = VmValueBuilderCommon::new();
        let mut builder = VmValueBuilder { common };
        builder.common.sink.write_byte(0u8);
        builder
    }

    pub fn write<T: VmValueEncoder>(&mut self, val: T) {
        val.serialize(self)
    }

    pub fn string(&mut self, method: &str) {
        self.common.string(method);
    }

    pub fn bytearray(&mut self, bytes: &[u8]) {
        self.common.bytearray(bytes);
    }

    pub fn address(&mut self, address: &Address) {
        self.common.address(address);
    }

    pub fn number(&mut self, amount: U128) {
        self.common.number(amount);
    }

    pub fn bool(&mut self, b: bool) {
        self.common.bool(b);
    }

    pub fn h256(&mut self, hash: &H256) {
        self.common.h256(hash);
    }

    pub fn bytes(self) -> Vec<u8> {
        self.common.sink.into()
    }
}

pub struct VmValueParser<'a> {
    pub source: Source<'a>,
}

impl<'a> VmValueParser<'a> {
    pub fn new(bs: &'a [u8]) -> Self {
        let mut source = Source::new(bs);
        let _version = source.read_byte(); //version
        Self { source }
    }

    pub fn read<T: VmValueDecoder<'a>>(&mut self) -> Result<T, Error> {
        T::deserialize(self)
    }

    pub fn string(&mut self) -> Result<&'a str, Error> {
        let ty = self.source.read_byte()?;
        if ty != TYPE_STRING {
            return Err(Error::TypeInconsistency);
        }
        let l = self.source.read_u32()?;
        let buf = self.source.next_bytes(l as usize)?;
        str::from_utf8(buf).map_err(|_| Error::InvalidUtf8)
    }

    pub fn bytearray(&mut self) -> Result<&'a [u8], Error> {
        let ty = self.source.read_byte()?;
        if ty != TYPE_BYTEARRAY || ty == TYPE_STRING {
            return Err(Error::TypeInconsistency);
        }
        let l = self.source.read_u32()?;
        self.source.next_bytes(l as usize)
    }

    pub fn address(&mut self) -> Result<&'a Address, Error> {
        let ty = self.source.read_byte()?;
        if ty != TYPE_ADDRESS {
            return Err(Error::TypeInconsistency);
        }
        self.source.read_address()
    }

    pub fn number(&mut self) -> Result<u128, Error> {
        let ty = self.source.read_byte()?;
        if ty != TYPE_INT {
            return Err(Error::TypeInconsistency);
        }
        self.source.read_u128()
    }

    pub fn bool(&mut self) -> Result<bool, Error> {
        let ty = self.source.read_byte()?;
        if ty == TYPE_BOOL {
            return self.source.read_bool();
        } else if ty == TYPE_INT {
            let res = self.source.read_u128()?;
            if res != 0 {
                return Ok(true);
            } else {
                return Ok(false);
            }
        }
        Err(Error::TypeInconsistency)
    }

    pub fn h256(&mut self) -> Result<&'a H256, Error> {
        let ty = self.source.read_byte()?;
        if ty != TYPE_H256 {
            return Err(Error::TypeInconsistency);
        }
        self.source.read_h256()
    }
}