Skip to content

Commit

Permalink
Simplify HashPrefix by converting to an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
nbougalis authored and carlhua committed Mar 5, 2020
1 parent ec13704 commit 16c659b
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 92 deletions.
1 change: 0 additions & 1 deletion Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/ripple/app/ledger/impl/LedgerMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/ripple/nodestore/impl/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
68 changes: 28 additions & 40 deletions src/ripple/protocol/HashPrefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>
Expand Down
5 changes: 3 additions & 2 deletions src/ripple/protocol/STObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -383,8 +384,8 @@ class STObject
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
{
Expand Down
2 changes: 2 additions & 0 deletions src/ripple/protocol/Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -84,6 +85,7 @@ class Serializer
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
Expand Down
39 changes: 0 additions & 39 deletions src/ripple/protocol/impl/HashPrefix.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/ripple/protocol/impl/STObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@ 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);
add (s, withAllFields);
return s.getSHA512Half ();
}

uint256 STObject::getSigningHash (std::uint32_t prefix) const
uint256 STObject::getSigningHash (HashPrefix prefix) const
{
Serializer s;
s.add32 (prefix);
Expand Down
12 changes: 12 additions & 0 deletions src/ripple/protocol/impl/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <ripple/basics/Log.h>
#include <ripple/protocol/digest.h>
#include <ripple/protocol/Serializer.h>
#include <type_traits>

namespace ripple {

Expand Down Expand Up @@ -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 ();
Expand Down
10 changes: 5 additions & 5 deletions src/ripple/shamap/impl/SHAMapTreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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),
Expand All @@ -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");
Expand All @@ -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();

Expand All @@ -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)
Expand Down

0 comments on commit 16c659b

Please sign in to comment.