openethereum/ethcore/src/log_entry.rs

121 lines
3.6 KiB
Rust
Raw Normal View History

2016-02-05 13:40:41 +01:00
// 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/>.
//! Block log.
2016-01-11 13:29:15 +01:00
use util::*;
use basic_types::LogBloom;
2016-02-03 13:20:32 +01:00
/// A record of execution for a `LOG` operation.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
2016-01-11 13:29:15 +01:00
pub struct LogEntry {
2016-02-03 13:20:32 +01:00
/// The address of the contract executing at the point of the `LOG` operation.
2016-01-11 13:29:15 +01:00
pub address: Address,
2016-02-03 13:20:32 +01:00
/// The topics associated with the `LOG` operation.
2016-01-11 13:29:15 +01:00
pub topics: Vec<H256>,
2016-02-03 13:20:32 +01:00
/// The data associated with the `LOG` operation.
2016-01-11 13:29:15 +01:00
pub data: Bytes,
}
2016-01-27 17:22:01 +01:00
impl Encodable for LogEntry {
2016-01-11 13:29:15 +01:00
fn rlp_append(&self, s: &mut RlpStream) {
2016-01-27 17:22:01 +01:00
s.begin_list(3);
2016-01-11 13:29:15 +01:00
s.append(&self.address);
2016-01-28 20:13:05 +01:00
s.append(&self.topics);
2016-01-11 13:29:15 +01:00
s.append(&self.data);
}
}
impl Decodable for LogEntry {
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
let d = decoder.as_rlp();
let entry = LogEntry {
address: try!(d.val_at(0)),
topics: try!(d.val_at(1)),
data: try!(d.val_at(2)),
};
Ok(entry)
}
}
impl HeapSizeOf for LogEntry {
fn heap_size_of_children(&self) -> usize {
self.topics.heap_size_of_children() + self.data.heap_size_of_children()
}
}
2016-01-11 13:29:15 +01:00
impl LogEntry {
2016-01-14 12:27:35 +01:00
/// Calculates the bloom of this log entry.
pub fn bloom(&self) -> LogBloom {
self.topics.iter().fold(LogBloom::from_bloomed(&self.address.sha3()), |b, t| b.with_bloomed(&t.sha3()))
}
2016-01-11 13:29:15 +01:00
}
2016-01-14 21:58:37 +01:00
impl FromJson for LogEntry {
/// Convert given JSON object to a LogEntry.
fn from_json(json: &Json) -> LogEntry {
// TODO: check bloom.
LogEntry {
address: xjson!(&json["address"]),
topics: xjson!(&json["topics"]),
data: xjson!(&json["data"]),
}
}
}
/// Log localized in a blockchain.
#[derive(Default, Debug, PartialEq)]
pub struct LocalizedLogEntry {
/// Plain log entry.
pub entry: LogEntry,
/// Block in which this log was created.
pub block_hash: H256,
/// Block number.
pub block_number: usize,
/// Hash of transaction in which this log was created.
pub transaction_hash: H256,
/// Index of transaction within block.
pub transaction_index: usize,
/// Log position in the block.
pub log_index: usize
}
impl Deref for LocalizedLogEntry {
type Target = LogEntry;
fn deref(&self) -> &Self::Target {
&self.entry
}
}
2016-01-11 13:29:15 +01:00
#[cfg(test)]
mod tests {
use util::*;
use super::LogEntry;
#[test]
fn test_empty_log_bloom() {
let bloom = H2048::from_str("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let log = LogEntry {
address: address,
topics: vec![],
data: vec![]
};
2016-01-11 13:29:15 +01:00
assert_eq!(log.bloom(), bloom);
}
}