rippled
ShardInfo.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 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 #include <ripple/app/main/Application.h>
21 #include <ripple/core/TimeKeeper.h>
22 #include <ripple/nodestore/ShardInfo.h>
23 #include <ripple/protocol/SecretKey.h>
24 #include <ripple/protocol/digest.h>
25 
26 namespace ripple {
27 namespace NodeStore {
28 
31 {
32  if (!finalized_.empty())
34  return {};
35 }
36 
39 {
40  std::string result;
41  if (!incomplete_.empty())
42  {
43  for (auto const& [shardIndex, incomplete] : incomplete_)
44  {
45  result += std::to_string(shardIndex) + ":" +
46  std::to_string(incomplete.percentProgress()) + ",";
47  }
48  result.pop_back();
49  }
50 
51  return result;
52 }
53 
54 bool
56  std::uint32_t shardIndex,
57  ShardState state,
58  std::uint32_t percentProgress)
59 {
60  if (state == ShardState::finalized)
61  {
62  if (boost::icl::contains(finalized_, shardIndex))
63  return false;
64 
65  finalized_.insert(shardIndex);
66  return true;
67  }
68 
69  return incomplete_.emplace(shardIndex, Incomplete(state, percentProgress))
70  .second;
71 }
72 
73 protocol::TMPeerShardInfoV2
75 {
76  protocol::TMPeerShardInfoV2 message;
77  Serializer s;
79 
80  // Set the message creation time
81  msgTimestamp_ = app.timeKeeper().now();
82  {
83  auto const timestamp{msgTimestamp_.time_since_epoch().count()};
84  message.set_timestamp(timestamp);
85  s.add32(timestamp);
86  }
87 
88  if (!incomplete_.empty())
89  {
90  message.mutable_incomplete()->Reserve(incomplete_.size());
91  for (auto const& [shardIndex, incomplete] : incomplete_)
92  {
93  auto tmIncomplete{message.add_incomplete()};
94 
95  tmIncomplete->set_shardindex(shardIndex);
96  s.add32(shardIndex);
97 
98  static_assert(std::is_same_v<
99  std::underlying_type_t<decltype(incomplete.state())>,
100  std::uint32_t>);
101  auto const state{static_cast<std::uint32_t>(incomplete.state())};
102  tmIncomplete->set_state(state);
103  s.add32(state);
104 
105  // Set progress if greater than zero
106  auto const percentProgress{incomplete.percentProgress()};
107  if (percentProgress > 0)
108  {
109  tmIncomplete->set_progress(percentProgress);
110  s.add32(percentProgress);
111  }
112  }
113  }
114 
115  if (!finalized_.empty())
116  {
117  auto const str{ripple::to_string(finalized_)};
118  message.set_finalized(str);
119  s.addRaw(str.data(), str.size());
120  }
121 
122  // Set the public key
123  auto const& publicKey{app.nodeIdentity().first};
124  message.set_publickey(publicKey.data(), publicKey.size());
125 
126  // Create a digital signature using the node private key
127  auto const signature{sign(publicKey, app.nodeIdentity().second, s.slice())};
128 
129  // Set the digital signature
130  message.set_signature(signature.data(), signature.size());
131 
132  return message;
133 }
134 
135 } // namespace NodeStore
136 } // namespace ripple
ripple::Application
Definition: Application.h:115
std::is_same_v
T is_same_v
std::string
STL class.
ripple::NodeStore::ShardInfo::incomplete_
std::map< std::uint32_t, Incomplete > incomplete_
Definition: ShardInfo.h:113
ripple::Application::timeKeeper
virtual TimeKeeper & timeKeeper()=0
ripple::NodeStore::ShardInfo::update
bool update(std::uint32_t shardIndex, ShardState state, std::uint32_t percentProgress)
Definition: ShardInfo.cpp:55
ripple::ShardState
ShardState
Shard states.
Definition: nodestore/Types.h:60
std::underlying_type_t
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
ripple::NodeStore::ShardInfo::Incomplete
Definition: ShardInfo.h:35
ripple::NodeStore::ShardInfo::finalized_
RangeSet< std::uint32_t > finalized_
Definition: ShardInfo.h:110
ripple::NodeStore::ShardInfo::incomplete
std::map< std::uint32_t, Incomplete > const & incomplete() const
Definition: ShardInfo.h:93
ripple::NodeStore::ShardInfo::makeMessage
protocol::TMPeerShardInfoV2 makeMessage(Application &app)
Definition: ShardInfo.cpp:74
ripple::Application::nodeIdentity
virtual std::pair< PublicKey, SecretKey > const & nodeIdentity()=0
ripple::Serializer::addRaw
int addRaw(Blob const &vector)
Definition: Serializer.cpp:100
std::to_string
T to_string(T... args)
ripple::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:63
ripple::ShardState::finalized
@ finalized
std::uint32_t
std::string::pop_back
T pop_back(T... args)
ripple::NodeStore::ShardInfo::msgTimestamp_
NetClock::time_point msgTimestamp_
Definition: ShardInfo.h:116
ripple::Serializer
Definition: Serializer.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sign
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &m)
Generate a signature for a message.
Definition: SecretKey.cpp:238
ripple::NodeStore::ShardInfo::incompleteToString
std::string incompleteToString() const
Definition: ShardInfo.cpp:38
ripple::NodeStore::ShardInfo::finalizedToString
std::string finalizedToString() const
Definition: ShardInfo.cpp:30
ripple::TimeKeeper::now
virtual time_point now() const override=0
Returns the estimate of wall time, in network time.
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::Serializer::add32
int add32(std::uint32_t i)
Definition: Serializer.cpp:38
ripple::HashPrefix::shardInfo
@ shardInfo
shard info for signing