mashina 0.1.1

Vapory State Machine
Documentation
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Tetsy Vapory.

// Tetsy Vapory 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.

// Tetsy Vapory 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 Tetsy Vapory.  If not, see <http://www.gnu.org/licenses/>.

//! Transaction Execution environment.

use std::{cmp, sync::Arc};

use vapory_types::{H256, U256, Address, BigEndianHash};
use tetsy_bytes::Bytes;
use log::{debug, trace, warn};

use account_state::{Backend as StateBackend, State, CleanupMode};
use common_types::{
	transaction::UNSIGNED_SENDER,
	log_entry::LogEntry,
};
use vapcore_trace::{Tracer, VMTracer};
use tetsy_vm::{
	self, ActionParams, ActionValue, EnvInfo, ActionType, Schedule,
	Ext, ContractCreateResult, MessageCallResult, CreateContractAddress,
	ReturnData, TrapKind
};

use crate::{
	Machine,
	substate::Substate,
	executive::{
		Executive,
		contract_address,
		into_message_call_result,
		into_contract_create_result,
		cleanup_mode
	},
};

/// Policy for handling output data on `RETURN` opcode.
pub enum OutputPolicy {
	/// Return reference to fixed sized output.
	/// Used for message calls.
	Return,
	/// Init new contract as soon as `RETURN` is called.
	InitContract,
}

/// Transaction properties that externalities need to know about.
pub struct OriginInfo {
	address: Address,
	origin: Address,
	gas_price: U256,
	value: U256,
}

impl OriginInfo {
	/// Populates origin info from action params.
	pub fn from(params: &ActionParams) -> Self {
		OriginInfo {
			address: params.address.clone(),
			origin: params.origin.clone(),
			gas_price: params.gas_price,
			value: match params.value {
				ActionValue::Transfer(val) | ActionValue::Apparent(val) => val
			},
		}
	}
}

/// Implementation of vvm Externalities.
pub struct Externalities<'a, T: 'a, V: 'a, B: 'a> {
	state: &'a mut State<B>,
	env_info: &'a EnvInfo,
	depth: usize,
	stack_depth: usize,
	origin_info: &'a OriginInfo,
	substate: &'a mut Substate,
	mashina: &'a Machine,
	schedule: &'a Schedule,
	output: OutputPolicy,
	tracer: &'a mut T,
	vm_tracer: &'a mut V,
	static_flag: bool,
}

impl<'a, T: 'a, V: 'a, B: 'a> Externalities<'a, T, V, B>
	where T: Tracer, V: VMTracer, B: StateBackend
{
	/// Basic `Externalities` constructor.
	pub fn new(
		state: &'a mut State<B>,
		env_info: &'a EnvInfo,
		mashina: &'a Machine,
		schedule: &'a Schedule,
		depth: usize,
		stack_depth: usize,
		origin_info: &'a OriginInfo,
		substate: &'a mut Substate,
		output: OutputPolicy,
		tracer: &'a mut T,
		vm_tracer: &'a mut V,
		static_flag: bool,
	) -> Self {
		Externalities {
			state,
			env_info,
			depth,
			stack_depth,
			origin_info,
			substate,
			mashina,
			schedule,
			output,
			tracer,
			vm_tracer,
			static_flag,
		}
	}
}

