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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

// Copyright 2021 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use crate::pos::mempool::{
    core_mempool::{
        index::{
            AccountTransactionIter, AccountTransactions, TTLIndex,
            TimelineIndex,
        },
        transaction::{MempoolTransaction, TimelineState},
        ttl_cache::TtlCache,
    },
    counters,
    logging::{LogEntry, LogEvent, LogSchema, TxnsLog},
};
use diem_config::config::MempoolConfig;
use diem_crypto::{hash::CryptoHash, HashValue};
use diem_logger::prelude::*;
use diem_types::{
    account_address::AccountAddress,
    mempool_status::{MempoolStatus, MempoolStatusCode},
    transaction::{SignedTransaction, TransactionPayload},
};
use std::{
    collections::{hash_map::Values, HashMap, HashSet},
    time::{Duration, SystemTime},
};

/// TransactionStore is in-memory storage for all transactions in mempool.
pub struct TransactionStore {
    // normal transactions
    transactions: AccountTransactions,
    // pivot decision helper structure
    pivot_decisions: HashMap<HashValue, HashSet<(AccountAddress, HashValue)>>,

    // TTLIndex based on client-specified expiration time
    expiration_time_index: TTLIndex,
    // TTLIndex based on system expiration time
    // we keep it separate from `expiration_time_index` so Mempool can't be
    // clogged  by old transactions even if it hasn't received commit
    // callbacks for a while
    system_ttl_index: TTLIndex,
    timeline_index: TimelineIndex,

    // configuration
    _capacity: usize,
}

pub type PivotDecisionIter<'a> =
    Values<'a, HashValue, HashSet<(AccountAddress, HashValue)>>;

impl TransactionStore {
    pub(crate) fn new(config: &MempoolConfig) -> Self {
        Self {
            // main DS
            transactions: AccountTransactions::new(),
            pivot_decisions: HashMap::new(),

            // various indexes
            system_ttl_index: TTLIndex::new(Box::new(
                |t: &MempoolTransaction| t.expiration_time,
            )),
            expiration_time_index: TTLIndex::new(Box::new(
                |t: &MempoolTransaction| {
                    Duration::from_secs(t.txn.expiration_timestamp_secs())
                },
            )),
            timeline_index: TimelineIndex::new(),

            // configuration
            _capacity: config.capacity,
        }
    }

    /// Fetch transaction by account address + hash.
    pub(crate) fn get(&self, hash: &HashValue) -> Option<SignedTransaction> {
        if let Some(txn) = self.transactions.get(hash) {
            return Some(txn.txn.clone());
        }
        None
    }

    /// Fetch pivot decisions by pivot hash.
    pub(crate) fn get_pivot_decisions(
        &self, hash: &HashValue,
    ) -> Vec<HashValue> {
        if let Some(decisions) = self.pivot_decisions.get(hash) {
            decisions
                .iter()
                .map(|(_, tx_hash)| tx_hash.clone())
                .collect::<_>()
        } else {
            vec![]
        }
    }

    /// Insert transaction into TransactionStore. Performs validation checks and
    /// updates indexes.
    pub(crate) fn insert(
        &mut self, mut txn: MempoolTransaction,
    ) -> MempoolStatus {
        let address = txn.get_sender();
        let hash = txn.get_hash();
        let has_tx = self.get(&hash).is_some();

        if has_tx {
            return MempoolStatus::new(MempoolStatusCode::Accepted);
        }

        self.timeline_index.insert(&mut txn);

        // TODO(linxi): evict transaction when mempool is full

        // insert into storage and other indexes
        self.system_ttl_index.insert(&txn);
        self.expiration_time_index.insert(&txn);

        let payload = txn.txn.clone().into_raw_transaction().into_payload();
        if let TransactionPayload::PivotDecision(pivot_decision) = payload {
            let pivot_decision_hash = pivot_decision.hash();
            self.pivot_decisions
                .entry(pivot_decision_hash)
                .or_insert_with(HashSet::new);
            if let Some(account_decision) =
                self.pivot_decisions.get_mut(&pivot_decision_hash)
            {
                diem_debug!("txpool::insert pivot {:?}", hash);
                account_decision.insert((address, hash));
            }
            self.transactions.insert(hash, txn, true);
        } else {
            self.transactions.insert(hash, txn, false);
        }
        self.track_indices();
        diem_debug!(
            LogSchema::new(LogEntry::AddTxn)
                .txns(TxnsLog::new_txn(address, hash)),
            hash = hash,
            has_tx = has_tx
        );

        MempoolStatus::new(MempoolStatusCode::Accepted)
    }

    fn track_indices(&self) {
        counters::core_mempool_index_size(
            counters::SYSTEM_TTL_INDEX_LABEL,
            self.system_ttl_index.size(),
        );
        counters::core_mempool_index_size(
            counters::EXPIRATION_TIME_INDEX_LABEL,
            self.expiration_time_index.size(),
        );
        counters::core_mempool_index_size(
            counters::TIMELINE_INDEX_LABEL,
            self.timeline_index.size(),
        );
    }

