cfx_rpc_eth_types/log.rs
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
// Copyright 2019-2021 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.
// OpenEthereum 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.
// OpenEthereum 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 OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.
use crate::{Bytes, Error};
use cfx_rpc_cfx_types::traits::BlockProvider;
use cfx_types::{H160, H256, U256, U64};
use primitives::{
    log_entry::{LocalizedLogEntry, LogEntry},
    EpochNumber,
};
use serde::{Deserialize, Serialize};
/// Log
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Log {
    #[serde(flatten)]
    /// Consensus log object
    pub inner: LogData,
    /// Block Hash
    pub block_hash: H256,
    /// Block Number
    pub block_number: U256,
    /// Transaction Hash
    pub transaction_hash: H256,
    /// Transaction Index
    pub transaction_index: U256,
    /// Log Index in Block
    pub log_index: Option<U256>,
    /// Log Index in Transaction
    pub transaction_log_index: Option<U256>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub block_timestamp: Option<U64>,
    /// Whether Log Type is Removed (Geth Compatibility Field)
    #[serde(default)]
    pub removed: bool,
}
impl Log {
    pub fn try_from_localized(
        e: LocalizedLogEntry, consensus: impl BlockProvider, removed: bool,
    ) -> Result<Log, Error> {
        // find pivot hash
        let epoch = consensus.get_block_epoch_number(&e.block_hash).ok_or(
            Error::InvalidParams(
                "blockHash".to_string(),
                "Unknown block".to_string(),
            ),
        )?;
        let hashes = consensus
            .get_block_hashes_by_epoch(EpochNumber::Number(epoch))
            .map_err(|_| {
                Error::InvalidParams(
                    "blockHash".to_string(),
                    "Unknown block".to_string(),
                )
            })?;
        let pivot_hash = hashes
            .last()
            .ok_or(Error::InternalError("Inconsistent state".to_string()))?;
        // construct RPC log
        Ok(Log {
            inner: LogData {
                address: e.entry.address,
                topics: e.entry.topics.into_iter().map(Into::into).collect(),
                data: e.entry.data.into(),
            },
            block_hash: *pivot_hash,
            // note: blocks in EVM space RPCs correspond to epochs
            block_number: e.epoch_number.into(),
            block_timestamp: e.block_timestamp.map(Into::into),
            transaction_hash: e.transaction_hash.into(),
            transaction_index: e.transaction_index.into(),
            log_index: Some(e.log_index.into()),
            transaction_log_index: Some(e.transaction_log_index.into()),
            removed,
        })
    }
    pub fn try_from(_e: LogEntry) -> Result<Log, String> {
        unimplemented!();
        // Ok(Log {
        //     address: RpcAddress::try_from_h160(e.address, network)?,
        //     topics: e.topics.into_iter().map(Into::into).collect(),
        //     data: e.data.into(),
        //     block_hash: None,
        //     epoch_number: None,
        //     transaction_hash: None,
        //     transaction_index: None,
        //     log_index: None,
        //     transaction_log_index: None,
        // })
    }
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
pub struct LogData {
    pub address: H160,
    pub topics: Vec<H256>,
    pub data: Bytes,
}