impl<'a, T: 'a, V: 'a, B: 'a> Ext for Externalities<'a, T, V, B>
	where T: Tracer, V: VMTracer, B: StateBackend
{
	fn initial_storage_at(&self, key: &H256) -> tetsy_vm::Result<H256> {
		if self.state.is_base_storage_root_unchanged(&self.origin_info.address)? {
			self.state.checkpoint_storage_at(0, &self.origin_info.address, key).map(|v| v.unwrap_or_default()).map_err(Into::into)
		} else {
			warn!(target: "externalities", "Detected existing account {:#x} where a forced contract creation happened.", self.origin_info.address);
			Ok(H256::zero())
		}
	}

	fn storage_at(&self, key: &H256) -> tetsy_vm::Result<H256> {
		self.state.storage_at(&self.origin_info.address, key).map_err(Into::into)
	}

	fn set_storage(&mut self, key: H256, value: H256) -> tetsy_vm::Result<()> {
		if self.static_flag {
			Err(tetsy_vm::Error::MutableCallInStaticContext)
		} else {
			self.state.set_storage(&self.origin_info.address, key, value).map_err(Into::into)
		}
	}

	fn exists(&self, address: &Address) -> tetsy_vm::Result<bool> {
		self.state.exists(address).map_err(Into::into)
	}

	fn exists_and_not_null(&self, address: &Address) -> tetsy_vm::Result<bool> {
		self.state.exists_and_not_null(address).map_err(Into::into)
	}

	fn origin_balance(&self) -> tetsy_vm::Result<U256> {
		self.balance(&self.origin_info.address).map_err(Into::into)
	}

	fn balance(&self, address: &Address) -> tetsy_vm::Result<U256> {
		self.state.balance(address).map_err(Into::into)
	}

	fn blockhash(&mut self, number: &U256) -> H256 {
		if self.env_info.number + 256 >= self.mashina.params().eip210_transition {
			let blockhash_contract_address = self.mashina.params().eip210_contract_address;
			let code_res = self.state.code(&blockhash_contract_address)
				.and_then(|code| self.state.code_hash(&blockhash_contract_address).map(|hash| (code, hash)))
				.and_then(|(code, hash)| self.state.code_version(&blockhash_contract_address).map(|version| (code, hash, version)));

			let (code, code_hash, code_version) = match code_res {
				Ok((code, hash, version)) => (code, hash, version),
				Err(_) => return H256::zero(),
			};
			let data: H256 = BigEndianHash::from_uint(number);

			let params = ActionParams {
				sender: self.origin_info.address.clone(),
				address: blockhash_contract_address.clone(),
				value: ActionValue::Apparent(self.origin_info.value),
				code_address: blockhash_contract_address.clone(),
				origin: self.origin_info.origin.clone(),
				gas: self.mashina.params().eip210_contract_gas,
				gas_price: 0.into(),
				code,
				code_hash,
				code_version,
				data: Some(data.as_bytes().to_vec()),
				action_type: ActionType::Call,
				params_type: tetsy_vm::ParamsType::Separate,
			};

			let mut ex = Executive::new(self.state, self.env_info, self.mashina, self.schedule);
			let r = ex.call_with_crossbeam(params, self.substate, self.stack_depth + 1, self.tracer, self.vm_tracer);
			let output = match &r {
				Ok(ref r) => H256::from_slice(&r.return_data[..32]),
				_ => H256::zero(),
			};
			trace!("ext: blockhash contract({}) -> {:?}({}) self.env_info.number={}\n", number, r, output, self.env_info.number);
			output
		} else {
			// TODO: comment out what this function expects from env_info, since it will produce panics if the latter is inconsistent
			match *number < U256::from(self.env_info.number) && number.low_u64() >= cmp::max(256, self.env_info.number) - 256 {
				true => {
					let index = self.env_info.number - number.low_u64() - 1;
					assert!(index < self.env_info.last_hashes.len() as u64, format!("Inconsistent env_info, should contain at least {:?} last hashes", index+1));
					let r = self.env_info.last_hashes[index as usize].clone();
					trace!("ext: blockhash({}) -> {} self.env_info.number={}\n", number, r, self.env_info.number);
					r
				},
				false => {
					trace!("ext: blockhash({}) -> null self.env_info.number={}\n", number, self.env_info.number);
					H256::zero()
				},
			}
		}
	}

	fn create(
		&mut self,
		gas: &U256,
		value: &U256,
		code: &[u8],
		parent_version: &U256,
		address_scheme: CreateContractAddress,
		trap: bool,
	) -> ::std::result::Result<ContractCreateResult, TrapKind> {
		// create new contract address
		let (address, code_hash) = match self.state.nonce(&self.origin_info.address) {
			Ok(nonce) => contract_address(address_scheme, &self.origin_info.address, &nonce, &code),
			Err(e) => {
				debug!(target: "ext", "Database corruption encountered: {:?}", e);
				return Ok(ContractCreateResult::Failed)
			}
		};

		let create_type = match address_scheme {
			CreateContractAddress::FromSenderAndNonce => ActionType::Create,
			CreateContractAddress::FromSenderSaltAndCodeHash(_) => ActionType::Create2,
			CreateContractAddress::FromSenderAndCodeHash => ActionType::Create2,
		};

		// prepare the params
		let params = ActionParams {
			code_address: address.clone(),
			address: address.clone(),
			sender: self.origin_info.address.clone(),
			origin: self.origin_info.origin.clone(),
			gas: *gas,
			gas_price: self.origin_info.gas_price,
			value: ActionValue::Transfer(*value),
			code: Some(Arc::new(code.to_vec())),
			code_hash,
			code_version: *parent_version,
			data: None,
			action_type: create_type,
			params_type: tetsy_vm::ParamsType::Embedded,
		};

		if !self.static_flag {
			if !self.schedule.keep_unsigned_nonce || params.sender != UNSIGNED_SENDER {
				if let Err(e) = self.state.inc_nonce(&self.origin_info.address) {
					debug!(target: "ext", "Database corruption encountered: {:?}", e);
					return Ok(ContractCreateResult::Failed)
				}
			}
		}

		if trap {
			return Err(TrapKind::Create(params, address));
		}

		// TODO: handle internal error separately
		let mut ex = Executive::from_parent(self.state, self.env_info, self.mashina, self.schedule, self.depth, self.static_flag);
		let out = ex.create_with_crossbeam(params, self.substate, self.stack_depth + 1, self.tracer, self.vm_tracer);
		Ok(into_contract_create_result(out, &address, self.substate))
	}

	fn call(
		&mut self,
		gas: &U256,
		sender_address: &Address,
		receive_address: &Address,
		value: Option<U256>,
		data: &[u8],
		code_address: &Address,
		call_type: ActionType,
		trap: bool,
	) -> ::std::result::Result<MessageCallResult, TrapKind> {
		trace!(target: "externalities", "call");

		let code_res = self.state.code(code_address)
			.and_then(|code| self.state.code_hash(code_address).map(|hash| (code, hash)))
			.and_then(|(code, hash)| self.state.code_version(code_address).map(|version| (code, hash, version)));

		let (code, code_hash, code_version) = match code_res {
			Ok((code, hash, version)) => (code, hash, version),
			Err(_) => return Ok(MessageCallResult::Failed),
		};

		let mut params = ActionParams {
			sender: sender_address.clone(),
			address: receive_address.clone(),
			value: ActionValue::Apparent(self.origin_info.value),
			code_address: code_address.clone(),
			origin: self.origin_info.origin.clone(),
			gas: *gas,
			gas_price: self.origin_info.gas_price,
			code,
			code_hash,
			code_version,
			data: Some(data.to_vec()),
			action_type: call_type,
			params_type: tetsy_vm::ParamsType::Separate,
		};

		if let Some(value) = value {
			params.value = ActionValue::Transfer(value);
		}

		if trap {
			return Err(TrapKind::Call(params));
		}

		let mut ex = Executive::from_parent(self.state, self.env_info, self.mashina, self.schedule, self.depth, self.static_flag);
		let out = ex.call_with_crossbeam(params, self.substate, self.stack_depth + 1, self.tracer, self.vm_tracer);
		Ok(into_message_call_result(out))
	}

	fn extcode(&self, address: &Address) -> tetsy_vm::Result<Option<Arc<Bytes>>> {
		Ok(self.state.code(address)?)
	}

	fn extcodehash(&self, address: &Address) -> tetsy_vm::Result<Option<H256>> {
		if self.state.exists_and_not_null(address)? {
			Ok(self.state.code_hash(address)?)
		} else {
			Ok(None)
		}
	}

	fn extcodesize(&self, address: &Address) -> tetsy_vm::Result<Option<usize>> {
		Ok(self.state.code_size(address)?)
	}

	fn log(&mut self, topics: Vec<H256>, data: &[u8]) -> tetsy_vm::Result<()> {
		if self.static_flag {
			return Err(tetsy_vm::Error::MutableCallInStaticContext);
		}

		let address = self.origin_info.address.clone();
		self.substate.logs.push(LogEntry {
			address,
			topics,
			data: data.to_vec()
		});

		Ok(())
	}

	fn ret(self, gas: &U256, data: &ReturnData, apply_state: bool) -> tetsy_vm::Result<U256>
		where Self: Sized {
		match self.output {
			OutputPolicy::Return => {
				Ok(*gas)
			},
			OutputPolicy::InitContract if apply_state => {
				let return_cost = U256::from(data.len()) * U256::from(self.schedule.create_data_gas);
				if return_cost > *gas || data.len() > self.schedule.create_data_limit {
					return match self.schedule.exceptional_failed_code_deposit {
						true => Err(tetsy_vm::Error::OutOfGas),
						false => Ok(*gas)
					}
				}
				self.state.init_code(&self.origin_info.address, data.to_vec())?;
				Ok(*gas - return_cost)
			},
			OutputPolicy::InitContract => {
				Ok(*gas)
			},
		}
	}

	fn suicide(&mut self, refund_address: &Address) -> tetsy_vm::Result<()> {
		if self.static_flag {
			return Err(tetsy_vm::Error::MutableCallInStaticContext);
		}

		let address = self.origin_info.address.clone();
		let balance = self.balance(&address)?;
		if &address == refund_address {
			// TODO [todr] To be consistent with CPP client we set balance to 0 in that case.
			self.state.sub_balance(&address, &balance, &mut CleanupMode::NoEmpty)?;
		} else {
			trace!(target: "ext", "Suiciding {} -> {} (xfer: {})", address, refund_address, balance);
			self.state.transfer_balance(
				&address,
				refund_address,
				&balance,
				cleanup_mode(&mut self.substate, &self.schedule)
			)?;
		}

		self.tracer.trace_suicide(address, balance, refund_address.clone());
		self.substate.suicides.insert(address);

		Ok(())
	}

	fn schedule(&self) -> &Schedule {
		&self.schedule
	}

	fn env_info(&self) -> &EnvInfo {
		self.env_info
	}

	fn chain_id(&self) -> u64 {
		self.mashina.params().chain_id
	}

	fn depth(&self) -> usize {
		self.depth
	}

	fn add_sstore_refund(&mut self, value: usize) {
		self.substate.sstore_clears_refund += value as i128;
	}

	fn sub_sstore_refund(&mut self, value: usize) {
		self.substate.sstore_clears_refund -= value as i128;
	}

	fn trace_next_instruction(&mut self, pc: usize, instruction: u8, current_gas: U256) -> bool {
		self.vm_tracer.trace_next_instruction(pc, instruction, current_gas)
	}

	fn trace_prepare_execute(&mut self, pc: usize, instruction: u8, gas_cost: U256, mem_written: Option<(usize, usize)>, store_written: Option<(U256, U256)>) {
		self.vm_tracer.trace_prepare_execute(pc, instruction, gas_cost, mem_written, store_written)
	}

	fn trace_failed(&mut self) {
		self.vm_tracer.trace_failed();
	}

	fn trace_executed(&mut self, gas_used: U256, stack_push: &[U256], mem: &[u8]) {
		self.vm_tracer.trace_executed(gas_used, stack_push, mem)
	}

	fn is_static(&self) -> bool {
		return self.static_flag
	}
}

