cfx_vm_types/
call_create_type.rs

1// Copyright 2015-2018 Parity Technologies (UK) Ltd.
2// This file is part of Parity.
3
4// Parity is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity.  If not, see <http://www.gnu.org/licenses/>.
16
17// Copyright 2019 Conflux Foundation. All rights reserved.
18// Conflux is free software and distributed under GNU General Public License.
19// See http://www.gnu.org/licenses/
20
21//! EVM call types.
22
23use super::CreateContractAddress;
24use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
25use serde::Serialize;
26
27/// The type of the call-like instruction.
28#[derive(Debug, PartialEq, Clone, Copy, Serialize)]
29#[serde(rename_all = "lowercase")]
30pub enum CallType {
31    /// Not a CALL.
32    None,
33    /// CALL.
34    Call,
35    /// CALLCODE.
36    CallCode,
37    /// DELEGATECALL.
38    DelegateCall,
39    /// STATICCALL
40    StaticCall,
41}
42
43impl Encodable for CallType {
44    fn rlp_append(&self, s: &mut RlpStream) {
45        let v = match *self {
46            CallType::None => 0u32,
47            CallType::Call => 1,
48            CallType::CallCode => 2,
49            CallType::DelegateCall => 3,
50            CallType::StaticCall => 4,
51        };
52        Encodable::rlp_append(&v, s);
53    }
54}
55
56impl Decodable for CallType {
57    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
58        rlp.as_val().and_then(|v| {
59            Ok(match v {
60                0u32 => CallType::None,
61                1 => CallType::Call,
62                2 => CallType::CallCode,
63                3 => CallType::DelegateCall,
64                4 => CallType::StaticCall,
65                _ => {
66                    return Err(DecoderError::Custom(
67                        "Invalid value of CallType item",
68                    ));
69                }
70            })
71        })
72    }
73}
74
75/// The type of the create-like instruction.
76#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize)]
77#[serde(rename_all = "lowercase")]
78pub enum CreateType {
79    /// Not a create
80    None,
81    /// CREATE
82    CREATE,
83    /// CREATE2
84    CREATE2,
85}
86
87impl CreateType {
88    pub fn from_address_scheme(address: &CreateContractAddress) -> CreateType {
89        match address {
90            CreateContractAddress::FromSenderNonce => CreateType::CREATE,
91            CreateContractAddress::FromSenderNonceAndCodeHash => {
92                CreateType::CREATE
93            }
94            CreateContractAddress::FromBlockNumberSenderNonceAndCodeHash => {
95                unreachable!("Inactivate address scheme")
96            }
97            CreateContractAddress::FromSenderSaltAndCodeHash(_) => {
98                CreateType::CREATE2
99            }
100        }
101    }
102}
103
104impl Encodable for CreateType {
105    fn rlp_append(&self, s: &mut RlpStream) {
106        let v = match *self {
107            CreateType::None => 0u32,
108            CreateType::CREATE => 1,
109            CreateType::CREATE2 => 2,
110        };
111        Encodable::rlp_append(&v, s);
112    }
113}
114
115impl Decodable for CreateType {
116    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
117        rlp.as_val().and_then(|v| {
118            Ok(match v {
119                0u32 => CreateType::None,
120                1 => CreateType::CREATE,
121                2 => CreateType::CREATE2,
122                _ => {
123                    return Err(DecoderError::Custom(
124                        "Invalid value of CreateType item",
125                    ));
126                }
127            })
128        })
129    }
130}
131
132#[cfg(test)]
133mod tests {
134    use super::CallType;
135    use rlp::*;
136
137    #[test]
138    fn encode_call_type() {
139        let ct = CallType::Call;
140
141        let mut s = RlpStream::new_list(2);
142        s.append(&ct);
143        assert!(!s.is_finished(), "List shouldn't finished yet");
144        s.append(&ct);
145        assert!(s.is_finished(), "List should be finished now");
146        s.out();
147    }
148
149    #[test]
150    fn should_encode_and_decode_call_type() {
151        let original = CallType::Call;
152        let encoded = encode(&original);
153        let decoded = decode(&encoded).expect("failure decoding CallType");
154        assert_eq!(original, decoded);
155    }
156}