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