rippled
ValidatorList.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2015 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_VALIDATORLIST_H_INCLUDED
21 #define RIPPLE_APP_MISC_VALIDATORLIST_H_INCLUDED
22 
23 #include <ripple/app/misc/Manifest.h>
24 #include <ripple/basics/Log.h>
25 #include <ripple/basics/UnorderedContainers.h>
26 #include <ripple/core/TimeKeeper.h>
27 #include <ripple/crypto/csprng.h>
28 #include <ripple/json/json_value.h>
29 #include <ripple/protocol/PublicKey.h>
30 #include <boost/iterator/counting_iterator.hpp>
31 #include <boost/range/adaptors.hpp>
32 #include <mutex>
33 #include <numeric>
34 #include <shared_mutex>
35 
36 namespace ripple {
37 
38 // predeclaration
39 class Overlay;
40 class HashRouter;
41 class STValidation;
42 
43 enum class ListDisposition {
45  accepted = 0,
46 
49 
52 
54  untrusted,
55 
57  stale,
58 
60  invalid
61 };
62 
64 to_string(ListDisposition disposition);
65 
69 {
70  explicit TrustChanges() = default;
71 
74 };
75 
120 {
122  {
123  explicit PublisherList() = default;
124 
125  bool available;
135  };
136 
140  boost::filesystem::path const dataPath_;
143 
145  boost::optional<std::size_t> minimumQuorum_;
146 
147  // Published lists stored by publisher master public key
149 
150  // Listed master public keys with the number of lists they appear on
152 
153  // The current list of trusted master keys
155 
156  // The current list of trusted signing keys. For those validators using
157  // a manifest, the signing key is the ephemeral key. For the ones using
158  // a seed, the signing key is the same as the master key.
160 
162 
163  // The master public keys of the current negative UNL
165 
166  // Currently supported version of publisher list format
167  static constexpr std::uint32_t requiredListVersion = 1;
168  static const std::string filePrefix_;
169 
170 public:
172  ManifestCache& validatorManifests,
173  ManifestCache& publisherManifests,
174  TimeKeeper& timeKeeper,
175  std::string const& databasePath,
176  beast::Journal j,
177  boost::optional<std::size_t> minimumQuorum = boost::none);
178  ~ValidatorList() = default;
179 
186  {
188  {
189  }
190 
192  ListDisposition d,
193  PublicKey key,
194  bool avail,
195  std::size_t seq)
196  : disposition(d), publisherKey(key), available(avail), sequence(seq)
197  {
198  }
199 
201  boost::optional<PublicKey> publisherKey;
202  bool available = false;
203  boost::optional<std::size_t> sequence;
204  };
205 
223  bool
224  load(
225  PublicKey const& localSigningKey,
226  std::vector<std::string> const& configKeys,
227  std::vector<std::string> const& publisherKeys);
228 
258  std::string const& manifest,
259  std::string const& blob,
260  std::string const& signature,
261  std::uint32_t version,
262  std::string siteUri,
263  uint256 const& hash,
264  Overlay& overlay,
265  HashRouter& hashRouter);
266 
290  applyList(
291  std::string const& manifest,
292  std::string const& blob,
293  std::string const& signature,
294  std::uint32_t version,
295  std::string siteUri,
296  boost::optional<uint256> const& hash = {});
297 
298  /* Attempt to read previously stored list files. Expected to only be
299  called when loading from URL fails.
300 
301  @return A list of valid file:// URLs, if any.
302 
303  @par Thread Safety
304 
305  May be called concurrently
306  */
308  loadLists();
309 
326  updateTrusted(hash_set<NodeID> const& seenValidators);
327 
342  quorum() const
343  {
344  return quorum_;
345  }
346 
355  bool
356  trusted(PublicKey const& identity) const;
357 
366  bool
367  listed(PublicKey const& identity) const;
368 
379  boost::optional<PublicKey>
380  getTrustedKey(PublicKey const& identity) const;
381 
392  boost::optional<PublicKey>
393  getListedKey(PublicKey const& identity) const;
394 
403  bool
404  trustedPublisher(PublicKey const& identity) const;
405 
412  PublicKey
413  localPublicKey() const;
414 
430  void
431  for_each_listed(std::function<void(PublicKey const&, bool)> func) const;
432 
460  void
462  std::string const& manifest,
463  std::string const& blob,
464  std::string const& signature,
465  std::uint32_t version,
466  PublicKey const& pubKey,
467  std::size_t sequence,
468  uint256 const& hash)> func) const;
469 
473  boost::optional<Json::Value>
474  getAvailable(boost::beast::string_view const& pubKey);
475 
478  count() const;
479 
489  boost::optional<TimeKeeper::time_point>
490  expires() const;
491 
498  getJson() const;
499 
505  QuorumKeys
507  {
509  return {quorum_, trustedSigningKeys_};
510  }
511 
517  getTrustedMasterKeys() const;
518 
524  getNegativeUNL() const;
525 
530  void
531  setNegativeUNL(hash_set<PublicKey> const& negUnl);
532 
541  std::vector<std::shared_ptr<STValidation>>&& validations) const;
542 
543 private:
546  boost::filesystem::path
547  GetCacheFileName(PublicKey const& pubKey);
548 
551  void
552  CacheValidatorFile(PublicKey const& pubKey, PublisherList const& publisher);
553 
563  verify(
564  Json::Value& list,
565  PublicKey& pubKey,
566  std::string const& manifest,
567  std::string const& blob,
568  std::string const& signature);
569 
580  bool
581  removePublisherList(PublicKey const& publisherKey);
582 
595  std::size_t unlSize,
596  std::size_t effectiveUnlSize,
597  std::size_t seenSize);
598 };
599 } // namespace ripple
600 
601 #endif
ripple::ValidatorList::getListedKey
boost::optional< PublicKey > getListedKey(PublicKey const &identity) const
Returns listed master public if public key is included on any lists.
Definition: ValidatorList.cpp:570
ripple::ValidatorList::PublisherList::sequence
std::size_t sequence
Definition: ValidatorList.h:127
ripple::ValidatorList::validatorManifests_
ManifestCache & validatorManifests_
Definition: ValidatorList.h:137
ripple::ValidatorList::for_each_listed
void for_each_listed(std::function< void(PublicKey const &, bool)> func) const
Invokes the callback once for every listed validation public key.
Definition: ValidatorList.cpp:764
ripple::ValidatorList::localPublicKey
PublicKey localPublicKey() const
Returns local validator public key.
Definition: ValidatorList.cpp:599
std::string
STL class.
std::shared_ptr
STL class.
ripple::ListDisposition
ListDisposition
Definition: ValidatorList.h:43
ripple::ValidatorList::PublisherList::rawSignature
std::string rawSignature
Definition: ValidatorList.h:132
ripple::ValidatorList::PublisherListStats::disposition
ListDisposition disposition
Definition: ValidatorList.h:200
ripple::TrustChanges
Changes in trusted nodes after updating validator list.
Definition: ValidatorList.h:68
ripple::ListDisposition::stale
@ stale
Trusted publisher key, but seq is too old.
ripple::ValidatorList::publisherLists_
hash_map< PublicKey, PublisherList > publisherLists_
Definition: ValidatorList.h:148
std::unordered_set
STL class.
std::pair
ripple::ValidatorList::PublisherListStats::PublisherListStats
PublisherListStats(ListDisposition d, PublicKey key, bool avail, std::size_t seq)
Definition: ValidatorList.h:191
ripple::ValidatorList::getAvailable
boost::optional< Json::Value > getAvailable(boost::beast::string_view const &pubKey)
Returns the current valid list for the given publisher key, if available, as a Json object.
Definition: ValidatorList.cpp:801
ripple::ValidatorList::GetCacheFileName
boost::filesystem::path GetCacheFileName(PublicKey const &pubKey)
Get the filename used for caching UNLs.
Definition: ValidatorList.cpp:198
std::vector
STL class.
ripple::ValidatorList::PublisherList::rawManifest
std::string rawManifest
Definition: ValidatorList.h:130
ripple::ValidatorList::trustedPublisher
bool trustedPublisher(PublicKey const &identity) const
Returns true if public key is a trusted publisher.
Definition: ValidatorList.cpp:592
ripple::ValidatorList::setNegativeUNL
void setNegativeUNL(hash_set< PublicKey > const &negUnl)
set the Negative UNL with validators' master public keys
Definition: ValidatorList.cpp:997
ripple::ValidatorList::getQuorumKeys
QuorumKeys getQuorumKeys() const
Get the quorum and all of the trusted keys.
Definition: ValidatorList.h:506
ripple::ValidatorList::PublisherListStats
Describes the result of processing a Validator List (UNL), including some of the information from the...
Definition: ValidatorList.h:185
ripple::ValidatorList::negativeUNL_
hash_set< PublicKey > negativeUNL_
Definition: ValidatorList.h:164
ripple::ValidatorList::filePrefix_
static const std::string filePrefix_
Definition: ValidatorList.h:168
ripple::ValidatorList::updateTrusted
TrustChanges updateTrusted(hash_set< NodeID > const &seenValidators)
Update trusted nodes.
Definition: ValidatorList.cpp:896
ripple::ValidatorList::j_
const beast::Journal j_
Definition: ValidatorList.h:141
ripple::ValidatorList::getTrustedMasterKeys
hash_set< PublicKey > getTrustedMasterKeys() const
get the trusted master public keys
Definition: ValidatorList.cpp:983
ripple::ValidatorList::PublisherList::expiration
TimeKeeper::time_point expiration
Definition: ValidatorList.h:128
ripple::ValidatorList::verify
ListDisposition verify(Json::Value &list, PublicKey &pubKey, std::string const &manifest, std::string const &blob, std::string const &signature)
Check response for trusted valid published list.
Definition: ValidatorList.cpp:491
std::shared_timed_mutex
STL class.
std::function
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:42
ripple::HashRouter
Routing table for objects identified by hash.
Definition: HashRouter.h:52
ripple::ValidatorList::mutex_
std::shared_timed_mutex mutex_
Definition: ValidatorList.h:142
ripple::ValidatorList::ValidatorList
ValidatorList(ManifestCache &validatorManifests, ManifestCache &publisherManifests, TimeKeeper &timeKeeper, std::string const &databasePath, beast::Journal j, boost::optional< std::size_t > minimumQuorum=boost::none)
Definition: ValidatorList.cpp:64
ripple::ValidatorList::getTrustedKey
boost::optional< PublicKey > getTrustedKey(PublicKey const &identity) const
Returns master public key if public key is trusted.
Definition: ValidatorList.cpp:581
ripple::base_uint< 256 >
ripple::ValidatorList::CacheValidatorFile
void CacheValidatorFile(PublicKey const &pubKey, PublisherList const &publisher)
Write a JSON UNL to a cache file.
Definition: ValidatorList.cpp:204
ripple::ValidatorList::PublisherList::rawVersion
std::uint32_t rawVersion
Definition: ValidatorList.h:133
ripple::ValidatorList::PublisherListStats::publisherKey
boost::optional< PublicKey > publisherKey
Definition: ValidatorList.h:201
ripple::ValidatorList::dataPath_
const boost::filesystem::path dataPath_
Definition: ValidatorList.h:140
ripple::ValidatorList::applyList
PublisherListStats applyList(std::string const &manifest, std::string const &blob, std::string const &signature, std::uint32_t version, std::string siteUri, boost::optional< uint256 > const &hash={})
Apply published list of public keys.
Definition: ValidatorList.cpp:296
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::ValidatorList::PublisherList::list
std::vector< PublicKey > list
Definition: ValidatorList.h:126
ripple::ValidatorList
Definition: ValidatorList.h:119
ripple::ValidatorList::PublisherListStats::available
bool available
Definition: ValidatorList.h:202
ripple::TrustChanges::removed
hash_set< NodeID > removed
Definition: ValidatorList.h:73
ripple::ValidatorList::minimumQuorum_
boost::optional< std::size_t > minimumQuorum_
Definition: ValidatorList.h:145
ripple::ValidatorList::publisherManifests_
ManifestCache & publisherManifests_
Definition: ValidatorList.h:138
ripple::ValidatorList::timeKeeper_
TimeKeeper & timeKeeper_
Definition: ValidatorList.h:139
ripple::ValidatorList::listed
bool listed(PublicKey const &identity) const
Returns true if public key is included on any lists.
Definition: ValidatorList.cpp:552
ripple::TrustChanges::TrustChanges
TrustChanges()=default
ripple::ValidatorList::PublisherList::siteUri
std::string siteUri
Definition: ValidatorList.h:129
ripple::ListDisposition::untrusted
@ untrusted
List signed by untrusted publisher key.
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::ValidatorList::requiredListVersion
static constexpr std::uint32_t requiredListVersion
Definition: ValidatorList.h:167
ripple::ValidatorList::applyListAndBroadcast
PublisherListStats applyListAndBroadcast(std::string const &manifest, std::string const &blob, std::string const &signature, std::uint32_t version, std::string siteUri, uint256 const &hash, Overlay &overlay, HashRouter &hashRouter)
Apply published list of public keys, then broadcast it to all peers that have not seen it or sent it.
Definition: ValidatorList.cpp:233
std::uint32_t
std::atomic< std::size_t >
ripple::TimeKeeper
Manages various times used by the server.
Definition: TimeKeeper.h:32
ripple::ListDisposition::unsupported_version
@ unsupported_version
List version is not supported.
ripple::ValidatorList::negativeUNLFilter
std::vector< std::shared_ptr< STValidation > > negativeUNLFilter(std::vector< std::shared_ptr< STValidation >> &&validations) const
Remove validations that are from validators on the negative UNL.
Definition: ValidatorList.cpp:1004
ripple::ValidatorList::PublisherList::available
bool available
Definition: ValidatorList.h:125
ripple::ValidatorList::PublisherList::rawBlob
std::string rawBlob
Definition: ValidatorList.h:131
ripple::ValidatorList::PublisherListStats::PublisherListStats
PublisherListStats(ListDisposition d)
Definition: ValidatorList.h:187
ripple::ValidatorList::calculateQuorum
std::size_t calculateQuorum(std::size_t unlSize, std::size_t effectiveUnlSize, std::size_t seenSize)
Return quorum for trusted validator set.
Definition: ValidatorList.cpp:832
ripple::ValidatorList::localPubKey_
PublicKey localPubKey_
Definition: ValidatorList.h:161
ripple::ManifestCache
Remembers manifests with the highest sequence number.
Definition: Manifest.h:209
ripple::ValidatorList::PublisherList::hash
uint256 hash
Definition: ValidatorList.h:134
ripple::ValidatorList::loadLists
std::vector< std::string > loadLists()
Definition: ValidatorList.cpp:438
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ValidatorList::trustedSigningKeys_
hash_set< PublicKey > trustedSigningKeys_
Definition: ValidatorList.h:159
ripple::ValidatorList::load
bool load(PublicKey const &localSigningKey, std::vector< std::string > const &configKeys, std::vector< std::string > const &publisherKeys)
Load configured trusted keys.
Definition: ValidatorList.cpp:82
ripple::ValidatorList::for_each_available
void for_each_available(std::function< void(std::string const &manifest, std::string const &blob, std::string const &signature, std::uint32_t version, PublicKey const &pubKey, std::size_t sequence, uint256 const &hash)> func) const
Invokes the callback once for every available publisher list's raw data members.
Definition: ValidatorList.cpp:774
ripple::ValidatorList::PublisherList::PublisherList
PublisherList()=default
ripple::Overlay
Manages the set of connected peers.
Definition: Overlay.h:52
ripple::CrawlOptions::Overlay
@ Overlay
Definition: OverlayImpl.cpp:47
ripple::ListDisposition::same_sequence
@ same_sequence
Same sequence as current list.
ripple::ValidatorList::PublisherListStats::sequence
boost::optional< std::size_t > sequence
Definition: ValidatorList.h:203
ripple::ListDisposition::invalid
@ invalid
Invalid format or signature.
ripple::ValidatorList::~ValidatorList
~ValidatorList()=default
ripple::TrustChanges::added
hash_set< NodeID > added
Definition: ValidatorList.h:72
mutex
ripple::ValidatorList::getJson
Json::Value getJson() const
Return a JSON representation of the state of the validator list.
Definition: ValidatorList.cpp:659
std::size_t
numeric
ripple::ValidatorList::expires
boost::optional< TimeKeeper::time_point > expires() const
Return the time when the validator list will expire.
Definition: ValidatorList.cpp:641
ripple::ValidatorList::trusted
bool trusted(PublicKey const &identity) const
Returns true if public key is trusted.
Definition: ValidatorList.cpp:561
ripple::ValidatorList::trustedMasterKeys_
hash_set< PublicKey > trustedMasterKeys_
Definition: ValidatorList.h:154
ripple::ValidatorList::getNegativeUNL
hash_set< PublicKey > getNegativeUNL() const
get the master public keys of Negative UNL validators
Definition: ValidatorList.cpp:990
ripple::ValidatorList::count
std::size_t count() const
Return the number of configured validator list sites.
Definition: ValidatorList.cpp:634
shared_mutex
std::unordered_map
STL class.
beast::abstract_clock< NetClock >::time_point
typename NetClock ::time_point time_point
Definition: abstract_clock.h:63
ripple::ValidatorList::keyListings_
hash_map< PublicKey, std::size_t > keyListings_
Definition: ValidatorList.h:151
ripple::ListDisposition::accepted
@ accepted
List is valid.
ripple::ValidatorList::quorum_
std::atomic< std::size_t > quorum_
Definition: ValidatorList.h:144
std::shared_lock
STL class.
ripple::ValidatorList::PublisherList
Definition: ValidatorList.h:121
ripple::ValidatorList::quorum
std::size_t quorum() const
Get quorum value for current trusted key set.
Definition: ValidatorList.h:342
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::ValidatorList::removePublisherList
bool removePublisherList(PublicKey const &publisherKey)
Stop trusting publisher's list of keys.
Definition: ValidatorList.cpp:606