rippled
STValidation.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_PROTOCOL_STVALIDATION_H_INCLUDED
21 #define RIPPLE_PROTOCOL_STVALIDATION_H_INCLUDED
22 
23 #include <ripple/basics/FeeUnits.h>
24 #include <ripple/basics/Log.h>
25 #include <ripple/protocol/PublicKey.h>
26 #include <ripple/protocol/STObject.h>
27 #include <ripple/protocol/SecretKey.h>
28 #include <cassert>
29 #include <cstdint>
30 #include <functional>
31 #include <memory>
32 #include <optional>
33 
34 namespace ripple {
35 
36 // Validation flags
37 
38 // This is a full (as opposed to a partial) validation
39 constexpr std::uint32_t vfFullValidation = 0x00000001;
40 
41 // The signature is fully canonical
42 constexpr std::uint32_t vfFullyCanonicalSig = 0x80000000;
43 
44 class STValidation final : public STObject, public CountedObject<STValidation>
45 {
46 public:
60  template <class LookupNodeID>
62  SerialIter& sit,
63  LookupNodeID&& lookupNodeID,
64  bool checkSignature)
66  , signingPubKey_([this]() {
67  auto const spk = getFieldVL(sfSigningPubKey);
68 
70  Throw<std::runtime_error>("Invalid public key in validation");
71 
72  return PublicKey{makeSlice(spk)};
73  }())
74  , nodeID_(lookupNodeID(signingPubKey_))
75  {
76  if (checkSignature && !isValid())
77  {
78  JLOG(debugLog().error()) << "Invalid signature in validation: "
80  Throw<std::runtime_error>("Invalid signature in validation");
81  }
82 
83  assert(nodeID_.isNonZero());
84  }
85 
94  template <typename F>
96  NetClock::time_point signTime,
97  PublicKey const& pk,
98  SecretKey const& sk,
99  NodeID const& nodeID,
100  F&& f)
102  , signingPubKey_(pk)
103  , nodeID_(nodeID)
104  , seenTime_(signTime)
105  {
106  assert(nodeID_.isNonZero());
107 
108  // First, set our own public key:
110  LogicError(
111  "We can only use secp256k1 keys for signing validations");
112 
114  setFieldU32(sfSigningTime, signTime.time_since_epoch().count());
115 
116  // Perform additional initialization
117  f(*this);
118 
119  // Finally, sign the validation and mark it as trusted:
122  setTrusted();
123 
124  // Check to ensure that all required fields are present.
125  for (auto const& e : validationFormat())
126  {
127  if (e.style() == soeREQUIRED && !isFieldPresent(e.sField()))
128  LogicError(
129  "Required field '" + e.sField().getName() +
130  "' missing from validation.");
131  }
132 
133  // We just signed this, so it should be valid.
134  valid_ = true;
135  }
136 
137  STBase*
138  copy(std::size_t n, void* buf) const override
139  {
140  return emplace(n, buf, *this);
141  }
142 
143  STBase*
144  move(std::size_t n, void* buf) override
145  {
146  return emplace(n, buf, std::move(*this));
147  }
148 
149  // Hash of the validated ledger
150  uint256
151  getLedgerHash() const;
152 
153  // Hash of consensus transaction set used to generate ledger
154  uint256
155  getConsensusHash() const;
156 
158  getSignTime() const;
159 
161  getSeenTime() const noexcept;
162 
163  PublicKey const&
164  getSignerPublic() const noexcept
165  {
166  return signingPubKey_;
167  }
168 
169  NodeID const&
170  getNodeID() const noexcept
171  {
172  return nodeID_;
173  }
174 
175  bool
176  isValid() const noexcept;
177 
178  bool
179  isFull() const noexcept;
180 
181  bool
182  isTrusted() const noexcept
183  {
184  return mTrusted;
185  }
186 
187  uint256
188  getSigningHash() const;
189 
190  void
192  {
193  mTrusted = true;
194  }
195 
196  void
198  {
199  mTrusted = false;
200  }
201 
202  void
204  {
205  seenTime_ = s;
206  }
207 
208  Blob
209  getSerialized() const;
210 
211  Blob
212  getSignature() const;
213 
214 private:
215  static SOTemplate const&
217 
218  bool mTrusted = false;
219 
220  // Determines the validity of the signature in this validation; unseated
221  // optional if we haven't yet checked it, a boolean otherwise.
223 
224  // The public key associated with the key used to sign this validation
226 
227  // The ID of the validator that issued this validation. For validators
228  // that use manifests this will be derived from the master public key.
230 
232 };
233 
234 } // namespace ripple
235 
236 #endif
ripple::STValidation::nodeID_
const NodeID nodeID_
Definition: STValidation.h:229
ripple::makeSlice
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:240
ripple::STValidation::move
STBase * move(std::size_t n, void *buf) override
Definition: STValidation.h:144
ripple::STValidation::getConsensusHash
uint256 getConsensusHash() const
Definition: STValidation.cpp:71
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::base_uint::isNonZero
bool isNonZero() const
Definition: base_uint.h:516
functional
std::vector< unsigned char >
ripple::sfSigningPubKey
const SF_VL sfSigningPubKey
ripple::STValidation::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STValidation.h:138
ripple::STValidation::getLedgerHash
uint256 getLedgerHash() const
Definition: STValidation.cpp:65
ripple::STValidation::signingPubKey_
const PublicKey signingPubKey_
Definition: STValidation.h:225
ripple::STValidation::mTrusted
bool mTrusted
Definition: STValidation.h:218
ripple::PublicKey::slice
Slice slice() const noexcept
Definition: PublicKey.h:123
ripple::soeREQUIRED
@ soeREQUIRED
Definition: SOTemplate.h:35
ripple::STObject::setFieldVL
void setFieldVL(SField const &field, Blob const &)
Definition: STObject.cpp:668
ripple::STObject::getFieldVL
Blob getFieldVL(SField const &field) const
Definition: STObject.cpp:568
ripple::STValidation::validationFormat
static SOTemplate const & validationFormat()
Definition: STValidation.cpp:29
ripple::STValidation::getSignature
Blob getSignature() const
Definition: STValidation.cpp:112
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::STValidation::isTrusted
bool isTrusted() const noexcept
Definition: STValidation.h:182
ripple::STValidation
Definition: STValidation.h:44
ripple::STValidation::getSignerPublic
PublicKey const & getSignerPublic() const noexcept
Definition: STValidation.h:164
ripple::vfFullyCanonicalSig
constexpr std::uint32_t vfFullyCanonicalSig
Definition: STValidation.h:42
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:149
ripple::publicKeyType
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:203
ripple::base_uint< 160, detail::NodeIDTag >
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
ripple::SOTemplate
Defines the fields and their attributes within a STObject.
Definition: SOTemplate.h:82
ripple::STValidation::valid_
std::optional< bool > valid_
Definition: STValidation.h:222
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::signDigest
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
Definition: SecretKey.cpp:212
ripple::JsonOptions::none
@ none
ripple::STValidation::getSerialized
Blob getSerialized() const
Definition: STValidation.cpp:118
ripple::STValidation::isFull
bool isFull() const noexcept
Definition: STValidation.cpp:106
std::chrono::time_point
cstdint
ripple::STValidation::setSeen
void setSeen(NetClock::time_point s)
Definition: STValidation.h:203
ripple::SerialIter
Definition: Serializer.h:310
std::uint32_t
ripple::SecretKey
A secret key.
Definition: SecretKey.h:36
ripple::STValidation::getSignTime
NetClock::time_point getSignTime() const
Definition: STValidation.cpp:77
memory
ripple::STValidation::seenTime_
NetClock::time_point seenTime_
Definition: STValidation.h:231
ripple::STValidation::isValid
bool isValid() const noexcept
Definition: STValidation.cpp:89
ripple::STValidation::setUntrusted
void setUntrusted()
Definition: STValidation.h:197
ripple::KeyType::secp256k1
@ secp256k1
ripple::STObject
Definition: STObject.h:51
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:62
ripple::LogicError
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition: contract.cpp:48
ripple::STObject::isFieldPresent
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:401
cassert
ripple::STValidation::setTrusted
void setTrusted()
Definition: STValidation.h:191
ripple::sfSignature
const SF_VL sfSignature
ripple::STValidation::getSeenTime
NetClock::time_point getSeenTime() const noexcept
Definition: STValidation.cpp:83
ripple::STObject::setFlag
bool setFlag(std::uint32_t)
Definition: STObject.cpp:424
ripple::STValidation::getSigningHash
uint256 getSigningHash() const
Definition: STValidation.cpp:59
ripple::sfValidation
const SField sfValidation
optional
std::size_t
ripple::vfFullValidation
constexpr std::uint32_t vfFullValidation
Definition: STValidation.h:39
ripple::STValidation::STValidation
STValidation(NetClock::time_point signTime, PublicKey const &pk, SecretKey const &sk, NodeID const &nodeID, F &&f)
Construct, sign and trust a new STValidation issued by this node.
Definition: STValidation.h:95
ripple::STObject::getJson
virtual Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:698
ripple::STValidation::getNodeID
NodeID const & getNodeID() const noexcept
Definition: STValidation.h:170
ripple::STObject::setFieldU32
void setFieldU32(SField const &field, std::uint32_t)
Definition: STObject.cpp:632
ripple::sfSigningTime
const SF_UINT32 sfSigningTime
ripple::STValidation::STValidation
STValidation(SerialIter &sit, LookupNodeID &&lookupNodeID, bool checkSignature)
Construct a STValidation from a peer from serialized data.
Definition: STValidation.h:61