forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransactionQueue.h
90 lines (73 loc) · 2.49 KB
/
TransactionQueue.h
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
#pragma once
// Copyright 2019 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "crypto/SecretKey.h"
#include "herder/TxSetFrame.h"
#include "transactions/TransactionFrame.h"
#include "util/HashOfHash.h"
#include "util/XDROperators.h"
#include "xdr/Stellar-transaction.h"
#include <deque>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace medida
{
class Counter;
}
namespace stellar
{
class Application;
class TransactionQueue
{
public:
enum class AddResult
{
ADD_STATUS_PENDING = 0,
ADD_STATUS_DUPLICATE,
ADD_STATUS_ERROR,
ADD_STATUS_TRY_AGAIN_LATER,
ADD_STATUS_COUNT
};
struct AccountTxQueueInfo
{
SequenceNumber mMaxSeq{0};
int64_t mTotalFees{0};
friend bool operator==(AccountTxQueueInfo const& x,
AccountTxQueueInfo const& y);
};
struct TxMap
{
AccountTxQueueInfo mCurrentQInfo;
std::unordered_map<Hash, TransactionFramePtr> mTransactions;
void addTx(TransactionFramePtr);
void recalculate();
};
typedef std::unordered_map<AccountID, std::shared_ptr<TxMap>> AccountTxMap;
explicit TransactionQueue(Application& app, int pendingDepth, int banDepth);
AddResult tryAdd(TransactionFramePtr tx);
// it is the responsibility of the caller to always remove such sets of
// transactions that remaining ones have their sequence numbers increasing
// by one
void remove(std::vector<TransactionFramePtr> const& txs);
// remove oldest transactions and move all other transactions to slots older
// by one, this results in newest queue slot being empty
void shift();
AccountTxQueueInfo
getAccountTransactionQueueInfo(AccountID const& accountID) const;
int countBanned(int index) const;
bool isBanned(Hash const& hash) const;
std::shared_ptr<TxSetFrame> toTxSet(Hash const& lclHash) const;
private:
Application& mApp;
std::vector<medida::Counter*> mSizeByAge;
std::deque<AccountTxMap> mPendingTransactions;
std::deque<std::unordered_set<Hash>> mBannedTransactions;
bool contains(TransactionFramePtr tx) const;
};
static const char* TX_STATUS_STRING[static_cast<int>(
TransactionQueue::AddResult::ADD_STATUS_COUNT)] = {
"PENDING", "DUPLICATE", "ERROR", "TRY_AGAIN_LATER"};
}