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 <boost/thread/shared_mutex.hpp>
33 #include <mutex>
34 #include <numeric>
35 #include <shared_mutex>
36 
37 namespace ripple {
38 
39 // predeclaration
40 class Overlay;
41 class HashRouter;
42 class STValidation;
43 
44 enum class ListDisposition {
46  accepted = 0,
47 
50 
53 
55  untrusted,
56 
58  stale,
59 
61  invalid
62 };
63 
65 to_string(ListDisposition disposition);
66 
70 {
71  explicit TrustChanges() = default;
72 
75 };
76 
121 {
123  {
124  explicit PublisherList() = default;
125 
126  bool available;
136  };
137 
141  boost::filesystem::path const dataPath_;
143  boost::shared_mutex mutable mutex_;
144 
146  boost::optional<std::size_t> minimumQuorum_;
147 
148  // Published lists stored by publisher master public key
150 
151  // Listed master public keys with the number of lists they appear on
153 
154  // The current list of trusted master keys
156 
157  // The current list of trusted signing keys. For those validators using
158  // a manifest, the signing key is the ephemeral key. For the ones using
159  // a seed, the signing key is the same as the master key.
161 
163 
164  // The master public keys of the current negative UNL
166 
167  // Currently supported version of publisher list format
168  static constexpr std::uint32_t requiredListVersion = 1;
169  static const std::string filePrefix_;
170 
171 public:
173  ManifestCache& validatorManifests,
174  ManifestCache& publisherManifests,
175  TimeKeeper& timeKeeper,
176  std::string const& databasePath,
177  beast::Journal j,
178  boost::optional<std::size_t> minimumQuorum = boost::none);
179  ~ValidatorList() = default;
180 
187  {
189  {
190  }
191 
193  ListDisposition d,
194  PublicKey key,
195  bool avail,
196  std::size_t seq)
197  : disposition(d), publisherKey(key), available(avail), sequence(seq)
198  {
199  }
200 
202  boost::optional<PublicKey> publisherKey;
203  bool available = false;
204  boost::optional<std::size_t> sequence;
205  };
206 
224  bool
225  load(
226  PublicKey const& localSigningKey,
227  std::vector<std::string> const& configKeys,
228  std::vector<std::string> const& publisherKeys);
229 
259  std::string const& manifest,
260  std::string const& blob,
261  std::string const& signature,
262  std::uint32_t version,
263  std::string siteUri,
264  uint256 const& hash,
265  Overlay& overlay,
266  HashRouter& hashRouter);
267 
291  applyList(
292  std::string const& manifest,
293  std::string const& blob,
294  std::string const& signature,
295  std::uint32_t version,
296  std::string siteUri,
297  boost::optional<uint256> const& hash = {});
298 
299  /* Attempt to read previously stored list files. Expected to only be
300  called when loading from URL fails.
301 
302  @return A list of valid file:// URLs, if any.
303 
304  @par Thread Safety
305 
306  May be called concurrently
307  */
309  loadLists();
310 
327  updateTrusted(hash_set<NodeID> const& seenValidators);
328 
343  quorum() const
344  {
345  return quorum_;
346  }
347 
356  bool
357  trusted(PublicKey const& identity) const;
358 
367  bool
368  listed(PublicKey const& identity) const;
369 
380  boost::optional<PublicKey>
381  getTrustedKey(PublicKey const& identity) const;
382 
393  boost::optional<PublicKey>
394  getListedKey(PublicKey const& identity) const;
395 
404  bool
405  trustedPublisher(PublicKey const& identity) const;
406 
413  PublicKey
414  localPublicKey() const;
415 
431  void
432  for_each_listed(std::function<void(PublicKey const&, bool)> func) const;
433 
461  void
463  std::string const& manifest,
464  std::string const& blob,
465  std::string const& signature,
466  std::uint32_t version,
467  PublicKey const& pubKey,
468  std::size_t sequence,
469  uint256 const& hash)> func) const;
470 
474  boost::optional<Json::Value>
475  getAvailable(boost::beast::string_view const& pubKey);
476 
479  count() const;
480 
490  boost::optional<TimeKeeper::time_point>
491  expires() const;
492 
499  getJson() const;
500 
506  QuorumKeys
508  {
509  std::shared_lock read_lock{mutex_};
510  return {quorum_, trustedSigningKeys_};
511  }
512 
518  getTrustedMasterKeys() const;
519 
525  getNegativeUNL() const;
526 
531  void
532  setNegativeUNL(hash_set<PublicKey> const& negUnl);
533 
542  std::vector<std::shared_ptr<STValidation>>&& validations) const;
543 
544 private:
547  boost::filesystem::path
548  GetCacheFileName(PublicKey const& pubKey);
549 
552  void
553  CacheValidatorFile(PublicKey const& pubKey, PublisherList const& publisher);
554 
564  verify(
565  Json::Value& list,
566  PublicKey& pubKey,
567  std::string const& manifest,
568  std::string const& blob,
569  std::string const& signature);
570 
581  bool
582  removePublisherList(PublicKey const& publisherKey);
583 
596  std::size_t unlSize,
597  std::size_t effectiveUnlSize,
598  std::size_t seenSize);
599 };
600 } // namespace ripple
601 
602 #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:128
ripple::ValidatorList::validatorManifests_
ManifestCache & validatorManifests_
Definition: ValidatorList.h:138
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:44
ripple::ValidatorList::PublisherList::rawSignature
std::string rawSignature
Definition: ValidatorList.h:133
ripple::ValidatorList::PublisherListStats::disposition
ListDisposition disposition
Definition: ValidatorList.h:201
ripple::TrustChanges
Changes in trusted nodes after updating validator list.
Definition: ValidatorList.h:69
ripple::ListDisposition::stale
@ stale
Trusted publisher key, but seq is too old.
ripple::ValidatorList::publisherLists_
hash_map< PublicKey, PublisherList > publisherLists_
Definition: ValidatorList.h:149
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:192
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:131
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:998
ripple::ValidatorList::getQuorumKeys
QuorumKeys getQuorumKeys() const
Get the quorum and all of the trusted keys.
Definition: ValidatorList.h:507
ripple::ValidatorList::PublisherListStats
Describes the result of processing a Validator List (UNL), including some of the information from the...
Definition: ValidatorList.h:186
ripple::ValidatorList::negativeUNL_
hash_set< PublicKey > negativeUNL_
Definition: ValidatorList.h:165
ripple::ValidatorList::filePrefix_
static const std::string filePrefix_
Definition: ValidatorList.h:169
ripple::ValidatorList::updateTrusted
TrustChanges updateTrusted(hash_set< NodeID > const &seenValidators)
Update trusted nodes.
Definition: ValidatorList.cpp:897
ripple::ValidatorList::j_
const beast::Journal j_
Definition: ValidatorList.h:142
ripple::ValidatorList::getTrustedMasterKeys
hash_set< PublicKey > getTrustedMasterKeys() const
get the trusted master public keys
Definition: ValidatorList.cpp:984
ripple::ValidatorList::PublisherList::expiration
TimeKeeper::time_point expiration
Definition: ValidatorList.h:129
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::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::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:134
ripple::ValidatorList::PublisherListStats::publisherKey
boost::optional< PublicKey > publisherKey
Definition: ValidatorList.h:202
ripple::ValidatorList::dataPath_
const boost::filesystem::path dataPath_
Definition: ValidatorList.h:141
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:127
ripple::ValidatorList
Definition: ValidatorList.h:120
ripple::ValidatorList::PublisherListStats::available
bool available
Definition: ValidatorList.h:203
ripple::TrustChanges::removed
hash_set< NodeID > removed
Definition: ValidatorList.h:74
ripple::ValidatorList::minimumQuorum_
boost::optional< std::size_t > minimumQuorum_
Definition: ValidatorList.h:146
ripple::ValidatorList::publisherManifests_
ManifestCache & publisherManifests_
Definition: ValidatorList.h:139
ripple::ValidatorList::timeKeeper_
TimeKeeper & timeKeeper_
Definition: ValidatorList.h:140
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:130
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:168
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:1005
ripple::ValidatorList::PublisherList::available
bool available
Definition: ValidatorList.h:126
ripple::ValidatorList::PublisherList::rawBlob
std::string rawBlob
Definition: ValidatorList.h:132
ripple::ValidatorList::PublisherListStats::PublisherListStats
PublisherListStats(ListDisposition d)
Definition: ValidatorList.h:188
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:833
ripple::ValidatorList::localPubKey_
PublicKey localPubKey_
Definition: ValidatorList.h:162
ripple::ManifestCache
Remembers manifests with the highest sequence number.
Definition: Manifest.h:209
ripple::ValidatorList::PublisherList::hash
uint256 hash
Definition: ValidatorList.h:135
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:160
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:204
ripple::ListDisposition::invalid
@ invalid
Invalid format or signature.
ripple::ValidatorList::mutex_
boost::shared_mutex mutex_
Definition: ValidatorList.h:143
ripple::ValidatorList::~ValidatorList
~ValidatorList()=default
ripple::TrustChanges::added
hash_set< NodeID > added
Definition: ValidatorList.h:73
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:155
ripple::ValidatorList::getNegativeUNL
hash_set< PublicKey > getNegativeUNL() const
get the master public keys of Negative UNL validators
Definition: ValidatorList.cpp:991
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:152
ripple::ListDisposition::accepted
@ accepted
List is valid.
ripple::ValidatorList::quorum_
std::atomic< std::size_t > quorum_
Definition: ValidatorList.h:145
std::shared_lock
STL class.
ripple::ValidatorList::PublisherList
Definition: ValidatorList.h:122
ripple::ValidatorList::quorum
std::size_t quorum() const
Get quorum value for current trusted key set.
Definition: ValidatorList.h:343
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