cfx_vm_types/
action_params.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 input params.
22use super::call_create_type::{CallType, CreateType};
23use cfx_bytes::Bytes;
24use cfx_types::{Address, Space, H256, U256};
25use std::sync::Arc;
26
27/// Transaction value
28#[derive(Clone, Debug)]
29pub enum ActionValue {
30    /// Value that should be transferred
31    Transfer(U256),
32    /// Apparent value for transaction (not transferred)
33    Apparent(U256),
34}
35
36/// Type of the way parameters encoded
37#[derive(Clone, Debug)]
38pub enum ParamsType {
39    /// Parameters are included in code
40    Embedded,
41    /// Parameters are passed in data section
42    Separate,
43}
44
45impl ActionValue {
46    /// Returns action value as U256.
47    pub fn value(&self) -> U256 {
48        match *self {
49            ActionValue::Transfer(x) | ActionValue::Apparent(x) => x,
50        }
51    }
52
53    /// Returns the transfer action value of the U256-convertable raw value
54    pub fn transfer<T: Into<U256>>(transfer_value: T) -> ActionValue {
55        ActionValue::Transfer(transfer_value.into())
56    }
57
58    /// Returns the apparent action value of the U256-convertable raw value
59    pub fn apparent<T: Into<U256>>(apparent_value: T) -> ActionValue {
60        ActionValue::Apparent(apparent_value.into())
61    }
62}
63
64/// Action (call/create) input params. Everything else should be specified in
65/// Externalities.
66#[derive(Clone, Debug)]
67pub struct ActionParams {
68    /// Space
69    pub space: Space,
70    /// Address of currently executed code.
71    pub code_address: Address,
72    /// Hash of currently executed code.
73    pub code_hash: H256,
74    /// Receive address. Usually equal to code_address,
75    /// except when called using CALLCODE.
76    pub address: Address,
77    /// Sender of current part of the transaction.
78    pub sender: Address,
79    /// This is the address of original sender of the transaction.
80    pub original_sender: Address,
81    /// This is the address of account who will pay collateral for storage in
82    /// the whole execution.
83    pub storage_owner: Address,
84    /// Gas paid up front for transaction execution
85    pub gas: U256,
86    /// Gas price.
87    pub gas_price: U256,
88    /// Transaction value.
89    pub value: ActionValue,
90    /// Code being executed.
91    pub code: Option<Arc<Bytes>>,
92    /// Input data.
93    pub data: Option<Bytes>,
94    /// Type of call
95    pub call_type: CallType,
96    /// Type of create
97    pub create_type: CreateType,
98    /// Param types encoding
99    pub params_type: ParamsType,
100}
101
102#[cfg(any(test, feature = "testonly_code"))]
103impl Default for ActionParams {
104    /// Returns default ActionParams initialized with zeros
105    fn default() -> ActionParams {
106        use keccak_hash::KECCAK_EMPTY;
107        ActionParams {
108            space: Space::Native,
109            code_address: Address::default(),
110            code_hash: KECCAK_EMPTY,
111            address: Address::default(),
112            sender: Address::default(),
113            original_sender: Address::default(),
114            storage_owner: Address::default(),
115            gas: U256::zero(),
116            gas_price: U256::zero(),
117            value: ActionValue::Transfer(U256::zero()),
118            code: None,
119            data: None,
120            call_type: CallType::Call,
121            create_type: CreateType::None,
122            params_type: ParamsType::Separate,
123        }
124    }
125}