mep_vm/
action_params.rs

1// Copyright 2015-2020 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum 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 Ethereum 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 Ethereum.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Evm input params.
18use ethereum_types::{U256, H256, Address};
19use bytes::Bytes;
20use hash::{keccak, KECCAK_EMPTY};
21use ethjson;
22
23use action_type::ActionType;
24
25use std::sync::Arc;
26
27/// Transaction value
28#[derive(Clone, Debug)]
29pub enum ActionValue {
30	/// Value that should be transfered
31	Transfer(U256),
32	/// Apparent value for transaction (not transfered)
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// TODO: should be a trait, possible to avoid cloning everything from a Transaction(/View).
65/// Action (call/create) input params. Everything else should be specified in Externalities.
66#[derive(Clone, Debug)]
67pub struct ActionParams {
68	/// Address of currently executed code.
69	pub code_address: Address,
70	/// Hash of currently executed code.
71	pub code_hash: Option<H256>,
72	/// Receive address. Usually equal to code_address,
73	/// except when called using CALLCODE.
74	pub address: Address,
75	/// Sender of current part of the transaction.
76	pub sender: Address,
77	/// Transaction initiator.
78	pub origin: Address,
79	/// Gas paid up front for transaction execution
80	pub gas: U256,
81	/// Gas price.
82	pub gas_price: U256,
83	/// Transaction value.
84	pub value: ActionValue,
85	/// Code being executed.
86	pub code: Option<Arc<Bytes>>,
87	/// Code version being executed.
88	pub code_version: U256,
89	/// Input data.
90	pub data: Option<Bytes>,
91	/// Type of action (e.g. CALL, DELEGATECALL, CREATE, etc.)
92	pub action_type: ActionType,
93	/// Param types encoding
94	pub params_type: ParamsType,
95}
96
97impl Default for ActionParams {
98	/// Returns default ActionParams initialized with zeros
99	fn default() -> ActionParams {
100		ActionParams {
101			code_address: Address::zero(),
102			code_hash: Some(KECCAK_EMPTY),
103			address: Address::zero(),
104			sender: Address::zero(),
105			origin: Address::zero(),
106			gas: U256::zero(),
107			gas_price: U256::zero(),
108			value: ActionValue::Transfer(U256::zero()),
109			code: None,
110			code_version: U256::zero(),
111			data: None,
112			action_type: ActionType::Create,
113			params_type: ParamsType::Separate,
114		}
115	}
116}
117
118impl From<ethjson::vm::Transaction> for ActionParams {
119	fn from(t: ethjson::vm::Transaction) -> Self {
120		let address: Address = t.address.into();
121		ActionParams {
122			code_address: Address::zero(),
123			code_hash: Some(keccak(&*t.code)),
124			address: address,
125			sender: t.sender.into(),
126			origin: t.origin.into(),
127			code: Some(Arc::new(t.code.into())),
128			code_version: t.code_version.into(),
129			data: Some(t.data.into()),
130			gas: t.gas.into(),
131			gas_price: t.gas_price.into(),
132			value: ActionValue::Transfer(t.value.into()),
133			action_type: ActionType::Call,
134			params_type: ParamsType::Separate,
135		}
136	}
137}