#[cfg(test)]
mod tests {
	use std::str::FromStr;
	use vapory_types::{U256, Address};
	use vvm::{EnvInfo, Ext, ActionType};
	use account_state::State;
	use vapcore::test_helpers::get_temp_state;
	use vapcore_trace::{NoopTracer, NoopVMTracer};

	use crate::{
		mashina::Machine,
		substate::Substate,
		test_helpers,
	};
	use super::*;

	fn get_test_origin() -> OriginInfo {
		OriginInfo {
			address: Address::zero(),
			origin: Address::zero(),
			gas_price: U256::zero(),
			value: U256::zero()
		}
	}

	fn get_test_env_info() -> EnvInfo {
		EnvInfo {
			number: 100,
			author: Address::from_low_u64_be(0),
			timestamp: 0,
			difficulty: 0.into(),
			last_hashes: Arc::new(vec![]),
			gas_used: 0.into(),
			gas_limit: 0.into(),
		}
	}

	struct TestSetup {
		state: State<::state_db::StateDB>,
		mashina: Machine,
		schedule: Schedule,
		sub_state: Substate,
		env_info: EnvInfo
	}

	impl Default for TestSetup {
		fn default() -> Self {
			TestSetup::new()
		}
	}

	impl TestSetup {
		fn new() -> Self {
			let mashina = test_helpers::load_mashina(include_bytes!("../../res/null_morden.json"));
			let env_info = get_test_env_info();
			let schedule = mashina.schedule(env_info.number);
			TestSetup {
				state: get_temp_state(),
				schedule,
				mashina,
				sub_state: Substate::new(),
				env_info,
			}
		}
	}

