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  SHAMapItem{t.id(), t.tx_.peekData()});
96  }
97 
103  bool
104  erase(Tx::ID const& entry)
105  {
106  return map_->delItem(entry);
107  }
108  };
109 
115  {
116  assert(map_);
117  }
118 
123  RCLTxSet(MutableTxSet const& m) : map_{m.map_->snapShot(false)}
124  {
125  }
126 
132  bool
133  exists(Tx::ID const& entry) const
134  {
135  return map_->hasItem(entry);
136  }
137 
150  find(Tx::ID const& entry) const
151  {
152  return map_->peekItem(entry);
153  }
154 
156  ID
157  id() const
158  {
159  return map_->getHash().as_uint256();
160  }
161 
171  compare(RCLTxSet const& j) const
172  {
173  SHAMap::Delta delta;
174 
175  // Bound the work we do in case of a malicious
176  // map_ from a trusted validator
177  map_->compare(*(j.map_), delta, 65536);
178 
180  for (auto const& [k, v] : delta)
181  {
182  assert((v.first && !v.second) || (v.second && !v.first));
183 
184  ret[k] = static_cast<bool>(v.first);
185  }
186  return ret;
187  }
188 
191 };
192 } // namespace ripple
193 #endif
ripple::RCLTxSet::RCLTxSet
RCLTxSet(MutableTxSet const &m)
Constructor from a previously created MutableTxSet.
Definition: RCLCxTx.h:123
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:150
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:190
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:457
ripple::RCLTxSet::MutableTxSet::map_
std::shared_ptr< SHAMap > map_
The SHAMap representing the transactions.
Definition: RCLCxTx.h:78
ripple::base_uint< 256 >
ripple::SHAMapItem::key
uint256 const & key() const
Definition: SHAMapItem.h:82
ripple::SHAMapItem
Definition: SHAMapItem.h:35
ripple::RCLTxSet::exists
bool exists(Tx::ID const &entry) const
Test if a transaction is in the set.
Definition: RCLCxTx.h:133
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:157
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:171
ripple::RCLTxSet::MutableTxSet
Definition: RCLCxTx.h:74
ripple::RCLTxSet::RCLTxSet
RCLTxSet(std::shared_ptr< SHAMap > m)
Constructor.
Definition: RCLCxTx.h:114
ripple::SHAMapItem::peekData
Blob const & peekData() const
Definition: SHAMapItem.h:88
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:104
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