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()) << "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  mNodeID = lookupNodeID(PublicKey(makeSlice(spk)));
92  assert(mNodeID.isNonZero());
93  }
94 
99  struct FeeSettings
100  {
101  boost::optional<std::uint32_t> loadFee;
102  boost::optional<XRPAmount> baseFee;
103  boost::optional<XRPAmount> reserveBase;
104  boost::optional<XRPAmount> reserveIncrement;
105  };
106 
127  STValidation(
128  uint256 const& ledgerHash,
129  std::uint32_t ledgerSeq,
130  uint256 const& consensusHash,
131  NetClock::time_point signTime,
132  PublicKey const& publicKey,
133  SecretKey const& secretKey,
134  NodeID const& nodeID,
135  bool isFull,
136  FeeSettings const& fees,
137  std::vector<uint256> const& amendments);
138 
139  STBase*
140  copy(std::size_t n, void* buf) const override
141  {
142  return emplace(n, buf, *this);
143  }
144 
145  STBase*
146  move(std::size_t n, void* buf) override
147  {
148  return emplace(n, buf, std::move(*this));
149  }
150 
151  // Hash of the validated ledger
152  uint256
153  getLedgerHash() const;
154 
155  // Hash of consensus transaction set used to generate ledger
156  uint256
157  getConsensusHash() const;
158 
160  getSignTime() const;
161 
163  getSeenTime() const;
164 
165  PublicKey
166  getSignerPublic() const;
167 
168  NodeID
169  getNodeID() const
170  {
171  return mNodeID;
172  }
173 
174  bool
175  isValid() const;
176 
177  bool
178  isFull() const;
179 
180  bool
181  isTrusted() const
182  {
183  return mTrusted;
184  }
185 
186  uint256
187  getSigningHash() const;
188 
189  void
191  {
192  mTrusted = true;
193  }
194 
195  void
197  {
198  mTrusted = false;
199  }
200 
201  void
203  {
204  mSeen = s;
205  }
206 
207  Blob
208  getSerialized() const;
209 
210  Blob
211  getSignature() const;
212 
213 private:
214  static SOTemplate const&
215  getFormat();
216 
218  bool mTrusted = false;
220 };
221 
222 } // namespace ripple
223 
224 #endif
ripple::STValidation::isValid
bool isValid() const
Definition: STValidation.cpp:122
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:146
ripple::STValidation::getConsensusHash
uint256 getConsensusHash() const
Definition: STValidation.cpp:104
ripple::STValidation::FeeSettings::reserveBase
boost::optional< XRPAmount > reserveBase
Definition: STValidation.h:103
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:110
std::shared_ptr< STValidation >
ripple::base_uint::isNonZero
bool isNonZero() const
Definition: base_uint.h:480
ripple::STValidation::isTrusted
bool isTrusted() const
Definition: STValidation.h:181
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:149
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:456
std::vector
STL class.
ripple::STValidation::FeeSettings::loadFee
boost::optional< std::uint32_t > loadFee
Definition: STValidation.h:101
ripple::STValidation::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STValidation.h:140
ripple::STValidation::getLedgerHash
uint256 getLedgerHash() const
Definition: STValidation.cpp:98
ripple::STValidation::mTrusted
bool mTrusted
Definition: STValidation.h:218
ripple::STObject::getFieldVL
Blob getFieldVL(SField const &field) const
Definition: STObject.cpp:579
ripple::STValidation::mNodeID
NodeID mNodeID
Definition: STValidation.h:217
ripple::STValidation::getSignature
Blob getSignature() const
Definition: STValidation.cpp:155
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::STValidation
Definition: STValidation.h:38
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:149
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:169
ripple::STValidation::getSerialized
Blob getSerialized() const
Definition: STValidation.cpp:161
ripple::STValidation::mSeen
NetClock::time_point mSeen
Definition: STValidation.h:219
std::chrono::time_point
cstdint
ripple::STValidation::setSeen
void setSeen(NetClock::time_point s)
Definition: STValidation.h:202
ripple::SerialIter
Definition: Serializer.h:368
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:110
ripple::STValidation::getNodeID
NodeID getNodeID() const
Definition: STValidation.h:169
memory
ripple::STValidation::setUntrusted
void setUntrusted()
Definition: STValidation.h:196
ripple::KeyType::secp256k1
@ secp256k1
ripple::STValidation::getSeenTime
NetClock::time_point getSeenTime() const
Definition: STValidation.cpp:116
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::STValidation::FeeSettings::baseFee
boost::optional< XRPAmount > baseFee
Definition: STValidation.h:102
ripple::vfFullyCanonicalSig
const std::uint32_t vfFullyCanonicalSig
Definition: STValidation.h:35
ripple::STValidation::setTrusted
void setTrusted()
Definition: STValidation.h:190
ripple::STValidation::getSigningHash
uint256 getSigningHash() const
Definition: STValidation.cpp:92
std::size_t
ripple::STValidation::getSignerPublic
PublicKey getSignerPublic() const
Definition: STValidation.cpp:143
ripple::STValidation::kFullFlag
@ kFullFlag
Definition: STValidation.h:50
ripple::STValidation::FeeSettings::reserveIncrement
boost::optional< XRPAmount > reserveIncrement
Definition: STValidation.h:104
ripple::STObject::getJson
virtual Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:709
ripple::STValidation::FeeSettings
Fees to set when issuing a new validation.
Definition: STValidation.h:99
ripple::STValidation::STValidation
STValidation(SerialIter &sit, LookupNodeID &&lookupNodeID, bool checkSignature)
Construct a STValidation from a peer.
Definition: STValidation.h:69