primitives/
transaction_index.rs

1// Copyright 2019 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
5use crate::CompatBool;
6use cfx_types::H256;
7use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
8use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
9
10/// Represents address of certain transaction within block
11#[derive(Debug, PartialEq, Eq, Hash, Clone, Default)]
12pub struct TransactionIndex {
13    /// Block hash
14    pub block_hash: H256,
15    /// Transaction index within the block
16    pub real_index: usize,
17    /// true when this index belongs to a phantom transaction
18    pub is_phantom: bool,
19    /// Transaction index to be used in RPC responses
20    pub rpc_index: Option<usize>,
21}
22
23impl MallocSizeOf for TransactionIndex {
24    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { 0 }
25}
26
27impl Encodable for TransactionIndex {
28    fn rlp_append(&self, s: &mut RlpStream) {
29        s.begin_list(4);
30        s.append(&self.block_hash);
31        s.append(&self.real_index);
32        s.append(&CompatBool(self.is_phantom));
33        s.append(&self.rpc_index);
34    }
35}
36
37impl Decodable for TransactionIndex {
38    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
39        match rlp.item_count()? {
40            2 => Ok(TransactionIndex {
41                block_hash: rlp.val_at(0)?,
42                real_index: rlp.val_at(1)?,
43                is_phantom: false,
44                rpc_index: None,
45            }),
46            3 => Ok(TransactionIndex {
47                block_hash: rlp.val_at(0)?,
48                real_index: rlp.val_at(1)?,
49                is_phantom: rlp.val_at::<CompatBool>(2)?.0,
50                rpc_index: None,
51            }),
52            4 => Ok(TransactionIndex {
53                block_hash: rlp.val_at(0)?,
54                real_index: rlp.val_at(1)?,
55                is_phantom: rlp.val_at::<CompatBool>(2)?.0,
56                rpc_index: rlp.val_at(3)?,
57            }),
58            _ => Err(DecoderError::RlpInvalidLength),
59        }
60    }
61}