Simplify HashPrefix by converting to an enum

This commit is contained in:
Nik Bougalis
2020-02-26 16:53:13 -08:00
committed by Carl Hua
parent ec137044a0
commit 16c659b22c
10 changed files with 57 additions and 92 deletions

View File

@@ -73,7 +73,6 @@ target_sources (xrpl_core PRIVATE
src/ripple/protocol/impl/BuildInfo.cpp
src/ripple/protocol/impl/ErrorCodes.cpp
src/ripple/protocol/impl/Feature.cpp
src/ripple/protocol/impl/HashPrefix.cpp
src/ripple/protocol/impl/Indexes.cpp
src/ripple/protocol/impl/InnerObjectFormats.cpp
src/ripple/protocol/impl/Issue.cpp

View File

@@ -34,6 +34,7 @@
#include <ripple/app/misc/ValidatorList.h>
#include <ripple/app/paths/PathRequests.h>
#include <ripple/basics/contract.h>
#include <ripple/basics/safe_cast.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/TaggedCache.h>
#include <ripple/basics/UptimeClock.h>
@@ -1497,7 +1498,7 @@ LedgerMaster::getCloseTimeByHash(LedgerHash const& ledgerHash,
(node->getData().size() >= 120))
{
SerialIter it (node->getData().data(), node->getData().size());
if (it.get32() == HashPrefix::ledgerMaster)
if (safe_cast<HashPrefix>(it.get32()) == HashPrefix::ledgerMaster)
{
it.skip (
4+8+32+ // seq drops parentHash

View File

@@ -24,6 +24,7 @@
#define LZ4_DISABLE_DEPRECATE_WARNINGS
#include <ripple/basics/contract.h>
#include <ripple/basics/safe_cast.h>
#include <nudb/detail/field.hpp>
#include <ripple/nodestore/impl/varint.h>
#include <ripple/nodestore/NodeObject.h>
@@ -240,7 +241,7 @@ nodeobject_compress (void const* in,
read<std::uint32_t>(is, unused);
read<std::uint8_t> (is, kind);
read<std::uint32_t>(is, prefix);
if (prefix == HashPrefix::innerNode)
if (safe_cast<HashPrefix>(prefix) == HashPrefix::innerNode)
{
std::size_t n = 0;
std::uint16_t mask = 0;
@@ -350,7 +351,7 @@ filter_inner (void* in, std::size_t in_size)
read<std::uint32_t>(is, unused);
read<std::uint8_t> (is, kind);
read<std::uint32_t>(is, prefix);
if (prefix == HashPrefix::innerNode)
if (safe_cast<HashPrefix>(prefix) == HashPrefix::innerNode)
{
ostream os(in, 9);
write<std::uint32_t>(os, 0);

View File

@@ -25,6 +25,20 @@
namespace ripple {
namespace detail {
constexpr
std::uint32_t
make_hash_prefix(char a, char b, char c)
{
return
(static_cast<std::uint32_t>(a) << 24) +
(static_cast<std::uint32_t>(b) << 16) +
(static_cast<std::uint32_t>(c) << 8);
}
}
/** Prefix for hashing functions.
These prefixes are inserted before the source material used to generate
@@ -36,69 +50,43 @@ namespace ripple {
three bytes formed from the ASCII equivalent of some arbitrary string. For
example "TXN".
@note Hash prefixes are part of the Ripple protocol.
@ingroup protocol
@note Hash prefixes are part of the protocol; you cannot, arbitrarily,
change the type or the value of any of these without causing breakage.
*/
class HashPrefix
enum class HashPrefix : std::uint32_t
{
private:
std::uint32_t m_prefix;
HashPrefix (char a, char b, char c)
: m_prefix (0)
{
m_prefix = a;
m_prefix = (m_prefix << 8) + b;
m_prefix = (m_prefix << 8) + c;
m_prefix = m_prefix << 8;
}
public:
HashPrefix(HashPrefix const&) = delete;
HashPrefix& operator=(HashPrefix const&) = delete;
/** Returns the hash prefix associated with this object */
operator std::uint32_t () const
{
return m_prefix;
}
// VFALCO TODO Expand the description to complete, concise sentences.
//
/** transaction plus signature to give transaction ID */
static HashPrefix const transactionID;
transactionID = detail::make_hash_prefix('T', 'X', 'N'),
/** transaction plus metadata */
static HashPrefix const txNode;
txNode = detail::make_hash_prefix('S', 'N', 'D'),
/** account state */
static HashPrefix const leafNode;
leafNode = detail::make_hash_prefix('M', 'L', 'N'),
/** inner node in V1 tree */
static HashPrefix const innerNode;
innerNode = detail::make_hash_prefix('M', 'I', 'N'),
/** ledger master data for signing */
static HashPrefix const ledgerMaster;
ledgerMaster = detail::make_hash_prefix('L', 'W', 'R'),
/** inner transaction to sign */
static HashPrefix const txSign;
txSign = detail::make_hash_prefix('S', 'T', 'X'),
/** inner transaction to multi-sign */
static HashPrefix const txMultiSign;
txMultiSign = detail::make_hash_prefix('S', 'M', 'T'),
/** validation for signing */
static HashPrefix const validation;
validation = detail::make_hash_prefix('V', 'A', 'L'),
/** proposal for signing */
static HashPrefix const proposal;
proposal = detail::make_hash_prefix('P', 'R', 'P'),
/** Manifest */
static HashPrefix const manifest;
manifest = detail::make_hash_prefix('M', 'A', 'N'),
/** Payment Channel Claim */
static HashPrefix const paymentChannelClaim;
paymentChannelClaim = detail::make_hash_prefix('C', 'L', 'M'),
};
template <class Hasher>

View File

@@ -25,6 +25,7 @@
#include <ripple/basics/CountedObject.h>
#include <ripple/basics/FeeUnits.h>
#include <ripple/basics/Slice.h>
#include <ripple/protocol/HashPrefix.h>
#include <ripple/protocol/STAmount.h>
#include <ripple/protocol/STPathSet.h>
#include <ripple/protocol/STVector256.h>
@@ -383,8 +384,8 @@ public:
bool isFlag(std::uint32_t) const;
std::uint32_t getFlags () const;
uint256 getHash (std::uint32_t prefix) const;
uint256 getSigningHash (std::uint32_t prefix) const;
uint256 getHash (HashPrefix prefix) const;
uint256 getSigningHash (HashPrefix prefix) const;
const STBase& peekAtIndex (int offset) const
{

View File

@@ -27,6 +27,7 @@
#include <ripple/basics/safe_cast.h>
#include <ripple/basics/Slice.h>
#include <ripple/beast/crypto/secure_erase.h>
#include <ripple/protocol/HashPrefix.h>
#include <ripple/protocol/SField.h>
#include <cassert>
#include <cstdint>
@@ -84,6 +85,7 @@ public:
int add8 (unsigned char byte);
int add16 (std::uint16_t);
int add32 (std::uint32_t); // ledger indexes, account sequence, timestamps
int add32 (HashPrefix);
int add64 (std::uint64_t); // native currency amounts
int add128 (const uint128&); // private key generators
int add256 (uint256 const& ); // transaction and ledger hashes

View File

@@ -1,39 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/protocol/HashPrefix.h>
namespace ripple {
// The prefix codes are part of the Ripple protocol
// and existing codes cannot be arbitrarily changed.
HashPrefix const HashPrefix::transactionID ('T', 'X', 'N');
HashPrefix const HashPrefix::txNode ('S', 'N', 'D');
HashPrefix const HashPrefix::leafNode ('M', 'L', 'N');
HashPrefix const HashPrefix::innerNode ('M', 'I', 'N');
HashPrefix const HashPrefix::ledgerMaster ('L', 'W', 'R');
HashPrefix const HashPrefix::txSign ('S', 'T', 'X');
HashPrefix const HashPrefix::txMultiSign ('S', 'M', 'T');
HashPrefix const HashPrefix::validation ('V', 'A', 'L');
HashPrefix const HashPrefix::proposal ('P', 'R', 'P');
HashPrefix const HashPrefix::manifest ('M', 'A', 'N');
HashPrefix const HashPrefix::paymentChannelClaim ('C', 'L', 'M');
} // ripple

View File

@@ -307,7 +307,7 @@ bool STObject::isEquivalent (const STBase& t) const
});
}
uint256 STObject::getHash (std::uint32_t prefix) const
uint256 STObject::getHash (HashPrefix prefix) const
{
Serializer s;
s.add32 (prefix);
@@ -315,7 +315,7 @@ uint256 STObject::getHash (std::uint32_t prefix) const
return s.getSHA512Half ();
}
uint256 STObject::getSigningHash (std::uint32_t prefix) const
uint256 STObject::getSigningHash (HashPrefix prefix) const
{
Serializer s;
s.add32 (prefix);

View File

@@ -21,6 +21,7 @@
#include <ripple/basics/Log.h>
#include <ripple/protocol/digest.h>
#include <ripple/protocol/Serializer.h>
#include <type_traits>
namespace ripple {
@@ -52,6 +53,17 @@ int Serializer::add32 (std::uint32_t i)
return ret;
}
int Serializer::add32 (HashPrefix p)
{
// This should never trigger; the size & type of a hash prefix are
// integral parts of the protocol and unlikely to ever change.
static_assert(std::is_same_v<std::uint32_t,
std::underlying_type_t<decltype(p)>>);
return add32(safe_cast<std::uint32_t>(p));
}
int Serializer::add64 (std::uint64_t i)
{
int ret = mData.size ();

View File

@@ -21,8 +21,8 @@
#include <ripple/basics/contract.h>
#include <ripple/basics/Log.h>
#include <ripple/protocol/digest.h>
#include <ripple/basics/safe_cast.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/protocol/HashPrefix.h>
#include <ripple/beast/core/LexicalCast.h>
#include <mutex>
@@ -194,7 +194,7 @@ SHAMapAbstractNode::make(Slice const& rawNode, std::uint32_t seq, SHANodeFormat
prefix |= rawNode[3];
Serializer s (rawNode.data() + 4, rawNode.size() - 4);
if (prefix == HashPrefix::transactionID)
if (safe_cast<HashPrefix>(prefix) == HashPrefix::transactionID)
{
auto item = std::make_shared<SHAMapItem const>(
sha512Half(rawNode),
@@ -203,7 +203,7 @@ SHAMapAbstractNode::make(Slice const& rawNode, std::uint32_t seq, SHANodeFormat
return std::make_shared<SHAMapTreeNode>(item, tnTRANSACTION_NM, seq, hash);
return std::make_shared<SHAMapTreeNode>(item, tnTRANSACTION_NM, seq);
}
else if (prefix == HashPrefix::leafNode)
else if (safe_cast<HashPrefix>(prefix) == HashPrefix::leafNode)
{
if (s.getLength () < 32)
Throw<std::runtime_error> ("short PLN node");
@@ -223,7 +223,7 @@ SHAMapAbstractNode::make(Slice const& rawNode, std::uint32_t seq, SHANodeFormat
return std::make_shared<SHAMapTreeNode>(item, tnACCOUNT_STATE, seq, hash);
return std::make_shared<SHAMapTreeNode>(item, tnACCOUNT_STATE, seq);
}
else if (prefix == HashPrefix::innerNode)
else if (safe_cast<HashPrefix>(prefix) == HashPrefix::innerNode)
{
auto len = s.getLength();
@@ -246,7 +246,7 @@ SHAMapAbstractNode::make(Slice const& rawNode, std::uint32_t seq, SHANodeFormat
ret->updateHash();
return ret;
}
else if (prefix == HashPrefix::txNode)
else if (safe_cast<HashPrefix>(prefix) == HashPrefix::txNode)
{
// transaction with metadata
if (s.getLength () < 32)