	#[test]
	fn can_be_created() {
		let mut setup = TestSetup::new();
		let state = &mut setup.state;
		let mut tracer = NoopTracer;
		let mut vm_tracer = NoopVMTracer;
		let origin_info = get_test_origin();

		let ext = Externalities::new(state, &setup.env_info, &setup.mashina, &setup.schedule, 0, 0, &origin_info, &mut setup.sub_state, OutputPolicy::InitContract, &mut tracer, &mut vm_tracer, false);

		assert_eq!(ext.env_info().number, 100);
	}

	#[test]
	fn can_return_block_hash_no_env() {
		let mut setup = TestSetup::new();
		let state = &mut setup.state;
		let mut tracer = NoopTracer;
		let mut vm_tracer = NoopVMTracer;
		let origin_info = get_test_origin();

		let mut ext = Externalities::new(state, &setup.env_info, &setup.mashina, &setup.schedule, 0, 0, &origin_info, &mut setup.sub_state, OutputPolicy::InitContract, &mut tracer, &mut vm_tracer, false);

		let hash = ext.blockhash(&"0000000000000000000000000000000000000000000000000000000000120000".parse::<U256>().unwrap());

		assert_eq!(hash, H256::zero());
	}

	#[test]
	fn can_return_block_hash() {
		let test_hash = H256::from_str("afafafafafafafafafafafbcbcbcbcbcbcbcbcbcbeeeeeeeeeeeeedddddddddd").unwrap();
		let test_env_number = 0x120001;

		let mut setup = TestSetup::new();
		{
			let env_info = &mut setup.env_info;
			env_info.number = test_env_number;
			let mut last_hashes = (*env_info.last_hashes).clone();
			last_hashes.push(test_hash.clone());
			env_info.last_hashes = Arc::new(last_hashes);
		}
		let state = &mut setup.state;
		let mut tracer = NoopTracer;
		let mut vm_tracer = NoopVMTracer;
		let origin_info = get_test_origin();

		let mut ext = Externalities::new(state, &setup.env_info, &setup.mashina, &setup.schedule, 0, 0, &origin_info, &mut setup.sub_state, OutputPolicy::InitContract, &mut tracer, &mut vm_tracer, false);

		let hash = ext.blockhash(&"0000000000000000000000000000000000000000000000000000000000120000".parse::<U256>().unwrap());

		assert_eq!(test_hash, hash);
	}

