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 <cstdint>
29 #include <functional>
30 #include <memory>
31 
32 namespace ripple {
33 
34 // Validation flags
36  0x80000000; // signature is fully canonical
37 
38 class STValidation final : public STObject, public CountedObject<STValidation>
39 {
40 public:
41  static char const*
43  {
44  return "STValidation";
45  }
46 
49 
50  enum { kFullFlag = 0x1 };
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())
80  << "Invalid public key in validation"
82  Throw<std::runtime_error> ("Invalid public key in validation");
83  }
84 
85  if (checkSignature && !isValid ())
86  {
87  JLOG (debugLog().error())
88  << "Invalid signature in validation"
90  Throw<std::runtime_error> ("Invalid signature in validation");
91  }
92 
93  mNodeID = lookupNodeID(PublicKey(makeSlice(spk)));
94  assert(mNodeID.isNonZero());
95  }
96 
101  struct FeeSettings
102  {
103  boost::optional<std::uint32_t> loadFee;
104  boost::optional<XRPAmount> baseFee;
105  boost::optional<XRPAmount> reserveBase;
106  boost::optional<XRPAmount> reserveIncrement;
107  };
108 
128  STValidation(
129  uint256 const& ledgerHash,
130  std::uint32_t ledgerSeq,
131  uint256 const& consensusHash,
132  NetClock::time_point signTime,
133  PublicKey const& publicKey,
134  SecretKey const& secretKey,
135  NodeID const& nodeID,
136  bool isFull,
137  FeeSettings const& fees,
138  std::vector<uint256> const& amendments);
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 mNodeID;
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  mSeen = s;
206  }
207 
208  Blob
209  getSerialized() const;
210 
211  Blob
212  getSignature() const;
213 
214 private:
215 
216  static SOTemplate const&
217  getFormat();
218 
220  bool mTrusted = false;
222 };
223 
224 } // ripple
225 
226 #endif
ripple::STValidation::isValid
bool isValid() const
Definition: STValidation.cpp:117
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:199
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:101
ripple::STValidation::FeeSettings::reserveBase
boost::optional< XRPAmount > reserveBase
Definition: STValidation.h:105
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:102
std::shared_ptr< STValidation >
ripple::base_uint::isNonZero
bool isNonZero() const
Definition: base_uint.h:430
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:207
functional
ripple::STValidation::isFull
bool isFull() const
Definition: STValidation.cpp:141
ripple::STValidation::getCountedObjectName
static char const * getCountedObjectName()
Definition: STValidation.h:42
ripple::sfSigningPubKey
const SF_Blob sfSigningPubKey(access, STI_VL, 3, "SigningPubKey")
Definition: SField.h:442
std::vector
STL class.
ripple::STValidation::FeeSettings::loadFee
boost::optional< std::uint32_t > loadFee
Definition: STValidation.h:103
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:96
ripple::STValidation::mTrusted
bool mTrusted
Definition: STValidation.h:220
ripple::STObject::getFieldVL
Blob getFieldVL(SField const &field) const
Definition: STObject.cpp:542
ripple::STValidation::mNodeID
NodeID mNodeID
Definition: STValidation.h:219
ripple::STValidation::getSignature
Blob getSignature() const
Definition: STValidation.cpp:146
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:417
ripple::STValidation
Definition: STValidation.h:38
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:161
ripple::base_uint< 256 >
ripple::SOTemplate
Defines the fields and their attributes within a STObject.
Definition: SOTemplate.h:75
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::JsonOptions::none
@ none
ripple::STValidation::getFormat
static SOTemplate const & getFormat()
Definition: STValidation.cpp:158
ripple::STValidation::getSerialized
Blob getSerialized() const
Definition: STValidation.cpp:151
ripple::STValidation::mSeen
NetClock::time_point mSeen
Definition: STValidation.h:221
std::chrono::time_point
cstdint
ripple::STValidation::setSeen
void setSeen(NetClock::time_point s)
Definition: STValidation.h:203
ripple::SerialIter
Definition: Serializer.h:311
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:320
ripple::STValidation::getSignTime
NetClock::time_point getSignTime() const
Definition: STValidation.cpp:107
ripple::STValidation::getNodeID
NodeID getNodeID() const
Definition: STValidation.h:170
memory
ripple::STValidation::setUntrusted
void setUntrusted()
Definition: STValidation.h:197
ripple::KeyType::secp256k1
@ secp256k1
ripple::STValidation::getSeenTime
NetClock::time_point getSeenTime() const
Definition: STValidation.cpp:112
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:65
ripple::STValidation::FeeSettings::baseFee
boost::optional< XRPAmount > baseFee
Definition: STValidation.h:104
ripple::vfFullyCanonicalSig
const std::uint32_t vfFullyCanonicalSig
Definition: STValidation.h:35
ripple::STValidation::setTrusted
void setTrusted()
Definition: STValidation.h:191
ripple::STValidation::getSigningHash
uint256 getSigningHash() const
Definition: STValidation.cpp:91
std::size_t
ripple::STValidation::getSignerPublic
PublicKey getSignerPublic() const
Definition: STValidation.cpp:136
ripple::STValidation::kFullFlag
@ kFullFlag
Definition: STValidation.h:50
ripple::STValidation::FeeSettings::reserveIncrement
boost::optional< XRPAmount > reserveIncrement
Definition: STValidation.h:106
ripple::STObject::getJson
virtual Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:657
ripple::STValidation::FeeSettings
Fees to set when issuing a new validation.
Definition: STValidation.h:101
ripple::STValidation::STValidation
STValidation(SerialIter &sit, LookupNodeID &&lookupNodeID, bool checkSignature)
Construct a STValidation from a peer.
Definition: STValidation.h:69