rippled
Loading...
Searching...
No Matches
LocalTxs.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/ledger/Ledger.h>
21#include <xrpld/app/ledger/LocalTxs.h>
22#include <xrpl/protocol/Indexes.h>
23
24/*
25 This code prevents scenarios like the following:
261) A client submits a transaction.
272) The transaction gets into the ledger this server
28 believes will be the consensus ledger.
293) The server builds a succeeding open ledger without the
30 transaction (because it's in the prior ledger).
314) The local consensus ledger is not the majority ledger
32 (due to network conditions, Byzantine fault, etcetera)
33 the majority ledger does not include the transaction.
345) The server builds a new open ledger that does not include
35 the transaction or have it in a prior ledger.
366) The client submits another transaction and gets a terPRE_SEQ
37 preliminary result.
387) The server does not relay that second transaction, at least
39 not yet.
40
41With this code, when step 5 happens, the first transaction will
42be applied to that open ledger so the second transaction will
43succeed normally at step 6. Transactions remain tracked and
44test-applied to all new open ledgers until seen in a fully-
45validated ledger
46*/
47
48namespace ripple {
49
50// This class wraps a pointer to a transaction along with
51// its expiration ledger. It also caches the issuing account.
53{
54public:
55 // The number of ledgers to hold a transaction is essentially
56 // arbitrary. It should be sufficient to allow the transaction to
57 // get into a fully-validated ledger.
58 static int const holdLedgers = 5;
59
61 : m_txn(txn)
62 , m_expire(index + holdLedgers)
63 , m_id(txn->getTransactionID())
64 , m_account(txn->getAccountID(sfAccount))
65 , m_seqProxy(txn->getSeqProxy())
66 {
67 if (txn->isFieldPresent(sfLastLedgerSequence))
68 m_expire =
69 std::min(m_expire, txn->getFieldU32(sfLastLedgerSequence) + 1);
70 }
71
72 uint256 const&
73 getID() const
74 {
75 return m_id;
76 }
77
80 {
81 return m_seqProxy;
82 }
83
84 bool
86 {
87 return i > m_expire;
88 }
89
91 getTX() const
92 {
93 return m_txn;
94 }
95
96 AccountID const&
97 getAccount() const
98 {
99 return m_account;
100 }
101
102private:
108};
109
110//------------------------------------------------------------------------------
111
112class LocalTxsImp : public LocalTxs
113{
114public:
115 LocalTxsImp() = default;
116
117 // Add a new transaction to the set of local transactions
118 void
120 override
121 {
123
124 m_txns.emplace_back(index, txn);
125 }
126
128 getTxSet() override
129 {
130 CanonicalTXSet tset(uint256{});
131
132 // Get the set of local transactions as a canonical
133 // set (so they apply in a valid order)
134 {
136
137 for (auto const& it : m_txns)
138 tset.insert(it.getTX());
139 }
140 return tset;
141 }
142
143 // Remove transactions that have either been accepted
144 // into a fully-validated ledger, are (now) impossible,
145 // or have expired
146 void
147 sweep(ReadView const& view) override
148 {
150
151 m_txns.remove_if([&view](auto const& txn) {
152 if (txn.isExpired(view.info().seq))
153 return true;
154 if (view.txExists(txn.getID()))
155 return true;
156
157 AccountID const acctID = txn.getAccount();
158 auto const sleAcct = view.read(keylet::account(acctID));
159
160 if (!sleAcct)
161 return false;
162
163 SeqProxy const acctSeq =
164 SeqProxy::sequence(sleAcct->getFieldU32(sfSequence));
165 SeqProxy const seqProx = txn.getSeqProxy();
166
167 if (seqProx.isSeq())
168 return acctSeq > seqProx; // Remove tefPAST_SEQ
169
170 if (seqProx.isTicket() && acctSeq.value() <= seqProx.value())
171 // Keep ticket from the future. Note, however, that the
172 // transaction will not be held indefinitely since LocalTxs
173 // will only hold a transaction for a maximum of 5 ledgers.
174 return false;
175
176 // Ticket should have been created by now. Remove if ticket
177 // does not exist.
178 return !view.exists(keylet::ticket(acctID, seqProx));
179 });
180 }
181
183 size() override
184 {
186
187 return m_txns.size();
188 }
189
190private:
193};
194
197{
198 return std::make_unique<LocalTxsImp>();
199}
200
201} // namespace ripple
Holds transactions which were deferred to the next pass of consensus.
std::shared_ptr< STTx const > m_txn
Definition: LocalTxs.cpp:103
AccountID m_account
Definition: LocalTxs.cpp:106
std::shared_ptr< STTx const > const & getTX() const
Definition: LocalTxs.cpp:91
LocalTx(LedgerIndex index, std::shared_ptr< STTx const > const &txn)
Definition: LocalTxs.cpp:60
LedgerIndex m_expire
Definition: LocalTxs.cpp:104
SeqProxy getSeqProxy() const
Definition: LocalTxs.cpp:79
SeqProxy m_seqProxy
Definition: LocalTxs.cpp:107
static int const holdLedgers
Definition: LocalTxs.cpp:58
uint256 const & getID() const
Definition: LocalTxs.cpp:73
bool isExpired(LedgerIndex i) const
Definition: LocalTxs.cpp:85
AccountID const & getAccount() const
Definition: LocalTxs.cpp:97
std::mutex m_lock
Definition: LocalTxs.cpp:191
void sweep(ReadView const &view) override
Definition: LocalTxs.cpp:147
void push_back(LedgerIndex index, std::shared_ptr< STTx const > const &txn) override
Definition: LocalTxs.cpp:119
std::list< LocalTx > m_txns
Definition: LocalTxs.cpp:192
CanonicalTXSet getTxSet() override
Definition: LocalTxs.cpp:128
std::size_t size() override
Definition: LocalTxs.cpp:183
A view into a ledger.
Definition: ReadView.h:51
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
virtual bool txExists(key_type const &key) const =0
Returns true if a tx exists in the tx map.
A type that represents either a sequence value or a ticket value.
Definition: SeqProxy.h:56
static constexpr SeqProxy sequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition: SeqProxy.h:76
constexpr bool isSeq() const
Definition: SeqProxy.h:88
constexpr std::uint32_t value() const
Definition: SeqProxy.h:82
constexpr bool isTicket() const
Definition: SeqProxy.h:94
T min(T... args)
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:175
static ticket_t const ticket
Definition: Indexes.h:170
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::unique_ptr< LocalTxs > make_LocalTxs()
Definition: LocalTxs.cpp:196