	#[test]
	#[should_panic]
	fn can_call_fail_empty() {
		let mut setup = TestSetup::new();
		let state = &mut setup.state;
		let mut tracer = NoopTracer;
		let mut vm_tracer = NoopVMTracer;
		let origin_info = get_test_origin();

		let mut ext = Externalities::new(state, &setup.env_info, &setup.mashina, &setup.schedule, 0, 0, &origin_info, &mut setup.sub_state, OutputPolicy::InitContract, &mut tracer, &mut vm_tracer, false);

		// this should panic because we have no balance on any account
		ext.call(
			&"0000000000000000000000000000000000000000000000000000000000120000".parse::<U256>().unwrap(),
			&Address::zero(),
			&Address::zero(),
			Some("0000000000000000000000000000000000000000000000000000000000150000".parse::<U256>().unwrap()),
			&[],
			&Address::zero(),
			ActionType::Call,
			false,
		).ok().unwrap();
	}

	#[test]
	fn can_log() {
		let log_data = vec![120u8, 110u8];
		let log_topics = vec![H256::from_str("af0fa234a6af46afa23faf23bcbc1c1cb4bcb7bcbe7e7e7ee3ee2edddddddddd").unwrap()];

		let mut setup = TestSetup::new();
		let state = &mut setup.state;
		let mut tracer = NoopTracer;
		let mut vm_tracer = NoopVMTracer;
		let origin_info = get_test_origin();

		{
			let mut ext = Externalities::new(state, &setup.env_info, &setup.mashina, &setup.schedule, 0, 0, &origin_info, &mut setup.sub_state, OutputPolicy::InitContract, &mut tracer, &mut vm_tracer, false);
			ext.log(log_topics, &log_data).unwrap();
		}

		assert_eq!(setup.sub_state.logs.len(), 1);
	}