    /// Handles transaction commit.
    /// It includes deletion of all transactions with sequence number <=
    /// `account_sequence_number` and potential promotion of sequential txns
    /// to PriorityIndex/TimelineIndex.
    pub(crate) fn commit_transaction(
        &mut self, _account: &AccountAddress, hash: HashValue,
    ) {
        let mut txns_log = TxnsLog::new();
        if let Some(transaction) = self.transactions.remove(&hash) {
            txns_log.add(transaction.get_sender(), transaction.get_hash());
            self.index_remove(&transaction);
            // handle pivot decision
            let payload = transaction.txn.into_raw_transaction().into_payload();
            if let TransactionPayload::PivotDecision(pivot_decision) = payload {
                let pivot_decision_hash = pivot_decision.hash();
                if let Some(indices) =
                    self.pivot_decisions.remove(&pivot_decision_hash)
                {
                    for (_, hash) in indices {
                        if let Some(txn) = self.transactions.remove(&hash) {
                            txns_log.add(txn.get_sender(), txn.get_hash());
                            self.index_remove(&txn);
                        }
                    }
                }
            }
        }
        diem_debug!(LogSchema::new(LogEntry::CleanCommittedTxn).txns(txns_log));
    }

    pub(crate) fn reject_transaction(
        &mut self, account: &AccountAddress, _hash: HashValue,
    ) {
        let mut txns_log = TxnsLog::new();
        let mut hashes = Vec::new();
        for txn in self.transactions.iter() {
            if txn.get_sender() == *account {
                txns_log.add(txn.get_sender(), txn.get_hash());
                hashes.push(txn.get_hash());
            }
        }
        for txn in self.transactions.iter_pivot_decision() {
            if txn.get_sender() == *account {
                txns_log.add(txn.get_sender(), txn.get_hash());
                hashes.push(txn.get_hash());
            }
        }
        for hash in hashes {
            if let Some(txn) = self.transactions.remove(&hash) {
                self.index_remove(&txn);
            }
        }
        diem_debug!(LogSchema::new(LogEntry::CleanRejectedTxn).txns(txns_log));
    }

    /// Removes transaction from all indexes.
    fn index_remove(&mut self, txn: &MempoolTransaction) {
        counters::CORE_MEMPOOL_REMOVED_TXNS.inc();
        self.system_ttl_index.remove(&txn);
        self.expiration_time_index.remove(&txn);
        self.timeline_index.remove(&txn);
        self.track_indices();
    }

    /// Read `count` transactions from timeline since `timeline_id`.
    /// Returns block of transactions and new last_timeline_id.
    pub(crate) fn read_timeline(
        &mut self, timeline_id: u64, count: usize,
    ) -> (Vec<SignedTransaction>, u64) {
        let mut batch = vec![];
        let mut last_timeline_id = timeline_id;
        for (_, hash) in self.timeline_index.read_timeline(timeline_id, count) {
            if let Some(txn) = self.transactions.get(&hash) {
                batch.push(txn.txn.clone());
                if let TimelineState::Ready(timeline_id) = txn.timeline_state {
                    last_timeline_id = timeline_id;
                }
            }
        }
        (batch, last_timeline_id)
    }

    pub(crate) fn timeline_range(
        &mut self, start_id: u64, end_id: u64,
    ) -> Vec<SignedTransaction> {
        self.timeline_index
            .timeline_range(start_id, end_id)
            .iter()
            .filter_map(|(_, hash)| {
                self.transactions.get(hash).map(|txn| txn.txn.clone())
            })
            .collect()
    }

    /// Garbage collect old transactions.
    pub(crate) fn gc_by_system_ttl(
        &mut self,
        metrics_cache: &TtlCache<(AccountAddress, HashValue), SystemTime>,
    ) {
        let now = diem_infallible::duration_since_epoch();

        self.gc(now, true, metrics_cache);
    }

    /// Garbage collect old transactions based on client-specified expiration
    /// time.
    pub(crate) fn gc_by_expiration_time(
        &mut self, block_time: Duration,
        metrics_cache: &TtlCache<(AccountAddress, HashValue), SystemTime>,
    ) {
        self.gc(block_time, false, metrics_cache);
    }

    fn gc(
        &mut self, now: Duration, by_system_ttl: bool,
        _metrics_cache: &TtlCache<(AccountAddress, HashValue), SystemTime>,
    ) {
        let (metric_label, index, log_event) = if by_system_ttl {
            (
                counters::GC_SYSTEM_TTL_LABEL,
                &mut self.system_ttl_index,
                LogEvent::SystemTTLExpiration,
            )
        } else {
            (
                counters::GC_CLIENT_EXP_LABEL,
                &mut self.expiration_time_index,
                LogEvent::ClientExpiration,
            )
        };
        counters::CORE_MEMPOOL_GC_EVENT_COUNT
            .with_label_values(&[metric_label])
            .inc();

        let mut gc_txns = index.gc(now);
        // sort the expired txns by order of sequence number per account
        gc_txns.sort_by_key(|key| (key.address, key.hash));
        let mut gc_iter = gc_txns.iter().peekable();

        let mut gc_txns_log = TxnsLog::new();
        while let Some(key) = gc_iter.next() {
            if let Some(txn) = self.transactions.remove(&key.hash) {
                gc_txns_log.add(txn.get_sender(), txn.get_hash());
                self.index_remove(&txn);
                if let TransactionPayload::PivotDecision(pivot_decision) =
                    txn.txn.into_raw_transaction().into_payload()
                {
                    self.pivot_decisions.remove(&pivot_decision.hash());
                }
            }
        }

        diem_debug!(LogSchema::event_log(LogEntry::GCRemoveTxns, log_event)
            .txns(gc_txns_log));
        self.track_indices();
    }

    pub(crate) fn iter(&self) -> AccountTransactionIter {
        self.transactions.iter()
    }

    pub(crate) fn iter_pivot_decision(&self) -> PivotDecisionIter {
        self.pivot_decisions.values()
    }
}