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, Eq, Default)]
29#[serde(rename_all = "lowercase")]
30pub enum CallType {
31    /// Not a CALL.
32    #[default]
33    None,
34    /// CALL.
35    Call,
36    /// CALLCODE.
37    CallCode,
38    /// DELEGATECALL.
39    DelegateCall,
40    /// STATICCALL
41    StaticCall,
42}
43
44impl Encodable for CallType {
45    fn rlp_append(&self, s: &mut RlpStream) {
46        let v = match *self {
47            CallType::None => 0u32,
48            CallType::Call => 1,
49            CallType::CallCode => 2,
50            CallType::DelegateCall => 3,
51            CallType::StaticCall => 4,
52        };
53        Encodable::rlp_append(&v, s);
54    }
55}
56
57impl Decodable for CallType {
58    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
59        rlp.as_val().and_then(|v| {
60            Ok(match v {
61                0u32 => CallType::None,
62                1 => CallType::Call,
63                2 => CallType::CallCode,
64                3 => CallType::DelegateCall,
65                4 => CallType::StaticCall,
66                _ => {
67                    return Err(DecoderError::Custom(
68                        "Invalid value of CallType item",
69                    ));
70                }
71            })
72        })
73    }
74}
75
76/// The type of the create-like instruction.
77#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Default)]
78#[serde(rename_all = "lowercase")]
79pub enum CreateType {
80    /// Not a create
81    #[default]
82    None,
83    /// CREATE
84    CREATE,
85    /// CREATE2
86    CREATE2,
87}
88
89impl CreateType {
90    pub fn from_address_scheme(
91        address: &CreateContractAddressType,
92    ) -> CreateType {
93        match address {
94            CreateContractAddressType::FromSenderNonce => CreateType::CREATE,
95            CreateContractAddressType::FromSenderNonceAndCodeHash => {
96                CreateType::CREATE
97            }
98            CreateContractAddressType::FromBlockNumberSenderNonceAndCodeHash => {
99                unreachable!("Inactivate address scheme")
100            }
101            CreateContractAddressType::FromSenderSaltAndCodeHash(_) => {
102                CreateType::CREATE2
103            }
104        }
105    }
106}
107
108impl Encodable for CreateType {
109    fn rlp_append(&self, s: &mut RlpStream) {
110        let v = match *self {
111            CreateType::None => 0u32,
112            CreateType::CREATE => 1,
113            CreateType::CREATE2 => 2,
114        };
115        Encodable::rlp_append(&v, s);
116    }
117}
118
119impl Decodable for CreateType {
120    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
121        rlp.as_val().and_then(|v| {
122            Ok(match v {
123                0u32 => CreateType::None,
124                1 => CreateType::CREATE,
125                2 => CreateType::CREATE2,
126                _ => {
127                    return Err(DecoderError::Custom(
128                        "Invalid value of CreateType item",
129                    ));
130                }
131            })
132        })
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::CallType;
139    use rlp::*;
140
141    #[test]
142    fn encode_call_type() {
143        let ct = CallType::Call;
144
145        let mut s = RlpStream::new_list(2);
146        s.append(&ct);
147        assert!(!s.is_finished(), "List shouldn't finished yet");
148        s.append(&ct);
149        assert!(s.is_finished(), "List should be finished now");
150        s.out();
151    }
152
153    #[test]
154    fn should_encode_and_decode_call_type() {
155        let original = CallType::Call;
156        let encoded = encode(&original);
157        let decoded = decode(&encoded).expect("failure decoding CallType");
158        assert_eq!(original, decoded);
159    }
160}