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

//! Flat trace module

use tetsy_rlp::{Rlp, RlpStream, Decodable, Encodable, DecoderError};
use tetsy_rlp_derive::{RlpEncodableWrapper, RlpDecodableWrapper};
use tetsy_util_mem::MallocSizeOf;
use vapory_types::Bloom;
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, MallocSizeOf)]
pub struct FlatTrace {
	#[ignore_malloc_size_of = "ignored for performance reason"]
	/// Type of action performed by a transaction.
	pub action: Action,
	#[ignore_malloc_size_of = "ignored for performance reason"]
	/// 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: Vec<usize>,
}

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

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_list::<usize, &usize>(&self.trace_address.iter().collect::<Vec<_>>());
	}
}

impl Decodable for FlatTrace {
	fn decode(d: &Rlp) -> Result<Self, DecoderError> {
		let v: Vec<usize> = d.list_at(3)?;
		let res = FlatTrace {
			action: d.val_at(0)?,
			result: d.val_at(1)?,
			subtraces: d.val_at(2)?,
			trace_address: v.into_iter().collect(),
		};

		Ok(res)
	}
}

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

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

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

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, RlpEncodableWrapper, RlpDecodableWrapper, MallocSizeOf)]
pub struct FlatBlockTraces(pub(crate) Vec<FlatTransactionTraces>);

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) -> Bloom {
		self.0.iter().fold(Default::default(), | bloom, tx_traces | bloom | tx_traces.bloom())
	}
}

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

#[cfg(test)]
mod tests {
	use tetsy_rlp::*;
	use crate::{
		FlatBlockTraces, FlatTransactionTraces, FlatTrace,
		trace::{Action, Res, CallResult, Call, CallType, Suicide, Reward, RewardType}
	};

	#[test]
	fn encode_flat_transaction_traces() {
		let ftt = FlatTransactionTraces::from(Vec::new());

		let mut s = RlpStream::new_list(2);
		s.append(&ftt);
		assert!(!s.is_finished(), "List shouldn't finished yet");
		s.append(&ftt);
		assert!(s.is_finished(), "List should be finished now");
		s.out();
	}

	#[test]
	fn encode_flat_block_traces() {
		let fbt = FlatBlockTraces::from(Vec::new());

		let mut s = RlpStream::new_list(2);
		s.append(&fbt);
		assert!(!s.is_finished(), "List shouldn't finished yet");
		s.append(&fbt);
		assert!(s.is_finished(), "List should be finished now");
		s.out();
	}

	#[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: Some(CallType::Call).into(),
			}),
			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: Some(CallType::Call).into(),
			}),
			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 flat_trace3 = FlatTrace {
			action: Action::Reward(Reward {
				author: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(),
				value: 10.into(),
				reward_type: RewardType::Uncle,
			}),
			result: Res::None,
			trace_address: vec![0].into_iter().collect(),
			subtraces: 0,
		};

		let flat_trace4 = FlatTrace {
			action: Action::Reward(Reward {
				author: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(),
				value: 10.into(),
				reward_type: RewardType::Block,
			}),
			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]),
			FlatTransactionTraces(vec![flat_trace3, flat_trace4])
		]);

		let encoded = ::tetsy_rlp::encode(&block_traces);
		let decoded = ::tetsy_rlp::decode(&encoded).expect("error decoding block traces");
		assert_eq!(block_traces, decoded);
	}
}