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 
33 namespace ripple {
34 
35 // Validation flags
36 
37 // This is a full (as opposed to a partial) validation
38 constexpr std::uint32_t vfFullValidation = 0x00000001;
39 
40 // The signature is fully canonical
41 constexpr std::uint32_t vfFullyCanonicalSig = 0x80000000;
42 
43 class STValidation final : public STObject, public CountedObject<STValidation>
44 {
45 public:
62  template <class LookupNodeID>
64  SerialIter& sit,
65  LookupNodeID&& lookupNodeID,
66  bool checkSignature)
68  {
69  auto const spk = getFieldVL(sfSigningPubKey);
70 
72  {
73  JLOG(debugLog().error()) << "Invalid public key in validation: "
75  Throw<std::runtime_error>("Invalid public key in validation");
76  }
77 
78  if (checkSignature && !isValid())
79  {
80  JLOG(debugLog().error()) << "Invalid signature in validation: "
82  Throw<std::runtime_error>("Invalid signature in validation");
83  }
84 
85  nodeID_ = lookupNodeID(PublicKey(makeSlice(spk)));
86  assert(nodeID_.isNonZero());
87  }
88 
97  template <typename F>
99  NetClock::time_point signTime,
100  PublicKey const& pk,
101  SecretKey const& sk,
102  NodeID const& nodeID,
103  F&& f)
105  , nodeID_(nodeID)
106  , seenTime_(signTime)
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 
134  STBase*
135  copy(std::size_t n, void* buf) const override
136  {
137  return emplace(n, buf, *this);
138  }
139 
140  STBase*
141  move(std::size_t n, void* buf) override
142  {
143  return emplace(n, buf, std::move(*this));
144  }
145 
146  // Hash of the validated ledger
147  uint256
148  getLedgerHash() const;
149 
150  // Hash of consensus transaction set used to generate ledger
151  uint256
152  getConsensusHash() const;
153 
155  getSignTime() const;
156 
158  getSeenTime() const;
159 
160  PublicKey
161  getSignerPublic() const;
162 
163  NodeID
164  getNodeID() const
165  {
166  return nodeID_;
167  }
168 
169  bool
170  isValid() const;
171 
172  bool
173  isFull() const;
174 
175  bool
176  isTrusted() const
177  {
178  return mTrusted;
179  }
180 
181  uint256
182  getSigningHash() const;
183 
184  void
186  {
187  mTrusted = true;
188  }
189 
190  void
192  {
193  mTrusted = false;
194  }
195 
196  void
198  {
199  seenTime_ = s;
200  }
201 
202  Blob
203  getSerialized() const;
204 
205  Blob
206  getSignature() const;
207 
208 private:
209  static SOTemplate const&
211 
213  bool mTrusted = false;
215 };
216 
217 } // namespace ripple
218 
219 #endif
ripple::STValidation::isValid
bool isValid() const
Definition: STValidation.cpp:87
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:141
ripple::STValidation::getConsensusHash
uint256 getConsensusHash() const
Definition: STValidation.cpp:69
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:444
ripple::STValidation::isTrusted
bool isTrusted() const
Definition: STValidation.h:176
ripple::publicKeyType
boost::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:203
functional
ripple::STValidation::isFull
bool isFull() const
Definition: STValidation.cpp:114
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:135
ripple::STValidation::getLedgerHash
uint256 getLedgerHash() const
Definition: STValidation.cpp:63
ripple::STValidation::mTrusted
bool mTrusted
Definition: STValidation.h:213
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:120
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::STValidation
Definition: STValidation.h:43
ripple::vfFullyCanonicalSig
constexpr std::uint32_t vfFullyCanonicalSig
Definition: STValidation.h:41
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:149
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::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:98
ripple::JsonOptions::none
@ none
ripple::STValidation::nodeID_
NodeID nodeID_
Definition: STValidation.h:212
ripple::STValidation::getSerialized
Blob getSerialized() const
Definition: STValidation.cpp:126
std::chrono::time_point
cstdint
ripple::STValidation::setSeen
void setSeen(NetClock::time_point s)
Definition: STValidation.h:197
ripple::SerialIter
Definition: Serializer.h:308
std::uint32_t
ripple::SecretKey
A secret key.
Definition: SecretKey.h:36
ripple::STValidation::getSignTime
NetClock::time_point getSignTime() const
Definition: STValidation.cpp:75
ripple::STValidation::getNodeID
NodeID getNodeID() const
Definition: STValidation.h:164
memory
ripple::STValidation::seenTime_
NetClock::time_point seenTime_
Definition: STValidation.h:214
ripple::STValidation::setUntrusted
void setUntrusted()
Definition: STValidation.h:191
ripple::KeyType::secp256k1
@ secp256k1
ripple::STValidation::getSeenTime
NetClock::time_point getSeenTime() const
Definition: STValidation.cpp:81
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:185
ripple::sfSignature
const SF_VL sfSignature
ripple::STObject::setFlag
bool setFlag(std::uint32_t)
Definition: STObject.cpp:424
ripple::STValidation::getSigningHash
uint256 getSigningHash() const
Definition: STValidation.cpp:57
ripple::sfValidation
const SField sfValidation
std::size_t
ripple::STValidation::getSignerPublic
PublicKey getSignerPublic() const
Definition: STValidation.cpp:108
ripple::vfFullValidation
constexpr std::uint32_t vfFullValidation
Definition: STValidation.h:38
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:98
ripple::STObject::getJson
virtual Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:698
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.
Definition: STValidation.h:63