	#[test]
	fn can_suicide() {
		let refund_account = &Address::zero();

		let mut setup = TestSetup::new();
		let state = &mut setup.state;
		let mut tracer = NoopTracer;
		let mut vm_tracer = NoopVMTracer;
		let origin_info = get_test_origin();

		{
			let mut ext = Externalities::new(state, &setup.env_info, &setup.mashina, &setup.schedule, 0, 0, &origin_info, &mut setup.sub_state, OutputPolicy::InitContract, &mut tracer, &mut vm_tracer, false);
			ext.suicide(refund_account).unwrap();
		}

		assert_eq!(setup.sub_state.suicides.len(), 1);
	}

	#[test]
	fn can_create() {
		use std::str::FromStr;

		let mut setup = TestSetup::new();
		let state = &mut setup.state;
		let mut tracer = NoopTracer;
		let mut vm_tracer = NoopVMTracer;
		let origin_info = get_test_origin();

		let address = {
			let mut ext = Externalities::new(state, &setup.env_info, &setup.mashina, &setup.schedule, 0, 0, &origin_info, &mut setup.sub_state, OutputPolicy::InitContract, &mut tracer, &mut vm_tracer, false);
			match ext.create(&U256::max_value(), &U256::zero(), &[], &U256::zero(), CreateContractAddress::FromSenderAndNonce, false) {
				Ok(ContractCreateResult::Created(address, _)) => address,
				_ => panic!("Test create failed; expected Created, got Failed/Reverted."),
			}
		};

		assert_eq!(address, Address::from_str("bd770416a3345f91e4b34576cb804a576fa48eb1").unwrap());
	}

	#[test]
	fn can_create2() {
		use std::str::FromStr;

		let mut setup = TestSetup::new();
		let state = &mut setup.state;
		let mut tracer = NoopTracer;
		let mut vm_tracer = NoopVMTracer;
		let origin_info = get_test_origin();

		let address = {
			let mut ext = Externalities::new(state, &setup.env_info, &setup.mashina, &setup.schedule, 0, 0, &origin_info, &mut setup.sub_state, OutputPolicy::InitContract, &mut tracer, &mut vm_tracer, false);

			match ext.create(&U256::max_value(), &U256::zero(), &[], &U256::zero(), CreateContractAddress::FromSenderSaltAndCodeHash(H256::zero()), false) {
				Ok(ContractCreateResult::Created(address, _)) => address,
				_ => panic!("Test create failed; expected Created, got Failed/Reverted."),
			}
		};

		assert_eq!(address, Address::from_str("e33c0c7f7df4809055c3eba6c09cfe4baf1bd9e0").unwrap());
	}
}