rippled
RCLCxTx.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2016 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_CONSENSUS_RCLCXTX_H_INCLUDED
21 #define RIPPLE_APP_CONSENSUS_RCLCXTX_H_INCLUDED
22 
23 #include <ripple/app/misc/CanonicalTXSet.h>
24 #include <ripple/basics/chrono.h>
25 #include <ripple/protocol/UintTypes.h>
26 #include <ripple/shamap/SHAMap.h>
27 
28 namespace ripple {
29 
35 class RCLCxTx
36 {
37 public:
39  using ID = uint256;
40 
45  RCLCxTx(boost::intrusive_ptr<SHAMapItem const> txn) : tx_(std::move(txn))
46  {
47  }
48 
50  ID const&
51  id() const
52  {
53  return tx_->key();
54  }
55 
57  boost::intrusive_ptr<SHAMapItem const> tx_;
58 };
59 
65 class RCLTxSet
66 {
67 public:
69  using ID = uint256;
71  using Tx = RCLCxTx;
72 
73  //< Provide a mutable view of a TxSet
75  {
76  friend class RCLTxSet;
79 
80  public:
81  MutableTxSet(RCLTxSet const& src) : map_{src.map_->snapShot(true)}
82  {
83  }
84 
90  bool
91  insert(Tx const& t)
92  {
93  return map_->addItem(SHAMapNodeType::tnTRANSACTION_NM, t.tx_);
94  }
95 
101  bool
102  erase(Tx::ID const& entry)
103  {
104  return map_->delItem(entry);
105  }
106  };
107 
113  {
114  assert(map_);
115  }
116 
121  RCLTxSet(MutableTxSet const& m) : map_{m.map_->snapShot(false)}
122  {
123  }
124 
130  bool
131  exists(Tx::ID const& entry) const
132  {
133  return map_->hasItem(entry);
134  }
135 
147  boost::intrusive_ptr<SHAMapItem const> const&
148  find(Tx::ID const& entry) const
149  {
150  return map_->peekItem(entry);
151  }
152 
154  ID
155  id() const
156  {
157  return map_->getHash().as_uint256();
158  }
159 
169  compare(RCLTxSet const& j) const
170  {
171  SHAMap::Delta delta;
172 
173  // Bound the work we do in case of a malicious
174  // map_ from a trusted validator
175  map_->compare(*(j.map_), delta, 65536);
176 
178  for (auto const& [k, v] : delta)
179  {
180  assert((v.first && !v.second) || (v.second && !v.first));
181 
182  ret[k] = static_cast<bool>(v.first);
183  }
184  return ret;
185  }
186 
189 };
190 } // namespace ripple
191 #endif
ripple::RCLTxSet::RCLTxSet
RCLTxSet(MutableTxSet const &m)
Constructor from a previously created MutableTxSet.
Definition: RCLCxTx.h:121
std::shared_ptr
STL class.
ripple::RCLTxSet::MutableTxSet::insert
bool insert(Tx const &t)
Insert a new transaction into the set.
Definition: RCLCxTx.h:91
ripple::RCLCxTx::ID
uint256 ID
Unique identifier/hash of transaction.
Definition: RCLCxTx.h:39
ripple::RCLTxSet::MutableTxSet::MutableTxSet
MutableTxSet(RCLTxSet const &src)
Definition: RCLCxTx.h:81
ripple::RCLTxSet::map_
std::shared_ptr< SHAMap > map_
The SHAMap representing the transactions.
Definition: RCLCxTx.h:188
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::RCLTxSet::MutableTxSet::map_
std::shared_ptr< SHAMap > map_
The SHAMap representing the transactions.
Definition: RCLCxTx.h:78
ripple::base_uint< 256 >
ripple::RCLCxTx::RCLCxTx
RCLCxTx(boost::intrusive_ptr< SHAMapItem const > txn)
Constructor.
Definition: RCLCxTx.h:45
ripple::RCLTxSet::exists
bool exists(Tx::ID const &entry) const
Test if a transaction is in the set.
Definition: RCLCxTx.h:131
ripple::RCLTxSet
Represents a set of transactions in RCLConsensus.
Definition: RCLCxTx.h:65
ripple::RCLTxSet::id
ID id() const
The unique ID/hash of the transaction set.
Definition: RCLCxTx.h:155
std::map
STL class.
ripple::RCLTxSet::find
boost::intrusive_ptr< SHAMapItem const > const & find(Tx::ID const &entry) const
Lookup a transaction.
Definition: RCLCxTx.h:148
ripple::RCLTxSet::compare
std::map< Tx::ID, bool > compare(RCLTxSet const &j) const
Find transactions not in common between this and another transaction set.
Definition: RCLCxTx.h:169
ripple::RCLTxSet::MutableTxSet
Definition: RCLCxTx.h:74
ripple::RCLTxSet::RCLTxSet
RCLTxSet(std::shared_ptr< SHAMap > m)
Constructor.
Definition: RCLCxTx.h:112
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std
STL namespace.
ripple::RCLTxSet::MutableTxSet::erase
bool erase(Tx::ID const &entry)
Remove a transaction from the set.
Definition: RCLCxTx.h:102
ripple::RCLTxSet::ID
uint256 ID
Unique identifier/hash of the set of transactions.
Definition: RCLCxTx.h:69
ripple::RCLCxTx
Represents a transaction in RCLConsensus.
Definition: RCLCxTx.h:35
ripple::RCLCxTx::tx_
boost::intrusive_ptr< SHAMapItem const > tx_
The SHAMapItem that represents the transaction.
Definition: RCLCxTx.h:57
ripple::RCLCxTx::id
ID const & id() const
The unique identifier/hash of the transaction.
Definition: RCLCxTx.h:51