1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

//! Vm execution env.

use bytes::Bytes;
use uint::Uint;
use hash::H256;
use blockchain::State;
use vm::{Transaction, Call, Env};

/// Represents vm execution environment before and after execution of transaction.
#[derive(Debug, PartialEq, Deserialize)]
pub struct Vm {
	/// Contract calls made internaly by executed transaction.
	#[serde(rename="callcreates")]
	pub calls: Option<Vec<Call>>,
	/// Env info.
	pub env: Env,
	/// Executed transaction
	#[serde(rename="exec")]
	pub transaction: Transaction,
	/// Gas left after transaction execution.
	#[serde(rename="gas")]
	pub gas_left: Option<Uint>,
	/// Hash of logs created during execution of transaction.
	pub logs: Option<H256>,
	/// Transaction output.
	#[serde(rename="out")]
	pub output: Option<Bytes>,
	/// Post execution vm state.
	#[serde(rename="post")]
	pub post_state: Option<State>,
	/// Pre execution vm state.
	#[serde(rename="pre")]
	pub pre_state: State,
}

impl Vm {
	/// Returns true if transaction execution run out of gas.
	pub fn out_of_gas(&self) -> bool {
		self.calls.is_none()
	}
}

#[cfg(test)]
mod tests {
	use serde_json;
	use vm::Vm;

	#[test]
	fn vm_deserialization() {
		let s = r#"{
			"callcreates" : [
			],
			"env" : {
				"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
				"currentDifficulty" : "0x0100",
				"currentGasLimit" : "0x0f4240",
				"currentNumber" : "0x00",
				"currentTimestamp" : "0x01"
			},
			"exec" : {
				"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
				"caller" : "cd1722f2947def4cf144679da39c4c32bdc35681",
				"code" : "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01600055",
				"data" : "0x",
				"gas" : "0x0186a0",
				"gasPrice" : "0x5af3107a4000",
				"origin" : "cd1722f2947def4cf144679da39c4c32bdc35681",
				"value" : "0x0de0b6b3a7640000"
			},
			"gas" : "0x013874",
			"logs" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
			"out" : "0x",
			"network" : "Frontier",
			"post" : {
				"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
					"balance" : "0x0de0b6b3a7640000",
					"code" : "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01600055",
					"nonce" : "0x00",
					"storage" : {
						"0x00" : "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
					}
				}
			},
			"pre" : {
				"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
					"balance" : "0x0de0b6b3a7640000",
					"code" : "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01600055",
					"nonce" : "0x00",
					"storage" : {
					}
				}
			}
		}"#;
		let _deserialized: Vm = serde_json::from_str(s).unwrap();
		// TODO: validate all fields
	}
}