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:
46  static char const*
48  {
49  return "STValidation";
50  }
51 
68  template <class LookupNodeID>
70  SerialIter& sit,
71  LookupNodeID&& lookupNodeID,
72  bool checkSignature)
74  {
75  auto const spk = getFieldVL(sfSigningPubKey);
76 
78  {
79  JLOG(debugLog().error()) << "Invalid public key in validation: "
81  Throw<std::runtime_error>("Invalid public key in validation");
82  }
83 
84  if (checkSignature && !isValid())
85  {
86  JLOG(debugLog().error()) << "Invalid signature in validation: "
88  Throw<std::runtime_error>("Invalid signature in validation");
89  }
90 
91  nodeID_ = lookupNodeID(PublicKey(makeSlice(spk)));
92  assert(nodeID_.isNonZero());
93  }
94 
103  template <typename F>
105  NetClock::time_point signTime,
106  PublicKey const& pk,
107  SecretKey const& sk,
108  NodeID const& nodeID,
109  F&& f)
111  , nodeID_(nodeID)
112  , seenTime_(signTime)
113  {
114  // First, set our own public key:
116  LogicError(
117  "We can only use secp256k1 keys for signing validations");
118 
120  setFieldU32(sfSigningTime, signTime.time_since_epoch().count());
121 
122  // Perform additional initialization
123  f(*this);
124 
125  // Finally, sign the validation and mark it as trusted:
128  setTrusted();
129 
130  // Check to ensure that all required fields are present.
131  for (auto const& e : validationFormat())
132  {
133  if (e.style() == soeREQUIRED && !isFieldPresent(e.sField()))
134  LogicError(
135  "Required field '" + e.sField().getName() +
136  "' missing from validation.");
137  }
138  }
139 
140  STBase*
141  copy(std::size_t n, void* buf) const override
142  {
143  return emplace(n, buf, *this);
144  }
145 
146  STBase*
147  move(std::size_t n, void* buf) override
148  {
149  return emplace(n, buf, std::move(*this));
150  }
151 
152  // Hash of the validated ledger
153  uint256
154  getLedgerHash() const;
155 
156  // Hash of consensus transaction set used to generate ledger
157  uint256
158  getConsensusHash() const;
159 
161  getSignTime() const;
162 
164  getSeenTime() const;
165 
166  PublicKey
167  getSignerPublic() const;
168 
169  NodeID
170  getNodeID() const
171  {
172  return nodeID_;
173  }
174 
175  bool
176  isValid() const;
177 
178  bool
179  isFull() const;
180 
181  bool
182  isTrusted() const
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 
219  bool mTrusted = false;
221 };
222 
223 } // namespace ripple
224 
225 #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:147
ripple::STValidation::getConsensusHash
uint256 getConsensusHash() const
Definition: STValidation.cpp:69
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:110
ripple::base_uint::isNonZero
bool isNonZero() const
Definition: base_uint.h:480
ripple::STValidation::isTrusted
bool isTrusted() const
Definition: STValidation.h:182
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
ripple::STValidation::getCountedObjectName
static char const * getCountedObjectName()
Definition: STValidation.h:47
ripple::sfSigningPubKey
const SF_Blob sfSigningPubKey(access, STI_VL, 3, "SigningPubKey")
Definition: SField.h:459
std::vector< unsigned char >
ripple::STValidation::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STValidation.h:141
ripple::STValidation::getLedgerHash
uint256 getLedgerHash() const
Definition: STValidation.cpp:63
ripple::STValidation::mTrusted
bool mTrusted
Definition: STValidation.h:219
ripple::PublicKey::slice
Slice slice() const noexcept
Definition: PublicKey.h:123
ripple::soeREQUIRED
@ soeREQUIRED
Definition: SOTemplate.h:34
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::sfSignature
const SF_Blob sfSignature(access, STI_VL, 6, "Signature", SField::sMD_Default, SField::notSigning)
Definition: SField.h:461
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:81
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::sfSigningTime
const SF_U32 sfSigningTime(access, STI_UINT32, 9, "SigningTime")
Definition: SField.h:361
ripple::STValidation::nodeID_
NodeID nodeID_
Definition: STValidation.h:218
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:203
ripple::SerialIter
Definition: Serializer.h:308
std::uint32_t
ripple::SecretKey
A secret key.
Definition: SecretKey.h:36
ripple::sfValidation
const SField sfValidation(access, STI_VALIDATION, 257, "Validation")
Definition: SField.h:335
ripple::STValidation::getSignTime
NetClock::time_point getSignTime() const
Definition: STValidation.cpp:75
ripple::STValidation::getNodeID
NodeID getNodeID() const
Definition: STValidation.h:170
memory
ripple::STValidation::seenTime_
NetClock::time_point seenTime_
Definition: STValidation.h:220
ripple::STValidation::setUntrusted
void setUntrusted()
Definition: STValidation.h:197
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:191
ripple::STObject::setFlag
bool setFlag(std::uint32_t)
Definition: STObject.cpp:424
ripple::STValidation::getSigningHash
uint256 getSigningHash() const
Definition: STValidation.cpp:57
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:104
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::STValidation::STValidation
STValidation(SerialIter &sit, LookupNodeID &&lookupNodeID, bool checkSignature)
Construct a STValidation from a peer.
Definition: STValidation.h:69