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(SHAMapItem const& txn) : tx_{txn}
46  {
47  }
48 
50  ID const&
51  id() const
52  {
53  return tx_.key();
54  }
55 
57  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(
95  }
96 
102  bool
103  erase(Tx::ID const& entry)
104  {
105  return map_->delItem(entry);
106  }
107  };
108 
114  {
115  assert(map_);
116  }
117 
122  RCLTxSet(MutableTxSet const& m) : map_{m.map_->snapShot(false)}
123  {
124  }
125 
131  bool
132  exists(Tx::ID const& entry) const
133  {
134  return map_->hasItem(entry);
135  }
136 
149  find(Tx::ID const& entry) const
150  {
151  return map_->peekItem(entry);
152  }
153 
155  ID
156  id() const
157  {
158  return map_->getHash().as_uint256();
159  }
160 
170  compare(RCLTxSet const& j) const
171  {
172  SHAMap::Delta delta;
173 
174  // Bound the work we do in case of a malicious
175  // map_ from a trusted validator
176  map_->compare(*(j.map_), delta, 65536);
177 
179  for (auto const& [k, v] : delta)
180  {
181  assert((v.first && !v.second) || (v.second && !v.first));
182 
183  ret[k] = static_cast<bool>(v.first);
184  }
185  return ret;
186  }
187 
190 };
191 } // namespace ripple
192 #endif
ripple::RCLTxSet::RCLTxSet
RCLTxSet(MutableTxSet const &m)
Constructor from a previously created MutableTxSet.
Definition: RCLCxTx.h:122
std::shared_ptr
STL class.
ripple::RCLTxSet::find
std::shared_ptr< const SHAMapItem > const & find(Tx::ID const &entry) const
Lookup a transaction.
Definition: RCLCxTx.h:149
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:189
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
ripple::SHAMapItem::key
uint256 const & key() const
Definition: SHAMapItem.h:45
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:549
ripple::RCLTxSet::MutableTxSet::map_
std::shared_ptr< SHAMap > map_
The SHAMap representing the transactions.
Definition: RCLCxTx.h:78
ripple::base_uint< 256 >
ripple::SHAMapItem
Definition: SHAMapItem.h:31
ripple::RCLTxSet::exists
bool exists(Tx::ID const &entry) const
Test if a transaction is in the set.
Definition: RCLCxTx.h:132
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:156
ripple::RCLCxTx::tx_
const SHAMapItem tx_
The SHAMapItem that represents the transaction.
Definition: RCLCxTx.h:57
std::map
STL class.
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:170
ripple::RCLTxSet::MutableTxSet
Definition: RCLCxTx.h:74
ripple::RCLTxSet::RCLTxSet
RCLTxSet(std::shared_ptr< SHAMap > m)
Constructor.
Definition: RCLCxTx.h:113
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RCLTxSet::MutableTxSet::erase
bool erase(Tx::ID const &entry)
Remove a transaction from the set.
Definition: RCLCxTx.h:103
ripple::RCLCxTx::RCLCxTx
RCLCxTx(SHAMapItem const &txn)
Constructor.
Definition: RCLCxTx.h:45
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::id
ID const & id() const
The unique identifier/hash of the transaction.
Definition: RCLCxTx.h:51