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
use crate::{
    blockcfg::{Value, ValueError},
    fragment::{Fragment, FragmentId},
};
use chain_core::property::Serialize;
use std::time::SystemTime;

pub struct PoolEntry {
    // reference of the fragment stored in the pool
    fragment_ref: FragmentId,
    /// fee of the fragment, does not include the fee of
    /// descendants entries or ancestors
    fragment_fee: Value,
    /// size of the fragment in the memory pool
    fragment_size: usize,
    /// time when the entry was added to the pool
    received_at: SystemTime,
    /// the fee of the accumulated descendant fragments
    /// does not include the fee of this entry
    descendants_fee: Value,
    /// the size of the accumulated descendant fragments
    /// Does not include the size of this entry
    descendants_size: usize,
    /// the fee of the accumulated ancestor fragments
    /// does not include the fee of this entry
    ancestors_fee: Value,
    /// the size of the accumulated ancestor fragments
    /// Does not include the size of this entry
    ancestors_size: usize,
}

impl PoolEntry {
    pub fn new(fragment: &Fragment) -> Self {
        let fragment_size = fragment.serialized_size();
        let fragment_ref = fragment.hash();
        // TODO: the fragment fee is not yet computed. Yet we should
        // have an explicit fee in the message. So we need to be able
        // to extract this information without the need to compute the
        // fee from the ledger's fee settings.
        let fragment_fee = Value::zero();

        PoolEntry {
            fragment_ref,
            fragment_fee,
            fragment_size,
            received_at: SystemTime::now(),

            // when this entry is added in the pool, it has no
            // descendant
            descendants_fee: Value::zero(),
            descendants_size: 0usize,

            // when this entry is added to the pool, we need to know
            // about the different entries in order to compute the following:
            ancestors_fee: Value::zero(),
            ancestors_size: 0usize,
        }
    }

    #[inline]
    pub fn fragment_ref(&self) -> &FragmentId {
        &self.fragment_ref
    }
    #[inline]
    pub fn fragment_fee(&self) -> &Value {
        &self.fragment_fee
    }
    #[inline]
    pub fn fragment_size(&self) -> &usize {
        &self.fragment_size
    }
    #[inline]
    pub fn received_at(&self) -> &SystemTime {
        &self.received_at
    }
    #[inline]
    pub fn with_descendants_fee(&self) -> Result<Value, ValueError> {
        self.descendants_fee + self.fragment_fee
    }
    #[inline]
    pub fn with_descendants_size(&self) -> usize {
        self.descendants_size + self.fragment_size
    }
    #[inline]
    pub fn with_ancestors_fee(&self) -> Result<Value, ValueError> {
        self.ancestors_fee + self.fragment_fee
    }
    #[inline]
    pub fn with_ancestors_size(&self) -> usize {
        self.ancestors_size + self.fragment_size
    }
}