rippled
CanonicalTXSet.h
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 #ifndef RIPPLE_APP_MISC_CANONICALTXSET_H_INCLUDED
21 #define RIPPLE_APP_MISC_CANONICALTXSET_H_INCLUDED
22 
23 #include <ripple/basics/CountedObject.h>
24 #include <ripple/protocol/RippleLedgerHash.h>
25 #include <ripple/protocol/STTx.h>
26 #include <ripple/protocol/SeqProxy.h>
27 
28 namespace ripple {
29 
37 // VFALCO TODO rename to SortedTxSet
38 class CanonicalTXSet : public CountedObject<CanonicalTXSet>
39 {
40 private:
41  class Key
42  {
43  public:
44  Key(uint256 const& account, SeqProxy seqProx, uint256 const& id)
45  : account_(account), txId_(id), seqProxy_(seqProx)
46  {
47  }
48 
49  friend bool
50  operator<(Key const& lhs, Key const& rhs);
51 
52  inline friend bool
53  operator>(Key const& lhs, Key const& rhs)
54  {
55  return rhs < lhs;
56  }
57 
58  inline friend bool
59  operator<=(Key const& lhs, Key const& rhs)
60  {
61  return !(lhs > rhs);
62  }
63 
64  inline friend bool
65  operator>=(Key const& lhs, Key const& rhs)
66  {
67  return !(lhs < rhs);
68  }
69 
70  inline friend bool
71  operator==(Key const& lhs, Key const& rhs)
72  {
73  return lhs.txId_ == rhs.txId_;
74  }
75 
76  inline friend bool
77  operator!=(Key const& lhs, Key const& rhs)
78  {
79  return !(lhs == rhs);
80  }
81 
82  uint256 const&
83  getAccount() const
84  {
85  return account_;
86  }
87 
88  uint256 const&
89  getTXID() const
90  {
91  return txId_;
92  }
93 
94  private:
98  };
99 
100  friend bool
101  operator<(Key const& lhs, Key const& rhs);
102 
103  // Calculate the salted key for the given account
104  uint256
105  accountKey(AccountID const& account);
106 
107 public:
108  using const_iterator =
110 
111 public:
112  explicit CanonicalTXSet(LedgerHash const& saltHash) : salt_(saltHash)
113  {
114  }
115 
116  void
118 
119  // Pops the next transaction on account that follows seqProx in the
120  // sort order. Normally called when a transaction is successfully
121  // applied to the open ledger so the next transaction can be resubmitted
122  // without waiting for ledger close.
123  //
124  // The return value is often null, when an account has no more
125  // transactions.
128 
129  void
130  reset(LedgerHash const& salt)
131  {
132  salt_ = salt;
133  map_.clear();
134  }
135 
138  {
139  return map_.erase(it);
140  }
141 
143  begin() const
144  {
145  return map_.begin();
146  }
147 
149  end() const
150  {
151  return map_.end();
152  }
153 
154  size_t
155  size() const
156  {
157  return map_.size();
158  }
159  bool
160  empty() const
161  {
162  return map_.empty();
163  }
164 
165  uint256 const&
166  key() const
167  {
168  return salt_;
169  }
170 
171 private:
173 
174  // Used to salt the accounts so people can't mine for low account numbers
176 };
177 
178 } // namespace ripple
179 
180 #endif
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::Dir::const_iterator
Definition: Directory.h:49
std::shared_ptr
STL class.
ripple::CanonicalTXSet::map_
std::map< Key, std::shared_ptr< STTx const > > map_
Definition: CanonicalTXSet.h:172
ripple::CanonicalTXSet::key
uint256 const & key() const
Definition: CanonicalTXSet.h:166
ripple::CanonicalTXSet::Key::operator==
friend bool operator==(Key const &lhs, Key const &rhs)
Definition: CanonicalTXSet.h:71
ripple::CanonicalTXSet::erase
const_iterator erase(const_iterator const &it)
Definition: CanonicalTXSet.h:137
ripple::CanonicalTXSet::popAcctTransaction
std::shared_ptr< STTx const > popAcctTransaction(std::shared_ptr< STTx const > const &tx)
Definition: CanonicalTXSet.cpp:62
ripple::CanonicalTXSet::Key::operator>=
friend bool operator>=(Key const &lhs, Key const &rhs)
Definition: CanonicalTXSet.h:65
ripple::CanonicalTXSet::salt_
uint256 salt_
Definition: CanonicalTXSet.h:175
ripple::CanonicalTXSet::Key::seqProxy_
SeqProxy seqProxy_
Definition: CanonicalTXSet.h:97
ripple::CanonicalTXSet
Holds transactions which were deferred to the next pass of consensus.
Definition: CanonicalTXSet.h:38
ripple::CanonicalTXSet::begin
const_iterator begin() const
Definition: CanonicalTXSet.h:143
ripple::CanonicalTXSet::Key::account_
uint256 account_
Definition: CanonicalTXSet.h:95
ripple::base_uint< 256 >
ripple::CanonicalTXSet::CanonicalTXSet
CanonicalTXSet(LedgerHash const &saltHash)
Definition: CanonicalTXSet.h:112
ripple::CanonicalTXSet::Key::getTXID
uint256 const & getTXID() const
Definition: CanonicalTXSet.h:89
ripple::CanonicalTXSet::Key::Key
Key(uint256 const &account, SeqProxy seqProx, uint256 const &id)
Definition: CanonicalTXSet.h:44
ripple::CanonicalTXSet::Key::getAccount
uint256 const & getAccount() const
Definition: CanonicalTXSet.h:83
ripple::CanonicalTXSet::Key::operator>
friend bool operator>(Key const &lhs, Key const &rhs)
Definition: CanonicalTXSet.h:53
std::map
STL class.
ripple::CanonicalTXSet::accountKey
uint256 accountKey(AccountID const &account)
Definition: CanonicalTXSet.cpp:43
ripple::CanonicalTXSet::insert
void insert(std::shared_ptr< STTx const > const &txn)
Definition: CanonicalTXSet.cpp:52
ripple::CanonicalTXSet::end
const_iterator end() const
Definition: CanonicalTXSet.h:149
ripple::CanonicalTXSet::Key
Definition: CanonicalTXSet.h:41
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::CanonicalTXSet::size
size_t size() const
Definition: CanonicalTXSet.h:155
ripple::CanonicalTXSet::const_iterator
std::map< Key, std::shared_ptr< STTx const > >::const_iterator const_iterator
Definition: CanonicalTXSet.h:109
ripple::CanonicalTXSet::Key::txId_
uint256 txId_
Definition: CanonicalTXSet.h:96
ripple::CanonicalTXSet::empty
bool empty() const
Definition: CanonicalTXSet.h:160
ripple::SeqProxy
A type that represents either a sequence value or a ticket value.
Definition: SeqProxy.h:55
ripple::CanonicalTXSet::operator<
friend bool operator<(Key const &lhs, Key const &rhs)
Definition: CanonicalTXSet.cpp:25
ripple::CanonicalTXSet::Key::operator<=
friend bool operator<=(Key const &lhs, Key const &rhs)
Definition: CanonicalTXSet.h:59
ripple::CanonicalTXSet::Key::operator!=
friend bool operator!=(Key const &lhs, Key const &rhs)
Definition: CanonicalTXSet.h:77
ripple::CanonicalTXSet::reset
void reset(LedgerHash const &salt)
Definition: CanonicalTXSet.h:130
ripple::CanonicalTXSet::Key::operator<
friend bool operator<(Key const &lhs, Key const &rhs)
Definition: CanonicalTXSet.cpp:25