cfx_rpc_eth_types/log.rs
1// Copyright 2019-2021 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5// Copyright 2015-2020 Parity Technologies (UK) Ltd.
6// This file is part of OpenEthereum.
7
8// OpenEthereum is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// OpenEthereum is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
20
21use crate::{Bytes, Error};
22use cfx_rpc_cfx_types::traits::BlockProvider;
23use cfx_types::{H160, H256, U256, U64};
24use primitives::{
25 log_entry::{LocalizedLogEntry, LogEntry},
26 EpochNumber,
27};
28use serde::{Deserialize, Serialize};
29
30/// Log
31#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
32#[serde(rename_all = "camelCase")]
33pub struct Log {
34 #[serde(flatten)]
35 /// Consensus log object
36 pub inner: LogData,
37 /// Block Hash
38 pub block_hash: H256,
39 /// Block Number
40 pub block_number: U256,
41 /// Transaction Hash
42 pub transaction_hash: H256,
43 /// Transaction Index
44 pub transaction_index: U256,
45 /// Log Index in Block
46 pub log_index: Option<U256>,
47 /// Log Index in Transaction
48 pub transaction_log_index: Option<U256>,
49 #[serde(skip_serializing_if = "Option::is_none", default)]
50 pub block_timestamp: Option<U64>,
51 /// Whether Log Type is Removed (Geth Compatibility Field)
52 #[serde(default)]
53 pub removed: bool,
54}
55
56impl Log {
57 pub fn try_from_localized(
58 e: LocalizedLogEntry, consensus: impl BlockProvider, removed: bool,
59 ) -> Result<Log, Error> {
60 // find pivot hash
61 let epoch = consensus.get_block_epoch_number(&e.block_hash).ok_or(
62 Error::InvalidParams(
63 "blockHash".to_string(),
64 "Unknown block".to_string(),
65 ),
66 )?;
67
68 let hashes = consensus
69 .get_block_hashes_by_epoch(EpochNumber::Number(epoch))
70 .map_err(|_| {
71 Error::InvalidParams(
72 "blockHash".to_string(),
73 "Unknown block".to_string(),
74 )
75 })?;
76
77 let pivot_hash = hashes
78 .last()
79 .ok_or(Error::InternalError("Inconsistent state".to_string()))?;
80
81 // construct RPC log
82 Ok(Log {
83 inner: LogData {
84 address: e.entry.address,
85 topics: e.entry.topics.into_iter().map(Into::into).collect(),
86 data: e.entry.data.into(),
87 },
88 block_hash: *pivot_hash,
89 // note: blocks in EVM space RPCs correspond to epochs
90 block_number: e.epoch_number.into(),
91 block_timestamp: e.block_timestamp.map(Into::into),
92 transaction_hash: e.transaction_hash.into(),
93 transaction_index: e.transaction_index.into(),
94 log_index: Some(e.log_index.into()),
95 transaction_log_index: Some(e.transaction_log_index.into()),
96 removed,
97 })
98 }
99
100 pub fn try_from(_e: LogEntry) -> Result<Log, String> {
101 unimplemented!();
102 // Ok(Log {
103 // address: RpcAddress::try_from_h160(e.address, network)?,
104 // topics: e.topics.into_iter().map(Into::into).collect(),
105 // data: e.data.into(),
106 // block_hash: None,
107 // epoch_number: None,
108 // transaction_hash: None,
109 // transaction_index: None,
110 // log_index: None,
111 // transaction_log_index: None,
112 // })
113 }
114}
115
116#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
117pub struct LogData {
118 pub address: H160,
119 pub topics: Vec<H256>,
120 pub data: Bytes,
121}