// Copyright 2015, 2016 Ethcore (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/>.

//! Flat trace module

use std::collections::VecDeque;
use rlp::*;
use util::HeapSizeOf;
use basic_types::LogBloom;
use super::trace::{Action, Res};

/// Trace localized in vector of traces produced by a single transaction.
///
/// Parent and children indexes refer to positions in this vector.
#[derive(Debug, PartialEq, Clone, Binary)]
pub struct FlatTrace {
	/// Type of action performed by a transaction.
	pub action: Action,
	/// Result of this action.
	pub result: Res,
	/// Number of subtraces.
	pub subtraces: usize,
	/// Exact location of trace.
	///
	/// [index in root, index in first CALL, index in second CALL, ...]
	pub trace_address: VecDeque<usize>,
}

impl FlatTrace {
	/// Returns bloom of the trace.
	pub fn bloom(&self) -> LogBloom {
		self.action.bloom() | self.result.bloom()
	}
}

impl HeapSizeOf for FlatTrace {
	fn heap_size_of_children(&self) -> usize {
		self.trace_address.heap_size_of_children()
	}
}

impl Encodable for FlatTrace {
	fn rlp_append(&self, s: &mut RlpStream) {
		s.begin_list(4);
		s.append(&self.action);
		s.append(&self.result);
		s.append(&self.subtraces);
		s.append(&self.trace_address.clone().into_iter().collect::<Vec<_>>());
	}
}

impl Decodable for FlatTrace {
	fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
		let d = decoder.as_rlp();
		let v: Vec<usize> = try!(d.val_at(3));
		let res = FlatTrace {
			action: try!(d.val_at(0)),
			result: try!(d.val_at(1)),
			subtraces: try!(d.val_at(2)),
			trace_address: v.into_iter().collect(),
		};

		Ok(res)
	}
}

/// Represents all traces produced by a single transaction.
#[derive(Debug, PartialEq, Clone)]
pub struct FlatTransactionTraces(Vec<FlatTrace>);

impl From<Vec<FlatTrace>> for FlatTransactionTraces {
	fn from(v: Vec<FlatTrace>) -> Self 

}

impl HeapSizeOf for FlatTransactionTraces {
	fn heap_size_of_children(&self) -> usize {
		self.0.heap_size_of_children()
	}
}

impl FlatTransactionTraces {
	/// Returns bloom of all traces in the collection.
	pub fn bloom(&self) -> LogBloom {
		self.0.iter().fold(Default::default(), | bloom, trace | bloom | trace.bloom())
	}
}

impl Encodable for FlatTransactionTraces {
	fn rlp_append(&self, s: &mut RlpStream) {
		s.append(&self.0);
	}
}

impl Decodable for FlatTransactionTraces {
	fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
		Ok(FlatTransactionTraces(try!(Decodable::decode(decoder))))
	}
}

impl Into<Vec<FlatTrace>> for FlatTransactionTraces {
	fn into(self) -> Vec<FlatTrace> {
		self.0
	}
}

/// Represents all traces produced by transactions in a single block.
#[derive(Debug, PartialEq, Clone, Default)]
pub struct FlatBlockTraces(Vec<FlatTransactionTraces>);

impl HeapSizeOf for FlatBlockTraces {
	fn heap_size_of_children(&self) -> usize {
		self.0.heap_size_of_children()
	}
}

impl From<Vec<FlatTransactionTraces>> for FlatBlockTraces {
	fn from(v: Vec<FlatTransactionTraces>) -> Self {
		FlatBlockTraces(v)
	}
}

impl FlatBlockTraces {
	/// Returns bloom of all traces in the block.
	pub fn bloom(&self) -> LogBloom {
		self.0.iter().fold(Default::default(), | bloom, tx_traces | bloom | tx_traces.bloom())
	}
}

impl Encodable for FlatBlockTraces {
	fn rlp_append(&self, s: &mut RlpStream) {
		s.append(&self.0);
	}
}

impl Decodable for FlatBlockTraces {
	fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
		Ok(FlatBlockTraces(try!(Decodable::decode(decoder))))
	}
}

impl Into<Vec<FlatTransactionTraces>> for FlatBlockTraces {
	fn into(self) -> Vec<FlatTransactionTraces> {
		self.0
	}
}

#[cfg(test)]
mod tests {
	use super::{FlatBlockTraces, FlatTransactionTraces, FlatTrace};
	use trace::trace::{Action, Res, CallResult, Call, Suicide};
	use types::executed::CallType;

	#[test]
	fn test_trace_serialization() {
		// block #51921

		let flat_trace = FlatTrace {
			action: Action::Call(Call {
				from: "8dda5e016e674683241bf671cced51e7239ea2bc".parse().unwrap(),
				to: "37a5e19cc2d49f244805d5c268c0e6f321965ab9".parse().unwrap(),
				value: "3627e8f712373c0000".parse().unwrap(),
				gas: 0x03e8.into(),
				input: vec![],
				call_type: CallType::Call,
			}),
			result: Res::Call(CallResult {
				gas_used: 0.into(),
				output: vec![],
			}),
			trace_address: Default::default(),
			subtraces: 0,
		};

		let flat_trace1 = FlatTrace {
			action: Action::Call(Call {
				from: "3d0768da09ce77d25e2d998e6a7b6ed4b9116c2d".parse().unwrap(),
				to: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(),
				value: 0.into(),
				gas: 0x010c78.into(),
				input: vec![0x41, 0xc0, 0xe1, 0xb5],
				call_type: CallType::Call,
			}),
			result: Res::Call(CallResult {
				gas_used: 0x0127.into(),
				output: vec![],
			}),
			trace_address: Default::default(),
			subtraces: 1,
		};

		let flat_trace2 = FlatTrace {
			action: Action::Suicide(Suicide {
				address: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(),
				balance: 0.into(),
				refund_address: "3d0768da09ce77d25e2d998e6a7b6ed4b9116c2d".parse().unwrap(),
			}),
			result: Res::None,
			trace_address: vec![0].into_iter().collect(),
			subtraces: 0,
		};

		let block_traces = FlatBlockTraces(vec![
			FlatTransactionTraces(vec![flat_trace]),
			FlatTransactionTraces(vec![flat_trace1, flat_trace2])
		]);

		let encoded = ::rlp::encode(&block_traces);
		let decoded = ::rlp::decode(&encoded);
		assert_eq!(block_traces, decoded);
	}
}

{
		FlatTransactionTraces(v)
	}