casper_types/transaction/
transaction_runtime.rs

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
use alloc::vec::Vec;
use core::fmt::{self, Display, Formatter};
#[cfg(feature = "datasize")]
use datasize::DataSize;
#[cfg(any(feature = "testing", test))]
use rand::{
    distributions::{Distribution, Standard},
    Rng,
};
#[cfg(feature = "json-schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[cfg(doc)]
use super::Transaction;
use crate::bytesrepr::{self, FromBytes, ToBytes, U8_SERIALIZED_LENGTH};
#[cfg(any(all(feature = "std", feature = "testing"), test))]
use crate::testing::TestRng;

/// The runtime used to execute a [`Transaction`].
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(
    feature = "json-schema",
    derive(JsonSchema),
    schemars(description = "Runtime used to execute a Transaction.")
)]
#[serde(deny_unknown_fields)]
#[repr(u8)]
pub enum TransactionRuntime {
    /// The Casper Version 1 Virtual Machine.
    VmCasperV1,
    /// The Casper Version 2 Virtual Machine.
    VmCasperV2,
}

impl Display for TransactionRuntime {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            TransactionRuntime::VmCasperV1 => write!(formatter, "vm-casper-v1"),
            TransactionRuntime::VmCasperV2 => write!(formatter, "vm-casper-v2"),
        }
    }
}

impl ToBytes for TransactionRuntime {
    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
        (*self as u8).write_bytes(writer)
    }

    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
        let mut buffer = bytesrepr::allocate_buffer(self)?;
        self.write_bytes(&mut buffer)?;
        Ok(buffer)
    }

    fn serialized_length(&self) -> usize {
        U8_SERIALIZED_LENGTH
    }
}

impl FromBytes for TransactionRuntime {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
        let (tag, remainder) = u8::from_bytes(bytes)?;
        match tag {
            v if v == TransactionRuntime::VmCasperV1 as u8 => {
                Ok((TransactionRuntime::VmCasperV1, remainder))
            }
            v if v == TransactionRuntime::VmCasperV2 as u8 => {
                Ok((TransactionRuntime::VmCasperV2, remainder))
            }
            _ => Err(bytesrepr::Error::Formatting),
        }
    }
}

#[cfg(any(all(feature = "std", feature = "testing"), test))]
impl TransactionRuntime {
    /// Generates a random instance using a `TestRng`.
    pub fn random(rng: &mut TestRng) -> Self {
        match rng.gen_range(0..=1) {
            0 => TransactionRuntime::VmCasperV1,
            1 => TransactionRuntime::VmCasperV2,
            _ => unreachable!(),
        }
    }
}

#[cfg(any(feature = "testing", test))]
impl Distribution<TransactionRuntime> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> TransactionRuntime {
        match rng.gen_range(0..=1) {
            0 => TransactionRuntime::VmCasperV1,
            1 => TransactionRuntime::VmCasperV2,
            _ => unreachable!(),
        }
    }
}

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

    #[test]
    fn bytesrepr_roundtrip() {
        for transaction_runtime in [
            TransactionRuntime::VmCasperV1,
            TransactionRuntime::VmCasperV2,
        ] {
            bytesrepr::test_serialization_roundtrip(&transaction_runtime);
        }
    }
}