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 cfx_types::CreateContractAddressType;
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(
89        address: &CreateContractAddressType,
90    ) -> CreateType {
91        match address {
92            CreateContractAddressType::FromSenderNonce => CreateType::CREATE,
93            CreateContractAddressType::FromSenderNonceAndCodeHash => {
94                CreateType::CREATE
95            }
96            CreateContractAddressType::FromBlockNumberSenderNonceAndCodeHash => {
97                unreachable!("Inactivate address scheme")
98            }
99            CreateContractAddressType::FromSenderSaltAndCodeHash(_) => {
100                CreateType::CREATE2
101            }
102        }
103    }
104}
105
106impl Encodable for CreateType {
107    fn rlp_append(&self, s: &mut RlpStream) {
108        let v = match *self {
109            CreateType::None => 0u32,
110            CreateType::CREATE => 1,
111            CreateType::CREATE2 => 2,
112        };
113        Encodable::rlp_append(&v, s);
114    }
115}
116
117impl Decodable for CreateType {
118    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
119        rlp.as_val().and_then(|v| {
120            Ok(match v {
121                0u32 => CreateType::None,
122                1 => CreateType::CREATE,
123                2 => CreateType::CREATE2,
124                _ => {
125                    return Err(DecoderError::Custom(
126                        "Invalid value of CreateType item",
127                    ));
128                }
129            })
130        })
131    }
132}
133
134#[cfg(test)]
135mod tests {
136    use super::CallType;
137    use rlp::*;
138
139    #[test]
140    fn encode_call_type() {
141        let ct = CallType::Call;
142
143        let mut s = RlpStream::new_list(2);
144        s.append(&ct);
145        assert!(!s.is_finished(), "List shouldn't finished yet");
146        s.append(&ct);
147        assert!(s.is_finished(), "List should be finished now");
148        s.out();
149    }
150
151    #[test]
152    fn should_encode_and_decode_call_type() {
153        let original = CallType::Call;
154        let encoded = encode(&original);
155        let decoded = decode(&encoded).expect("failure decoding CallType");
156        assert_eq!(original, decoded);
157    }
158}