rippled
AmendmentTable.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_AMENDMENTTABLE_H_INCLUDED
21 #define RIPPLE_APP_MISC_AMENDMENTTABLE_H_INCLUDED
22 
23 #include <ripple/app/ledger/Ledger.h>
24 #include <ripple/core/ConfigSections.h>
25 #include <ripple/protocol/Feature.h>
26 #include <ripple/protocol/Protocol.h>
27 #include <ripple/protocol/STValidation.h>
28 
29 #include <optional>
30 
31 namespace ripple {
32 
38 {
39 public:
40  struct FeatureInfo
41  {
42  FeatureInfo() = delete;
44  : name(n), feature(f), vote(v)
45  {
46  }
47 
51  };
52 
53  virtual ~AmendmentTable() = default;
54 
55  virtual uint256
56  find(std::string const& name) const = 0;
57 
58  virtual bool
59  veto(uint256 const& amendment) = 0;
60  virtual bool
61  unVeto(uint256 const& amendment) = 0;
62 
63  virtual bool
64  enable(uint256 const& amendment) = 0;
65 
66  virtual bool
67  isEnabled(uint256 const& amendment) const = 0;
68  virtual bool
69  isSupported(uint256 const& amendment) const = 0;
70 
77  virtual bool
78  hasUnsupportedEnabled() const = 0;
79 
81  firstUnsupportedExpected() const = 0;
82 
83  virtual Json::Value
84  getJson() const = 0;
85 
87  virtual Json::Value
88  getJson(uint256 const& amendment) const = 0;
89 
91  void
93  std::shared_ptr<ReadView const> const& lastValidatedLedger)
94  {
95  if (needValidatedLedger(lastValidatedLedger->seq()))
97  lastValidatedLedger->seq(),
98  getEnabledAmendments(*lastValidatedLedger),
99  getMajorityAmendments(*lastValidatedLedger));
100  }
101 
105  virtual bool
106  needValidatedLedger(LedgerIndex seq) const = 0;
107 
108  virtual void
110  LedgerIndex ledgerSeq,
111  std::set<uint256> const& enabled,
112  majorityAmendments_t const& majority) = 0;
113 
114  // Called when the set of trusted validators changes.
115  virtual void
116  trustChanged(hash_set<PublicKey> const& allTrusted) = 0;
117 
118  // Called by the consensus code when we need to
119  // inject pseudo-transactions
121  doVoting(
122  Rules const& rules,
123  NetClock::time_point closeTime,
124  std::set<uint256> const& enabledAmendments,
125  majorityAmendments_t const& majorityAmendments,
126  std::vector<std::shared_ptr<STValidation>> const& valSet) = 0;
127 
128  // Called by the consensus code when we need to
129  // add feature entries to a validation
130  virtual std::vector<uint256>
131  doValidation(std::set<uint256> const& enabled) const = 0;
132 
133  // The set of amendments to enable in the genesis ledger
134  // This will return all known, non-vetoed amendments.
135  // If we ever have two amendments that should not both be
136  // enabled at the same time, we should ensure one is vetoed.
137  virtual std::vector<uint256>
138  getDesired() const = 0;
139 
140  // The function below adapts the API callers expect to the
141  // internal amendment table API. This allows the amendment
142  // table implementation to be independent of the ledger
143  // implementation. These APIs will merge when the view code
144  // supports a full ledger API
145 
146  void
148  std::shared_ptr<ReadView const> const& lastClosedLedger,
149  std::vector<std::shared_ptr<STValidation>> const& parentValidations,
150  std::shared_ptr<SHAMap> const& initialPosition)
151  {
152  // Ask implementation what to do
153  auto actions = doVoting(
154  lastClosedLedger->rules(),
155  lastClosedLedger->parentCloseTime(),
156  getEnabledAmendments(*lastClosedLedger),
157  getMajorityAmendments(*lastClosedLedger),
158  parentValidations);
159 
160  // Inject appropriate pseudo-transactions
161  for (auto const& it : actions)
162  {
163  STTx amendTx(
164  ttAMENDMENT,
165  [&it, seq = lastClosedLedger->seq() + 1](auto& obj) {
166  obj.setAccountID(sfAccount, AccountID());
167  obj.setFieldH256(sfAmendment, it.first);
168  obj.setFieldU32(sfLedgerSequence, seq);
169 
170  if (it.second != 0)
171  obj.setFieldU32(sfFlags, it.second);
172  });
173 
174  Serializer s;
175  amendTx.add(s);
176 
177  initialPosition->addGiveItem(
179  make_shamapitem(amendTx.getTransactionID(), s.slice()));
180  }
181  }
182 };
183 
186  Application& app,
187  std::chrono::seconds majorityTime,
189  Section const& enabled,
190  Section const& vetoed,
191  beast::Journal journal);
192 
193 } // namespace ripple
194 
195 #endif
ripple::AmendmentTable::FeatureInfo::name
const std::string name
Definition: AmendmentTable.h:48
ripple::getMajorityAmendments
majorityAmendments_t getMajorityAmendments(ReadView const &view)
Definition: View.cpp:638
ripple::AmendmentTable::FeatureInfo::vote
const VoteBehavior vote
Definition: AmendmentTable.h:50
ripple::getEnabledAmendments
std::set< uint256 > getEnabledAmendments(ReadView const &view)
Definition: View.cpp:621
ripple::AmendmentTable::doVoting
virtual std::map< uint256, std::uint32_t > doVoting(Rules const &rules, NetClock::time_point closeTime, std::set< uint256 > const &enabledAmendments, majorityAmendments_t const &majorityAmendments, std::vector< std::shared_ptr< STValidation >> const &valSet)=0
std::string
STL class.
std::shared_ptr
STL class.
ripple::AmendmentTable::FeatureInfo::feature
const uint256 feature
Definition: AmendmentTable.h:49
ripple::AmendmentTable::veto
virtual bool veto(uint256 const &amendment)=0
std::unordered_set
STL class.
ripple::AmendmentTable::find
virtual uint256 find(std::string const &name) const =0
ripple::make_shamapitem
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
Definition: SHAMapItem.h:160
std::vector
STL class.
ripple::AmendmentTable::doVoting
void doVoting(std::shared_ptr< ReadView const > const &lastClosedLedger, std::vector< std::shared_ptr< STValidation >> const &parentValidations, std::shared_ptr< SHAMap > const &initialPosition)
Definition: AmendmentTable.h:147
std::chrono::seconds
ripple::AmendmentTable::trustChanged
virtual void trustChanged(hash_set< PublicKey > const &allTrusted)=0
ripple::AmendmentTable::doValidatedLedger
void doValidatedLedger(std::shared_ptr< ReadView const > const &lastValidatedLedger)
Called when a new fully-validated ledger is accepted.
Definition: AmendmentTable.h:92
ripple::AmendmentTable::getJson
virtual Json::Value getJson() const =0
ripple::ttAMENDMENT
@ ttAMENDMENT
This system-generated transaction type is used to update the status of the various amendments.
Definition: TxFormats.h:198
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
ripple::base_uint< 256 >
ripple::AmendmentTable::doValidation
virtual std::vector< uint256 > doValidation(std::set< uint256 > const &enabled) const =0
ripple::AmendmentTable::unVeto
virtual bool unVeto(uint256 const &amendment)=0
ripple::VoteBehavior
VoteBehavior
Definition: Feature.h:69
ripple::AmendmentTable::~AmendmentTable
virtual ~AmendmentTable()=default
ripple::AmendmentTable::firstUnsupportedExpected
virtual std::optional< NetClock::time_point > firstUnsupportedExpected() const =0
ripple::AmendmentTable::isEnabled
virtual bool isEnabled(uint256 const &amendment) const =0
ripple::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:64
std::chrono::time_point
ripple::STTx
Definition: STTx.h:46
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
std::map
STL class.
ripple::AmendmentTable::enable
virtual bool enable(uint256 const &amendment)=0
ripple::AmendmentTable::FeatureInfo
Definition: AmendmentTable.h:40
ripple::STTx::getTransactionID
uint256 getTransactionID() const
Definition: STTx.h:193
ripple::AmendmentTable::hasUnsupportedEnabled
virtual bool hasUnsupportedEnabled() const =0
returns true if one or more amendments on the network have been enabled that this server does not sup...
ripple::AmendmentTable::isSupported
virtual bool isSupported(uint256 const &amendment) const =0
ripple::Serializer
Definition: Serializer.h:40
ripple::STObject::add
void add(Serializer &s) const override
Definition: STObject.cpp:85
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::AmendmentTable::FeatureInfo::FeatureInfo
FeatureInfo()=delete
ripple::Rules
Rules controlling protocol behavior.
Definition: Rules.h:33
optional
ripple::make_AmendmentTable
std::unique_ptr< AmendmentTable > make_AmendmentTable(Application &app, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed, beast::Journal journal)
Definition: AmendmentTable.cpp:973
ripple::AmendmentTable::FeatureInfo::FeatureInfo
FeatureInfo(std::string const &n, uint256 const &f, VoteBehavior v)
Definition: AmendmentTable.h:43
ripple::AmendmentTable
The amendment table stores the list of enabled and potential amendments.
Definition: AmendmentTable.h:37
std::unique_ptr
STL class.
ripple::AmendmentTable::needValidatedLedger
virtual bool needValidatedLedger(LedgerIndex seq) const =0
Called to determine whether the amendment logic needs to process a new validated ledger.
std::set
STL class.
ripple::AmendmentTable::getDesired
virtual std::vector< uint256 > getDesired() const =0
Json::Value
Represents a JSON value.
Definition: json_value.h:145