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 
131  STBase*
132  copy(std::size_t n, void* buf) const override
133  {
134  return emplace(n, buf, *this);
135  }
136 
137  STBase*
138  move(std::size_t n, void* buf) override
139  {
140  return emplace(n, buf, std::move(*this));
141  }
142 
143  // Hash of the validated ledger
144  uint256
145  getLedgerHash() const;
146 
147  // Hash of consensus transaction set used to generate ledger
148  uint256
149  getConsensusHash() const;
150 
152  getSignTime() const;
153 
155  getSeenTime() const;
156 
157  PublicKey
158  getSignerPublic() const;
159 
160  NodeID
161  getNodeID() const
162  {
163  return nodeID_;
164  }
165 
166  bool
167  isValid() const;
168 
169  bool
170  isFull() const;
171 
172  bool
173  isTrusted() const
174  {
175  return mTrusted;
176  }
177 
178  uint256
179  getSigningHash() const;
180 
181  void
183  {
184  mTrusted = true;
185  }
186 
187  void
189  {
190  mTrusted = false;
191  }
192 
193  void
195  {
196  seenTime_ = s;
197  }
198 
199  Blob
200  getSerialized() const;
201 
202  Blob
203  getSignature() const;
204 
205 private:
206  static SOTemplate const&
208 
210  bool mTrusted = false;
212 };
213 
214 } // namespace ripple
215 
216 #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:194
ripple::STValidation::move
STBase * move(std::size_t n, void *buf) override
Definition: STValidation.h:138
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:173
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:458
std::vector< unsigned char >
ripple::STValidation::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STValidation.h:132
ripple::STValidation::getLedgerHash
uint256 getLedgerHash() const
Definition: STValidation.cpp:63
ripple::STValidation::mTrusted
bool mTrusted
Definition: STValidation.h:210
ripple::PublicKey::slice
Slice slice() const noexcept
Definition: PublicKey.h:123
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:460
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:360
ripple::STValidation::nodeID_
NodeID nodeID_
Definition: STValidation.h:209
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:194
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:161
memory
ripple::STValidation::seenTime_
NetClock::time_point seenTime_
Definition: STValidation.h:211
ripple::STValidation::setUntrusted
void setUntrusted()
Definition: STValidation.h:188
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
cassert
ripple::STValidation::setTrusted
void setTrusted()
Definition: STValidation.h:182
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