rippled
STValidation.cpp
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 #include <ripple/protocol/STValidation.h>
21 #include <ripple/protocol/HashPrefix.h>
22 #include <ripple/basics/contract.h>
23 #include <ripple/basics/Log.h>
24 #include <ripple/json/to_string.h>
25 
26 namespace ripple {
27 
29  uint256 const& ledgerHash,
30  std::uint32_t ledgerSeq,
31  uint256 const& consensusHash,
32  NetClock::time_point signTime,
33  PublicKey const& publicKey,
34  SecretKey const& secretKey,
35  NodeID const& nodeID,
36  bool isFull,
37  FeeSettings const& fees,
38  std::vector<uint256> const& amendments)
39  : STObject(getFormat(), sfValidation), mNodeID(nodeID), mSeen(signTime)
40 {
41  // This is our own public key and it should always be valid.
42  if (!publicKeyType(publicKey))
43  LogicError("Invalid validation public key");
44  assert(mNodeID.isNonZero());
45  setFieldH256(sfLedgerHash, ledgerHash);
46  setFieldH256(sfConsensusHash, consensusHash);
47  setFieldU32(sfSigningTime, signTime.time_since_epoch().count());
48 
49  setFieldVL(sfSigningPubKey, publicKey.slice());
50  if (isFull)
52 
53  setFieldU32(sfLedgerSequence, ledgerSeq);
54 
55  if (fees.loadFee)
57 
58  // IF any of the values are out of the valid range, don't send a value.
59  // They should not be an issue, though, because the voting
60  // process (FeeVoteImpl) ignores any out of range values.
61  if (fees.baseFee)
62  {
63  if (auto const v = fees.baseFee->dropsAs<std::uint64_t>())
65  }
66 
67  if (fees.reserveBase)
68  {
69  if (auto const v = fees.reserveBase->dropsAs<std::uint32_t>())
71  }
72 
73  if (fees.reserveIncrement)
74  {
75  if (auto const v = fees.reserveIncrement->dropsAs<std::uint32_t>())
77  }
78 
79  if (!amendments.empty())
81 
83 
84  auto const signingHash = getSigningHash();
85  setFieldVL(
86  sfSignature, signDigest(getSignerPublic(), secretKey, signingHash));
87 
88  setTrusted();
89 }
90 
92 {
94 }
95 
97 {
98  return getFieldH256 (sfLedgerHash);
99 }
100 
102 {
103  return getFieldH256 (sfConsensusHash);
104 }
105 
108 {
110 }
111 
113 {
114  return mSeen;
115 }
116 
118 {
119  try
120  {
122  return false;
123 
127  }
128  catch (std::exception const&)
129  {
130  JLOG (debugLog().error())
131  << "Exception validating validation";
132  return false;
133  }
134 }
135 
137 {
139 }
140 
141 bool STValidation::isFull () const
142 {
143  return (getFlags () & kFullFlag) != 0;
144 }
145 
147 {
148  return getFieldVL (sfSignature);
149 }
150 
152 {
153  Serializer s;
154  add (s);
155  return s.peekData ();
156 }
157 
159 {
160  struct FormatHolder
161  {
162  SOTemplate format
163  {
164  { sfFlags, soeREQUIRED },
168  { sfLoadFee, soeOPTIONAL },
170  { sfBaseFee, soeOPTIONAL },
177  { sfCookie, soeOPTIONAL },
178  };
179  };
180 
181  static const FormatHolder holder;
182 
183  return holder.format;
184 }
185 
186 } // ripple
ripple::STValidation::isValid
bool isValid() const
Definition: STValidation.cpp:117
ripple::sfLoadFee
const SF_U32 sfLoadFee(access, STI_UINT32, 24, "LoadFee")
Definition: SField.h:361
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::getConsensusHash
uint256 getConsensusHash() const
Definition: STValidation.cpp:101
ripple::STValidation::FeeSettings::reserveBase
boost::optional< XRPAmount > reserveBase
Definition: STValidation.h:105
std::exception
STL class.
ripple::base_uint::isNonZero
bool isNonZero() const
Definition: base_uint.h:430
ripple::publicKeyType
boost::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:207
ripple::STObject::setFieldV256
void setFieldV256(SField const &field, STVector256 const &v)
Definition: STObject.cpp:621
ripple::STValidation::isFull
bool isFull() const
Definition: STValidation.cpp:141
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::getLedgerHash
uint256 getLedgerHash() const
Definition: STValidation.cpp:96
ripple::sfFlags
const SF_U32 sfFlags(access, STI_UINT32, 2, "Flags")
Definition: SField.h:338
std::chrono::duration
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:631
ripple::STObject::getFieldVL
Blob getFieldVL(SField const &field) const
Definition: STObject.cpp:542
ripple::STValidation::mNodeID
NodeID mNodeID
Definition: STValidation.h:219
ripple::sfReserveBase
const SF_U32 sfReserveBase(access, STI_UINT32, 31, "ReserveBase")
Definition: SField.h:368
ripple::STValidation::getSignature
Blob getSignature() const
Definition: STValidation.cpp:146
ripple::sfSignature
const SF_Blob sfSignature(access, STI_VL, 6, "Signature", SField::sMD_Default, SField::notSigning)
Definition: SField.h:444
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:417
ripple::base_uint< 256 >
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:75
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:100
ripple::STObject::setFieldH256
void setFieldH256(SField const &field, uint256 const &)
Definition: STObject.cpp:616
ripple::soeOPTIONAL
@ soeOPTIONAL
Definition: SOTemplate.h:36
ripple::sfSigningTime
const SF_U32 sfSigningTime(access, STI_UINT32, 9, "SigningTime")
Definition: SField.h:345
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
ripple::sfLedgerSequence
const SF_U32 sfLedgerSequence(access, STI_UINT32, 6, "LedgerSequence")
Definition: SField.h:342
std::chrono::time_point
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:439
ripple::HashPrefix::validation
@ validation
validation for signing
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::verifyDigest
bool verifyDigest(PublicKey const &publicKey, uint256 const &digest, Slice const &sig, bool mustBeFullyCanonical)
Verify a secp256k1 signature on the digest of a message.
Definition: PublicKey.cpp:222
ripple::KeyType::secp256k1
@ secp256k1
ripple::STValidation::getSeenTime
NetClock::time_point getSeenTime() const
Definition: STValidation.cpp:112
ripple::Serializer
Definition: Serializer.h:43
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::STObject::getSigningHash
uint256 getSigningHash(HashPrefix prefix) const
Definition: STObject.cpp:318
ripple::STObject::add
virtual void add(Serializer &s) const override
Definition: STObject.h:343
ripple::STValidation::FeeSettings::baseFee
boost::optional< XRPAmount > baseFee
Definition: STValidation.h:104
ripple::sfReserveIncrement
const SF_U32 sfReserveIncrement(access, STI_UINT32, 32, "ReserveIncrement")
Definition: SField.h:369
ripple::LogicError
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition: contract.cpp:50
ripple::Serializer::peekData
Blob const & peekData() const
Definition: Serializer.h:173
ripple::vfFullyCanonicalSig
const std::uint32_t vfFullyCanonicalSig
Definition: STValidation.h:35
ripple::STValidation::setTrusted
void setTrusted()
Definition: STValidation.h:191
ripple::sfCloseTime
const SF_U32 sfCloseTime(access, STI_UINT32, 7, "CloseTime")
Definition: SField.h:343
ripple::STObject::setFlag
bool setFlag(std::uint32_t)
Definition: STObject.cpp:412
ripple::STVector256
Definition: STVector256.h:29
std::vector::empty
T empty(T... args)
ripple::STValidation::getSigningHash
uint256 getSigningHash() const
Definition: STValidation.cpp:91
ripple::STValidation::getSignerPublic
PublicKey getSignerPublic() const
Definition: STValidation.cpp:136
ripple::sfCookie
const SF_U64 sfCookie(access, STI_UINT64, 10,"Cookie")
Definition: SField.h:388
ripple::sfAmendments
const SF_Vec256 sfAmendments(access, STI_VECTOR256, 3, "Amendments")
Definition: SField.h:475
ripple::STObject::getFieldU32
std::uint32_t getFieldU32(SField const &field) const
Definition: STObject.cpp:512
ripple::STValidation::kFullFlag
@ kFullFlag
Definition: STValidation.h:50
ripple::STValidation::FeeSettings::reserveIncrement
boost::optional< XRPAmount > reserveIncrement
Definition: STValidation.h:106
ripple::sfBaseFee
const SF_U64 sfBaseFee(access, STI_UINT64, 5, "BaseFee")
Definition: SField.h:383
ripple::STObject::setFieldU64
void setFieldU64(SField const &field, std::uint64_t)
Definition: STObject.cpp:606
ripple::sfConsensusHash
const SF_U256 sfConsensusHash(access, STI_HASH256, 23, "ConsensusHash")
Definition: SField.h:419
ripple::STValidation::FeeSettings
Fees to set when issuing a new validation.
Definition: STValidation.h:101
ripple::STObject::setFieldU32
void setFieldU32(SField const &field, std::uint32_t)
Definition: STObject.cpp:601
ripple::sfLedgerHash
const SF_U256 sfLedgerHash(access, STI_HASH256, 1, "LedgerHash")
Definition: SField.h:401
ripple::STObject::getFieldH256
uint256 getFieldH256(SField const &field) const
Definition: STObject.cpp:532
ripple::STValidation::STValidation
STValidation(SerialIter &sit, LookupNodeID &&lookupNodeID, bool checkSignature)
Construct a STValidation from a peer.
Definition: STValidation.h:69